main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const { app, BrowserWindow } = require('electron');
  2. const file = require('./helper/file');
  3. const window = require('./helper/window');
  4. const path = require('path');
  5. const { execSync } = require('child_process');
  6. const { program } = require('commander');
  7. app.allowRendererProcessReuse = true;
  8. // Handle creating/removing shortcuts on Windows when installing/uninstalling.
  9. if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  10. app.quit();
  11. }
  12. /* --- Initialisation --- */
  13. // Global var
  14. global.mainWindow;
  15. let launchInfo;
  16. let indexFile = 'src/index.html';
  17. // Add version and option
  18. program.version('0.0.1');
  19. program
  20. .option('-s, --src', 'Load the file from src folder instead of the app folder');
  21. program.parse(process.argv);
  22. // Check Option
  23. const folder = program.src ? '/' : '/app/';
  24. /* --- Functions --- */
  25. // Main function
  26. async function main() {
  27. // Check if launch-info.json exist
  28. console.log('Reading launch info');
  29. if (file.exist(path.join(__dirname, '/data/launch-info.json'))) {
  30. launchInfo = require('./data/launch-info');
  31. // Reset data if change source between src and app
  32. if (launchInfo.folder !== folder) {
  33. indexFile = 'src/reset.html';
  34. }
  35. // Update launch info
  36. launchInfo.folder = folder
  37. } else {
  38. launchInfo = {
  39. folder: folder
  40. }
  41. }
  42. // Edit launch info file
  43. file.put(path.join(__dirname, '/data/launch-info.json'), JSON.stringify(launchInfo));
  44. // if launch the advanced electron app
  45. if (!program.src) {
  46. // Loading main process files
  47. loadMainProcessFiles();
  48. // Compile SCSS file from app
  49. console.info('Compiling SCSS files in CSS');
  50. let scssFile = path.join(__dirname, folder, 'src/css/style.scss');
  51. let cssFile = path.join(__dirname, folder, 'src/css/style.min.css');
  52. execSync(`node-sass --output-style compressed ${scssFile} ${cssFile}`);
  53. scssFile = path.join(__dirname, folder, 'src/css/materialize/materialize.scss');
  54. cssFile = path.join(__dirname, folder, 'src/css/materialize.min.css');
  55. execSync(`node-sass --output-style compressed ${scssFile} ${cssFile}`);
  56. }
  57. // Load main window
  58. console.info('Load main window');
  59. createMainWindow(program.src);
  60. }
  61. // Create main window function
  62. function createMainWindow(simple = false) {
  63. if (simple) {
  64. mainWindow = window.simple(path.join(__dirname, folder, indexFile));
  65. } else {
  66. mainWindow = window.new(path.join(__dirname, folder, indexFile));
  67. }
  68. mainWindow.on('close', () => {
  69. mainWindow = null;
  70. });
  71. }
  72. function loadMainProcessFiles() {
  73. const mainProcessFiles = file.list(path.join(__dirname, folder, 'main/'));
  74. mainProcessFiles.forEach(f => {
  75. const filepath = path.join(__dirname, folder, 'main/', f)
  76. if (file.isFile(filepath)) {
  77. require(filepath);
  78. }
  79. });
  80. }
  81. /* --- Electron app actions --- */
  82. // This method will be called when Electron is ready
  83. app.on('ready', main);
  84. // Quit when all windows are closed.
  85. app.on('window-all-closed', () => {
  86. // On OS X it is common for applications and their menu bar
  87. // to stay active until the user quits explicitly with Cmd + Q
  88. if (process.platform !== 'darwin') {
  89. app.quit();
  90. }
  91. });
  92. // Re-open
  93. app.on('activate', () => {
  94. // On OS X it's common to re-create a window in the app when the
  95. // dock icon is clicked and there are no other windows open.
  96. if (BrowserWindow.getAllWindows().length === 0) {
  97. createMainWindow(program.src);
  98. }
  99. });