main.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. let 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.app === program.src) {
  33. indexFile = 'src/reset.html';
  34. }
  35. // Update launch info
  36. launchInfo.app = !program.src;
  37. } else {
  38. launchInfo = {
  39. app: !program.src
  40. }
  41. }
  42. // Edit launch info file
  43. file.put(path.join(__dirname, '/data/launch-info.json'), JSON.stringify(launchInfo));
  44. // Compile SCSS file from app
  45. if (!program.src) {
  46. console.info('Compiling SCSS files in CSS');
  47. let scssFile = path.join(__dirname, folder, 'src/css/style.scss');
  48. let cssFile = path.join(__dirname, folder, 'src/css/style.min.css');
  49. execSync(`node-sass --output-style compressed ${scssFile} ${cssFile}`);
  50. scssFile = path.join(__dirname, folder, 'src/css/materialize/materialize.scss');
  51. cssFile = path.join(__dirname, folder, 'src/css/materialize.min.css');
  52. execSync(`node-sass --output-style compressed ${scssFile} ${cssFile}`);
  53. }
  54. // Load main window
  55. console.info('Load main window');
  56. createMainWindow(program.src);
  57. }
  58. // Create main window function
  59. function createMainWindow(simple = false) {
  60. if (simple) {
  61. mainWindow = window.simple(path.join(__dirname, folder, indexFile));
  62. } else {
  63. mainWindow = window.new(path.join(__dirname, folder, indexFile));
  64. }
  65. mainWindow.on('close', () => {
  66. mainWindow = null;
  67. });
  68. }
  69. /* --- Electron app actions --- */
  70. // This method will be called when Electron is ready
  71. app.on('ready', main);
  72. // Quit when all windows are closed.
  73. app.on('window-all-closed', () => {
  74. // On OS X it is common for applications and their menu bar
  75. // to stay active until the user quits explicitly with Cmd + Q
  76. if (process.platform !== 'darwin') {
  77. app.quit();
  78. }
  79. });
  80. // Re-open
  81. app.on('activate', () => {
  82. // On OS X it's common to re-create a window in the app when the
  83. // dock icon is clicked and there are no other windows open.
  84. if (BrowserWindow.getAllWindows().length === 0) {
  85. createMainWindow(program.src);
  86. }
  87. });