jan/electron/main.ts
Louis 5250061c11
feat: Jan Server, API and decoupled clients (#948)
* chore: expose fs apis

* chore: correct electron import path

* update download api

Signed-off-by: James <james@jan.ai>

* update chat_completion

Signed-off-by: James <james@jan.ai>

* fix electron import

Signed-off-by: James <james@jan.ai>

* feat: adding API support at 1337 (#991)

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>

* feat: Add /chat/completion api and handler

* chore: add todo for modelList

* chore: read engine.json for openai chat_completion (#1030)

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>

* refactor: move routes to shared node module

* refactor: exported modules from core with types (#1172)

* refactor: exported modules from core with types

* fix: fix file reading args

* refactor: fileManager handles

* fix: app issues with server refactoring

* refactor: shared server module (#1210)

* chore: resolve main

* chore: update makefile

---------

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
Co-authored-by: NamH <NamNh0122@gmail.com>
Co-authored-by: hiro <vuonghoainam.work@gmail.com>
2023-12-28 17:57:39 +07:00

92 lines
2.2 KiB
TypeScript

import { app, BrowserWindow } from 'electron'
import { join } from 'path'
import { setupMenu } from './utils/menu'
import { createUserSpace } from './utils/path'
/**
* Managers
**/
import { WindowManager } from './managers/window'
import { ExtensionManager, ModuleManager } from '@janhq/core/node'
/**
* IPC Handlers
**/
import { handleDownloaderIPCs } from './handlers/download'
import { handleExtensionIPCs } from './handlers/extension'
import { handleFileMangerIPCs } from './handlers/fileManager'
import { handleAppIPCs } from './handlers/app'
import { handleAppUpdates } from './handlers/update'
import { handleFsIPCs } from './handlers/fs'
import { migrateExtensions } from './utils/migration'
/**
* Server
*/
import { startServer } from '@janhq/server'
app
.whenReady()
.then(createUserSpace)
.then(migrateExtensions)
.then(ExtensionManager.instance.setupExtensions)
.then(setupMenu)
.then(handleIPCs)
.then(handleAppUpdates)
.then(createMainWindow)
.then(startServer)
.then(() => {
app.on('activate', () => {
if (!BrowserWindow.getAllWindows().length) {
createMainWindow()
}
})
})
app.on('window-all-closed', () => {
ModuleManager.instance.clearImportedModules()
app.quit()
})
app.on('quit', () => {
ModuleManager.instance.clearImportedModules()
app.quit()
})
function createMainWindow() {
/* Create main window */
const mainWindow = WindowManager.instance.createWindow({
webPreferences: {
nodeIntegration: true,
preload: join(__dirname, 'preload.js'),
webSecurity: false,
},
})
const startURL = app.isPackaged
? `file://${join(__dirname, '..', 'renderer', 'index.html')}`
: 'http://localhost:3000'
/* Load frontend app to the window */
mainWindow.loadURL(startURL)
mainWindow.once('ready-to-show', () => mainWindow?.show())
mainWindow.on('closed', () => {
if (process.platform !== 'darwin') app.quit()
})
/* Enable dev tools for development */
if (!app.isPackaged) mainWindow.webContents.openDevTools()
}
/**
* Handles various IPC messages from the renderer process.
*/
function handleIPCs() {
handleFsIPCs()
handleDownloaderIPCs()
handleExtensionIPCs()
handleAppIPCs()
handleFileMangerIPCs()
}