diff --git a/electron/handlers/native.ts b/electron/handlers/native.ts index 0457c0a3e0..d11dc40d62 100644 --- a/electron/handlers/native.ts +++ b/electron/handlers/native.ts @@ -9,13 +9,19 @@ import { } from '@janhq/core/node' import { menu } from '../utils/menu' import { join } from 'path' -import { getAppConfigurations, getJanDataFolderPath, legacyDataPath, updateAppConfiguration } from './../utils/path' +import { + getAppConfigurations, + getJanDataFolderPath, + legacyDataPath, + updateAppConfiguration, +} from './../utils/path' import { readdirSync, writeFileSync, readFileSync, existsSync, mkdirSync, + lstatSync, } from 'fs' import { dump, load } from 'js-yaml' const isMac = process.platform === 'darwin' @@ -224,7 +230,6 @@ export function handleAppIPCs() { }) ipcMain.handle(NativeRoute.syncModelFileToCortex, async (_event) => { - // Read models from legacy data folder const janModelFolderPath = join(legacyDataPath(), 'models') const allModelFolders = readdirSync(janModelFolderPath) @@ -242,9 +247,20 @@ export function handleAppIPCs() { for (const modelName of allModelFolders) { const modelFolderPath = join(janModelFolderPath, modelName) + // check if exist and is a directory + if (!existsSync(modelFolderPath)) { + console.debug(`Model folder ${modelFolderPath} does not exist`) + continue + } + + // check if it is a directory + if (!lstatSync(modelFolderPath).isDirectory()) { + console.debug(`${modelFolderPath} is not a directory`) + continue + } + try { const filesInModelFolder = readdirSync(modelFolderPath) - const destinationPath = join(destinationFolderPath, modelName) const modelJsonFullPath = join( @@ -252,6 +268,10 @@ export function handleAppIPCs() { modelName, 'model.json' ) + if (!existsSync(modelJsonFullPath)) { + console.error(`Model json file not found in ${modelName}`) + continue + } const model = JSON.parse(readFileSync(modelJsonFullPath, 'utf-8')) const fileNames: string[] = model.sources.map((x: any) => x.filename) @@ -438,13 +458,17 @@ export function handleAppIPCs() { // Migrate models const janModelsPath = join(path, 'models') if (existsSync(janModelsPath)) { - const modelYamls = readdirSync(janModelsPath).filter((x) => - x.endsWith('.yaml') || x.endsWith('.yml') + const modelYamls = readdirSync(janModelsPath).filter( + (x) => x.endsWith('.yaml') || x.endsWith('.yml') ) - for(const yaml of modelYamls) { + for (const yaml of modelYamls) { const modelPath = join(janModelsPath, yaml) const model = load(readFileSync(modelPath, 'utf-8')) as any - if('files' in model && Array.isArray(model.files) && model.files.length > 0) { + if ( + 'files' in model && + Array.isArray(model.files) && + model.files.length > 0 + ) { model.files[0] = model.files[0].replace(currentJanDataFolder, path) } writeFileSync(modelPath, dump(model)) diff --git a/electron/main.ts b/electron/main.ts index e928177b5d..7687ee3b5e 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -34,6 +34,8 @@ const mainPath = join(rendererPath, 'index.html') const mainUrl = 'http://localhost:3000' +import { dependencies } from './package.json' + const gotTheLock = app.requestSingleInstanceLock() if (process.defaultApp) { @@ -52,7 +54,7 @@ const createMainWindow = () => { } log.initialize() -log.info('Log from the main process') +log.info('Starting jan from main thread..') // replace all console.log to log Object.assign(console, log.functions) @@ -63,7 +65,7 @@ const host = '127.0.0.1' app .whenReady() - .then(setupCore) + .then(() => setupCore(dependencies['cortexso'] ?? 'Not found')) .then(() => { if (!gotTheLock) { app.quit() @@ -90,9 +92,9 @@ app .then(() => { const appConfiguration = getAppConfigurations() const janDataFolder = appConfiguration.dataFolderPath - + start('jan', host, cortexJsPort, cortexCppPort, janDataFolder) -}) + }) .then(createUserSpace) .then(migrate) .then(setupMenu) @@ -128,7 +130,6 @@ app.once('window-all-closed', async () => { cleanUpAndQuit() }) - async function stopApiServer() { // this function is not meant to be success. It will throw an error. try { diff --git a/electron/tsconfig.json b/electron/tsconfig.json index 11c9d85770..9a3f5823c6 100644 --- a/electron/tsconfig.json +++ b/electron/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "resolveJsonModule": true, "target": "es5", "module": "commonjs", "noImplicitAny": true, diff --git a/electron/utils/menu.ts b/electron/utils/menu.ts index 8f6cad42d7..1abb0c6ab8 100644 --- a/electron/utils/menu.ts +++ b/electron/utils/menu.ts @@ -12,7 +12,7 @@ const template: (Electron.MenuItemConstructorOptions | Electron.MenuItem)[] = [ click: () => dialog.showMessageBox({ title: `Jan`, - message: `Jan Version v${app.getVersion()}\n\nCopyright © 2024 Jan`, + message: `Jan Version v${app.getVersion()}\nCortex Version ${global.core.cortexVersion()}\n\nCopyright © 2024 Jan`, }), }, { @@ -33,7 +33,9 @@ const template: (Electron.MenuItemConstructorOptions | Electron.MenuItem)[] = [ } }) .catch((error) => { - console.error('Error checking for updates:' + JSON.stringify(error)) + console.error( + 'Error checking for updates:' + JSON.stringify(error) + ) }), }, { type: 'separator' }, diff --git a/electron/utils/setup.ts b/electron/utils/setup.ts index 437e21f977..5efc198bfe 100644 --- a/electron/utils/setup.ts +++ b/electron/utils/setup.ts @@ -1,16 +1,16 @@ import { app } from 'electron' import Store from 'electron-store' - const DEFAULT_WIDTH = 1000 const DEFAULT_HEIGHT = 800 const storage = new Store() -export const setupCore = async () => { +export const setupCore = async (cortexsoVersion: string) => { // Setup core api for main process global.core = { // Define appPath function for app to retrieve app path globally appPath: () => app.getPath('userData'), + cortexVersion: () => cortexsoVersion, } }