From cfec5f9493ca221f6008b1336ab6742e8ad53355 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 8 Dec 2023 16:52:00 +0700 Subject: [PATCH] chore: universal module definition (#902) --- core/.editorconfig | 13 +++++ core/.gitignore | 12 +++++ core/package.json | 94 +++++++++++++++++++++++++++++-------- core/rollup.config.ts | 37 +++++++++++++++ core/src/@global/index.d.ts | 9 ++-- core/src/core.ts | 34 ++++++-------- core/src/events.ts | 6 +-- core/src/fs.ts | 22 ++++----- core/src/types/index.ts | 2 +- core/tsconfig.json | 22 +++++---- core/tslint.json | 6 +++ 11 files changed, 191 insertions(+), 66 deletions(-) create mode 100644 core/.editorconfig create mode 100644 core/.gitignore create mode 100644 core/rollup.config.ts create mode 100644 core/tslint.json diff --git a/core/.editorconfig b/core/.editorconfig new file mode 100644 index 000000000..92b9bb1e5 --- /dev/null +++ b/core/.editorconfig @@ -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 diff --git a/core/.gitignore b/core/.gitignore new file mode 100644 index 000000000..d626d098e --- /dev/null +++ b/core/.gitignore @@ -0,0 +1,12 @@ +node_modules +coverage +.nyc_output +.DS_Store +*.log +.vscode +.idea +dist +compiled +.awcache +.rpt2_cache +docs diff --git a/core/package.json b/core/package.json index 69be586f3..daea105f7 100644 --- a/core/package.json +++ b/core/package.json @@ -8,28 +8,84 @@ ], "homepage": "https://jan.ai", "license": "AGPL-3.0", - "main": "lib/index.js", - "types": "lib/index.d.ts", - "directories": { - "lib": "lib", - "test": "__tests__" - }, - "exports": { - ".": "./lib/index.js" - }, + "main": "dist/core.umd.js", + "module": "dist/core.es5.js", + "typings": "dist/types/index.d.ts", "files": [ - "lib", - "README.md", - "LICENSE.md", - "package.json", - "!.DS_Store" + "dist" ], + "author": "Jan ", + "repository": { + "type": "git", + "url": "" + }, + "engines": { + "node": ">=6.0.0" + }, "scripts": { - "test": "echo \"Error: run tests from root\" && exit 1", - "build": "tsc" + "lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'", + "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": { - "@types/node": "^12.0.2", - "typescript": "^5.2.2" - } + "@types/node": "^10.11.0", + "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": {} } diff --git a/core/rollup.config.ts b/core/rollup.config.ts new file mode 100644 index 000000000..5e1762c96 --- /dev/null +++ b/core/rollup.config.ts @@ -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(), + ], +} diff --git a/core/src/@global/index.d.ts b/core/src/@global/index.d.ts index 0e52252e3..b2d55fc1c 100644 --- a/core/src/@global/index.d.ts +++ b/core/src/@global/index.d.ts @@ -1,7 +1,10 @@ -export {}; +export {} declare global { - interface Window { - core?: any; + namespace NodeJS { + interface Global { + core: any + } } + var core: any | undefined } diff --git a/core/src/core.ts b/core/src/core.ts index 0e032f4d9..b86c6fa6f 100644 --- a/core/src/core.ts +++ b/core/src/core.ts @@ -7,12 +7,11 @@ * @returns Promise * */ -const executeOnMain: ( - extension: string, - method: string, - ...args: any[] -) => Promise = (extension, method, ...args) => - window.core?.api?.invokeExtensionFunc(extension, method, ...args); +const executeOnMain: (extension: string, method: string, ...args: any[]) => Promise = ( + extension, + method, + ...args +) => global.core?.api?.invokeExtensionFunc(extension, method, ...args) /** * 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. * @returns {Promise} A promise that resolves when the file is downloaded. */ -const downloadFile: (url: string, fileName: string) => Promise = ( - url, - fileName -) => window.core?.api?.downloadFile(url, fileName); +const downloadFile: (url: string, fileName: string) => Promise = (url, fileName) => + global.core?.api?.downloadFile(url, fileName) /** * Aborts the download of a specific file. @@ -31,20 +28,20 @@ const downloadFile: (url: string, fileName: string) => Promise = ( * @returns {Promise} A promise that resolves when the download has been aborted. */ const abortDownload: (fileName: string) => Promise = (fileName) => - window.core.api?.abortDownload(fileName); + global.core.api?.abortDownload(fileName) /** * Retrieves the path to the app data directory using the `coreAPI` object. * 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. */ -const appDataPath: () => Promise = () => window.core.api?.appDataPath(); +const appDataPath: () => Promise = () => global.core.api?.appDataPath() /** * Gets the user space path. * @returns {Promise} A Promise that resolves with the user space path. */ -const getUserSpace = (): Promise => window.core.api?.getUserSpace(); +const getUserSpace = (): Promise => global.core.api?.getUserSpace() /** * Opens the file explorer at a specific path. @@ -52,10 +49,9 @@ const getUserSpace = (): Promise => window.core.api?.getUserSpace(); * @returns {Promise} A promise that resolves when the file explorer is opened. */ const openFileExplorer: (path: string) => Promise = (path) => - window.core.api?.openFileExplorer(path); + global.core.api?.openFileExplorer(path) -const getResourcePath: () => Promise = () => - window.core.api?.getResourcePath(); +const getResourcePath: () => Promise = () => global.core.api?.getResourcePath() /** * Register extension point function type definition @@ -64,8 +60,8 @@ export type RegisterExtensionPoint = ( extensionName: string, extensionId: string, method: Function, - priority?: number -) => void; + priority?: number, +) => void /** * Functions exports @@ -78,4 +74,4 @@ export { getUserSpace, openFileExplorer, getResourcePath, -}; +} diff --git a/core/src/events.ts b/core/src/events.ts index f62aa1113..f588daad7 100644 --- a/core/src/events.ts +++ b/core/src/events.ts @@ -20,7 +20,7 @@ const on: (eventName: string, handler: Function) => void = ( eventName, 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, 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. */ const emit: (eventName: string, object: any) => void = (eventName, object) => { - window.core?.events?.emit(eventName, object); + global.core?.events?.emit(eventName, object); }; export const events = { diff --git a/core/src/fs.ts b/core/src/fs.ts index d12b473bf..4013479dd 100644 --- a/core/src/fs.ts +++ b/core/src/fs.ts @@ -5,7 +5,7 @@ * @returns {Promise} A Promise that resolves when the file is written successfully. */ const writeFile: (path: string, data: string) => Promise = (path, data) => - window.core.api?.writeFile(path, data); + global.core.api?.writeFile(path, data); /** * Checks whether the path is a directory. @@ -13,7 +13,7 @@ const writeFile: (path: string, data: string) => Promise = (path, data) => * @returns {boolean} A boolean indicating whether the path is a directory. */ const isDirectory = (path: string): Promise => - window.core.api?.isDirectory(path); + global.core.api?.isDirectory(path); /** * Reads the contents of a file at the specified path. @@ -21,23 +21,21 @@ const isDirectory = (path: string): Promise => * @returns {Promise} A Promise that resolves with the contents of the file. */ const readFile: (path: string) => Promise = (path) => - window.core.api?.readFile(path); - + global.core.api?.readFile(path); /** * List the directory files * @param {string} path - The path of the directory to list files. * @returns {Promise} A Promise that resolves with the contents of the directory. */ const listFiles: (path: string) => Promise = (path) => - window.core.api?.listFiles(path); - + global.core.api?.listFiles(path); /** * Creates a directory at the specified path. * @param {string} path - The path of the directory to create. * @returns {Promise} A Promise that resolves when the directory is created successfully. */ const mkdir: (path: string) => Promise = (path) => - window.core.api?.mkdir(path); + global.core.api?.mkdir(path); /** * Removes a directory at the specified path. @@ -45,14 +43,14 @@ const mkdir: (path: string) => Promise = (path) => * @returns {Promise} A Promise that resolves when the directory is removed successfully. */ const rmdir: (path: string) => Promise = (path) => - window.core.api?.rmdir(path); + global.core.api?.rmdir(path); /** * Deletes a file from the local file system. * @param {string} path - The path of the file to delete. * @returns {Promise} A Promise that resolves when the file is deleted. */ const deleteFile: (path: string) => Promise = (path) => - window.core.api?.deleteFile(path); + global.core.api?.deleteFile(path); /** * Appends data to a file at the specified path. @@ -60,10 +58,10 @@ const deleteFile: (path: string) => Promise = (path) => * @param data data to append */ const appendFile: (path: string, data: string) => Promise = (path, data) => - window.core.api?.appendFile(path, data); + global.core.api?.appendFile(path, data); const copyFile: (src: string, dest: string) => Promise = (src, dest) => - window.core.api?.copyFile(src, dest); + global.core.api?.copyFile(src, dest); /** * Reads a file line by line. @@ -71,7 +69,7 @@ const copyFile: (src: string, dest: string) => Promise = (src, dest) => * @returns {Promise} A promise that resolves to the lines of the file. */ const readLineByLine: (path: string) => Promise = (path) => - window.core.api?.readLineByLine(path); + global.core.api?.readLineByLine(path); export const fs = { isDirectory, diff --git a/core/src/types/index.ts b/core/src/types/index.ts index dcbc20ca3..87343aa65 100644 --- a/core/src/types/index.ts +++ b/core/src/types/index.ts @@ -218,7 +218,7 @@ export interface Model { * Default: "to_download" * Enum: "to_download" "downloading" "ready" "running" */ - state: ModelState; + state?: ModelState; /** * The model settings. diff --git a/core/tsconfig.json b/core/tsconfig.json index 62caccdcb..19b2d29ad 100644 --- a/core/tsconfig.json +++ b/core/tsconfig.json @@ -1,15 +1,19 @@ { "compilerOptions": { - "target": "es2016", - "module": "ES6", - "outDir": "./lib", - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, + "moduleResolution": "node", + "target": "es5", + "module": "es2015", + "lib": ["es2015", "es2016", "es2017", "dom"], "strict": true, - "skipLibCheck": true, + "sourceMap": 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"], - "exclude": ["lib", "node_modules", "**/*.test.ts", "**/__mocks__/*"] + "include": ["src"] } diff --git a/core/tslint.json b/core/tslint.json new file mode 100644 index 000000000..398a41670 --- /dev/null +++ b/core/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": [ + "tslint-config-standard", + "tslint-config-prettier" + ] +} \ No newline at end of file