12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- const webpack = require("webpack");
- const { resolve } = require("path");
- const ora = require("ora");
- const { spawn } = require("child_process");
- const rm = require("rimraf");
- const chalk = require("chalk");
- const webpackConfig = require("./webpack.config.main.js");
- const electron = require("electron");
- const spinner = ora("building electron...");
- spinner.start();
- let electronProcess = null;
- const isDev = process.env.NODE_ENV !== "production";
- const startMain = () => {
- return new Promise((resolve) => {
- rm("./dist/main.js", (removeErr) => {
- if (removeErr) {
- throw removeErr;
- }
- webpackConfig.mode = isDev ? "development" : "production";
- const compiler = webpack(webpackConfig);
- compiler.watch({}, (err, stats) => {
- if (err) throw err;
- spinner.stop();
- process.stdout.write(
- stats.toString({
- colors: true,
- modules: false,
- children: false,
- chunks: false,
- chunkModules: false,
- }) + "\n\n"
- );
- if (stats.hasErrors()) {
- console.log(chalk.red("Build failed with errors.\n"));
- process.exit(1);
- }
- resolve();
- console.log(
- chalk.cyan(
- `Build complete in ${stats.endTime - stats.startTime}ms.\n`
- )
- );
- if (process.env.IS_BUILDER === "builder") process.exit();
- });
- });
- });
- };
- const startElectron = () => {
- if (process.env.IS_BUILDER === "builder") return;
- var args = ["--inspect=5858", resolve(__dirname, "../dist/main.js")];
- electronProcess = spawn(electron, args);
- if (isDev) {
- electronProcess.stdout.on("data", (data) => {
- console.log(chalk.blue(data.toString()));
- });
- electronProcess.stderr.on("data", (data) => {
- console.log(chalk.blue(data.toString()));
- });
- }
- electronProcess.on("close", () => {
- process.exit();
- });
- };
- async function init() {
- try {
- await startMain();
- await startElectron();
- } catch (error) {
- console.error(error);
- }
- }
- init();
|