From 4a18e4a51814e433e370b779a513b690d36c95e3 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 12 Jan 2026 01:20:36 +0800 Subject: [PATCH] update --- config/app-schema.json | 95 ------------------------- config/demo/inline-app.json | 15 ---- config/demo/micro-app.json | 17 ----- config/schema.json | 13 ---- demo/package.json | 16 ----- demo/src/app/a.ts | 5 -- demo/src/env.ts | 9 --- demo/src/index.ts | 4 -- demo/tsconfig.json | 34 --------- package.json | 28 ++------ src/config.ts | 134 ------------------------------------ src/env.ts | 81 +++++++++++++++++++--- src/file-store.ts | 6 +- src/index.ts | 1 + src/pkgs.ts | 18 ----- src/read-app-schema.ts | 97 -------------------------- tsup.config.ts | 20 ------ 17 files changed, 81 insertions(+), 512 deletions(-) delete mode 100644 config/app-schema.json delete mode 100644 config/demo/inline-app.json delete mode 100644 config/demo/micro-app.json delete mode 100644 config/schema.json delete mode 100644 demo/package.json delete mode 100644 demo/src/app/a.ts delete mode 100644 demo/src/env.ts delete mode 100644 demo/src/index.ts delete mode 100644 demo/tsconfig.json delete mode 100644 src/config.ts create mode 100644 src/index.ts delete mode 100644 src/pkgs.ts delete mode 100644 src/read-app-schema.ts delete mode 100644 tsup.config.ts diff --git a/config/app-schema.json b/config/app-schema.json deleted file mode 100644 index a5c527c..0000000 --- a/config/app-schema.json +++ /dev/null @@ -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" - ] -} \ No newline at end of file diff --git a/config/demo/inline-app.json b/config/demo/inline-app.json deleted file mode 100644 index a20b829..0000000 --- a/config/demo/inline-app.json +++ /dev/null @@ -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 - } - } -} \ No newline at end of file diff --git a/config/demo/micro-app.json b/config/demo/micro-app.json deleted file mode 100644 index 7b8d23d..0000000 --- a/config/demo/micro-app.json +++ /dev/null @@ -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 - } - } - } -} \ No newline at end of file diff --git a/config/schema.json b/config/schema.json deleted file mode 100644 index 7010054..0000000 --- a/config/schema.json +++ /dev/null @@ -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" - ] -} \ No newline at end of file diff --git a/demo/package.json b/demo/package.json deleted file mode 100644 index dee79f5..0000000 --- a/demo/package.json +++ /dev/null @@ -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 ", - "license": "MIT", - "type": "module", - "dependencies": { - "@kevisual/use-config": "link:.." - } -} diff --git a/demo/src/app/a.ts b/demo/src/app/a.ts deleted file mode 100644 index d6efe4e..0000000 --- a/demo/src/app/a.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { getApps } from '@kevisual/user-config/pkgs'; - -const apps = getApps(); - -// apps.micro \ No newline at end of file diff --git a/demo/src/env.ts b/demo/src/env.ts deleted file mode 100644 index 511f2e8..0000000 --- a/demo/src/env.ts +++ /dev/null @@ -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); diff --git a/demo/src/index.ts b/demo/src/index.ts deleted file mode 100644 index 9add667..0000000 --- a/demo/src/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { useConfig } from '@kevisual/use-config'; - -// console.log(useConfig); -console.log(useConfig()); diff --git a/demo/tsconfig.json b/demo/tsconfig.json deleted file mode 100644 index 918f972..0000000 --- a/demo/tsconfig.json +++ /dev/null @@ -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", - ] -} \ No newline at end of file diff --git a/package.json b/package.json index cbec536..a19815b 100644 --- a/package.json +++ b/package.json @@ -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 ", @@ -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": { diff --git a/src/config.ts b/src/config.ts deleted file mode 100644 index 0bc0676..0000000 --- a/src/config.ts +++ /dev/null @@ -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 = (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; -}; diff --git a/src/env.ts b/src/env.ts index 4e46449..2f716e2 100644 --- a/src/env.ts +++ b/src/env.ts @@ -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 = { 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 }); +}; diff --git a/src/file-store.ts b/src/file-store.ts index 4ca3b43..1e43421 100644 --- a/src/file-store.ts +++ b/src/file-store.ts @@ -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 = { /** 检查是否需要 */ diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..d8725fa --- /dev/null +++ b/src/index.ts @@ -0,0 +1 @@ +export * from './env.ts' \ No newline at end of file diff --git a/src/pkgs.ts b/src/pkgs.ts deleted file mode 100644 index a886e76..0000000 --- a/src/pkgs.ts +++ /dev/null @@ -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 || {}; -}; diff --git a/src/read-app-schema.ts b/src/read-app-schema.ts deleted file mode 100644 index 2bf55dd..0000000 --- a/src/read-app-schema.ts +++ /dev/null @@ -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; diff --git a/tsup.config.ts b/tsup.config.ts deleted file mode 100644 index 883b8e7..0000000 --- a/tsup.config.ts +++ /dev/null @@ -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', -});