index.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. const application = new Application(configStore)
  26. ipcMain.on('app:new-window', () => {
  27. application.newWindow()
  28. })
  29. process.on('uncaughtException' as any, err => {
  30. console.log(err)
  31. application.broadcast('uncaughtException', err)
  32. })
  33. if (argv.d) {
  34. electronDebug({
  35. isEnabled: true,
  36. showDevTools: true,
  37. devToolsMode: 'undocked',
  38. })
  39. }
  40. app.on('activate', async () => {
  41. if (!application.hasWindows()) {
  42. application.newWindow()
  43. } else {
  44. application.focus()
  45. }
  46. })
  47. app.on('second-instance', async (_event, newArgv, cwd) => {
  48. application.handleSecondInstance(newArgv, cwd)
  49. })
  50. if (!app.requestSingleInstanceLock()) {
  51. app.quit()
  52. app.exit(0)
  53. }
  54. app.on('ready', async () => {
  55. if (process.platform === 'darwin') {
  56. app.dock.setMenu(Menu.buildFromTemplate([
  57. {
  58. label: 'New window',
  59. click () {
  60. this.app.newWindow()
  61. },
  62. },
  63. ]))
  64. }
  65. application.init()
  66. const window = await application.newWindow({ hidden: argv.hidden })
  67. await window.ready
  68. window.passCliArguments(process.argv, process.cwd(), false)
  69. window.focus()
  70. })