WMS_MAIN/script/method-doc-common.cjs
liufumingren 6e6a84101e fix(doc): method-doc 脚本 --env production 未真正切到生产库
database/config.js 的 development 与 production 导出同一份 baseConfig,实际连接目标只由
WMS_DB_TARGET 决定,导致脚本文档里的 --env production 会静默连开发库。loadDbConfig 在
env=production 且未显式设置 WMS_DB_TARGET 时补设该变量。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-19 18:17:23 +08:00

135 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* methods/system.json 库表化维护脚本共用工具。
*
* 环境:`--env production` 或 `NODE_ENV=production` 走生产库;默认开发库。
* JSON 字段参数(headers/params/response/errors)支持内联 JSON 或 `@文件路径`。
*/
const fs = require('node:fs');
const path = require('node:path');
const mysql = require('mysql2/promise');
function resolveEnv(argv = process.argv) {
const idx = argv.indexOf('--env');
if (idx > -1 && argv[idx + 1] && !argv[idx + 1].startsWith('--')) {
return argv[idx + 1] === 'production' ? 'production' : 'development';
}
return process.env.NODE_ENV === 'production' ? 'production' : 'development';
}
function loadDbConfig(env) {
// database/config.js 只按 WMS_DB_TARGET 选连接目标(development / production 导出同一个 baseConfig),
// 因此 --env production 必须先落到 WMS_DB_TARGET,否则会静默连开发库。
if (env === 'production' && !process.env.WMS_DB_TARGET) {
process.env.WMS_DB_TARGET = 'production';
}
const dbConfig = require(path.join(__dirname, '..', 'database', 'config'));
return dbConfig[env];
}
function warnIfProduction(env) {
if (env !== 'production') return;
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
console.log('[DB-CONFIG] ⚠ 警告:目标为【生产数据库】');
console.log('[DB-CONFIG] ⚠ 请确认这是预期环境;如属误判,请去掉 --env production 或清空 NODE_ENV。');
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
}
async function createConnection(env) {
warnIfProduction(env);
const cfg = loadDbConfig(env);
return mysql.createConnection({
host: cfg.host,
port: cfg.port,
user: cfg.username,
password: cfg.password,
database: cfg.database,
charset: 'utf8mb4',
});
}
/** 简单参数解析:--key value / --flag;返回 Map。
* @param {string[]} [argv] 参数数组(默认 process.argv)。
*/
function parseArgs(argv = process.argv) {
const map = new Map();
for (let i = 2; i < argv.length; i++) {
const token = argv[i];
if (!token.startsWith('--')) continue;
const key = token.slice(2);
const next = argv[i + 1];
if (next !== undefined && !next.startsWith('--')) {
map.set(key, next);
i++;
} else {
map.set(key, '');
}
}
return map;
}
/** JSON 参数:内联 JSON 或 @文件路径。
* @param {string|undefined} value 参数值。
*/
function readJsonArg(value) {
if (value === undefined || value === null || value === '') return null;
const text = String(value);
if (text.startsWith('@')) {
return JSON.parse(fs.readFileSync(text.slice(1), 'utf8'));
}
return JSON.parse(text);
}
/** 解析 JSON 列(DB 返回 string/object 两种可能)。
* @param {string|object|null|undefined} raw 原始值。
*/
function parseJsonColumn(raw) {
if (raw === null || raw === undefined) return null;
if (typeof raw === 'object') return raw;
return JSON.parse(String(raw));
}
/** 版本号补丁位 +1:0.3.169 -> 0.3.170。
* @param {string} version 当前版本。
*/
function bumpVersion(version) {
const parts = String(version || '0.3.0').split('.');
const last = parts.length - 1;
parts[last] = String((Number(parts[last]) || 0) + 1);
return parts.join('.');
}
function today() {
const d = new Date();
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
}
/** Date/字符串统一格式化为 YYYY-MM-DD。
* @param {Date|string} value 日期值。
*/
function formatDate(value) {
if (value instanceof Date) {
return `${value.getFullYear()}-${String(value.getMonth() + 1).padStart(2, '0')}-${String(value.getDate()).padStart(2, '0')}`;
}
return String(value || '').slice(0, 10);
}
function fail(message) {
console.error(`[method-doc] ${message}`);
process.exit(1);
}
module.exports = {
resolveEnv,
createConnection,
parseArgs,
readJsonArg,
parseJsonColumn,
bumpVersion,
today,
formatDate,
fail,
};