Skip to content

Commit

Permalink
Merge pull request #4373 from janhq/chore/refactor-core-and-server-pa…
Browse files Browse the repository at this point in the history
…ckages

chore: refactor core and server packages
  • Loading branch information
louis-jan authored Dec 31, 2024
2 parents 2fe4514 + 1bb9d6c commit a9529f2
Show file tree
Hide file tree
Showing 17 changed files with 469 additions and 1,998 deletions.
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 @@ import { DownloadRequest, FileStat, NetworkConfig, SystemInformation } from '../
* @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 @@ const executeOnMain: (extension: string, method: string, ...args: any[]) => Prom
*
* @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)

/**
* Aborts the download of a specific file.
Expand All @@ -41,7 +48,8 @@ const abortDownload: (fileName: string) => Promise<any> = (fileName) =>
*
* @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 @@ const joinPath: (paths: string[]) => Promise<string> = (paths) =>
* @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)

/**
* 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)

/**
* Opens an external URL in the default web browser.
Expand All @@ -87,13 +97,15 @@ const openExternalUrl: (url: string) => Promise<any> = (url) =>
*
* @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()

/**
* 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()

/**
* Log to file from browser processes.
Expand All @@ -111,8 +123,10 @@ const log: (message: string, fileName?: string) => void = (message, fileName) =>
*
* @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)

/**
* Get system information
Expand Down Expand Up @@ -159,5 +173,4 @@ export {
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 @@ export class App implements Processor {
/**
* 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 Down Expand Up @@ -79,26 +76,4 @@ export class App implements Processor {
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

0 comments on commit a9529f2

Please sign in to comment.