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: refactor core and server packages #4373

Merged
merged 2 commits into from
Dec 31, 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ test_results.html
electron/shared/**

# docs
docs/yarn.lock
docs/yarn.lock
electron/.version.bak
26 changes: 10 additions & 16 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"name": "@janhq/core",
"version": "0.1.10",
"description": "Jan app core lib",
"description": "Core library for the Jan AI application framework",
"keywords": [
"jan",
"core"
],
"homepage": "https://jan.ai",
"license": "AGPL-3.0",
"browser": "dist/core.es5.js",
"main": "dist/core.es5.js",
"module": "dist/index.cjs.js",
"browser": "dist/index.js",
"main": "dist/index.js",
"module": "dist/node/index.cjs.js",
"typings": "dist/types/index.d.ts",
"files": [
"dist",
"types"
],
"author": "Jan <[email protected]>",
"exports": {
".": "./dist/core.es5.js",
".": "./dist/index.js",
"./node": "./dist/node/index.cjs.js"
},
"typesVersions": {
"*": {
".": [
"./dist/core.es5.js.map",
"./dist/index.js.map",
"./dist/types/index.d.ts"
],
"node": [
Expand All @@ -37,14 +37,13 @@
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
"test": "jest",
"prebuild": "rimraf dist",
"build": "rollup -c rollup.config.ts"
"build": "tsc -p . && rolldown -c rolldown.config.mjs"
},
"devDependencies": {
"@npmcli/arborist": "^9.0.0",
"@rollup/plugin-replace": "^5.0.5",
"@npmcli/arborist": "^7.1.0",
"@types/jest": "^29.5.14",
"@types/node": "^22.10.0",
"@types/pacote": "^11.1.8",
"@types/pacote": "^11.1.7",
"@types/request": "^2.48.12",
"electron": "33.2.1",
"eslint": "8.57.0",
Expand All @@ -56,12 +55,7 @@
"request": "^2.88.2",
"request-progress": "^3.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.38.5",
"rollup-plugin-commonjs": "^9.1.8",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-sourcemaps": "^0.6.3",
"rollup-plugin-typescript2": "^0.36.0",
"rolldown": "1.0.0-beta.1",
"ts-jest": "^29.2.5",
"tslib": "^2.6.2",
"typescript": "^5.3.3"
Expand Down
51 changes: 51 additions & 0 deletions core/rolldown.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { defineConfig } from 'rolldown'
import pkgJson from './package.json' with { type: 'json' }

export default defineConfig([
{
input: 'src/index.ts',
output: {
format: 'esm',
file: 'dist/index.js',
sourcemap: true,
},
platform: 'browser',
external: ['path'],
define: {
NODE: JSON.stringify(`${pkgJson.name}/${pkgJson.node}`),
VERSION: JSON.stringify(pkgJson.version),
},
},
{
input: 'src/node/index.ts',
external: [
'fs/promises',
'path',
'pacote',
'@types/pacote',
'@npmcli/arborist',
'ulidx',
'node-fetch',
'fs',
'request',
'crypto',
'url',
'http',
'os',
'util',
'child_process',
'electron',
'request-progress',
],
output: {
format: 'cjs',
file: 'dist/node/index.cjs.js',
sourcemap: true,
inlineDynamicImports: true,
},
resolve: {
extensions: ['.js', '.ts'],
},
platform: 'node',
},
])
93 changes: 0 additions & 93 deletions core/rollup.config.ts

This file was deleted.

49 changes: 31 additions & 18 deletions core/src/browser/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { DownloadRequest, FileStat, NetworkConfig, SystemInformation } from '../types'
import {
DownloadRequest,
FileStat,
NetworkConfig,
SystemInformation,
} from '../types'

/**
* Execute a extension module function in main process
Expand All @@ -9,11 +14,12 @@
* @returns Promise<any>
*
*/
const executeOnMain: (extension: string, method: string, ...args: any[]) => Promise<any> = (
extension,
method,
...args
) => globalThis.core?.api?.invokeExtensionFunc(extension, method, ...args)
const executeOnMain: (
extension: string,
method: string,
...args: any[]
) => Promise<any> = (extension, method, ...args) =>
globalThis.core?.api?.invokeExtensionFunc(extension, method, ...args)

/**
* Downloads a file from a URL and saves it to the local file system.
Expand All @@ -23,10 +29,11 @@
*
* @returns {Promise<any>} A promise that resolves when the file is downloaded.
*/
const downloadFile: (downloadRequest: DownloadRequest, network?: NetworkConfig) => Promise<any> = (
downloadRequest,
network
) => globalThis.core?.api?.downloadFile(downloadRequest, network)
const downloadFile: (
downloadRequest: DownloadRequest,
network?: NetworkConfig
) => Promise<any> = (downloadRequest, network) =>
globalThis.core?.api?.downloadFile(downloadRequest, network)

Check warning on line 36 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

36 line is not covered with tests

