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",
"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 <service@jan.ai>",
"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": {}
}

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 {
interface Window {
core?: any;
namespace NodeJS {
interface Global {
core: any
}
}
var core: any | undefined
}

View File

@ -7,12 +7,11 @@
* @returns Promise<any>
*
*/
const executeOnMain: (
extension: string,
method: string,
...args: any[]
) => Promise<any> = (extension, method, ...args) =>
window.core?.api?.invokeExtensionFunc(extension, method, ...args);
const executeOnMain: (extension: string, method: string, ...args: any[]) => Promise<any> = (
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<any>} A promise that resolves when the file is downloaded.
*/
const downloadFile: (url: string, fileName: string) => Promise<any> = (
url,
fileName
) => window.core?.api?.downloadFile(url, fileName);
const downloadFile: (url: string, fileName: string) => Promise<any> = (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<any> = (
* @returns {Promise<any>} A promise that resolves when the download has been aborted.
*/
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.
* 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<any> = () => window.core.api?.appDataPath();
const appDataPath: () => Promise<any> = () => global.core.api?.appDataPath()
/**
* Gets 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.
@ -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.
*/
const openFileExplorer: (path: string) => Promise<any> = (path) =>
window.core.api?.openFileExplorer(path);
global.core.api?.openFileExplorer(path)
const getResourcePath: () => Promise<string> = () =>
window.core.api?.getResourcePath();
const getResourcePath: () => Promise<string> = () => 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,
};
}

View File

@ -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 = {

View File

@ -5,7 +5,7 @@
* @returns {Promise<any>} A Promise that resolves when the file is written successfully.
*/
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.
@ -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.
*/
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.
@ -21,23 +21,21 @@ const isDirectory = (path: string): Promise<boolean> =>
* @returns {Promise<any>} A Promise that resolves with the contents of the file.
*/
const readFile: (path: string) => Promise<any> = (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<any>} A Promise that resolves with the contents of the directory.
*/
const listFiles: (path: string) => Promise<any> = (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<any>} A Promise that resolves when the directory is created successfully.
*/
const mkdir: (path: string) => Promise<any> = (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<any> = (path) =>
* @returns {Promise<any>} A Promise that resolves when the directory is removed successfully.
*/
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.
* @param {string} path - The path of the file to delete.
* @returns {Promise<any>} A Promise that resolves when the file is deleted.
*/
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.
@ -60,10 +58,10 @@ const deleteFile: (path: string) => Promise<any> = (path) =>
* @param data data to append
*/
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) =>
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<any> = (src, dest) =>
* @returns {Promise<any>} A promise that resolves to the lines of the file.
*/
const readLineByLine: (path: string) => Promise<any> = (path) =>
window.core.api?.readLineByLine(path);
global.core.api?.readLineByLine(path);
export const fs = {
isDirectory,

View File

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

View File

@ -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"]
}

6
core/tslint.json Normal file
View File

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