chore: universal module definition (#902)

This commit is contained in:
Louis 2023-12-08 16:52:00 +07:00 committed by GitHub
parent 9b00f6a31f
commit cfec5f9493
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 191 additions and 66 deletions

13
core/.editorconfig Normal file
View File

@ -0,0 +1,13 @@
#root = true
[*]
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100
indent_size = 2
[*.md]
trim_trailing_whitespace = false

12
core/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
node_modules
coverage
.nyc_output
.DS_Store
*.log
.vscode
.idea
dist
compiled
.awcache
.rpt2_cache
docs

View File

@ -8,28 +8,84 @@
], ],
"homepage": "https://jan.ai", "homepage": "https://jan.ai",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"main": "lib/index.js", "main": "dist/core.umd.js",
"types": "lib/index.d.ts", "module": "dist/core.es5.js",
"directories": { "typings": "dist/types/index.d.ts",
"lib": "lib",
"test": "__tests__"
},
"exports": {
".": "./lib/index.js"
},
"files": [ "files": [
"lib", "dist"
"README.md",
"LICENSE.md",
"package.json",
"!.DS_Store"
], ],
"author": "Jan <service@jan.ai>",
"repository": {
"type": "git",
"url": ""
},
"engines": {
"node": ">=6.0.0"
},
"scripts": { "scripts": {
"test": "echo \"Error: run tests from root\" && exit 1", "lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
"build": "tsc" "prebuild": "rimraf dist",
"build": "tsc --module commonjs && rollup -c rollup.config.ts",
"start": "rollup -c rollup.config.ts -w"
},
"lint-staged": {
"{src,test}/**/*.ts": [
"prettier --write",
"git add"
]
},
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
},
"jest": {
"transform": {
".(ts|tsx)": "ts-jest"
},
"testEnvironment": "node",
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"/test/"
],
"coverageThreshold": {
"global": {
"branches": 90,
"functions": 95,
"lines": 95,
"statements": 95
}
},
"collectCoverageFrom": [
"src/*.{js,ts}"
]
},
"prettier": {
"semi": false,
"singleQuote": true
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^12.0.2", "@types/node": "^10.11.0",
"typescript": "^5.2.2" "rollup": "^2.38.5",
} "rollup-plugin-commonjs": "^9.1.8",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"rollup-plugin-typescript2": "^0.29.0",
"ts-node": "^7.0.1",
"typescript": "^3.0.3",
"tslib": "^2.6.2"
},
"dependencies": {}
} }

37
core/rollup.config.ts Normal file
View File

