This commit is contained in:
2026-01-12 01:20:36 +08:00
parent d2bb88e1eb
commit 4a18e4a518
17 changed files with 81 additions and 512 deletions

View File

@@ -1,95 +0,0 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "App Configuration Schema",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"system-app",
"micro-app",
"gateway-app",
"pm2-system-app"
],
"$comment": "Type must be either 'system-app' or 'micro-app' or 'gateway-app' or 'pm2-system-app'."
},
"home": {
"type": "string",
"pattern": "^/.*",
"$comment": "https://kevisual.xiongxiao.me"
},
"single": {
"type": "boolean",
"$comment": "是否单例模式,独立启动服务。"
},
"port": {
"type": "integer",
"minimum": 0,
"maximum": 65535,
"$comment": "开发和单例启动服务的端口"
},
"remote": {
"type": "object",
"properties": {
"host": {
"type": "string",
"format": "hostname"
},
"path": {
"type": "string",
"pattern": "^/.*"
}
},
"required": [
"host",
"path"
],
"$comment": "远程服务的地址和路径"
},
"micro": {
"type": "object",
"properties": {
"serve": {
"type": "object",
"properties": {
"name": {
"type": "string",
"pattern": "^[a-z0-9-]+$",
"$comment": "服务名称"
},
"port": {
"type": "integer",
"minimum": 0,
"maximum": 65535,
"$comment": "启动的服务端口"
}
},
"required": []
},
"remote": {
"type": "object",
"properties": {
"host": {
"type": "string",
"format": "hostname"
},
"port": {
"type": "integer",
"minimum": 0,
"maximum": 65535,
"$comment": "链接的远程地址的端口"
}
},
"required": [
"host",
"port"
]
}
},
"required": []
}
},
"required": [
"type"
]
}

View File

@@ -1,15 +0,0 @@
{
"$schema": "../schema.json",
"app": {
"type": "inline-app",
"port": 14000,
"remote": {
"host": "localhost",
"path": "/api/router"
},
"micro": {
"name": "micro-app",
"port": 3001
}
}
}

View File

@@ -1,17 +0,0 @@
{
"$schema": "../schema.json",
"app": {
"type": "micro-app",
"port": 14000,
"remote": {
"host": "localhost",
"path": "/api/router"
},
"micro": {
"remote": {
"host": "localhost",
"port": 3001
}
}
}
}

View File

@@ -1,13 +0,0 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Packages JSON Schema",
"type": "object",
"properties": {
"app": {
"$ref": "./app-schema.json"
}
},
"required": [
"app"
]
}

View File

@@ -1,16 +0,0 @@
{
"name": "demo",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me>",
"license": "MIT",
"type": "module",
"dependencies": {
"@kevisual/use-config": "link:.."
}
}

View File

@@ -1,5 +0,0 @@
import { getApps } from '@kevisual/user-config/pkgs';
const apps = getApps();
// apps.micro

View File

@@ -1,9 +0,0 @@
import fs from 'fs';
import { useConfig, useKey } from '@kevisual/use-config/env';
const config = useConfig();
console.log(config);
const a = useKey('a');
console.log(a);

View File

@@ -1,4 +0,0 @@
import { useConfig } from '@kevisual/use-config';
// console.log(useConfig);
console.log(useConfig());

View File

@@ -1,34 +0,0 @@
{
"compilerOptions": {
"module": "NodeNext",
"target": "esnext",
"noImplicitAny": false,
"outDir": "./dist",
"sourceMap": false,
"allowJs": true,
"newLine": "LF",
"baseUrl": "./",
"typeRoots": [
"node_modules/@types",
],
"declaration": false,
"noEmit": true,
"allowImportingTsExtensions": true,
"moduleResolution": "NodeNext",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"paths": {
"@/*": [
"src/*"
],
}
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"rollup.config.js",
]
}

View File

