use import instead of eval to load js

This commit is contained in:
codetypes committed 2025-03-24 23:31:40 +08:00
1 parent ceef9db425
commit 6eb48532a4
7 files changed
+56 -49

No files matched your search

+4 -5
View File
@@ -377,7 +377,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
}
},
buildProject: () => {
buildProject: async () => {
const workspace = get();
if (workspace.path) {
if (!buildDir) {
@@ -394,7 +394,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
const debug = console.debug;
console.debug = () => {};
try {
const hasError = b3util.buildProject(workspace.path, buildDir);
const hasError = await b3util.buildProject(workspace.path, buildDir);
if (hasError) {
message.error(i18n.t("buildFailed"));
} else {
@@ -409,7 +409,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
}
},
batchProject: () => {
batchProject: async () => {
const workspace = get();
const scriptPath = dialog.showOpenDialogSync({
properties: ["openFile"],
@@ -420,8 +420,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
let hasError = false;
try {
console.log("run script", scriptPath);
const str = fs.readFileSync(scriptPath, "utf8");
const batch = eval(str) as b3util.BatchScript;
const batch = await b3util.loadModule(scriptPath);
batch.onSetup?.({
fs,
path: Path,
+14 -5
View File
@@ -1,6 +1,5 @@
import * as fs from "fs";
import { ExpressionEvaluator, NodeDef } from "../behavior3/src/behavior3";
import { type WorkspaceModel } from "../contexts/workspace-context";
import {
FileVarDecl,
ImportDef,
@@ -19,7 +18,7 @@ import {
VERSION,
} from "./b3type";
import Path from "./path";
import { readJson, readTree } from "./util";
import { readJson, readTree, readWorkspace } from "./util";
export class NodeDefs extends Map<string, NodeDef> {
get(key: string): NodeDef {
@@ -813,9 +812,9 @@ export const processBatch = (tree: TreeModel | null, path: string, batch: BatchS
return tree;
};
export const buildProject = (project: string, buildDir: string) => {
export const buildProject = async (project: string, buildDir: string) => {
let hasError = false;
const settings = readJson<WorkspaceModel>(project).settings;
const settings = readWorkspace(project).settings;
let buildScript: BatchScript | undefined;
if (settings.checkExpr) {
setCheckExpr(true);
@@ -823,7 +822,7 @@ export const buildProject = (project: string, buildDir: string) => {
if (settings.buildScript) {
const scriptPath = workdir + "/" + settings.buildScript;
try {
buildScript = eval(fs.readFileSync(scriptPath, "utf8"));
buildScript = await loadModule(scriptPath);
} catch (e) {
console.error(`'${scriptPath}' is not a valid build script`);
}
@@ -870,6 +869,16 @@ export const buildProject = (project: string, buildDir: string) => {
return hasError;
};
export const loadModule = async (path: string) => {
try {
const module = await import(path);
return module;
} catch (e) {
console.error(`load module error: ${path}`, e);
}
return null;
};
export const createFileData = (data: TreeGraphData, includeSubtree?: boolean) => {
const nodeData: NodeModel = {
id: Number(data.id),