main.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const { app, BrowserWindow } = require('electron');
  2. const window = require('./helper/window');
  3. const path = require('path');
  4. const { execSync } = require('child_process');
  5. const { program } = require('commander');
  6. // Handle creating/removing shortcuts on Windows when installing/uninstalling.
  7. if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  8. app.quit();
  9. }
  10. /* --- Initialisation --- */
  11. // Global var
  12. let mainWindow;
  13. let indexFile = 'src/index.html';
  14. // Add version and option
  15. program.version('0.0.1');
  16. program
  17. .option('-s, --src', 'Load the file from src folder instead of the app folder');
  18. program.parse(process.argv);
  19. // Check Option
  20. const folder = program.src ? '/' : '/app/';
  21. /* --- Functions --- */
  22. // Main function
  23. async function main() {
  24. if (!program.src) {
  25. // Compile SCSS file from app
  26. console.info('Compiling SCSS files in CSS');
  27. let scssFile = path.join(__dirname, folder, 'src/css/style.scss');
  28. let cssFile = path.join(__dirname, folder, 'src/css/style.min.css');
  29. execSync(`node-sass --output-style compressed ${scssFile} ${cssFile}`);
  30. scssFile = path.join(__dirname, folder, 'src/css/materialize/materialize.scss');
  31. cssFile = path.join(__dirname, folder, 'src/css/materialize.min.css');
  32. execSync(`node-sass --output-style compressed ${scssFile} ${cssFile}`);
  33. }
  34. // Load main window
  35. console.info('Load main window');
  36. createMainWindow(program.src);
  37. }
  38. // Create main window function
  39. async function createMainWindow(simple = false) {
  40. if (simple) {
  41. mainWindow = window.simple(path.join(__dirname, folder, indexFile));
  42. } else {
  43. mainWindow = window.new(path.join(__dirname, folder, indexFile));
  44. }
  45. mainWindow.on('close', () => {
  46. mainWindow = null;
  47. });
  48. }
  49. /* --- Electron app actions --- */
  50. // This method will be called when Electron is ready
  51. app.on('ready', main);
  52. // Quit when all windows are closed.
  53. app.on('window-all-closed', () => {
  54. // On OS X it is common for applications and their menu bar
  55. // to stay active until the user quits explicitly with Cmd + Q
  56. if (process.platform !== 'darwin') {
  57. app.quit();
  58. }
  59. });
  60. // Re-open
  61. app.on('activate', () => {
  62. // On OS X it's common to re-create a window in the app when the
  63. // dock icon is clicked and there are no other windows open.
  64. if (BrowserWindow.getAllWindows().length === 0) {
  65. createMainWindow(program.src);
  66. }
  67. });