main.js 3.6 KB

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