Skip to content

Commit

Permalink
chore: universal module definition
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Dec 7, 2023
1 parent 6ed83fc commit 96d7a6a
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 66 deletions.
13 changes: 13 additions & 0 deletions core/.editorconfig
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules
coverage
.nyc_output
.DS_Store
*.log
.vscode
.idea
dist
compiled
.awcache
.rpt2_cache
docs
94 changes: 75 additions & 19 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"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 changes: 37 additions & 0 deletions core/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -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(),
],
}
9 changes: 6 additions & 3 deletions core/src/@global/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export {};
export {}

declare global {
interface Window {
core?: any;
namespace NodeJS {
interface Global {
core: any
}
}
var core: any | undefined
}
34 changes: 15 additions & 19 deletions core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,51 @@
* @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.
* @param {string} url - The URL of the file to download.
* @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.
* @param {string} fileName - The name of the file whose download is to be aborted.
* @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.
* @param {string} path - The path to open in the file explorer.
* @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
Expand All @@ -64,8 +60,8 @@ export type RegisterExtensionPoint = (
extensionName: string,
extensionId: string,
method: Function,
priority?: number
) => void;
priority?: number,
) => void

/**
* Functions exports
Expand All @@ -78,4 +74,4 @@ export {
getUserSpace,
openFileExplorer,
getResourcePath,
};
}
6 changes: 3 additions & 3 deletions core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand All @@ -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);
};

/**
Expand All @@ -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 = {
Expand Down
22 changes: 10 additions & 12 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,71 @@
* @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.
* @param path - The path to check.
* @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.
* @param {string} path - The path of the file to read.
* @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.
* @param {string} path - The path of the directory to remove.
* @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.
* @param path path to the file
* @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.
* @param {string} path - The path of the file to read.
* @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,
Expand Down
2 changes: 1 addition & 1 deletion core/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export interface Model {
* Default: "to_download"
* Enum: "to_download" "downloading" "ready" "running"
*/
state: ModelState;
state?: ModelState;

/**
* The model settings.
Expand Down
Loading

0 comments on commit 96d7a6a

Please sign in to comment.