Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add cortex version #3318

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions electron/handlers/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -242,16 +247,31 @@ 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(
janModelFolderPath,
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)
Expand Down Expand Up @@ -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))
Expand Down
11 changes: 6 additions & 5 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions electron/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"target": "es5",
"module": "commonjs",
"noImplicitAny": true,
Expand Down
6 changes: 4 additions & 2 deletions electron/utils/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
}),
},
{
Expand All @@ -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' },
Expand Down
4 changes: 2 additions & 2 deletions electron/utils/setup.ts
Original file line number Diff line number Diff line change
@@ -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,
}
}

Expand Down
Loading