main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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('1.0.0');
  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. launchInfo.export = config.defaultExportToMarkdown ? 'md' : 'json';
  41. }
  42. // Update launch info
  43. launchInfo.folder = folder
  44. // Read export method
  45. console.log(launchInfo.export);
  46. exportToMd = launchInfo.export === 'md';
  47. console.log(exportToMd);
  48. } else {
  49. // Create default launch info
  50. launchInfo = {
  51. folder: folder,
  52. export: exportToMd ? 'md' : 'json'
  53. }
  54. }
  55. // if launch the advanced electron app
  56. if (!program.src) {
  57. // Loading main process files
  58. loadMainProcessFiles();
  59. // Compile SCSS file from app
  60. console.info('Compiling SCSS files in CSS');
  61. let scssFile = path.join(__dirname, folder, 'common/css/style.scss');
  62. let cssFile = path.join(__dirname, folder, 'common/css/style.min.css');
  63. execSync(`node-sass --output-style compressed ${scssFile} ${cssFile}`);
  64. scssFile = path.join(__dirname, folder, 'common/css/materialize/materialize.scss');
  65. cssFile = path.join(__dirname, folder, 'common/css/materialize.min.css');
  66. execSync(`node-sass --output-style compressed ${scssFile} ${cssFile}`);
  67. }
  68. // Load main window
  69. console.info('Load main window');
  70. createMainWindow(program.src);
  71. }
  72. // Create main window function
  73. function createMainWindow(simple = false) {
  74. if (simple) {
  75. mainWindow = window.simple(path.join(__dirname, folder, indexFile));
  76. } else {
  77. mainWindow = window.new(path.join(__dirname, folder, indexFile));
  78. }
  79. mainWindow.on('close', () => {
  80. mainWindow = null;
  81. });
  82. }
  83. function loadMainProcessFiles() {
  84. const mainProcessFiles = file.list(path.join(__dirname, '/app/main/'));
  85. mainProcessFiles.forEach(f => {
  86. const filepath = path.join(__dirname, '/app/main/', f)
  87. if (file.isFile(filepath)) {
  88. require(filepath);
  89. }
  90. });
  91. }
  92. /* --- Electron app actions --- */
  93. // This method will be called when Electron is ready
  94. app.on('ready', main);
  95. // Quit when all windows are closed.
  96. app.on('window-all-closed', () => {
  97. // On OS X it is common for applications and their menu bar
  98. // to stay active until the user quits explicitly with Cmd + Q
  99. // The application don't quit if the tray is active
  100. if (process.platform !== 'darwin' && trayInactive) {
  101. app.quit();
  102. }
  103. });
  104. // Re-open
  105. app.on('activate', () => {
  106. // On OS X it's common to re-create a window in the app when the
  107. // dock icon is clicked and there are no other windows open.
  108. if (BrowserWindow.getAllWindows().length === 0) {
  109. createMainWindow(program.src);
  110. }
  111. });
  112. // Application stop
  113. app.on('quit', () => {
  114. // Write launch info file
  115. launchInfo.export = exportToMd ? 'md' : 'json';
  116. file.put(path.join(__dirname, '/data/launch-info.json'), JSON.stringify(launchInfo));
  117. // Delete export files
  118. if(file.exist('./data/dragdrop/')) {
  119. console.log('Deleting all exported files')
  120. del.sync(['./data/dragdrop/**', './data/dragdrop']);
  121. }
  122. });