101 lines
4.5 KiB
JavaScript
101 lines
4.5 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 更新 API 方法文档(部分更新;建议完成后执行 method-doc-export.cjs 回写 methods/system.json)。
|
||
*
|
||
* 用法:
|
||
* node script/method-doc-update.cjs --method wechat.xxx \
|
||
* [--module wechat] [--group 微信模块] [--description "新说明"] \
|
||
* [--login 1|0] [--auth 1|0] [--status 1|0] \
|
||
* [--headers @h.json] [--params @p.json] [--response @r.json] [--errors @e.json] \
|
||
* [--bump] [--doc system] [--env production]
|
||
*
|
||
* 至少提供除 --method/--env/--doc 外的一个字段;JSON 字段支持内联 JSON 或 @文件路径。
|
||
*/
|
||
|
||
const {
|
||
resolveEnv, createConnection, parseArgs, readJsonArg, parseJsonColumn, bumpVersion, today, fail,
|
||
} = require('./method-doc-common.cjs');
|
||
|
||
(async () => {
|
||
const args = parseArgs();
|
||
const env = resolveEnv();
|
||
const docName = args.get('doc') || 'system';
|
||
|
||
const method = args.get('method');
|
||
if (!method) fail('缺少 --method(方法名)');
|
||
|
||
const updates = [];
|
||
const values = [];
|
||
const push = (column, value) => {
|
||
updates.push(`${column}=?`);
|
||
values.push(value);
|
||
};
|
||
|
||
if (args.has('description')) push('description', args.get('description') || '');
|
||
if (args.has('module')) push('module', args.get('module'));
|
||
if (args.has('group')) push('group_name', args.get('group') || '');
|
||
if (args.has('login')) push('is_need_login', String(args.get('login')) === '1' ? 1 : 0);
|
||
if (args.has('auth')) push('is_need_auth', String(args.get('auth')) === '1' ? 1 : 0);
|
||
if (args.has('status')) push('status', String(args.get('status')) === '1' ? 1 : 0);
|
||
if (args.has('headers')) push('headers', JSON.stringify(readJsonArg(args.get('headers')) || []));
|
||
if (args.has('params')) push('params', JSON.stringify(readJsonArg(args.get('params')) || []));
|
||
if (args.has('response')) push('response', JSON.stringify(readJsonArg(args.get('response')) || []));
|
||
if (args.has('errors')) push('errors', JSON.stringify(readJsonArg(args.get('errors')) || []));
|
||
|
||
if (updates.length === 0) {
|
||
fail('未提供任何要更新的字段(description/module/group/login/auth/status/headers/params/response/errors)');
|
||
}
|
||
|
||
const bump = args.has('bump');
|
||
const conn = await createConnection(env);
|
||
|
||
// 读取当前行,payload 原文基础上应用变更(保持未改字段原键序)
|
||
const [ currentRows ] = await conn.query(
|
||
'SELECT method, module, group_name, description, is_need_login, is_need_auth, headers, params, response, errors, payload FROM wms_api_method_doc WHERE method=? LIMIT 1',
|
||
[ method ]
|
||
);
|
||
if (!currentRows[0]) {
|
||
await conn.end();
|
||
fail(`method 不存在(${method}),请先用 method-doc-create.cjs 创建`);
|
||
}
|
||
const current = currentRows[0];
|
||
const item = current.payload ? parseJsonColumn(current.payload) : {
|
||
method: current.method,
|
||
description: current.description || '',
|
||
isNeedAuth: !!current.is_need_auth,
|
||
isNeedLogin: !!current.is_need_login,
|
||
headers: parseJsonColumn(current.headers) || [],
|
||
params: parseJsonColumn(current.params) || [],
|
||
response: parseJsonColumn(current.response) || [],
|
||
errors: parseJsonColumn(current.errors) || [],
|
||
};
|
||
|
||
if (args.has('description')) item.description = args.get('description') || '';
|
||
if (args.has('login')) item.isNeedLogin = String(args.get('login')) === '1';
|
||
if (args.has('auth')) item.isNeedAuth = String(args.get('auth')) === '1';
|
||
if (args.has('headers')) item.headers = readJsonArg(args.get('headers')) || [];
|
||
if (args.has('params')) item.params = readJsonArg(args.get('params')) || [];
|
||
if (args.has('response')) item.response = readJsonArg(args.get('response')) || [];
|
||
if (args.has('errors')) item.errors = readJsonArg(args.get('errors')) || [];
|
||
|
||
const [ result ] = await conn.query(
|
||
`UPDATE wms_api_method_doc SET ${updates.join(', ')}, updated_at=NOW(), payload=? WHERE method=?`,
|
||
[ ...values, JSON.stringify(item), method ]
|
||
);
|
||
if (result.affectedRows === 0) {
|
||
await conn.end();
|
||
fail(`method 不存在(${method})`);
|
||
}
|
||
|
||
if (bump) {
|
||
const [ metaRows ] = await conn.query('SELECT version FROM wms_method_doc_meta WHERE doc_name=?', [ docName ]);
|
||
const next = bumpVersion(metaRows[0] ? metaRows[0].version : '0.3.0');
|
||
await conn.query('UPDATE wms_method_doc_meta SET version=?, updated_at=? WHERE doc_name=?', [ next, today(), docName ]);
|
||
console.log(`[method-doc] 文档版本已升至 ${next}`);
|
||
}
|
||
|
||
await conn.end();
|
||
console.log(`[method-doc] 已更新:${method}(字段:${updates.map(u => u.split('=')[0]).join('/')}${bump ? '' : ',未 --bump 版本未变'})(env=${env})`);
|
||
})().catch(e => fail(e.message));
|