Skip to content

Commit

Permalink
chore: use yaml config
Browse files Browse the repository at this point in the history
  • Loading branch information
couriourc committed Sep 8, 2024
1 parent 6a1ff9f commit e33f284
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
10 changes: 0 additions & 10 deletions example/.simple-mock

This file was deleted.

10 changes: 10 additions & 0 deletions example/.simple-mock.yaml
Original file line number Diff line number Diff line change
@@ -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:
40 changes: 23 additions & 17 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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>', 'cwd')
.option('-p , --port <port>', 'server port')
.option('-s , --silent <silent>', 'silent output')
.option('-w , --watch <watch>', 'silent output')
Expand All @@ -38,34 +39,39 @@ program.option('-d, --debug', 'output extra debugging')
;
const command = program.parse(process.argv);
// E 配置启动命令信息
const parsedConfig = dotenv.parse<IConfigParameter>(await safeRun(() => {
const configFilePath = cwd(command.getOptionValue('config') ?? "./.simple-mock");
const parsedConfig = yaml.parse<IConfigParameter>(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<IConfigParameter>);
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));
}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e33f284

Please sign in to comment.