From e33f284f4d51f08972f72bfa2372e91da566ac6c Mon Sep 17 00:00:00 2001 From: godakid Date: Sun, 8 Sep 2024 11:50:30 +0800 Subject: [PATCH] chore: use yaml config --- example/.simple-mock | 10 ---------- example/.simple-mock.yaml | 10 ++++++++++ index.ts | 40 ++++++++++++++++++++++----------------- package.json | 6 ++++-- pnpm-lock.yaml | 16 +++++++++++++--- 5 files changed, 50 insertions(+), 32 deletions(-) delete mode 100644 example/.simple-mock create mode 100644 example/.simple-mock.yaml diff --git a/example/.simple-mock b/example/.simple-mock deleted file mode 100644 index e15c419..0000000 --- a/example/.simple-mock +++ /dev/null @@ -1,10 +0,0 @@ -ROOT_DIR=. -API_DIR=apis -STATIC_DIR=./static -STATIC_ROUTE_PREFIX=static -PORT=3000 -SILENT= -ERROR_LOG_FILE_PATH=./error.log -LOG_SIZE=10M -DEBUG_LOG_FILE_PATH=./debug.log -WATCH= diff --git a/example/.simple-mock.yaml b/example/.simple-mock.yaml new file mode 100644 index 0000000..b83ed4e --- /dev/null +++ b/example/.simple-mock.yaml @@ -0,0 +1,10 @@ +ROOT_DIR: . +API_DIR: apis +STATIC_DIR: ./static +STATIC_ROUTE_PREFIX: static +PORT: 3000 +SILENT: +ERROR_LOG_FILE_PATH: ./error.log +LOG_SIZE: 10M +DEBUG_LOG_FILE_PATH: ./debug.log +WATCH: diff --git a/index.ts b/index.ts index 9f16b16..18e73bc 100644 --- a/index.ts +++ b/index.ts @@ -4,10 +4,9 @@ import Elysia from 'elysia'; import * as path from "path"; import * as Mock from "mockjs"; import Mustache from "mustache"; -import dotenv from 'dotenv'; import * as fs from "fs"; import Logger from "@ptkdev/logger"; -import {pick} from "underscore"; +import _, {pick} from "underscore"; import {program} from "commander"; import {mergeDeep} from "elysia/utils"; import {Table} from "console-table-printer"; @@ -20,13 +19,15 @@ import {logger as midLogger} from '@grotto/logysia'; import {safeRun} from "./src/utils"; import type {IConfigParameter} from "./src/types"; import {COLOR_MAPS, DEFAULT_CONFIG} from "./src/constants"; +import yaml from "yaml"; -const cwd = (p?: string) => path.resolve(process.cwd(), p ?? ''); +const cwd = (...p: string[]) => path.resolve(process.cwd(), ...p); // 配置优先级 argv > 文件 > 默认配置 // 读取配置文件,加载配置 // S 配置启动命令信息 program.option('-d, --debug', 'output extra debugging') + .option('-r , --cwd ', 'cwd') .option('-p , --port ', 'server port') .option('-s , --silent ', 'silent output') .option('-w , --watch ', 'silent output') @@ -38,34 +39,39 @@ program.option('-d, --debug', 'output extra debugging') ; const command = program.parse(process.argv); // E 配置启动命令信息 -const parsedConfig = dotenv.parse(await safeRun(() => { - const configFilePath = cwd(command.getOptionValue('config') ?? "./.simple-mock"); +const parsedConfig = yaml.parse(await safeRun(() => { + const configFilePath = cwd(command.getOptionValue('cwd') ?? DEFAULT_CONFIG.ROOT_DIR, command.getOptionValue('config') ?? "./.simple-mock.yaml"); if (!fs.existsSync(configFilePath)) { - // @ts-ignore - return Buffer.from(''); + return ''; } - return fs.readFileSync(configFilePath); + return fs.readFileSync(configFilePath).toString(); }, // @ts-ignore - Buffer.from("") + "" )); // S 相关配置 +const choice = (cmd: any, parsed: any, defaultConfig: any) => { + return cmd ?? + _.isNull(parsed) ? + defaultConfig : + parsed; +}; const config = mergeDeep(DEFAULT_CONFIG, { - PORT: command.getOptionValue('port') ?? parsedConfig?.PORT ?? DEFAULT_CONFIG.PORT, - SILENT: command.getOptionValue("silent") ?? parsedConfig?.SILENT ?? DEFAULT_CONFIG.SILENT, - ERROR_LOG_FILE_PATH: command.getOptionValue("error_log") ?? parsedConfig?.ERROR_LOG_FILE_PATH ?? DEFAULT_CONFIG.ERROR_LOG_FILE_PATH, - DEBUG_LOG_FILE_PATH: command.getOptionValue("debug_log") ?? parsedConfig?.DEBUG_LOG_FILE_PATH ?? DEFAULT_CONFIG.DEBUG_LOG_FILE_PATH, - WATCH: command.getOptionValue("watch") ?? parsedConfig?.WATCH ?? DEFAULT_CONFIG.WATCH, - STATIC_DIR: command.getOptionValue("static-dir") ?? parsedConfig?.STATIC_DIR ?? DEFAULT_CONFIG.STATIC_DIR, - STATIC_ROUTE_PREFIX: command.getOptionValue("static-route-prefix") ?? parsedConfig?.STATIC_ROUTE_PREFIX ?? DEFAULT_CONFIG.STATIC_ROUTE_PREFIX + PORT: choice(command.getOptionValue('port'), parsedConfig?.PORT, DEFAULT_CONFIG.PORT), + SILENT: choice(command.getOptionValue("silent"), parsedConfig?.SILENT, DEFAULT_CONFIG.SILENT), + ERROR_LOG_FILE_PATH: choice(command.getOptionValue("error_log"), parsedConfig?.ERROR_LOG_FILE_PATH, DEFAULT_CONFIG.ERROR_LOG_FILE_PATH), + DEBUG_LOG_FILE_PATH: choice(command.getOptionValue("debug_log"), parsedConfig?.DEBUG_LOG_FILE_PATH, DEFAULT_CONFIG.DEBUG_LOG_FILE_PATH), + WATCH: choice(command.getOptionValue("watch"), parsedConfig?.WATCH, DEFAULT_CONFIG.WATCH), + STATIC_DIR: choice(command.getOptionValue("static-dir"), parsedConfig?.STATIC_DIR, DEFAULT_CONFIG.STATIC_DIR), + STATIC_ROUTE_PREFIX: choice(command.getOptionValue("static-route-prefix"), parsedConfig?.STATIC_ROUTE_PREFIX, DEFAULT_CONFIG.STATIC_ROUTE_PREFIX) } as Partial); const resolve = (p?: string) => path.resolve(cwd(config.ROOT_DIR), p ?? ''); // E 相关配置 -// 创建默认配置文件夹 { + // 创建默认配置文件夹 if (!fs.existsSync(resolve(config.STATIC_DIR))) { fs.mkdirSync(resolve(config.STATIC_DIR)); } diff --git a/package.json b/package.json index 2bf3c54..a0f9ba4 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "build:all": "bun run build:win-x64 && bun run build:linux-x64 && bun run build:linux-x64-baseline && bun run build:linux-arm64 && bun run build:darwin-arm64 && bun run build:darwin-x64", "build": "cross-env NODE_ENV=production bun run build:win-x64", "dev": "cross-env NODE_ENV=development bun --watch run index.ts", - "serve": "bun run index.ts" + "serve": "bun run index.ts", + "clean": "pnpx rimraf dist" }, "peerDependencies": { "typescript": "^5.0.0" @@ -34,11 +35,12 @@ "c-log": "^0.0.3", "commander": "^12.1.0", "console-table-printer": "^2.12.1", + "cross-env": "^7.0.3", "dotenv": "^16.4.5", "elysia": "^1.1.12", "mockjs": "^1.1.0", "mustache": "^4.2.0", "underscore": "^1.13.7", - "cross-env": "^7.0.3" + "yaml": "^2.5.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5744f8f..e98f097 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,6 +32,9 @@ importers: console-table-printer: specifier: ^2.12.1 version: 2.12.1 + cross-env: + specifier: ^7.0.3 + version: 7.0.3 dotenv: specifier: ^16.4.5 version: 16.4.5 @@ -50,6 +53,9 @@ importers: underscore: specifier: ^1.13.7 version: 1.13.7 + yaml: + specifier: ^2.5.1 + version: 2.5.1 devDependencies: '@types/bun': specifier: latest @@ -69,9 +75,6 @@ importers: chalk: specifier: ^5.3.0 version: 5.3.0 - cross-env: - specifier: ^7.0.3 - version: 7.0.3 packages: @@ -307,6 +310,11 @@ packages: engines: {node: '>= 8'} hasBin: true + yaml@2.5.1: + resolution: {integrity: sha512-bLQOjaX/ADgQ20isPJRvF0iRUHIxVhYvr53Of7wGcWlO2jvtUlH5m87DsmulFVxRpNLOnI4tB6p/oh8D7kpn9Q==} + engines: {node: '>= 14'} + hasBin: true + snapshots: '@elysiajs/cors@1.1.1(elysia@1.1.12(@sinclair/typebox@0.33.9)(typescript@5.5.4))': @@ -510,3 +518,5 @@ snapshots: which@2.0.2: dependencies: isexe: 2.0.0 + + yaml@2.5.1: {}