electron-main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { app, BrowserWindow, ipcMain } from 'electron'
  2. import ipc from 'electron-better-ipc'
  3. import path from 'path'
  4. import configFile from './configstore'
  5. import * as componentCompiler from './rollup'
  6. import { Repo } from './remote-repos'
  7. import { is } from 'electron-util'
  8. import './store'
  9. const componentsPath = is.development
  10. ? `${__statics}/plugins`
  11. : configFile.get('plugins.destination')
  12. const sahibDocumentsPath = configFile.get('plugins.source')
  13. if (process.env.PROD) {
  14. global.__statics = path.join(__dirname, 'statics').replace(/\\/g, '\\\\')
  15. }
  16. console.log('Rollup watch starten …')
  17. componentCompiler.rollupSetup({
  18. statics: __statics,
  19. source: sahibDocumentsPath,
  20. dest: componentsPath,
  21. is: is
  22. })
  23. let mainWindow
  24. let pdfWindow = null
  25. function createPDFWindow () {
  26. pdfWindow = new BrowserWindow({
  27. show: false,
  28. parent: mainWindow,
  29. width: 800,
  30. height: 600,
  31. webPreferences: {
  32. plugins: true
  33. }
  34. })
  35. pdfWindow.on('closed', () => {
  36. pdfWindow = null
  37. })
  38. }
  39. function createWindow () {
  40. let { width, height } = configFile.get('windowBounds.main')
  41. mainWindow = new BrowserWindow({
  42. title: 'Sahib.js',
  43. width: width,
  44. height: height,
  45. useContentSize: true,
  46. icon: path.join(__dirname, '../icons/linux-256x256.png')
  47. })
  48. mainWindow.loadURL(process.env.APP_URL)
  49. // mainWindow.openDevTools()
  50. mainWindow.on('closed', () => {
  51. mainWindow = null
  52. })
  53. mainWindow.on('resize', () => {
  54. let { width, height } = mainWindow.getBounds()
  55. configFile.set('windowBounds.main', { width, height })
  56. })
  57. mainWindow.webContents.on('did-finish-load', () => {
  58. const watcher = componentCompiler.rollupWatch()
  59. watcher.on('event', event => {
  60. if (event.code === 'END') {
  61. console.log(
  62. new Date().toLocaleDateString('de-DE', {
  63. hour: 'numeric',
  64. minute: 'numeric',
  65. second: 'numeric'
  66. })
  67. )
  68. mainWindow.webContents.send('recompile')
  69. }
  70. })
  71. })
  72. }
  73. app.on('ready', createWindow)
  74. app.on('window-all-closed', () => {
  75. if (process.platform !== 'darwin') {
  76. app.quit()
  77. }
  78. })
  79. app.on('activate', () => {
  80. if (mainWindow === null) {
  81. createWindow()
  82. }
  83. })
  84. ipcMain.on('view-pdf', () => {
  85. if (pdfWindow === null) {
  86. createPDFWindow()
  87. }
  88. pdfWindow.loadURL(`file://${app.getPath('userData')}/print.pdf`)
  89. pdfWindow.show()
  90. })
  91. ipcMain.on('compileDokumente', () => {
  92. compileDokumente()
  93. })
  94. ipcMain.on('pullDokumente', (event, arg) => {
  95. configFile.get('plugins.remoteRepos').forEach(repo => {
  96. const repoPath = `${configFile.get('plugins.source')}/${repo.name}`
  97. Repo.pull(repoPath)
  98. })
  99. compileDokumente()
  100. })
  101. async function compileDokumente () {
  102. await componentCompiler.rollupBuild()
  103. console.log(
  104. new Date().toLocaleDateString('de-DE', {
  105. hour: 'numeric',
  106. minute: 'numeric',
  107. second: 'numeric'
  108. })
  109. )
  110. mainWindow.webContents.send('recompile')
  111. }
  112. ipc.answerRenderer('getRemoteRepos', async getRemoteRepos => {
  113. return configFile.get('plugins.remoteRepos') || []
  114. })
  115. ipc.answerRenderer('setRemoteRepos', async remoteRepos => {
  116. configFile.set('plugins.remoteRepos', remoteRepos)
  117. })
  118. ipc.answerRenderer('cloneRemoteRepo', async remoteRepo => {
  119. const pluginsSource = configFile.get('plugins.source')
  120. const name = await Repo.clone(remoteRepo, pluginsSource)
  121. compileDokumente()
  122. return name
  123. })
  124. ipc.answerRenderer('setDB', async db => {
  125. console.log('Verbindungsdaten speichern …')
  126. configFile.set('db', db)
  127. })