use import instead of eval to load js
This commit is contained in:
1 parent
ceef9db425
commit
6eb48532a4
7 files changed
+56
-49
No files matched your search
@@ -59,7 +59,7 @@ if (buildOutput || buildProject || buildHelp) {
|
|||||||
b3util.initWorkdir(workdir, (msg) => {
|
b3util.initWorkdir(workdir, (msg) => {
|
||||||
console.error(`${msg}`);
|
console.error(`${msg}`);
|
||||||
});
|
});
|
||||||
const hasError = b3util.buildProject(project, buildDir);
|
const hasError = await b3util.buildProject(project, buildDir);
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
console.error("build failed***");
|
console.error("build failed***");
|
||||||
app.quit();
|
app.quit();
|
||||||
@@ -142,7 +142,6 @@ async function createWindow(projectPath?: string) {
|
|||||||
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
|
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
|
||||||
nodeIntegration: true,
|
nodeIntegration: true,
|
||||||
contextIsolation: false,
|
contextIsolation: false,
|
||||||
webSecurity: false,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<!-- <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" /> -->
|
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
|
||||||
<title>Behavior3 Editor</title>
|
<title>Behavior3 Editor</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
+21
-20
@@ -36,23 +36,24 @@
|
|||||||
onComplete?(status: "success" | "failure"): void;
|
onComplete?(status: "success" | "failure"): void;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
({
|
export const onSetup = (env) => {
|
||||||
onSetup: (env) => {
|
console.log("onSetup", env);
|
||||||
console.log("onSetup", env);
|
console.log(env.workdir);
|
||||||
console.log(env.workdir);
|
console.log(env.nodeDefs.get("Wait"));
|
||||||
console.log(env.nodeDefs.get("Wait"));
|
console.log(env.path.dirname("sample/scripts/build.js"));
|
||||||
console.log(env.path.dirname("sample/scripts/build.js"));
|
console.log(env.fs.readFileSync("sample/scripts/build.js", "utf8"));
|
||||||
console.log(env.fs.readFileSync("sample/scripts/build.js", "utf8"));
|
};
|
||||||
},
|
|
||||||
processTree: (tree) => {
|
export const processTree = (tree) => {
|
||||||
console.log(`processTree ${tree.name}`);
|
console.log(`processTree ${tree.name}`);
|
||||||
return tree;
|
return tree;
|
||||||
},
|
};
|
||||||
processNode: (node, tree) => {
|
|
||||||
console.log(`processNode ${tree.name} ${node.name}#${node.id}`);
|
export const processNode = (node, tree) => {
|
||||||
return node;
|
console.log(`processNode ${tree.name} ${node.name}#${node.id}`);
|
||||||
},
|
return node;
|
||||||
onComplete: (status) => {
|
};
|
||||||
console.log(`onComplete ${status}`);
|
|
||||||
},
|
export const onComplete = (status) => {
|
||||||
});
|
console.log(`onComplete ${status}`);
|
||||||
|
};
|
||||||
+14
-15
@@ -36,18 +36,17 @@
|
|||||||
onComplete?(status: "success" | "failure"): void;
|
onComplete?(status: "success" | "failure"): void;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
({
|
|
||||||
processTree: (tree) => {
|
export const onSetup = (env) => {
|
||||||
console.log(`processTree ${tree.name}`);
|
console.log(env);
|
||||||
if (tree.name == "hero") {
|
};
|
||||||
return tree;
|
|
||||||
}
|
export const processTree = (tree) => {
|
||||||
},
|
console.log(`processTree ${tree.name}`);
|
||||||
processNode: (node, tree) => {
|
return tree;
|
||||||
console.log(`processNode ${tree.name}.${node.id}`);
|
};
|
||||||
if (node.name == "GetPos") {
|
|
||||||
return null;
|
export const processNode = (node, tree) => {
|
||||||
}
|
console.log(`processNode ${tree.name}.${node.id}`);
|
||||||
return node;
|
return node;
|
||||||
},
|
};
|
||||||
});
|
|
||||||
@@ -377,7 +377,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
buildProject: () => {
|
buildProject: async () => {
|
||||||
const workspace = get();
|
const workspace = get();
|
||||||
if (workspace.path) {
|
if (workspace.path) {
|
||||||
if (!buildDir) {
|
if (!buildDir) {
|
||||||
@@ -394,7 +394,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
|||||||
const debug = console.debug;
|
const debug = console.debug;
|
||||||
console.debug = () => {};
|
console.debug = () => {};
|
||||||
try {
|
try {
|
||||||
const hasError = b3util.buildProject(workspace.path, buildDir);
|
const hasError = await b3util.buildProject(workspace.path, buildDir);
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
message.error(i18n.t("buildFailed"));
|
message.error(i18n.t("buildFailed"));
|
||||||
} else {
|
} else {
|
||||||
@@ -409,7 +409,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
batchProject: () => {
|
batchProject: async () => {
|
||||||
const workspace = get();
|
const workspace = get();
|
||||||
const scriptPath = dialog.showOpenDialogSync({
|
const scriptPath = dialog.showOpenDialogSync({
|
||||||
properties: ["openFile"],
|
properties: ["openFile"],
|
||||||
@@ -420,8 +420,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
|||||||
let hasError = false;
|
let hasError = false;
|
||||||
try {
|
try {
|
||||||
console.log("run script", scriptPath);
|
console.log("run script", scriptPath);
|
||||||
const str = fs.readFileSync(scriptPath, "utf8");
|
const batch = await b3util.loadModule(scriptPath);
|
||||||
const batch = eval(str) as b3util.BatchScript;
|
|
||||||
batch.onSetup?.({
|
batch.onSetup?.({
|
||||||
fs,
|
fs,
|
||||||
path: Path,
|
path: Path,
|
||||||
|
|||||||
+14
-5
@@ -1,6 +1,5 @@
|
|||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { ExpressionEvaluator, NodeDef } from "../behavior3/src/behavior3";
|
import { ExpressionEvaluator, NodeDef } from "../behavior3/src/behavior3";
|
||||||
import { type WorkspaceModel } from "../contexts/workspace-context";
|
|
||||||
import {
|
import {
|
||||||
FileVarDecl,
|
FileVarDecl,
|
||||||
ImportDef,
|
ImportDef,
|
||||||
@@ -19,7 +18,7 @@ import {
|
|||||||
VERSION,
|
VERSION,
|
||||||
} from "./b3type";
|
} from "./b3type";
|
||||||
import Path from "./path";
|
import Path from "./path";
|
||||||
import { readJson, readTree } from "./util";
|
import { readJson, readTree, readWorkspace } from "./util";
|
||||||
|
|
||||||
export class NodeDefs extends Map<string, NodeDef> {
|
export class NodeDefs extends Map<string, NodeDef> {
|
||||||
get(key: string): NodeDef {
|
get(key: string): NodeDef {
|
||||||
@@ -813,9 +812,9 @@ export const processBatch = (tree: TreeModel | null, path: string, batch: BatchS
|
|||||||
return tree;
|
return tree;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const buildProject = (project: string, buildDir: string) => {
|
export const buildProject = async (project: string, buildDir: string) => {
|
||||||
let hasError = false;
|
let hasError = false;
|
||||||
const settings = readJson<WorkspaceModel>(project).settings;
|
const settings = readWorkspace(project).settings;
|
||||||
let buildScript: BatchScript | undefined;
|
let buildScript: BatchScript | undefined;
|
||||||
if (settings.checkExpr) {
|
if (settings.checkExpr) {
|
||||||
setCheckExpr(true);
|
setCheckExpr(true);
|
||||||
@@ -823,7 +822,7 @@ export const buildProject = (project: string, buildDir: string) => {
|
|||||||
if (settings.buildScript) {
|
if (settings.buildScript) {
|
||||||
const scriptPath = workdir + "/" + settings.buildScript;
|
const scriptPath = workdir + "/" + settings.buildScript;
|
||||||
try {
|
try {
|
||||||
buildScript = eval(fs.readFileSync(scriptPath, "utf8"));
|
buildScript = await loadModule(scriptPath);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`'${scriptPath}' is not a valid build script`);
|
console.error(`'${scriptPath}' is not a valid build script`);
|
||||||
}
|
}
|
||||||
@@ -870,6 +869,16 @@ export const buildProject = (project: string, buildDir: string) => {
|
|||||||
return hasError;
|
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) => {
|
export const createFileData = (data: TreeGraphData, includeSubtree?: boolean) => {
|
||||||
const nodeData: NodeModel = {
|
const nodeData: NodeModel = {
|
||||||
id: Number(data.id),
|
id: Number(data.id),
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,6 @@ import pkg from "./package.json";
|
|||||||
export default defineConfig(({ command }) => {
|
export default defineConfig(({ command }) => {
|
||||||
rmSync("dist-electron", { recursive: true, force: true });
|
rmSync("dist-electron", { recursive: true, force: true });
|
||||||
|
|
||||||
const isServe = command === "serve";
|
|
||||||
const isBuild = false; //command === "build";
|
const isBuild = false; //command === "build";
|
||||||
const sourcemap = true; //isServe || !!process.env.VSCODE_DEBUG;
|
const sourcemap = true; //isServe || !!process.env.VSCODE_DEBUG;
|
||||||
|
|
||||||
@@ -38,6 +37,7 @@ export default defineConfig(({ command }) => {
|
|||||||
vite: {
|
vite: {
|
||||||
build: {
|
build: {
|
||||||
sourcemap,
|
sourcemap,
|
||||||
|
target: ["chrome89", "edge89", "firefox89", "safari15"],
|
||||||
minify: isBuild,
|
minify: isBuild,
|
||||||
outDir: "dist-electron/main",
|
outDir: "dist-electron/main",
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
|
|||||||
Reference in new issue
Block a user