@@ -1,9 +1,9 @@
{
"name": "@kevisual/use-config",
"version": "1.0.21",
"version": "1.0.22",
"types": "dist/config.d.ts",
"scripts": {
"build": "npm run clean && tsup",
"build": "npm run clean && code-builder build --dts",
"watch": "tsup --watch",
"clean": "rimraf dist"
},
@@ -12,8 +12,7 @@
},
"files": [
"dist",
"src",
"config"
"src"
],
"keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me>",
@@ -21,13 +20,8 @@
"type": "module",
"devDependencies": {
"@kevisual/context": "^0.0.4",
"@types/node": "^24.10.1",
"dotenv": "^17.2.3",
"fast-glob": "^3.3.3",
"json-schema-to-ts": "^3.1.1",
"json5": "^2.2.3",
"tsup": "^8.5.1",
"typescript": "^5.9.3"
"@types/node": "^25.0.6",
"dotenv": "^17.2.3"
},
"exports": {
".": {
@@ -37,18 +31,6 @@
"./env": {
"import": "./dist/env.mjs",
"types": "./dist/env.d.ts"
},
"./pkgs": {
"import": "./dist/pkgs.mjs",
"types": "./dist/pkgs.d.ts"
},
"./context": {
"import": "./dist/context.mjs",
"types": "./dist/context.d.ts"
},
"./file-store": {
"import": "./dist/file-store.mjs",
"types": "./dist/file-store.d.ts"
}
},
"peerDependencies": {

View File

@@ -1,134 +0,0 @@
import JSON5 from 'json5';
import fs from 'fs';
import path from 'path';
// 配置类型
export type Config = {
port: number;
tokenSecret?: string;
redis?: {
host?: string;
port?: number;
password?: string;
version?: string;
[key: string]: any;
};
posgtres?: {
host?: string;
port?: number;
user?: string;
password?: string;
database?: string;
[key: string]: any;
};
minio?: {
endPoint?: string;
bucketName?: string;
useSSL?: boolean;
accessKey?: string;
secretKey?: string;
[key: string]: any;
};
mongo?: {
uri?: string;
[key: string]: any;
};
[key: string]: any;
};
export const initConfig: Config = {
port: 3000,
};
export const fileIsExist = (path: string) => {
try {
fs.accessSync(path, fs.constants.F_OK);
return true;
} catch (e) {
return false;
}
};
export const getCwdDirname = () => {
return path.resolve();
};
/**
* 查找文件3级查找。往上查询
* @param fileName 文件名
* @returns
*/
export const getConfigFile = (fileName = 'app.config.json5') => {
const dirname = getCwdDirname();
// 本级
const benPath = dirname + '/' + fileName;
const ben = fileIsExist(benPath);
if (ben) return benPath;
// 上级
const lastPath = path.join(dirname, '../' + fileName);
const last = fileIsExist(lastPath);
if (last) return lastPath;
// 上上级
const lastLastPath = path.join(dirname, '../../' + fileName);
const lastLast = fileIsExist(lastLastPath);
if (lastLast) return lastLastPath;
return '';
};
// 初始化读取配置文件
export const init = (initConfigBase?: any): Config => {
const dirname = getCwdDirname();
try {
// 配置读取路径3级判断
const filePath = getConfigFile();
console.log('config pathname:', filePath);
if (!filePath) {
throw new Error('未找到配置文件');
}
const configString = fs.readFileSync(filePath, {
encoding: 'utf-8',
});
const value = JSON5.parse(configString);
return value;
} 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);
}
return initConfig;
}
};
/**
* 从全局获取
* @param initConfig 在全局未找到配置时,初始化配置的内容
*
* @returns Config
* @deprecated 请使用 @kevisual/use-config/env 代替 1.0.15 版本删除
*/
export const useConfig = <T>(initConfig?: any): Config & T => {
const config = (global as any).config;
const _config = config || init(initConfig);
!config && ((global as any)['config'] = _config);
return _config;
};
export const useConfigContext = (key: string, value: any): any => {
const _context = global as any;
if (key && value) {
_context[key] = value;
return _context;
}
if (key) {
return _context[key];
}
return _context;
};
export const deleteConfigContext = (key: string): any => {
const _context = global as any;
if (key && _context[key]) {
delete _context[key];
return _context;
}
return _context;
};

View File

