/** * electron 打包 */ import { join } from "path" import { exec, spawn, ChildProcess } from "child_process" import { watch, rollup, OutputOptions } from "rollup" import electron from "electron" import { waitOn } from "./utils" import options from "./rollup.config" import { main } from "../package.json" const ora = require("ora") const dotenv = require("dotenv") const chalk = require("chalk") const minimist = require("minimist") dotenv.config({ path: join(__dirname, "../.env") }) const argv = minimist(process.argv.slice(2)) const opts = options(argv.env) const TAG = "[build-main.ts]" const spinner = ora(`${TAG} Electron build...`) if (argv.watch) { waitOn({ port: process.env.PORT as string }).then(msg => { const watcher = watch(opts) let child: ChildProcess | null let manualRestart = false watcher.on("change", filename => { const log = chalk.green(`change -- ${filename}`) console.log(TAG, log) }) watcher.on("event", ev => { if (ev.code === "END") { if (child && child.kill) { console.log(child.pid) manualRestart = true // child.pid // child.kill() process.kill( child.pid) child = null setTimeout(() => { manualRestart = false }, 5000) } child = exec(`electron-forge start --inspect-electron --app-path ${join(__dirname, `../${main}`)}`, { env: Object.assign(process.env, { NODE_ENV: argv.env }), }) child.on("close", () => { if (!manualRestart) process.exit() }) } else if (ev.code === "ERROR") { console.log(ev.error) } }) }) } else { spinner.start() rollup(opts) .then(build => { spinner.stop() console.log(TAG, chalk.green("Electron build successed.")) build.write(opts.output as OutputOptions) }) .catch(error => { spinner.stop() console.log(`\n${TAG} ${chalk.red("构建报错")}\n`, error, "\n") }) }