/**
* Aborts the download of a specific file.
Expand All @@ -41,7 +48,8 @@
*
* @returns {Promise<string>} A Promise that resolves with Jan's data folder path.
*/
const getJanDataFolderPath = (): Promise<string> => globalThis.core.api?.getJanDataFolderPath()
const getJanDataFolderPath = (): Promise<string> =>
globalThis.core.api?.getJanDataFolderPath()

/**
* Opens the file explorer at a specific path.
Expand All @@ -64,14 +72,16 @@
* @param path - The file path to retrieve dirname.
* @returns {Promise<string>} A promise that resolves the dirname.
*/
const dirName: (path: string) => Promise<string> = (path) => globalThis.core.api?.dirName(path)
const dirName: (path: string) => Promise<string> = (path) =>
globalThis.core.api?.dirName(path)

Check warning on line 76 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

76 line is not covered with tests

/**
* Retrieve the basename from an url.
* @param path - The path to retrieve.
* @returns {Promise<string>} A promise that resolves with the basename.
*/
const baseName: (paths: string) => Promise<string> = (path) => globalThis.core.api?.baseName(path)
const baseName: (paths: string) => Promise<string> = (path) =>
globalThis.core.api?.baseName(path)

Check warning on line 84 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

84 line is not covered with tests

/**
* Opens an external URL in the default web browser.
Expand All @@ -87,13 +97,15 @@
*
* @returns {Promise<string>} - A promise that resolves with the resource path.
*/
const getResourcePath: () => Promise<string> = () => globalThis.core.api?.getResourcePath()
const getResourcePath: () => Promise<string> = () =>
globalThis.core.api?.getResourcePath()

Check warning on line 101 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

101 line is not covered with tests

/**
* Gets the user's home path.
* @returns return user's home path
*/
const getUserHomePath = (): Promise<string> => globalThis.core.api?.getUserHomePath()
const getUserHomePath = (): Promise<string> =>
globalThis.core.api?.getUserHomePath()

Check warning on line 108 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

108 line is not covered with tests

/**
* Log to file from browser processes.
Expand All @@ -101,7 +113,7 @@
* @param message - Message to log.
*/
const log: (message: string, fileName?: string) => void = (message, fileName) =>
globalThis.core.api?.log(message, fileName)

Check warning on line 116 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

116 line is not covered with tests

/**
* Check whether the path is a subdirectory of another path.
Expand All @@ -111,15 +123,17 @@
*
* @returns {Promise<boolean>} - A promise that resolves with a boolean indicating whether the path is a subdirectory.
*/
const isSubdirectory: (from: string, to: string) => Promise<boolean> = (from: string, to: string) =>
globalThis.core.api?.isSubdirectory(from, to)
const isSubdirectory: (from: string, to: string) => Promise<boolean> = (
from: string,
to: string
) => globalThis.core.api?.isSubdirectory(from, to)

Check warning on line 129 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

129 line is not covered with tests

/**
* Get system information
* @returns {Promise<any>} - A promise that resolves with the system information.
*/
const systemInformation: () => Promise<SystemInformation> = () =>
globalThis.core.api?.systemInformation()

Check warning on line 136 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

136 line is not covered with tests

/**
* Show toast message from browser processes.
Expand All @@ -128,7 +142,7 @@
* @returns
*/
const showToast: (title: string, message: string) => void = (title, message) =>
globalThis.core.api?.showToast(title, message)

Check warning on line 145 in core/src/browser/core.ts

View workflow job for this annotation

GitHub Actions / coverage-check

145 line is not covered with tests

/**
* Register extension point function type definition
Expand Down Expand Up @@ -159,5 +173,4 @@
systemInformation,
showToast,
dirName,
FileStat,
}
25 changes: 0 additions & 25 deletions core/src/node/api/processors/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
/**
* Checks if the given path is a subdirectory of the given directory.
*
* @param _event - The IPC event object.
* @param from - The path to check.
* @param to - The directory to check against.
*
* @returns {Promise<boolean>} - A promise that resolves with the result.
*/
isSubdirectory(from: any, to: any) {
const rel = relative(from, to)
Expand All @@ -69,7 +66,7 @@
* Log message to log file.
*/
log(args: any) {
writeLog(args)

Check warning on line 69 in core/src/node/api/processors/app.ts

View workflow job for this annotation

GitHub Actions / coverage-check

69 line is not covered with tests
}

getAppConfigurations() {
Expand All @@ -79,26 +76,4 @@
async updateAppConfiguration(args: any) {
await updateAppConfiguration(args)
}

/**
* Start Jan API Server.
*/
async startServer(args?: any) {
const { startServer } = require('@janhq/server')
return startServer({
host: args?.host,
port: args?.port,
isCorsEnabled: args?.isCorsEnabled,
isVerboseEnabled: args?.isVerboseEnabled,
prefix: args?.prefix,
})
}

/**
* Stop Jan API Server.
*/
stopServer() {
const { stopServer } = require('@janhq/server')
return stopServer()
}
}
Loading
Loading