main.js 1.7 KB

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