Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

122 righe
3.1 KiB

  1. process.env.NODE_ENV = 'development';
  2. const Vite = require('vite');
  3. const ChildProcess = require('child_process');
  4. const Path = require('path');
  5. const Chalk = require('chalk');
  6. const Chokidar = require('chokidar');
  7. const Electron = require('electron');
  8. const compileTs = require('./private/tsc');
  9. const FileSystem = require('fs');
  10. const { EOL } = require('os');
  11. let viteServer = null;
  12. let electronProcess = null;
  13. let electronProcessLocker = false;
  14. let rendererPort = 0;
  15. async function startRenderer() {
  16. viteServer = await Vite.createServer({
  17. configFile: Path.join(__dirname, '..', 'vite.config.js'),
  18. mode: 'development',
  19. });
  20. return viteServer.listen();
  21. }
  22. async function startElectron() {
  23. if (electronProcess) { // single instance lock
  24. return;
  25. }
  26. try {
  27. await compileTs(Path.join(__dirname, '..', 'src', 'main'));
  28. } catch {
  29. console.log(Chalk.redBright('Could not start Electron because of the above typescript error(s).'));
  30. electronProcessLocker = false;
  31. return;
  32. }
  33. const args = [
  34. Path.join(__dirname, '..', 'build', 'main', 'main.js'),
  35. rendererPort,
  36. ];
  37. electronProcess = ChildProcess.spawn(Electron, args);
  38. electronProcessLocker = false;
  39. electronProcess.stdout.on('data', data => {
  40. if (data == EOL) {
  41. return;
  42. }
  43. process.stdout.write(Chalk.blueBright(`[electron] `) + Chalk.white(data.toString()))
  44. });
  45. electronProcess.stderr.on('data', data =>
  46. process.stderr.write(Chalk.blueBright(`[electron] `) + Chalk.white(data.toString()))
  47. );
  48. electronProcess.on('exit', () => stop());
  49. }
  50. function restartElectron() {
  51. if (electronProcess) {
  52. electronProcess.removeAllListeners('exit');
  53. electronProcess.kill();
  54. electronProcess = null;
  55. }
  56. if (!electronProcessLocker) {
  57. electronProcessLocker = true;
  58. startElectron();
  59. }
  60. }
  61. function copyStaticFiles() {
  62. copy('static');
  63. }
  64. /*
  65. The working dir of Electron is build/main instead of src/main because of TS.
  66. tsc does not copy static files, so copy them over manually for dev server.
  67. */
  68. function copy(path) {
  69. FileSystem.cpSync(
  70. Path.join(__dirname, '..', 'src', 'main', path),
  71. Path.join(__dirname, '..', 'build', 'main', path),
  72. { recursive: true }
  73. );
  74. }
  75. function stop() {
  76. viteServer.close();
  77. process.exit();
  78. }
  79. async function start() {
  80. console.log(`${Chalk.greenBright('=======================================')}`);
  81. console.log(`${Chalk.greenBright('Starting Electron + Vite Dev Server...')}`);
  82. console.log(`${Chalk.greenBright('=======================================')}`);
  83. const devServer = await startRenderer();
  84. rendererPort = devServer.config.server.port;
  85. copyStaticFiles();
  86. startElectron();
  87. const path = Path.join(__dirname, '..', 'src', 'main');
  88. Chokidar.watch(path, {
  89. cwd: path,
  90. }).on('change', (path) => {
  91. console.log(Chalk.blueBright(`[electron] `) + `Change in ${path}. reloading... 🚀`);
  92. if (path.startsWith(Path.join('static', '/'))) {
  93. copy(path);
  94. }
  95. restartElectron();
  96. });
  97. }
  98. start();