@lark-apaas/miaoda-cli 0.1.3-alpha.4bf312e → 0.1.3-alpha.67da0bb
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/app/api.js +3 -3
- package/dist/api/app/schemas.js +43 -43
- package/dist/api/db/api.js +108 -112
- package/dist/api/db/client.js +41 -41
- package/dist/api/db/parsers.js +20 -20
- package/dist/api/db/sql-keywords.js +87 -87
- package/dist/api/deploy/api.js +5 -5
- package/dist/api/deploy/schemas.js +32 -32
- package/dist/api/file/api.js +74 -87
- package/dist/api/file/client.js +22 -22
- package/dist/api/file/detect.js +3 -3
- package/dist/api/file/parsers.js +18 -7
- package/dist/api/observability/api.js +6 -6
- package/dist/api/observability/schemas.js +14 -14
- package/dist/api/plugin/api.js +31 -31
- package/dist/cli/commands/app/index.js +12 -12
- package/dist/cli/commands/db/index.js +268 -163
- package/dist/cli/commands/deploy/index.js +28 -28
- package/dist/cli/commands/file/index.js +81 -61
- package/dist/cli/commands/observability/index.js +69 -69
- package/dist/cli/commands/plugin/index.js +27 -27
- package/dist/cli/commands/shared.js +10 -10
- package/dist/cli/handlers/app/update.js +2 -2
- package/dist/cli/handlers/db/audit.js +52 -43
- package/dist/cli/handlers/db/changelog.js +25 -12
- package/dist/cli/handlers/db/data.js +31 -30
- package/dist/cli/handlers/db/migration.js +27 -27
- package/dist/cli/handlers/db/quota.js +3 -3
- package/dist/cli/handlers/db/recovery.js +111 -82
- package/dist/cli/handlers/db/schema.js +33 -33
- package/dist/cli/handlers/db/sql.js +69 -69
- package/dist/cli/handlers/deploy/deploy.js +4 -4
- package/dist/cli/handlers/deploy/error-log.js +1 -1
- package/dist/cli/handlers/deploy/get.js +3 -3
- package/dist/cli/handlers/deploy/polling.js +11 -11
- package/dist/cli/handlers/file/cp.js +30 -30
- package/dist/cli/handlers/file/ls.js +5 -5
- package/dist/cli/handlers/file/quota.js +2 -2
- package/dist/cli/handlers/file/rm.js +24 -24
- package/dist/cli/handlers/file/sign.js +3 -3
- package/dist/cli/handlers/file/stat.js +10 -9
- package/dist/cli/handlers/observability/analytics.js +47 -47
- package/dist/cli/handlers/observability/helpers.js +2 -2
- package/dist/cli/handlers/observability/log.js +9 -9
- package/dist/cli/handlers/observability/metric.js +26 -26
- package/dist/cli/handlers/observability/trace.js +5 -5
- package/dist/cli/handlers/plugin/plugin-local.js +53 -53
- package/dist/cli/handlers/plugin/plugin.js +15 -15
- package/dist/cli/help.js +16 -16
- package/dist/main.js +12 -12
- package/dist/utils/args.js +1 -1
- package/dist/utils/colors.js +2 -2
- package/dist/utils/config.js +2 -2
- package/dist/utils/devops-error.js +9 -9
- package/dist/utils/error.js +2 -2
- package/dist/utils/git.js +4 -4
- package/dist/utils/http.js +19 -19
- package/dist/utils/output.js +47 -47
- package/dist/utils/poll.js +2 -2
- package/dist/utils/render.js +27 -27
- package/dist/utils/time.js +47 -42
- package/package.json +1 -1
|
@@ -4,13 +4,13 @@ exports.registerPluginCommands = registerPluginCommands;
|
|
|
4
4
|
const index_1 = require("../../../cli/handlers/plugin/index");
|
|
5
5
|
function registerPluginCommands(program) {
|
|
6
6
|
const pluginCmd = program
|
|
7
|
-
.command(
|
|
8
|
-
.description(
|
|
9
|
-
.usage(
|
|
7
|
+
.command('plugin')
|
|
8
|
+
.description('插件管理:安装/更新/移除插件包,查询 capability 实例')
|
|
9
|
+
.usage('<command> [flags]');
|
|
10
10
|
pluginCmd.action(() => {
|
|
11
11
|
pluginCmd.outputHelp();
|
|
12
12
|
});
|
|
13
|
-
pluginCmd.addHelpText(
|
|
13
|
+
pluginCmd.addHelpText('after', `
|
|
14
14
|
概念
|
|
15
15
|
- 插件包 (plugin package):.tgz 包,释放到 ./node_modules/<name>
|
|
16
16
|
- actionPlugins:./package.json 里的字段,记录 { "@scope/name": "version" },作为
|
|
@@ -26,10 +26,10 @@ function registerPluginCommands(program) {
|
|
|
26
26
|
- remove / list / list-packages:纯本地 FS,不走网络
|
|
27
27
|
`);
|
|
28
28
|
pluginCmd
|
|
29
|
-
.command(
|
|
30
|
-
.description(
|
|
31
|
-
.argument(
|
|
32
|
-
.addHelpText(
|
|
29
|
+
.command('install')
|
|
30
|
+
.description('拉取插件包并安装到当前应用项目')
|
|
31
|
+
.argument('<names...>', '插件名,格式 @scope/name 或 @scope/name@version;未带版本默认 latest')
|
|
32
|
+
.addHelpText('after', `
|
|
33
33
|
行为
|
|
34
34
|
- 拉取 .tgz 到本地缓存,解压到 ./node_modules/<name>
|
|
35
35
|
- 在 ./package.json 的 actionPlugins 字段记录 <name>: <version>
|
|
@@ -62,10 +62,10 @@ JSON 输出
|
|
|
62
62
|
await (0, index_1.handlePluginInstall)({ names });
|
|
63
63
|
});
|
|
64
64
|
pluginCmd
|
|
65
|
-
.command(
|
|
66
|
-
.description(
|
|
67
|
-
.argument(
|
|
68
|
-
.addHelpText(
|
|
65
|
+
.command('update')
|
|
66
|
+
.description('把已安装插件升级到 latest 版本')
|
|
67
|
+
.argument('<names...>', '插件名,格式 @scope/name;带 @version 也只取 name 部分')
|
|
68
|
+
.addHelpText('after', `
|
|
69
69
|
行为
|
|
70
70
|
- 仅对已在 actionPlugins 里的插件生效;未安装的插件返回 notInstalled
|
|
71
71
|
- 拉取 latest 版本 .tgz,覆盖安装到 ./node_modules/<name>
|
|
@@ -97,10 +97,10 @@ JSON 输出
|
|
|
97
97
|
await (0, index_1.handlePluginUpdate)({ names });
|
|
98
98
|
});
|
|
99
99
|
pluginCmd
|
|
100
|
-
.command(
|
|
101
|
-
.description(
|
|
102
|
-
.argument(
|
|
103
|
-
.addHelpText(
|
|
100
|
+
.command('remove')
|
|
101
|
+
.description('从当前项目移除一个已安装的插件')
|
|
102
|
+
.argument('<name>', '插件名,格式 @scope/name')
|
|
103
|
+
.addHelpText('after', `
|
|
104
104
|
行为
|
|
105
105
|
- 删除 ./node_modules/<name> 目录
|
|
106
106
|
- 从 ./package.json 的 actionPlugins 字段移除对应条目
|
|
@@ -123,9 +123,9 @@ JSON 输出
|
|
|
123
123
|
(0, index_1.handlePluginRemove)({ name });
|
|
124
124
|
});
|
|
125
125
|
pluginCmd
|
|
126
|
-
.command(
|
|
127
|
-
.description(
|
|
128
|
-
.addHelpText(
|
|
126
|
+
.command('init')
|
|
127
|
+
.description('按 package.json 的 actionPlugins 批量安装所有插件')
|
|
128
|
+
.addHelpText('after', `
|
|
129
129
|
行为
|
|
130
130
|
- 读取 ./package.json 的 actionPlugins,对每个条目按固定版本安装
|
|
131
131
|
- 已经是目标版本的跳过(幂等),用于 clone 项目后首次同步 / CI
|
|
@@ -156,11 +156,11 @@ JSON 输出
|
|
|
156
156
|
await (0, index_1.handlePluginInit)();
|
|
157
157
|
});
|
|
158
158
|
pluginCmd
|
|
159
|
-
.command(
|
|
160
|
-
.description(
|
|
161
|
-
.option(
|
|
162
|
-
.option(
|
|
163
|
-
.addHelpText(
|
|
159
|
+
.command('list')
|
|
160
|
+
.description('列出当前项目的 capability 实例(./server/capabilities/*.json)')
|
|
161
|
+
.option('--summary', '不 hydrate(不充血),直接返回 capability 文件的原始 JSON')
|
|
162
|
+
.option('--id <id>', '只查询指定 id 的单个 capability')
|
|
163
|
+
.addHelpText('after', `
|
|
164
164
|
行为
|
|
165
165
|
- 扫描 ./server/capabilities/*.json(排除 capabilities.json)
|
|
166
166
|
- 默认对每条 capability 做 hydrate:读取 ./node_modules/<pluginKey>/manifest.json,
|
|
@@ -190,9 +190,9 @@ JSON 输出
|
|
|
190
190
|
await (0, index_1.handlePluginList)(opts);
|
|
191
191
|
});
|
|
192
192
|
pluginCmd
|
|
193
|
-
.command(
|
|
194
|
-
.description(
|
|
195
|
-
.addHelpText(
|
|
193
|
+
.command('list-packages')
|
|
194
|
+
.description('列出 package.json actionPlugins 里已声明的插件包')
|
|
195
|
+
.addHelpText('after', `
|
|
196
196
|
行为
|
|
197
197
|
- 读取 ./package.json 的 actionPlugins 字段
|
|
198
198
|
- 只看声明,不校验 ./node_modules 下是否实际存在
|
|
@@ -16,10 +16,10 @@ const args_1 = require("../../utils/args");
|
|
|
16
16
|
Object.defineProperty(exports, "failArgs", { enumerable: true, get: function () { return args_1.failArgs; } });
|
|
17
17
|
/** --app-id option,需要应用上下文的命令自行 .addOption(appIdOption()) */
|
|
18
18
|
function appIdOption() {
|
|
19
|
-
return new commander_1.Option(
|
|
19
|
+
return new commander_1.Option('--app-id <id>', '指定目标应用').env('MIAODA_APP_ID');
|
|
20
20
|
}
|
|
21
21
|
function branchOption() {
|
|
22
|
-
return new commander_1.Option(
|
|
22
|
+
return new commander_1.Option('--branch <branch>', '分支(优先级:--branch > 当前仓库 HEAD 分支;非 git 目录回退应用默认分支)').env('LOCAL_MOCK_MIAODA_DEPLOY_BRANCH');
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
25
|
* soft-required: Commander 类型上 optional,runtime 校验必填。
|
|
@@ -36,8 +36,8 @@ function softRequiredOption(name, desc) {
|
|
|
36
36
|
function resolveAppId(opts) {
|
|
37
37
|
const id = opts.appId ?? process.env.MIAODA_APP_ID ?? process.env.app_id;
|
|
38
38
|
if (!id) {
|
|
39
|
-
throw new error_1.AppError(
|
|
40
|
-
next_actions: [
|
|
39
|
+
throw new error_1.AppError('APP_ID_MISSING', '未指定应用', {
|
|
40
|
+
next_actions: ['设置 export MIAODA_APP_ID=<id>'],
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
return id;
|
|
@@ -53,7 +53,7 @@ function withHelp(cmd, handler) {
|
|
|
53
53
|
await handler(...args);
|
|
54
54
|
}
|
|
55
55
|
catch (err) {
|
|
56
|
-
if (err instanceof error_1.AppError && err.code ===
|
|
56
|
+
if (err instanceof error_1.AppError && err.code === 'ARGS_INVALID') {
|
|
57
57
|
process.stderr.write(`Error: ${err.message}\n`);
|
|
58
58
|
cmd.outputHelp();
|
|
59
59
|
process.exitCode = 2;
|
|
@@ -81,7 +81,7 @@ function caseInsensitiveChoice(canonical) {
|
|
|
81
81
|
return (raw) => {
|
|
82
82
|
const hit = map.get(raw.toLowerCase());
|
|
83
83
|
if (hit === undefined) {
|
|
84
|
-
throw new commander_1.InvalidArgumentError(`Allowed choices are ${canonical.join(
|
|
84
|
+
throw new commander_1.InvalidArgumentError(`Allowed choices are ${canonical.join(', ')} (case-insensitive).`);
|
|
85
85
|
}
|
|
86
86
|
return hit;
|
|
87
87
|
};
|
|
@@ -98,8 +98,8 @@ function validateTimeOptions(opts, ...names) {
|
|
|
98
98
|
const value = opts[name];
|
|
99
99
|
if (value === undefined)
|
|
100
100
|
continue;
|
|
101
|
-
if (typeof value !==
|
|
102
|
-
(0, args_1.failArgs)(`--${name.replace(/([A-Z])/g,
|
|
101
|
+
if (typeof value !== 'string') {
|
|
102
|
+
(0, args_1.failArgs)(`--${name.replace(/([A-Z])/g, '-$1').toLowerCase()} 必须是时间字符串`);
|
|
103
103
|
}
|
|
104
104
|
(0, time_1.parseTimeToMs)(value);
|
|
105
105
|
}
|
|
@@ -118,8 +118,8 @@ function validateTimeOptions(opts, ...names) {
|
|
|
118
118
|
*/
|
|
119
119
|
function rejectCliOverride(cmd, ...optNames) {
|
|
120
120
|
for (const name of optNames) {
|
|
121
|
-
if (cmd.getOptionValueSource(name) ===
|
|
122
|
-
const flag =
|
|
121
|
+
if (cmd.getOptionValueSource(name) === 'cli') {
|
|
122
|
+
const flag = '--' + name.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
123
123
|
(0, args_1.failArgs)(`${flag} 由沙箱/本地配置注入,不允许显式传递`);
|
|
124
124
|
}
|
|
125
125
|
}
|
|
@@ -41,7 +41,7 @@ const index_1 = require("../../../api/app/index");
|
|
|
41
41
|
/** miaoda app update [--app-id <id>] [--name <n>] [--description <d>] */
|
|
42
42
|
async function handleAppUpdate(opts) {
|
|
43
43
|
if (opts.name === undefined && opts.description === undefined) {
|
|
44
|
-
(0, args_1.failArgs)(
|
|
44
|
+
(0, args_1.failArgs)('至少需要 --name 或 --description 中的一个');
|
|
45
45
|
}
|
|
46
46
|
const appID = opts.appId;
|
|
47
47
|
await api.app.updateAppMeta({
|
|
@@ -53,7 +53,7 @@ async function handleAppUpdate(opts) {
|
|
|
53
53
|
const resp = await api.app.getAppInfo(appID);
|
|
54
54
|
const meta = resp.appInfo?.appMeta ?? {};
|
|
55
55
|
if (!(0, output_1.isJsonMode)()) {
|
|
56
|
-
process.stdout.write(
|
|
56
|
+
process.stdout.write('✓ App updated successfully\n');
|
|
57
57
|
}
|
|
58
58
|
(0, output_1.emit)({ data: meta }, index_1.appMetaSchema);
|
|
59
59
|
}
|
|
@@ -38,19 +38,19 @@ exports.handleDbAuditEnable = handleDbAuditEnable;
|
|
|
38
38
|
exports.handleDbAuditDisable = handleDbAuditDisable;
|
|
39
39
|
exports.handleDbAuditList = handleDbAuditList;
|
|
40
40
|
const api = __importStar(require("../../../api/index"));
|
|
41
|
-
const index_1 = require("../../../api/file/index");
|
|
42
41
|
const shared_1 = require("../../../cli/commands/shared");
|
|
43
42
|
const colors_1 = require("../../../utils/colors");
|
|
44
43
|
const error_1 = require("../../../utils/error");
|
|
45
44
|
const output_1 = require("../../../utils/output");
|
|
46
45
|
const render_1 = require("../../../utils/render");
|
|
47
|
-
const
|
|
46
|
+
const time_1 = require("../../../utils/time");
|
|
47
|
+
const VALID_RETENTION = new Set(['7d', '30d', '180d', '360d', 'forever']);
|
|
48
48
|
async function handleDbAuditStatus(table, opts) {
|
|
49
49
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
50
50
|
const items = await api.db.getAuditStatus({ appId, table, dbBranch: opts.env });
|
|
51
51
|
const rows = items.map(toAuditStatus);
|
|
52
52
|
// 单表查询但后端没记录 → 占位 enabled=false
|
|
53
|
-
if (table !== undefined && table !==
|
|
53
|
+
if (table !== undefined && table !== '' && rows.length === 0) {
|
|
54
54
|
rows.push({ table, enabled: false, enabled_at: null, retention: null });
|
|
55
55
|
}
|
|
56
56
|
// PRD JSON:单表返 object,多表返 array
|
|
@@ -64,7 +64,7 @@ async function handleDbAuditStatus(table, opts) {
|
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
if (rows.length === 0) {
|
|
67
|
-
(0, output_1.emit)(
|
|
67
|
+
(0, output_1.emit)('No audit configuration found.');
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
70
|
const tty = (0, render_1.isStdoutTty)();
|
|
@@ -72,20 +72,20 @@ async function handleDbAuditStatus(table, opts) {
|
|
|
72
72
|
if (table !== undefined && rows.length === 1) {
|
|
73
73
|
const it = rows[0];
|
|
74
74
|
(0, output_1.emit)((0, render_1.renderKeyValue)([
|
|
75
|
-
[
|
|
76
|
-
[
|
|
77
|
-
[
|
|
78
|
-
[
|
|
75
|
+
['Table', it.table],
|
|
76
|
+
['Enabled', boolToYesNo(it.enabled)],
|
|
77
|
+
['Enabled at', it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : '—'],
|
|
78
|
+
['Retention', it.retention ?? '—'],
|
|
79
79
|
], tty));
|
|
80
80
|
return;
|
|
81
81
|
}
|
|
82
82
|
// 列表 → table 形态
|
|
83
|
-
const headers = [
|
|
83
|
+
const headers = ['table', 'enabled', 'enabled_at', 'retention'];
|
|
84
84
|
const out = rows.map((it) => [
|
|
85
85
|
it.table,
|
|
86
86
|
boolToYesNo(it.enabled),
|
|
87
|
-
it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) :
|
|
88
|
-
it.retention ??
|
|
87
|
+
it.enabled_at ? (0, render_1.formatTime)(it.enabled_at, tty) : '—',
|
|
88
|
+
it.retention ?? '—',
|
|
89
89
|
]);
|
|
90
90
|
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, out) : (0, render_1.renderTsv)(headers, out));
|
|
91
91
|
}
|
|
@@ -99,16 +99,16 @@ function toAuditStatus(s) {
|
|
|
99
99
|
}
|
|
100
100
|
async function handleDbAuditEnable(table, opts) {
|
|
101
101
|
if (!table) {
|
|
102
|
-
throw new error_1.AppError(
|
|
102
|
+
throw new error_1.AppError('ARGS_INVALID', 'table name is required', {
|
|
103
103
|
next_actions: [
|
|
104
|
-
|
|
104
|
+
'Usage: miaoda db audit enable <table> [--retention 7d|30d|180d|360d|forever]',
|
|
105
105
|
],
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
|
-
const retention = opts.retention ??
|
|
108
|
+
const retention = opts.retention ?? '7d';
|
|
109
109
|
if (!VALID_RETENTION.has(retention)) {
|
|
110
|
-
throw new error_1.AppError(
|
|
111
|
-
next_actions: [
|
|
110
|
+
throw new error_1.AppError('INVALID_RETENTION', `Invalid retention '${retention}'`, {
|
|
111
|
+
next_actions: ['Allowed values: 7d, 30d, 180d, 360d, forever.'],
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
114
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
@@ -124,7 +124,7 @@ async function handleDbAuditEnable(table, opts) {
|
|
|
124
124
|
}
|
|
125
125
|
catch (err) {
|
|
126
126
|
// PRD: 重复 enable 报错时附带 hint,引导用户去 status 看 retention 或换值更新
|
|
127
|
-
if (err instanceof error_1.AppError && err.code ===
|
|
127
|
+
if (err instanceof error_1.AppError && err.code === 'DB_API_k_dl_1300030') {
|
|
128
128
|
throw new error_1.AppError(err.code, err.message, {
|
|
129
129
|
next_actions: [
|
|
130
130
|
`Use \`miaoda db audit status ${table}\` to check current retention, or run this command with a different --retention to update.`,
|
|
@@ -143,15 +143,15 @@ async function handleDbAuditEnable(table, opts) {
|
|
|
143
143
|
return;
|
|
144
144
|
}
|
|
145
145
|
const tty = (0, render_1.isStdoutTty)();
|
|
146
|
-
const prefix = tty ? "✓" : "OK";
|
|
147
146
|
// c.highlight 内部按 TTY/NO_COLOR/FORCE_COLOR 自动决定是否染色,无需再判 tty
|
|
148
147
|
const tableLabel = colors_1.c.highlight(`'${status.table}'`);
|
|
149
|
-
|
|
148
|
+
const body = `Audit enabled for table ${tableLabel} (retention: ${status.retention ?? retention})`;
|
|
149
|
+
(0, output_1.emit)(tty ? colors_1.c.success(`✓ ${body}`) : `OK ${body}`);
|
|
150
150
|
}
|
|
151
151
|
async function handleDbAuditDisable(table, opts) {
|
|
152
152
|
if (!table) {
|
|
153
|
-
throw new error_1.AppError(
|
|
154
|
-
next_actions: [
|
|
153
|
+
throw new error_1.AppError('ARGS_INVALID', 'table name is required', {
|
|
154
|
+
next_actions: ['Usage: miaoda db audit disable <table>'],
|
|
155
155
|
});
|
|
156
156
|
}
|
|
157
157
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
@@ -166,9 +166,9 @@ async function handleDbAuditDisable(table, opts) {
|
|
|
166
166
|
}
|
|
167
167
|
catch (err) {
|
|
168
168
|
// PRD: 重复 disable / 未启用就 disable 报错时附带 hint,引导用户去看哪些表已开启
|
|
169
|
-
if (err instanceof error_1.AppError && err.code ===
|
|
169
|
+
if (err instanceof error_1.AppError && err.code === 'DB_API_k_dl_1300031') {
|
|
170
170
|
throw new error_1.AppError(err.code, err.message, {
|
|
171
|
-
next_actions: [
|
|
171
|
+
next_actions: ['Use `miaoda db audit status` to see which tables have audit enabled.'],
|
|
172
172
|
});
|
|
173
173
|
}
|
|
174
174
|
throw err;
|
|
@@ -181,14 +181,14 @@ async function handleDbAuditDisable(table, opts) {
|
|
|
181
181
|
return;
|
|
182
182
|
}
|
|
183
183
|
const tty = (0, render_1.isStdoutTty)();
|
|
184
|
-
const
|
|
185
|
-
(0, output_1.emit)(
|
|
184
|
+
const body = `Audit disabled for table ${colors_1.c.highlight(`'${status.table}'`)}`;
|
|
185
|
+
(0, output_1.emit)(tty ? colors_1.c.success(`✓ ${body}`) : `OK ${body}`);
|
|
186
186
|
}
|
|
187
187
|
async function handleDbAuditList(tables, opts) {
|
|
188
188
|
if (tables.length === 0) {
|
|
189
|
-
throw new error_1.AppError(
|
|
189
|
+
throw new error_1.AppError('ARGS_INVALID', 'at least one table is required', {
|
|
190
190
|
next_actions: [
|
|
191
|
-
|
|
191
|
+
'Usage: miaoda db audit list <table> [<table>...] [--since ...] [--until ...]',
|
|
192
192
|
],
|
|
193
193
|
});
|
|
194
194
|
}
|
|
@@ -196,8 +196,8 @@ async function handleDbAuditList(tables, opts) {
|
|
|
196
196
|
const limit = opts.limit ?? 20;
|
|
197
197
|
// 时间字段归一化为 ISO 8601 UTC(服务端按 event_time 字段比较)。cursor 由
|
|
198
198
|
// 后端管理(本质也是 ISO 时间),CLI 不再混用 cursor/since。
|
|
199
|
-
const since = normalizeTime(opts.since,
|
|
200
|
-
const until = normalizeTime(opts.until,
|
|
199
|
+
const since = normalizeTime(opts.since, '--since');
|
|
200
|
+
const until = normalizeTime(opts.until, '--until');
|
|
201
201
|
const result = await api.db.listAuditLog({
|
|
202
202
|
appId,
|
|
203
203
|
tables,
|
|
@@ -224,16 +224,16 @@ async function handleDbAuditList(tables, opts) {
|
|
|
224
224
|
}));
|
|
225
225
|
(0, output_1.emitPaged)(items, result.nextCursor, result.hasMore);
|
|
226
226
|
if (skipped.length > 0) {
|
|
227
|
-
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(
|
|
227
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(', ')})\n`);
|
|
228
228
|
}
|
|
229
229
|
if (visible.length === 0)
|
|
230
230
|
process.exitCode = 1;
|
|
231
231
|
return;
|
|
232
232
|
}
|
|
233
233
|
if (visible.length === 0) {
|
|
234
|
-
(0, output_1.emit)(
|
|
234
|
+
(0, output_1.emit)('No audit log entries found.');
|
|
235
235
|
if (skipped.length > 0) {
|
|
236
|
-
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(
|
|
236
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(', ')})\n`);
|
|
237
237
|
}
|
|
238
238
|
process.exitCode = 1;
|
|
239
239
|
return;
|
|
@@ -242,32 +242,41 @@ async function handleDbAuditList(tables, opts) {
|
|
|
242
242
|
// PRD:多表渲染时第一列是 target_table,单表时去掉这一列
|
|
243
243
|
const isMultiTable = tables.length > 1;
|
|
244
244
|
const headers = isMultiTable
|
|
245
|
-
? [
|
|
246
|
-
: [
|
|
245
|
+
? ['target_table', 'event_time', 'type', 'event_id', 'operator', 'summary']
|
|
246
|
+
: ['event_time', 'type', 'event_id', 'operator', 'summary'];
|
|
247
247
|
const rows = visible.map((it) => {
|
|
248
248
|
const eventTime = (0, render_1.formatTime)(it.eventTime, tty);
|
|
249
249
|
// event_id 完整透传——PRD 截图里的 "..." 只是文档省略写法,不是 CLI 行为
|
|
250
|
-
const cells = [eventTime, it.type, it.eventId, it.operator ||
|
|
250
|
+
const cells = [eventTime, it.type, it.eventId, it.operator || '—', it.summary];
|
|
251
251
|
return isMultiTable ? [it.targetTable, ...cells] : cells;
|
|
252
252
|
});
|
|
253
253
|
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows));
|
|
254
254
|
if (skipped.length > 0) {
|
|
255
|
-
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(
|
|
255
|
+
process.stderr.write(`(${String(skipped.length)} table(s) skipped: ${skipped.join(', ')})\n`);
|
|
256
256
|
}
|
|
257
257
|
if (result.hasMore && result.nextCursor) {
|
|
258
258
|
process.stderr.write(`(more results; use --cursor '${result.nextCursor}')\n`);
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
261
|
// ── helpers ──
|
|
262
|
+
/**
|
|
263
|
+
* 把用户传入的 since/until 归一成 ISO 8601 UTC 字符串;空串返 undefined。
|
|
264
|
+
* 支持格式见 utils/time.ts::TIMESTAMP_HELP(相对时间 / 日期 / 本地 datetime / ISO 8601)。
|
|
265
|
+
*/
|
|
262
266
|
function normalizeTime(input, flagName) {
|
|
263
|
-
if (input === undefined || input ===
|
|
267
|
+
if (input === undefined || input === '')
|
|
264
268
|
return undefined;
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
269
|
+
try {
|
|
270
|
+
return new Date((0, time_1.parseTimeToMs)(input)).toISOString();
|
|
271
|
+
}
|
|
272
|
+
catch (err) {
|
|
273
|
+
if (err instanceof error_1.AppError) {
|
|
274
|
+
throw new error_1.AppError(err.code, `${flagName}: ${err.message}`, {
|
|
275
|
+
next_actions: err.next_actions,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
throw err;
|
|
268
279
|
}
|
|
269
|
-
const ms = (0, index_1.parseTimeFilterMs)(input, flagName);
|
|
270
|
-
return new Date(ms).toISOString();
|
|
271
280
|
}
|
|
272
281
|
// 服务端 before/after 是 JSON 字符串透传,CLI JSON 输出再反序列化回对象,
|
|
273
282
|
// 给下游 jq 等工具消费。失败时透传原始字符串避免数据丢失。
|
|
@@ -281,5 +290,5 @@ function safeParseJson(s) {
|
|
|
281
290
|
}
|
|
282
291
|
// PRD 输出 enabled 列用 yes/no 而非 true/false
|
|
283
292
|
function boolToYesNo(b) {
|
|
284
|
-
return b ?
|
|
293
|
+
return b ? 'yes' : 'no';
|
|
285
294
|
}
|
|
@@ -35,10 +35,11 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.handleDbChangelog = handleDbChangelog;
|
|
37
37
|
const api = __importStar(require("../../../api/index"));
|
|
38
|
-
const index_1 = require("../../../api/file/index");
|
|
39
38
|
const shared_1 = require("../../../cli/commands/shared");
|
|
39
|
+
const error_1 = require("../../../utils/error");
|
|
40
40
|
const output_1 = require("../../../utils/output");
|
|
41
41
|
const render_1 = require("../../../utils/render");
|
|
42
|
+
const time_1 = require("../../../utils/time");
|
|
42
43
|
function toRow(it) {
|
|
43
44
|
return {
|
|
44
45
|
change_id: it.changeId,
|
|
@@ -50,17 +51,29 @@ function toRow(it) {
|
|
|
50
51
|
statement: it.statement,
|
|
51
52
|
};
|
|
52
53
|
}
|
|
53
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* 把用户传入的 since/until 归一成 ISO 8601 UTC 字符串;空串返 undefined。
|
|
56
|
+
* 支持格式见 utils/time.ts::TIMESTAMP_HELP(相对时间 / 日期 / 本地 datetime / ISO 8601)。
|
|
57
|
+
*/
|
|
54
58
|
function normalizeTime(input, flagName) {
|
|
55
|
-
if (input === undefined || input ===
|
|
59
|
+
if (input === undefined || input === '')
|
|
56
60
|
return undefined;
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
try {
|
|
62
|
+
return new Date((0, time_1.parseTimeToMs)(input)).toISOString();
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
if (err instanceof error_1.AppError) {
|
|
66
|
+
throw new error_1.AppError(err.code, `${flagName}: ${err.message}`, {
|
|
67
|
+
next_actions: err.next_actions,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
59
72
|
}
|
|
60
73
|
async function handleDbChangelog(opts) {
|
|
61
74
|
const appId = (0, shared_1.resolveAppId)(opts);
|
|
62
|
-
const since = normalizeTime(opts.since,
|
|
63
|
-
const until = normalizeTime(opts.until,
|
|
75
|
+
const since = normalizeTime(opts.since, '--since');
|
|
76
|
+
const until = normalizeTime(opts.until, '--until');
|
|
64
77
|
const allItems = [];
|
|
65
78
|
let cursor = opts.cursor;
|
|
66
79
|
let lastCursor = null;
|
|
@@ -96,19 +109,19 @@ async function handleDbChangelog(opts) {
|
|
|
96
109
|
return;
|
|
97
110
|
}
|
|
98
111
|
if (allItems.length === 0) {
|
|
99
|
-
(0, output_1.emit)(
|
|
112
|
+
(0, output_1.emit)('No DDL changes found.');
|
|
100
113
|
return;
|
|
101
114
|
}
|
|
102
115
|
const tty = (0, render_1.isStdoutTty)();
|
|
103
116
|
// PRD: change_id / changed_at / operator / target_table / change_type / summary
|
|
104
|
-
const headers = [
|
|
117
|
+
const headers = ['change_id', 'changed_at', 'operator', 'target_table', 'change_type', 'summary'];
|
|
105
118
|
const rows = allItems.map((it) => [
|
|
106
119
|
it.changeId,
|
|
107
120
|
(0, render_1.formatTime)(it.changedAt, tty),
|
|
108
|
-
it.operator ||
|
|
109
|
-
it.targetTable ||
|
|
121
|
+
it.operator || '—',
|
|
122
|
+
it.targetTable || '—',
|
|
110
123
|
it.changeType,
|
|
111
|
-
it.summary ||
|
|
124
|
+
it.summary || '—',
|
|
112
125
|
]);
|
|
113
126
|
(0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows));
|
|
114
127
|
if (!opts.all && lastHasMore && lastCursor) {
|
|
@@ -38,6 +38,7 @@ exports.handleDbDataExport = handleDbDataExport;
|
|
|
38
38
|
const fs = __importStar(require("node:fs/promises"));
|
|
39
39
|
const path = __importStar(require("node:path"));
|
|
40
40
|
const api = __importStar(require("../../../api/index"));
|
|
41
|
+
const colors_1 = require("../../../utils/colors");
|
|
41
42
|
const error_1 = require("../../../utils/error");
|
|
42
43
|
const output_1 = require("../../../utils/output");
|
|
43
44
|
const render_1 = require("../../../utils/render");
|
|
@@ -47,30 +48,30 @@ const MAX_ROWS = 5000;
|
|
|
47
48
|
async function handleDbDataImport(file, opts) {
|
|
48
49
|
const appId = opts.appId;
|
|
49
50
|
const ext = path.extname(file).toLowerCase();
|
|
50
|
-
const format = resolveFormat(opts.format, ext,
|
|
51
|
+
const format = resolveFormat(opts.format, ext, 'import');
|
|
51
52
|
let body;
|
|
52
53
|
try {
|
|
53
54
|
body = await fs.readFile(file);
|
|
54
55
|
}
|
|
55
56
|
catch (err) {
|
|
56
57
|
const code = err.code;
|
|
57
|
-
if (code ===
|
|
58
|
-
throw new error_1.AppError(
|
|
59
|
-
next_actions: [
|
|
58
|
+
if (code === 'ENOENT') {
|
|
59
|
+
throw new error_1.AppError('IMPORT_FILE_NOT_FOUND', `Local file '${file}' does not exist`, {
|
|
60
|
+
next_actions: ['Check the file path.'],
|
|
60
61
|
});
|
|
61
62
|
}
|
|
62
63
|
throw err;
|
|
63
64
|
}
|
|
64
65
|
if (body.length > MAX_SIZE_BYTES) {
|
|
65
|
-
throw new error_1.AppError(
|
|
66
|
+
throw new error_1.AppError('IMPORT_SIZE_EXCEEDED', `Import exceeds 1 MB limit (file is ${String(body.length)} bytes)`, { next_actions: ['Split the file into chunks of ≤ 5000 rows / 1 MB and import separately.'] });
|
|
66
67
|
}
|
|
67
68
|
const rowCount = countRows(body, format);
|
|
68
69
|
if (rowCount > MAX_ROWS) {
|
|
69
|
-
throw new error_1.AppError(
|
|
70
|
+
throw new error_1.AppError('IMPORT_ROWS_EXCEEDED', `Import exceeds 5000 rows limit (file has ${String(rowCount)} rows)`, { next_actions: ['Split the file into chunks of ≤ 5000 rows / 1 MB and import separately.'] });
|
|
70
71
|
}
|
|
71
72
|
const tableName = opts.table ?? path.basename(file, ext);
|
|
72
73
|
if (!tableName) {
|
|
73
|
-
throw new error_1.AppError(
|
|
74
|
+
throw new error_1.AppError('ARGS_INVALID', 'Cannot infer target table from file name; specify --table');
|
|
74
75
|
}
|
|
75
76
|
const result = await api.db.importData({
|
|
76
77
|
appId,
|
|
@@ -90,28 +91,28 @@ async function handleDbDataImport(file, opts) {
|
|
|
90
91
|
}
|
|
91
92
|
const tty = (0, render_1.isStdoutTty)();
|
|
92
93
|
(0, output_1.emit)(tty
|
|
93
|
-
? `✓ Imported ${file} → table '${result.tableName}' (${String(result.recordCount)} rows)`
|
|
94
|
+
? colors_1.c.success(`✓ Imported ${file} → table '${result.tableName}' (${String(result.recordCount)} rows)`)
|
|
94
95
|
: `OK Imported ${file} -> table '${result.tableName}' (${String(result.recordCount)} rows)`);
|
|
95
96
|
}
|
|
96
97
|
async function handleDbDataExport(table, opts) {
|
|
97
98
|
const appId = opts.appId;
|
|
98
|
-
const format = resolveFormat(opts.format, undefined,
|
|
99
|
+
const format = resolveFormat(opts.format, undefined, 'export', 'csv');
|
|
99
100
|
const outputPath = opts.file ?? `${table}.${format}`;
|
|
100
101
|
const limit = opts.limit ? Number(opts.limit) : MAX_ROWS;
|
|
101
102
|
if (!Number.isInteger(limit) || limit <= 0 || limit > MAX_ROWS) {
|
|
102
|
-
throw new error_1.AppError(
|
|
103
|
+
throw new error_1.AppError('ARGS_INVALID', `--limit must be a positive integer ≤ ${String(MAX_ROWS)}`);
|
|
103
104
|
}
|
|
104
105
|
if (!opts.force) {
|
|
105
106
|
try {
|
|
106
107
|
await fs.access(outputPath);
|
|
107
|
-
throw new error_1.AppError(
|
|
108
|
-
next_actions: [
|
|
108
|
+
throw new error_1.AppError('FILE_ALREADY_EXISTS', `Output file '${outputPath}' already exists`, {
|
|
109
|
+
next_actions: ['Use -f to specify a different path, or --force to overwrite.'],
|
|
109
110
|
});
|
|
110
111
|
}
|
|
111
112
|
catch (err) {
|
|
112
113
|
if (err instanceof error_1.AppError)
|
|
113
114
|
throw err;
|
|
114
|
-
if (err.code !==
|
|
115
|
+
if (err.code !== 'ENOENT')
|
|
115
116
|
throw err;
|
|
116
117
|
}
|
|
117
118
|
}
|
|
@@ -122,7 +123,7 @@ async function handleDbDataExport(table, opts) {
|
|
|
122
123
|
limit,
|
|
123
124
|
});
|
|
124
125
|
if (result.body.length > MAX_SIZE_BYTES) {
|
|
125
|
-
throw new error_1.AppError(
|
|
126
|
+
throw new error_1.AppError('EXPORT_SIZE_EXCEEDED', `Export exceeds 1 MB limit (body is ${String(result.body.length)} bytes)`, {
|
|
126
127
|
next_actions: [
|
|
127
128
|
`Filter the table with "miaoda db sql" (e.g. WHERE/LIMIT) and export smaller subsets.`,
|
|
128
129
|
],
|
|
@@ -144,33 +145,33 @@ async function handleDbDataExport(table, opts) {
|
|
|
144
145
|
}
|
|
145
146
|
const tty = (0, render_1.isStdoutTty)();
|
|
146
147
|
(0, output_1.emit)(tty
|
|
147
|
-
? `✓ Exported ${table} → ${outputPath} (${String(rows)} rows)`
|
|
148
|
+
? colors_1.c.success(`✓ Exported ${table} → ${outputPath} (${String(rows)} rows)`)
|
|
148
149
|
: `OK Exported ${table} -> ${outputPath} (${String(rows)} rows)`);
|
|
149
150
|
}
|
|
150
151
|
// ── 共用辅助 ──
|
|
151
152
|
function resolveFormat(explicit, ext, scope, fallback) {
|
|
152
|
-
const raw = (explicit ?? ext ?? fallback ??
|
|
153
|
-
if (raw ===
|
|
154
|
-
return
|
|
155
|
-
if (raw ===
|
|
156
|
-
return
|
|
153
|
+
const raw = (explicit ?? ext ?? fallback ?? '').replace(/^\./, '').toLowerCase();
|
|
154
|
+
if (raw === 'csv')
|
|
155
|
+
return 'csv';
|
|
156
|
+
if (raw === 'json')
|
|
157
|
+
return 'json';
|
|
157
158
|
// sql 仅 export 路径接受 —— import 端后端仍只支持 csv/json。
|
|
158
|
-
if (raw ===
|
|
159
|
-
return
|
|
160
|
-
const code = scope ===
|
|
161
|
-
throw new error_1.AppError(code, `Unrecognized format '${raw ||
|
|
159
|
+
if (raw === 'sql' && scope === 'export')
|
|
160
|
+
return 'sql';
|
|
161
|
+
const code = scope === 'import' ? 'IMPORT_FORMAT_UNSUPPORTED' : 'EXPORT_FORMAT_UNSUPPORTED';
|
|
162
|
+
throw new error_1.AppError(code, `Unrecognized format '${raw || '(unspecified)'}'`, {
|
|
162
163
|
next_actions: [
|
|
163
|
-
scope ===
|
|
164
|
-
?
|
|
165
|
-
:
|
|
164
|
+
scope === 'import'
|
|
165
|
+
? 'Supported formats: .csv, .json. Convert the file first, or rename with the correct extension.'
|
|
166
|
+
: 'Supported formats: csv, json, sql. Pass --format csv|json|sql.',
|
|
166
167
|
],
|
|
167
168
|
});
|
|
168
169
|
}
|
|
169
170
|
function countRows(body, format) {
|
|
170
|
-
if (format ===
|
|
171
|
+
if (format === 'csv') {
|
|
171
172
|
// 粗估:非空行数 - 表头 1 行
|
|
172
173
|
let lines = 0;
|
|
173
|
-
const text = body.toString(
|
|
174
|
+
const text = body.toString('utf8');
|
|
174
175
|
for (const line of text.split(/\r?\n/)) {
|
|
175
176
|
if (line.length > 0)
|
|
176
177
|
lines += 1;
|
|
@@ -179,7 +180,7 @@ function countRows(body, format) {
|
|
|
179
180
|
}
|
|
180
181
|
// JSON:期望是顶层数组
|
|
181
182
|
try {
|
|
182
|
-
const parsed = JSON.parse(body.toString(
|
|
183
|
+
const parsed = JSON.parse(body.toString('utf8'));
|
|
183
184
|
if (Array.isArray(parsed))
|
|
184
185
|
return parsed.length;
|
|
185
186
|
return 1;
|