index.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { app, ipcMain, Menu, dialog } from 'electron'
  2. // set defaults of environment variables
  3. import 'dotenv/config'
  4. process.env.TABBY_PLUGINS ??= ''
  5. process.env.CONFIG_DIRECTORY ??= app.getPath('userData')
  6. import 'v8-compile-cache'
  7. import './portable'
  8. import 'source-map-support/register'
  9. import './sentry'
  10. import './lru'
  11. import { parseArgs } from './cli'
  12. import { Application } from './app'
  13. import electronDebug = require('electron-debug')
  14. import { loadConfig } from './config'
  15. const argv = parseArgs(process.argv, process.cwd())
  16. // eslint-disable-next-line @typescript-eslint/init-declarations
  17. let configStore: any
  18. try {
  19. configStore = loadConfig()
  20. } catch (err) {
  21. dialog.showErrorBox('Could not read config', err.message)
  22. app.exit(1)
  23. }
  24. const application = new Application(configStore)
  25. ipcMain.on('app:new-window', () => {
  26. application.newWindow()
  27. })
  28. process.on('uncaughtException' as any, err => {
  29. console.log(err)
  30. application.broadcast('uncaughtException', err)
  31. })
  32. if (argv.d) {
  33. electronDebug({
  34. isEnabled: true,
  35. showDevTools: true,
  36. devToolsMode: 'undocked',
  37. })
  38. }
  39. app.on('activate', async () => {
  40. if (!application.hasWindows()) {
  41. application.newWindow()
  42. } else {
  43. application.focus()
  44. }
  45. })
  46. app.on('second-instance', async (_event, newArgv, cwd) => {
  47. application.handleSecondInstance(newArgv, cwd)
  48. })
  49. if (!app.requestSingleInstanceLock()) {
  50. app.quit()
  51. app.exit(0)
  52. }
  53. app.on('ready', async () => {
  54. if (process.platform === 'darwin') {
  55. app.dock.setMenu(Menu.buildFromTemplate([
  56. {
  57. label: 'New window',
  58. click () {
  59. this.app.newWindow()
  60. },
  61. },
  62. ]))
  63. }
  64. application.init()
  65. const window = await application.newWindow({ hidden: argv.hidden })
  66. await window.ready
  67. window.passCliArguments(process.argv, process.cwd(), false)
  68. window.focus()
  69. })