index.ts 2.0 KB

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