fix batch script only load once

This commit is contained in:
codetypes committed 2025-03-25 12:57:33 +08:00
1 parent f6b02d92c6
commit 49bfc76fc6
4 files changed
+21 -18

No files matched your search

+4 -4
View File
@@ -30,8 +30,8 @@
interface BatchScript { interface BatchScript {
onSetup?(env: Env): void; onSetup?(env: Env): void;
processTree?(tree: TreeModel, path: string): TreeModel | null; onProcessTree?(tree: TreeModel, path: string): TreeModel | null;
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null; onProcessNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
onWriteFile?(path: string, tree: TreeModel): void; onWriteFile?(path: string, tree: TreeModel): void;
onComplete?(status: "success" | "failure"): void; onComplete?(status: "success" | "failure"): void;
} }
@@ -44,12 +44,12 @@ export const onSetup = (env) => {
console.log(env.fs.readFileSync("sample/scripts/build.js", "utf8")); console.log(env.fs.readFileSync("sample/scripts/build.js", "utf8"));
}; };
export const processTree = (tree) => { export const onProcessTree = (tree) => {
console.log(`processTree ${tree.name}`); console.log(`processTree ${tree.name}`);
return tree; return tree;
}; };
export const processNode = (node, tree) => { export const onProcessNode = (node, tree) => {
console.log(`processNode ${tree.name} ${node.name}#${node.id}`); console.log(`processNode ${tree.name} ${node.name}#${node.id}`);
return node; return node;
}; };
+4 -4
View File
@@ -30,8 +30,8 @@
interface BatchScript { interface BatchScript {
onSetup?(env: Env): void; onSetup?(env: Env): void;
processTree?(tree: TreeModel, path: string): TreeModel | null; onProcessTree?(tree: TreeModel, path: string): TreeModel | null;
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null; onProcessNode?(node: NodeModel, tree: TreeModel): NodeModel | null;
onWriteFile?(path: string, tree: TreeModel): void; onWriteFile?(path: string, tree: TreeModel): void;
onComplete?(status: "success" | "failure"): void; onComplete?(status: "success" | "failure"): void;
} }
@@ -41,12 +41,12 @@ export const onSetup = (env) => {
console.log(env); console.log(env);
}; };
export const processTree = (tree) => { export const onProcessTree = (tree) => {
console.log(`processTree ${tree.name}`); console.log(`processTree ${tree.name}`);
return tree; return tree;
}; };
export const processNode = (node, tree) => { export const onProcessNode = (node, tree) => {
console.log(`processNode ${tree.name}.${node.id}`); console.log(`processNode ${tree.name}.${node.id}`);
return node; return node;
}; };
+1
View File
@@ -455,6 +455,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
data.files?.forEach((file) => { data.files?.forEach((file) => {
workspace.allFiles.set(file.path, { path: file.path, desc: file.desc, exists: false }); workspace.allFiles.set(file.path, { path: file.path, desc: file.desc, exists: false });
}); });
process.chdir(Path.dirname(workspace.path));
}, },
saveWorkspace: () => { saveWorkspace: () => {
+12 -10
View File
@@ -35,8 +35,8 @@ type Env = {
export interface BatchScript { export interface BatchScript {
onSetup?(env: Env): void; onSetup?(env: Env): void;
processTree?(tree: TreeModel, path: string): TreeModel | null; onProcessTree?(tree: TreeModel, path: string): TreeModel | null;
processNode?(node: NodeModel, tree: TreeModel): NodeModel | null; onProcessNode?(node: NodeModel): NodeModel | null;
onWriteFile?(path: string, tree: TreeModel): void; onWriteFile?(path: string, tree: TreeModel): void;
onComplete?(status: "success" | "failure"): void; onComplete?(status: "success" | "failure"): void;
} }
@@ -787,13 +787,13 @@ export const processBatch = (tree: TreeModel | null, path: string, batch: BatchS
if (!tree) { if (!tree) {
return null; return null;
} }
if (batch.processTree) { if (batch.onProcessTree) {
tree = batch.processTree(tree, path); tree = batch.onProcessTree(tree, path);
} }
if (!tree) { if (!tree) {
return null; return null;
} }
if (batch.processNode) { if (batch.onProcessNode) {
const processNode = (node: NodeModel) => { const processNode = (node: NodeModel) => {
if (node.children) { if (node.children) {
const children: NodeModel[] = []; const children: NodeModel[] = [];
@@ -805,7 +805,7 @@ export const processBatch = (tree: TreeModel | null, path: string, batch: BatchS
}); });
node.children = children; node.children = children;
} }
return batch.processNode?.(node, tree); return batch.onProcessNode?.(node);
}; };
tree.root = processNode(tree.root) ?? ({} as NodeModel); tree.root = processNode(tree.root) ?? ({} as NodeModel);
} }
@@ -871,12 +871,14 @@ export const buildProject = async (project: string, buildDir: string) => {
export const loadModule = async (path: string) => { export const loadModule = async (path: string) => {
try { try {
const module = await import(path); if (typeof require !== "undefined" && require.cache) {
return module; delete require.cache[require.resolve(path)];
} catch (e) {
console.error(`load module error: ${path}`, e);
} }
return await import(`${path}?t=${Date.now()}`);
} catch (e) {
console.error(`failed to load module: ${path}`, e);
return null; return null;
}
}; };
export const createFileData = (data: TreeGraphData, includeSubtree?: boolean) => { export const createFileData = (data: TreeGraphData, includeSubtree?: boolean) => {