This commit is contained in:
熊潇 2025-02-26 00:46:53 +08:00
parent 7403e09ea3
commit 950a4cd905
6 changed files with 51 additions and 20 deletions

24
README.md Normal file
View File

@ -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
}
}
}
```

View File

@ -8,9 +8,10 @@
"enum": [ "enum": [
"system-app", "system-app",
"micro-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": { "home": {
"type": "string", "type": "string",

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/use-config", "name": "@kevisual/use-config",
"version": "1.0.7", "version": "1.0.8",
"types": "dist/config.d.ts", "types": "dist/config.d.ts",
"scripts": { "scripts": {
"build": "npm run clean && rollup -c", "build": "npm run clean && rollup -c",
@ -21,24 +21,24 @@
"type": "module", "type": "module",
"devDependencies": { "devDependencies": {
"@rollup/plugin-alias": "^5.1.1", "@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-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.3.0", "@rollup/plugin-node-resolve": "^16.0.0",
"@rollup/plugin-replace": "^6.0.1", "@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-typescript": "^12.1.1", "@rollup/plugin-typescript": "^12.1.2",
"@types/node": "^22.10.1", "@types/node": "^22.13.5",
"chalk": "^5.3.0", "chalk": "^5.4.1",
"commander": "^12.1.0", "commander": "^13.1.0",
"glob": "^11.0.0", "glob": "^11.0.1",
"json-schema-to-ts": "^3.1.1", "json-schema-to-ts": "^3.1.1",
"json5": "^2.2.3", "json5": "^2.2.3",
"rollup": "^4.27.4", "rollup": "^4.34.8",
"rollup-plugin-copy": "^3.5.0", "rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.1.1", "rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1", "rollup-plugin-esbuild": "^6.2.0",
"rollup-plugin-inject": "^3.0.2", "rollup-plugin-inject": "^3.0.2",
"tslib": "^2.8.1", "tslib": "^2.8.1",
"typescript": "^5.7.2" "typescript": "^5.7.3"
}, },
"exports": { "exports": {
".": { ".": {

View File

@ -49,6 +49,11 @@ export const fileIsExist = (path: string) => {
export const getDirname = () => { export const getDirname = () => {
return path.resolve(); return path.resolve();
}; };
/**
* 3
* @param fileName
* @returns
*/
export const getConfigFile = (fileName = 'app.config.json5') => { export const getConfigFile = (fileName = 'app.config.json5') => {
const dirname = getDirname(); const dirname = getDirname();
// 本级 // 本级
@ -83,12 +88,13 @@ export const init = (initConfigBase?: any): Config => {
} catch (e) { } catch (e) {
const root = dirname + '/app.config.json5'; const root = dirname + '/app.config.json5';
if (!fileIsExist(root)) { if (!fileIsExist(root)) {
console.error('未找到配置文件,初始化配置', root);
fs.writeFileSync(root, JSON5.stringify(initConfigBase || initConfig, null, 2), { fs.writeFileSync(root, JSON5.stringify(initConfigBase || initConfig, null, 2), {
encoding: 'utf8', encoding: 'utf8',
}); });
} else {
console.error('error', e);
} }
console.error('未找到配置文件,初始化配置', root);
// console.error('error', e);
return initConfig; return initConfig;
} }
}; };

View File

@ -15,19 +15,19 @@ export const useContext = <T = GlobalContext>(initContext?: GlobalContext): T =>
return global.context; return global.context;
}; };
export const useContextKey = <T>(key: string, init: () => T): T => { export const useContextKey = <T = any>(key: string, init?: () => T): T => {
const _context = useContext({}); const _context = useContext({});
if (key && typeof _context[key] !== 'undefined') { if (key && typeof _context[key] !== 'undefined') {
return _context[key]; return _context[key];
} }
if (key && init) { if (key && init) {
_context[key] = init(); _context[key] = (init as () => T)();
return _context[key] as any; return _context[key] as any;
} }
return _context as any; return _context as any;
}; };
export const useContextKeySync = async <T = any>(key: string, init: () => Promise<T>): Promise<T> => { export const useContextKeySync = async <T = any>(key: string, init?: () => Promise<T>): Promise<T> => {
const _context = useContext({}); const _context = useContext({});
if (key && typeof _context[key] !== 'undefined') { if (key && typeof _context[key] !== 'undefined') {
return _context[key]; return _context[key];

View File

@ -3,7 +3,7 @@ import type { App } from './read-app-schema.ts';
import { getConfigFile } from './config.ts'; import { getConfigFile } from './config.ts';
export const getPkgs = () => { export const getPkgs = () => {
const configFile = getConfigFile(); const configFile = getConfigFile('package.json');
if (!configFile) { if (!configFile) {
console.error('配置文件不存在'); console.error('配置文件不存在');
return {}; return {};