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