index.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 from '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. // Register tabby:// URL scheme
  28. if (process.defaultApp) {
  29. if (process.argv.length >= 2) {
  30. app.setAsDefaultProtocolClient('tabby', process.execPath, [process.argv[1]])
  31. }
  32. } else {
  33. app.setAsDefaultProtocolClient('tabby')
  34. }
  35. ipcMain.on('app:new-window', () => {
  36. application.newWindow()
  37. })
  38. process.on('uncaughtException' as any, err => {
  39. console.log(err)
  40. application.broadcast('uncaughtException', err)
  41. })
  42. if (argv.d) {
  43. electronDebug({
  44. isEnabled: true,
  45. showDevTools: true,
  46. devToolsMode: 'undocked',
  47. })
  48. }
  49. app.on('activate', async () => {
  50. if (!application.hasWindows()) {
  51. application.newWindow()
  52. } else {
  53. application.focus()
  54. }
  55. })
  56. // Handle URL scheme on macOS
  57. app.on('open-url', async (event, url) => {
  58. event.preventDefault()
  59. console.log('Received open-url event:', url)
  60. if (!application.hasWindows()) {
  61. process.argv.push(url)
  62. } else {
  63. await app.whenReady()
  64. application.handleSecondInstance([url], process.cwd())
  65. }
  66. })
  67. app.on('second-instance', async (_event, newArgv, cwd) => {
  68. application.handleSecondInstance(newArgv, cwd)
  69. })
  70. if (!app.requestSingleInstanceLock()) {
  71. app.quit()
  72. app.exit(0)
  73. }
  74. app.on('ready', async () => {
  75. if (process.platform === 'darwin') {
  76. app.dock.setMenu(Menu.buildFromTemplate([
  77. {
  78. label: 'New window',
  79. click () {
  80. this.app.newWindow()
  81. },
  82. },
  83. ]))
  84. }
  85. application.init()
  86. const window = await application.newWindow({ hidden: argv.hidden })
  87. await window.ready
  88. window.passCliArguments(process.argv, process.cwd(), false)
  89. window.focus()
  90. })