main.js 3.6 KB

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