diff --git a/README.md b/README.md new file mode 100644 index 0000000..113dc6b --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# use-config + +使用`app.config.json5`作为配置文件。 + + +## schema类型模版 + +```json +{ + "$schema": "../schema.json", + "app": { + "type": "inline-app", + "port": 14000, + "remote": { + "host": "localhost", + "path": "/api/router" + }, + "micro": { + "name": "micro-app", + "port": 3001 + } + } +} +``` diff --git a/config/app-schema.json b/config/app-schema.json index e772a01..a5c527c 100644 --- a/config/app-schema.json +++ b/config/app-schema.json @@ -8,9 +8,10 @@ "enum": [ "system-app", "micro-app", - "gateway-app" + "gateway-app", + "pm2-system-app" ], - "$comment": "Type must be either 'system-app' or 'micro-app' or 'gateway-app'." + "$comment": "Type must be either 'system-app' or 'micro-app' or 'gateway-app' or 'pm2-system-app'." }, "home": { "type": "string", diff --git a/package.json b/package.json index d3e217e..a958330 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/use-config", - "version": "1.0.7", + "version": "1.0.8", "types": "dist/config.d.ts", "scripts": { "build": "npm run clean && rollup -c", @@ -21,24 +21,24 @@ "type": "module", "devDependencies": { "@rollup/plugin-alias": "^5.1.1", - "@rollup/plugin-commonjs": "^28.0.1", + "@rollup/plugin-commonjs": "^28.0.2", "@rollup/plugin-json": "^6.1.0", - "@rollup/plugin-node-resolve": "^15.3.0", - "@rollup/plugin-replace": "^6.0.1", - "@rollup/plugin-typescript": "^12.1.1", - "@types/node": "^22.10.1", - "chalk": "^5.3.0", - "commander": "^12.1.0", - "glob": "^11.0.0", + "@rollup/plugin-node-resolve": "^16.0.0", + "@rollup/plugin-replace": "^6.0.2", + "@rollup/plugin-typescript": "^12.1.2", + "@types/node": "^22.13.5", + "chalk": "^5.4.1", + "commander": "^13.1.0", + "glob": "^11.0.1", "json-schema-to-ts": "^3.1.1", "json5": "^2.2.3", - "rollup": "^4.27.4", + "rollup": "^4.34.8", "rollup-plugin-copy": "^3.5.0", "rollup-plugin-dts": "^6.1.1", - "rollup-plugin-esbuild": "^6.1.1", + "rollup-plugin-esbuild": "^6.2.0", "rollup-plugin-inject": "^3.0.2", "tslib": "^2.8.1", - "typescript": "^5.7.2" + "typescript": "^5.7.3" }, "exports": { ".": { diff --git a/src/config.ts b/src/config.ts index 4016451..9e3ebdc 100644 --- a/src/config.ts +++ b/src/config.ts @@ -49,6 +49,11 @@ export const fileIsExist = (path: string) => { export const getDirname = () => { return path.resolve(); }; +/** + * 查找文件,3级查找。往上查询 + * @param fileName 文件名 + * @returns + */ export const getConfigFile = (fileName = 'app.config.json5') => { const dirname = getDirname(); // 本级 @@ -83,12 +88,13 @@ export const init = (initConfigBase?: any): Config => { } catch (e) { const root = dirname + '/app.config.json5'; if (!fileIsExist(root)) { + console.error('未找到配置文件,初始化配置', root); fs.writeFileSync(root, JSON5.stringify(initConfigBase || initConfig, null, 2), { encoding: 'utf8', }); + } else { + console.error('error', e); } - console.error('未找到配置文件,初始化配置', root); - // console.error('error', e); return initConfig; } }; diff --git a/src/context.ts b/src/context.ts index 4f6dc3e..1998501 100644 --- a/src/context.ts +++ b/src/context.ts @@ -15,19 +15,19 @@ export const useContext = (initContext?: GlobalContext): T => return global.context; }; -export const useContextKey = (key: string, init: () => T): T => { +export const useContextKey = (key: string, init?: () => T): T => { const _context = useContext({}); if (key && typeof _context[key] !== 'undefined') { return _context[key]; } if (key && init) { - _context[key] = init(); + _context[key] = (init as () => T)(); return _context[key] as any; } return _context as any; }; -export const useContextKeySync = async (key: string, init: () => Promise): Promise => { +export const useContextKeySync = async (key: string, init?: () => Promise): Promise => { const _context = useContext({}); if (key && typeof _context[key] !== 'undefined') { return _context[key]; diff --git a/src/pkgs.ts b/src/pkgs.ts index 7ffa136..a886e76 100644 --- a/src/pkgs.ts +++ b/src/pkgs.ts @@ -3,7 +3,7 @@ import type { App } from './read-app-schema.ts'; import { getConfigFile } from './config.ts'; export const getPkgs = () => { - const configFile = getConfigFile(); + const configFile = getConfigFile('package.json'); if (!configFile) { console.error('配置文件不存在'); return {};