@ -0,0 +1,37 @@
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import sourceMaps from 'rollup-plugin-sourcemaps'
import typescript from 'rollup-plugin-typescript2'
import json from 'rollup-plugin-json'
const pkg = require('./package.json')
const libraryName = 'core'
export default {
input: `src/index.ts`,
output: [
{ file: pkg.main, name: libraryName, format: 'umd', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true },
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: [],
watch: {
include: 'src/**',
},
plugins: [
// Allow json resolution
json(),
// Compile TypeScript files
typescript({ useTsconfigDeclarationDir: true }),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve(),
// Resolve source maps to the original source
sourceMaps(),
],
}

View File

@ -1,7 +1,10 @@
export {}; export {}
declare global { declare global {
interface Window { namespace NodeJS {
core?: any; interface Global {
core: any
} }
}
var core: any | undefined
} }

View File

@ -7,12 +7,11 @@
* @returns Promise<any> * @returns Promise<any>
* *
*/ */
const executeOnMain: ( const executeOnMain: (extension: string, method: string, ...args: any[]) => Promise<any> = (
extension: string, extension,
method: string, method,
...args: any[] ...args
) => Promise<any> = (extension, method, ...args) => ) => global.core?.api?.invokeExtensionFunc(extension, method, ...args)
window.core?.api?.invokeExtensionFunc(extension, method, ...args);
/** /**
* Downloads a file from a URL and saves it to the local file system. * Downloads a file from a URL and saves it to the local file system.
@ -20,10 +19,8 @@ const executeOnMain: (
* @param {string} fileName - The name to use for the downloaded file. * @param {string} fileName - The name to use for the downloaded file.
* @returns {Promise<any>} A promise that resolves when the file is downloaded. * @returns {Promise<any>} A promise that resolves when the file is downloaded.
*/ */
const downloadFile: (url: string, fileName: string) => Promise<any> = ( const downloadFile: (url: string, fileName: string) => Promise<any> = (url, fileName) =>
url, global.core?.api?.downloadFile(url, fileName)
fileName
) => window.core?.api?.downloadFile(url, fileName);
/** /**
* Aborts the download of a specific file. * Aborts the download of a specific file.
@ -31,20 +28,20 @@ const downloadFile: (url: string, fileName: string) => Promise<any> = (
* @returns {Promise<any>} A promise that resolves when the download has been aborted. * @returns {Promise<any>} A promise that resolves when the download has been aborted.
*/ */
const abortDownload: (fileName: string) => Promise<any> = (fileName) => const abortDownload: (fileName: string) => Promise<any> = (fileName) =>
window.core.api?.abortDownload(fileName); global.core.api?.abortDownload(fileName)
/** /**
* Retrieves the path to the app data directory using the `coreAPI` object. * Retrieves the path to the app data directory using the `coreAPI` object.
* If the `coreAPI` object is not available, the function returns `undefined`. * If the `coreAPI` object is not available, the function returns `undefined`.
* @returns A Promise that resolves with the path to the app data directory, or `undefined` if the `coreAPI` object is not available. * @returns A Promise that resolves with the path to the app data directory, or `undefined` if the `coreAPI` object is not available.
*/ */
const appDataPath: () => Promise<any> = () => window.core.api?.appDataPath(); const appDataPath: () => Promise<any> = () => global.core.api?.appDataPath()
/** /**
* Gets the user space path. * Gets the user space path.
* @returns {Promise<any>} A Promise that resolves with the user space path. * @returns {Promise<any>} A Promise that resolves with the user space path.
*/ */
const getUserSpace = (): Promise<string> => window.core.api?.getUserSpace(); const getUserSpace = (): Promise<string> => global.core.api?.getUserSpace()
/** /**
* Opens the file explorer at a specific path. * Opens the file explorer at a specific path.
@ -52,10 +49,9 @@ const getUserSpace = (): Promise<string> => window.core.api?.getUserSpace();
* @returns {Promise<any>} A promise that resolves when the file explorer is opened. * @returns {Promise<any>} A promise that resolves when the file explorer is opened.
*/ */
const openFileExplorer: (path: string) => Promise<any> = (path) => const openFileExplorer: (path: string) => Promise<any> = (path) =>
window.core.api?.openFileExplorer(path); global.core.api?.openFileExplorer(path)
const getResourcePath: () => Promise<string> = () => const getResourcePath: () => Promise<string> = () => global.core.api?.getResourcePath()
window.core.api?.getResourcePath();
/** /**
* Register extension point function type definition * Register extension point function type definition
@ -64,8 +60,8 @@ export type RegisterExtensionPoint = (
extensionName: string, extensionName: string,
extensionId: string, extensionId: string,
method: Function, method: Function,
priority?: number priority?: number,
) => void; ) => void
/** /**
* Functions exports * Functions exports
@ -78,4 +74,4 @@ export {
getUserSpace, getUserSpace,
openFileExplorer, openFileExplorer,
getResourcePath, getResourcePath,
}; }

View File

@ -20,7 +20,7 @@ const on: (eventName: string, handler: Function) => void = (
eventName, eventName,
handler handler
) => { ) => {
window.core?.events?.on(eventName, handler); global.core?.events?.on(eventName, handler);
}; };
/** /**
@ -33,7 +33,7 @@ const off: (eventName: string, handler: Function) => void = (
eventName, eventName,
handler handler
) => { ) => {
window.core?.events?.off(eventName, handler); global.core?.events?.off(eventName, handler);
}; };
/** /**
@ -43,7 +43,7 @@ const off: (eventName: string, handler: Function) => void = (
* @param object The object to pass to the event callback. * @param object The object to pass to the event callback.
*/ */
const emit: (eventName: string, object: any) => void = (eventName, object) => { const emit: (eventName: string, object: any) => void = (eventName, object) => {
window.core?.events?.emit(eventName, object); global.core?.events?.emit(eventName, object);
}; };
export const events = { export const events = {

View File

@ -5,7 +5,7 @@
* @returns {Promise<any>} A Promise that resolves when the file is written successfully. * @returns {Promise<any>} A Promise that resolves when the file is written successfully.
*/ */
const writeFile: (path: string, data: string) => Promise<any> = (path, data) => const writeFile: (path: string, data: string) => Promise<any> = (path, data) =>
window.core.api?.writeFile(path, data); global.core.api?.writeFile(path, data);
/** /**
* Checks whether the path is a directory. * Checks whether the path is a directory.
@ -13,7 +13,7 @@ const writeFile: (path: string, data: string) => Promise<any> = (path, data) =>
* @returns {boolean} A boolean indicating whether the path is a directory. * @returns {boolean} A boolean indicating whether the path is a directory.
*/ */
const isDirectory = (path: string): Promise<boolean> => const isDirectory = (path: string): Promise<boolean> =>
window.core.api?.isDirectory(path); global.core.api?.isDirectory(path);
/** /**
* Reads the contents of a file at the specified path. * Reads the contents of a file at the specified path.
@ -21,23 +21,21 @@ const isDirectory = (path: string): Promise<boolean> =>
* @returns {Promise<any>} A Promise that resolves with the contents of the file. * @returns {Promise<any>} A Promise that resolves with the contents of the file.
*/ */
const readFile: (path: string) => Promise<any> = (path) => const readFile: (path: string) => Promise<any> = (path) =>
window.core.api?.readFile(path); global.core.api?.readFile(path);
/** /**
* List the directory files * List the directory files
* @param {string} path - The path of the directory to list files. * @param {string} path - The path of the directory to list files.
* @returns {Promise<any>} A Promise that resolves with the contents of the directory. * @returns {Promise<any>} A Promise that resolves with the contents of the directory.
*/ */
const listFiles: (path: string) => Promise<any> = (path) => const listFiles: (path: string) => Promise<any> = (path) =>
window.core.api?.listFiles(path); global.core.api?.listFiles(path);
/** /**
* Creates a directory at the specified path. * Creates a directory at the specified path.
* @param {string} path - The path of the directory to create. * @param {string} path - The path of the directory to create.
* @returns {Promise<any>} A Promise that resolves when the directory is created successfully. * @returns {Promise<any>} A Promise that resolves when the directory is created successfully.
*/ */
const mkdir: (path: string) => Promise<any> = (path) => const mkdir: (path: string) => Promise<any> = (path) =>
window.core.api?.mkdir(path); global.core.api?.mkdir(path);
/** /**
* Removes a directory at the specified path. * Removes a directory at the specified path.
@ -45,14 +43,14 @@ const mkdir: (path: string) => Promise<any> = (path) =>
* @returns {Promise<any>} A Promise that resolves when the directory is removed successfully. * @returns {Promise<any>} A Promise that resolves when the directory is removed successfully.
*/ */
const rmdir: (path: string) => Promise<any> = (path) => const rmdir: (path: string) => Promise<any> = (path) =>
window.core.api?.rmdir(path); global.core.api?.rmdir(path);
/** /**
* Deletes a file from the local file system. * Deletes a file from the local file system.
* @param {string} path - The path of the file to delete. * @param {string} path - The path of the file to delete.
* @returns {Promise<any>} A Promise that resolves when the file is deleted. * @returns {Promise<any>} A Promise that resolves when the file is deleted.
*/ */
const deleteFile: (path: string) => Promise<any> = (path) => const deleteFile: (path: string) => Promise<any> = (path) =>
window.core.api?.deleteFile(path); global.core.api?.deleteFile(path);
/** /**
* Appends data to a file at the specified path. * Appends data to a file at the specified path.
@ -60,10 +58,10 @@ const deleteFile: (path: string) => Promise<any> = (path) =>
* @param data data to append * @param data data to append
*/ */
const appendFile: (path: string, data: string) => Promise<any> = (path, data) => const appendFile: (path: string, data: string) => Promise<any> = (path, data) =>
window.core.api?.appendFile(path, data); global.core.api?.appendFile(path, data);
const copyFile: (src: string, dest: string) => Promise<any> = (src, dest) => const copyFile: (src: string, dest: string) => Promise<any> = (src, dest) =>
window.core.api?.copyFile(src, dest); global.core.api?.copyFile(src, dest);
/** /**
* Reads a file line by line. * Reads a file line by line.
@ -71,7 +69,7 @@ const copyFile: (src: string, dest: string) => Promise<any> = (src, dest) =>
* @returns {Promise<any>} A promise that resolves to the lines of the file. * @returns {Promise<any>} A promise that resolves to the lines of the file.
*/ */
const readLineByLine: (path: string) => Promise<any> = (path) => const readLineByLine: (path: string) => Promise<any> = (path) =>
window.core.api?.readLineByLine(path); global.core.api?.readLineByLine(path);
export const fs = { export const fs = {
isDirectory, isDirectory,

View File

@ -218,7 +218,7 @@ export interface Model {
* Default: "to_download" * Default: "to_download"
* Enum: "to_download" "downloading" "ready" "running" * Enum: "to_download" "downloading" "ready" "running"
*/ */
state: ModelState; state?: ModelState;
/** /**
* The model settings. * The model settings.

View File

@ -1,15 +1,19 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es2016", "moduleResolution": "node",
"module": "ES6", "target": "es5",
"outDir": "./lib", "module": "es2015",
"esModuleInterop": true, "lib": ["es2015", "es2016", "es2017", "dom"],
"forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"skipLibCheck": true, "sourceMap": true,
"declaration": true, "declaration": true,
"rootDir": "./src" "allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declarationDir": "dist/types",
"outDir": "dist/lib",
"importHelpers": true,
"typeRoots": ["node_modules/@types"]
}, },
"include": ["./src"], "include": ["src"]
"exclude": ["lib", "node_modules", "**/*.test.ts", "**/__mocks__/*"]
} }

6
core/tslint.json Normal file
View File

@ -0,0 +1,6 @@
{
"extends": [
"tslint-config-standard",
"tslint-config-prettier"
]
}