main.js 3.7 KB

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