@@ -1,10 +1,10 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
// 配置类型
export type Config = {
export type Config<T = {}> = {
PORT: string;
// redis
REDIS_HOST?: string;
@@ -19,17 +19,60 @@ export type Config = {
POSTGRES_PASSWORD?: string;
POSTGRES_USER?: string;
POSTGRES_DB?: string;
// mongo
MONGO_URI?: string;
// DB: for drizzleorm
DATABASE_URL?: string;
// minio
MINIO_ENDPOINT?: string;
MINIO_BUCKET_NAME?: string;
MINIO_ACCESS_KEY?: string;
MINIO_SECRET_KEY?: string;
MINIO_USE_SSL?: string;
// mongo
MONGO_URI?: string;
//
// s3
S3_ACCESS_KEY_ID?: string;
S3_SECRET_ACCESS_KEY?: string;
S3_REGION?: string;
S3_BUCKET_NAME?: string;
S3_ENDPOINT?: string;
S3_FORCE_PATH_STYLE?: string;
// KEVISUAL 服务相关
KEVISUAL_TOKEN?: string;
KEVISUAL_API_URL?: string;
// AI
KEVISUAL_NEW_API?: string;
BAILIAN_API_KEY?: string;
ZHIPU_API_KEY?: string;
MINIMAX_API_KEY?: string;
JIMENG_API_URL?: string;
JIMENG_API_KEY?: string;
JIMENG_TIMEOUT?: string;
OPENCODE_URL?: string;
OPENCODE_API_KEY?: string;
// ## Notify
FEISHU_NOTIFY_WEBHOOK_URL?: string;
// NOCODEB
NOCODB_URL?: string;
NOCODB_API_KEY?: string;
// MEILISEARCH
MEILISEARCH_URL?: string;
MEILISEARCH_API_KEY?: string
// PocketBase
POCKETBASE_URL?: string;
POCKETBASE_TOKEN?: string;
// 其他自定义配置
[key: string]: any;
};
} & T;
export const initConfig: Config = {
PORT: '3000',
};
@@ -101,7 +144,7 @@ type GetConfigOpts = ConfigOpts & { dotenvOpts?: dotenv.DotenvConfigOptions };
*/
export const getConfig = (opts?: GetConfigOpts): Config => {
if (opts?.dotenvOpts) {
const prased = dotenv.config(opts.dotenvOpts).parsed as Config;
const prased = dotenv.config({ quiet: true, ...opts.dotenvOpts }).parsed as Config;
if (prased) {
return prased;
} else {
@@ -114,7 +157,7 @@ export const getConfig = (opts?: GetConfigOpts): Config => {
if (!filePath) {
throw new Error('未找到配置文件');
}
const value = dotenv.config({ path: filePath }).parsed as Config;
const value = dotenv.config({ quiet: true, path: filePath }).parsed as Config;
return value;
};
@@ -264,3 +307,23 @@ export const getDevInputs = (files: string[] = []) => {
};
});
};
export type Opts = {
/** 检查是否需要 */
needExists: boolean;
};
export const useFileStore = (str: string, opts?: Opts): string => {
const publicPath = process.cwd();
const filePath = path.join(publicPath, str);
if (opts?.needExists) {
if (!fileIsExist(filePath)) {
fs.mkdirSync(filePath, { recursive: true });
}
}
return filePath;
};
export const usePublicStore = () => {
return useFileStore('public', { needExists: true });
};

View File

@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs';
import { fileIsExist } from './config.ts';
import path from 'node:path';
import fs from 'node:fs';
import { fileIsExist } from './env.ts';
export type Opts = {
/** 检查是否需要 */

1
src/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './env.ts'

View File

@@ -1,18 +0,0 @@
import fs from 'fs';
import type { App } from './read-app-schema.ts';
import { getConfigFile } from './config.ts';
export const getPkgs = () => {
const configFile = getConfigFile('package.json');
if (!configFile) {
console.error('配置文件不存在');
return {};
}
const config = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
return config;
};
export type { App };
export const getApp = (): App => {
const config = getPkgs();
return config.app || {};
};

View File

@@ -1,97 +0,0 @@
import { FromSchema } from 'json-schema-to-ts';
const App = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "App Configuration Schema",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"system-app",
"micro-app",
"gateway-app"
],
"$comment": "Type must be either 'system-app' or 'micro-app' or 'gateway-app'."
},
"home": {
"type": "string",
"pattern": "^/.*",
"$comment": "https://kevisual.xiongxiao.me"
},
"single": {
"type": "boolean",
"$comment": "是否单例模式,独立启动服务。"
},
"port": {
"type": "integer",
"minimum": 0,
"maximum": 65535,
"$comment": "开发和单例启动服务的端口"
},
"remote": {
"type": "object",
"properties": {
"host": {
"type": "string",
"format": "hostname"
},
"path": {
"type": "string",
"pattern": "^/.*"
}
},
"required": [
"host",
"path"
],
"$comment": "远程服务的地址和路径"
},
"micro": {
"type": "object",
"properties": {
"serve": {
"type": "object",
"properties": {
"name": {
"type": "string",
"pattern": "^[a-z0-9-]+$",
"$comment": "服务名称"
},
"port": {
"type": "integer",
"minimum": 0,
"maximum": 65535,
"$comment": "启动的服务端口"
}
},
"required": []
},
"remote": {
"type": "object",
"properties": {
"host": {
"type": "string",
"format": "hostname"
},
"port": {
"type": "integer",
"minimum": 0,
"maximum": 65535,
"$comment": "链接的远程地址的端口"
}
},
"required": [
"host",
"port"
]
}
},
"required": []
}
},
"required": [
"type"
]
} as const;
export type App = FromSchema<typeof App>;

View File

@@ -1,20 +0,0 @@
import { defineConfig } from 'tsup';
// import glob from 'fast-glob';
// const services = glob.sync('src/services/*.ts');
const entrys = ['src/index.ts', 'src/config.ts', 'src/pkgs.ts', 'src/context.ts', 'src/file-store.ts', 'src/env.ts'];
export default defineConfig({
entry: entrys,
outExtension: ({ format }) => ({
js: format === 'esm' ? '.mjs' : '.js',
}),
splitting: false,
sourcemap: false,
clean: true,
format: 'esm',
external: ['dotenv'],
dts: true,
outDir: 'dist',
tsconfig: 'tsconfig.json',
});