temp
This commit is contained in:
		
							
								
								
									
										24
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								README.md
									
									
									
									
									
										Normal 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
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
@@ -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",
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										24
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								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": {
 | 
			
		||||
    ".": {
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -15,19 +15,19 @@ export const useContext = <T = GlobalContext>(initContext?: GlobalContext): T =>
 | 
			
		||||
  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({});
 | 
			
		||||
  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 <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({});
 | 
			
		||||
  if (key && typeof _context[key] !== 'undefined') {
 | 
			
		||||
    return _context[key];
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {};
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user