dev.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const webpack = require("webpack");
  2. const { resolve } = require("path");
  3. const ora = require("ora");
  4. const { spawn } = require("child_process");
  5. const rm = require("rimraf");
  6. const chalk = require("chalk");
  7. const webpackConfig = require("./webpack.config.main.js");
  8. const electron = require("electron");
  9. const spinner = ora("building development electron...");
  10. spinner.start();
  11. let electronProcess = null;
  12. let manualRestart = false;
  13. const startMain = () => {
  14. return new Promise((resolve) => {
  15. rm("./dist/main.js", (removeErr) => {
  16. if (removeErr) {
  17. throw removeErr;
  18. }
  19. webpackConfig.mode = "development";
  20. const compiler = webpack(webpackConfig);
  21. compiler.watch({}, (err, stats) => {
  22. if (err) throw err;
  23. spinner.stop();
  24. process.stdout.write(
  25. stats.toString({
  26. colors: true,
  27. modules: false,
  28. children: false,
  29. chunks: false,
  30. chunkModules: false,
  31. }) + "\n\n"
  32. );
  33. if (stats.hasErrors()) {
  34. console.log(chalk.red("Build failed with errors.\n"));
  35. process.exit(1);
  36. }
  37. if (electronProcess && electronProcess.kill) {
  38. manualRestart = true;
  39. process.kill(electronProcess.pid);
  40. electronProcess = null;
  41. startElectron();
  42. setTimeout(() => {
  43. manualRestart = false;
  44. }, 5000);
  45. }
  46. resolve();
  47. console.log(
  48. chalk.cyan(
  49. `Build complete development in ${
  50. stats.endTime - stats.startTime
  51. }ms.\n`
  52. )
  53. );
  54. });
  55. });
  56. });
  57. };
  58. const startElectron = () => {
  59. var args = ["--inspect=5858", resolve(__dirname, "../dist/main.js")];
  60. electronProcess = spawn(electron, args);
  61. electronProcess.stdout.on("data", (data) => {
  62. console.log(chalk.blue(data.toString()));
  63. });
  64. electronProcess.stderr.on("data", (data) => {
  65. console.log(chalk.blue(data.toString()));
  66. });
  67. electronProcess.on("close", () => {
  68. if (!manualRestart) process.exit();
  69. });
  70. };
  71. async function init() {
  72. try {
  73. await startMain();
  74. await startElectron();
  75. } catch (error) {
  76. console.error(error);
  77. }
  78. }
  79. init();