main.js 3.8 KB

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