From 3de69035945858628ebaf5892e77e3cf05ce1075 Mon Sep 17 00:00:00 2001 From: codetypes Date: Sat, 1 Mar 2025 16:55:36 +0800 Subject: [PATCH] support build from command line --- electron/main/index.ts | 158 ++++++++++++++++++++++------------------- package.json | 2 +- vite.config.ts | 8 ++- 3 files changed, 93 insertions(+), 75 deletions(-) diff --git a/electron/main/index.ts b/electron/main/index.ts index f0f7b41..0e5e3f3 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -1,7 +1,91 @@ import { app, BrowserWindow, ipcMain, shell } from "electron"; +import * as fs from "node:fs"; import { release } from "node:os"; import path, { join } from "node:path"; import { argv } from "node:process"; +import * as b3util from "../../src/misc/b3util"; +import Path from "../../src/misc/path"; + +// Check command line arguments +let buildProject: string | undefined; +let buildOutput: string | undefined; +let buildHelp: boolean = false; + +for (let i = 0; i < argv.length; i++) { + const arg = argv[i]; + if (arg === "--project") { + buildProject = argv[i + 1]; + i++; + } else if (arg === "--build") { + buildOutput = argv[i + 1]; + i++; + } else if (arg === "--help") { + buildHelp = true; + } +} + +const printHelp = () => { + console.log("Usage: Behavior3 Editor [options]"); + console.log("Options:"); + console.log(" --project Set the project path"); + console.log(" --build Set the build output path"); + console.log(" --help Print this help"); +}; + +if (buildOutput || buildProject || buildHelp) { + if (buildHelp) { + printHelp(); + app.quit(); + process.exit(1); + } else if (!buildOutput || !buildProject) { + console.error("build output or project is not set"); + printHelp(); + app.quit(); + process.exit(1); + } + try { + let hasError = false; + const project = Path.posixPath(buildProject!); + const buildDir = Path.posixPath(buildOutput!); + console.log("start build project:", project); + if (!project.endsWith(".b3-workspace")) { + throw new Error(`'${project}' is not a workspace`); + } + const workdir = Path.dirname(project); + b3util.initWorkdir(workdir, (msg) => { + console.error(`${msg}`); + }); + for (const path of Path.ls(Path.dirname(project), true)) { + if (path.endsWith(".json")) { + const buildpath = buildDir + "/" + path.substring(workdir.length + 1); + const treeModel = b3util.createBuildData(path); + if (treeModel && treeModel.export === false) { + console.log("skip:", buildpath); + continue; + } + console.log("build:", buildpath); + if (!b3util.checkNodeData(treeModel?.root)) { + hasError = true; + } + fs.mkdirSync(Path.dirname(buildpath), { recursive: true }); + fs.writeFileSync(buildpath, JSON.stringify(treeModel, null, 2)); + } + } + if (hasError) { + console.error("build failed***"); + app.quit(); + process.exit(1); + } else { + console.log("build completed"); + } + } catch (error) { + console.error("build failed***"); + app.quit(); + process.exit(1); + } + app.quit(); + process.exit(0); +} // The built directory structure // @@ -13,7 +97,7 @@ import { argv } from "node:process"; // ├─┬ dist // │ └── index.html > Electron-Renderer // -process.env.DIST_ELECTRON = join(__dirname, "../"); +process.env.DIST_ELECTRON = join(__dirname, "../../"); process.env.DIST = join(process.env.DIST_ELECTRON, "../dist"); process.env.PUBLIC = process.env.VITE_DEV_SERVER_URL ? join(process.env.DIST_ELECTRON, "../public") @@ -45,78 +129,6 @@ const url = process.env.VITE_DEV_SERVER_URL; const indexHtml = join(process.env.DIST, "index.html"); const windows: Workspace[] = []; -let buildProject: string | undefined; -let buildOutput: string | undefined; -let buildHelp: boolean = false; - -for (let i = 0; i < argv.length; i++) { - const arg = argv[i]; - if (arg === "--project") { - buildProject = argv[i + 1]; - i++; - } else if (arg === "--build") { - buildOutput = argv[i + 1]; - i++; - } else if (arg === "--help") { - buildHelp = true; - } -} - -const printHelp = () => { - console.log("Usage: Behavior3 Editor [options]"); - console.log("Options:"); - console.log(" --project Set the project path"); - console.log(" --build Set the build output path"); - console.log(" --help Print this help"); -}; - -if (buildOutput || buildProject || buildHelp) { - if (buildHelp) { - printHelp(); - app.exit(0); - } else if (!buildOutput || !buildProject) { - console.error("build output or project is not set"); - printHelp(); - app.exit(0); - } - // try { - // let hasError = false; - // const project = Path.posixPath(buildProject!); - // const buildDir = Path.posixPath(buildOutput!); - // if (!project.endsWith(".b3-workspace")) { - // throw new Error(`'${project}' is not a workspace`); - // } - // const workdir = Path.dirname(project); - // b3util.initWorkdir(workdir, (msg) => { - // console.error(msg); - // }); - // for (const path of Path.ls(Path.dirname(project), true)) { - // if (path.endsWith(".json")) { - // const buildpath = buildDir + "/" + path.substring(workdir.length + 1); - // const treeModel = b3util.createBuildData(path); - // if (treeModel && treeModel.export === false) { - // console.log("skip:", buildpath); - // continue; - // } - // console.log("build:", buildpath); - // if (!b3util.checkNodeData(treeModel?.root)) { - // hasError = true; - // } - // fs.mkdirSync(Path.dirname(buildpath), { recursive: true }); - // fs.writeFileSync(buildpath, JSON.stringify(treeModel, null, 2)); - // } - // } - // if (hasError) { - // console.error("build failed"); - // } else { - // console.log("build completed"); - // } - // } catch (error) { - // console.error("build failed"); - // } - app.exit(0); -} - async function createWindow(projectPath?: string) { const win = new BrowserWindow({ title: "Behaviour3 Editor", diff --git a/package.json b/package.json index 501d483..3267777 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "behavior3editor", "version": "1.7.0", - "main": "dist-electron/main/index.js", + "main": "dist-electron/electron/main/index.js", "description": "行为树编辑器", "author": "zhandouxiaojiji & codetypes", "license": "MIT", diff --git a/vite.config.ts b/vite.config.ts index 9e57afb..04476cf 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -19,7 +19,13 @@ export default defineConfig(({ command }) => { plugins: [ react(), electron({ - include: ["electron"], + include: [ + "electron", + "src/misc/b3type.ts", + "src/misc/b3util.ts", + "src/misc/path.ts", + "src/misc/util.ts", + ], transformOptions: { sourcemap, },