@lark-apaas/miaoda-cli 0.1.0-alpha.08508f4

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.
Files changed (47) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +66 -0
  3. package/bin/miaoda.js +2 -0
  4. package/dist/api/db/api.js +200 -0
  5. package/dist/api/db/client.js +166 -0
  6. package/dist/api/db/index.js +16 -0
  7. package/dist/api/db/parsers.js +161 -0
  8. package/dist/api/db/types.js +10 -0
  9. package/dist/api/file/api.js +425 -0
  10. package/dist/api/file/client.js +199 -0
  11. package/dist/api/file/detect.js +56 -0
  12. package/dist/api/file/index.js +17 -0
  13. package/dist/api/file/parsers.js +72 -0
  14. package/dist/api/file/types.js +3 -0
  15. package/dist/api/index.js +42 -0
  16. package/dist/api/plugin/api.js +243 -0
  17. package/dist/api/plugin/index.js +12 -0
  18. package/dist/api/plugin/types.js +3 -0
  19. package/dist/cli/commands/db/index.js +69 -0
  20. package/dist/cli/commands/file/index.js +75 -0
  21. package/dist/cli/commands/index.js +11 -0
  22. package/dist/cli/commands/plugin/index.js +204 -0
  23. package/dist/cli/commands/shared.js +52 -0
  24. package/dist/cli/handlers/db/data.js +168 -0
  25. package/dist/cli/handlers/db/index.js +11 -0
  26. package/dist/cli/handlers/db/schema.js +163 -0
  27. package/dist/cli/handlers/db/sql.js +252 -0
  28. package/dist/cli/handlers/file/cp.js +220 -0
  29. package/dist/cli/handlers/file/index.js +13 -0
  30. package/dist/cli/handlers/file/ls.js +110 -0
  31. package/dist/cli/handlers/file/rm.js +263 -0
  32. package/dist/cli/handlers/file/sign.js +96 -0
  33. package/dist/cli/handlers/file/stat.js +97 -0
  34. package/dist/cli/handlers/index.js +19 -0
  35. package/dist/cli/handlers/plugin/index.js +10 -0
  36. package/dist/cli/handlers/plugin/plugin-local.js +382 -0
  37. package/dist/cli/handlers/plugin/plugin.js +308 -0
  38. package/dist/main.js +31 -0
  39. package/dist/utils/config.js +31 -0
  40. package/dist/utils/error.js +38 -0
  41. package/dist/utils/http.js +67 -0
  42. package/dist/utils/index.js +27 -0
  43. package/dist/utils/log_id.js +13 -0
  44. package/dist/utils/logger.js +15 -0
  45. package/dist/utils/output.js +72 -0
  46. package/dist/utils/render.js +187 -0
  47. package/package.json +53 -0
package/dist/main.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const commander_1 = require("commander");
7
+ const index_1 = require("./cli/commands/index");
8
+ const config_1 = require("./utils/config");
9
+ const log_id_1 = require("./utils/log_id");
10
+ const output_1 = require("./utils/output");
11
+ const package_json_1 = __importDefault(require("../package.json"));
12
+ const program = new commander_1.Command();
13
+ const { version } = package_json_1.default;
14
+ program
15
+ .name("miaoda")
16
+ .description("妙搭平台命令行工具")
17
+ .version(version, "-v, --version", "显示版本号")
18
+ .option("--json [fields]", "JSON 输出,可选字段级选择")
19
+ .option("--output <format>", "输出格式(pretty|json)", "pretty")
20
+ .option("--verbose", "debug 日志到 stderr")
21
+ .helpOption("-h, --help", "显示帮助信息")
22
+ .hook("preAction", (_thisCmd, actionCmd) => {
23
+ (0, log_id_1.generateLogId)();
24
+ const opts = actionCmd.optsWithGlobals();
25
+ (0, config_1.initConfigFromOpts)(opts);
26
+ });
27
+ (0, index_1.registerCommands)(program);
28
+ program.parseAsync(process.argv).catch((err) => {
29
+ (0, output_1.emitError)(err);
30
+ process.exitCode = 1;
31
+ });
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getConfig = getConfig;
4
+ exports.setConfig = setConfig;
5
+ exports.resetConfig = resetConfig;
6
+ exports.initConfigFromOpts = initConfigFromOpts;
7
+ const defaults = {
8
+ json: false,
9
+ output: "pretty",
10
+ verbose: false,
11
+ };
12
+ let config = { ...defaults };
13
+ function getConfig() {
14
+ return config;
15
+ }
16
+ function setConfig(partial) {
17
+ config = { ...config, ...partial };
18
+ }
19
+ function resetConfig() {
20
+ config = { ...defaults };
21
+ }
22
+ /** 从 CLI 全局 option 初始化 config */
23
+ function initConfigFromOpts(opts) {
24
+ const json = opts.json;
25
+ const output = opts.output;
26
+ setConfig({
27
+ json: json ?? defaults.json,
28
+ output: output === "json" || json ? "json" : defaults.output,
29
+ verbose: opts.verbose ?? defaults.verbose,
30
+ });
31
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpError = exports.AppError = void 0;
4
+ class AppError extends Error {
5
+ code;
6
+ retryable;
7
+ next_actions;
8
+ statement_index;
9
+ constructor(code, message, opts) {
10
+ super(message);
11
+ this.name = "AppError";
12
+ this.code = code;
13
+ this.retryable = opts?.retryable ?? false;
14
+ this.next_actions = opts?.next_actions ?? [];
15
+ this.statement_index = opts?.statement_index;
16
+ }
17
+ toJSON() {
18
+ return {
19
+ code: this.code,
20
+ message: this.message,
21
+ retryable: this.retryable,
22
+ next_actions: this.next_actions,
23
+ statement_index: this.statement_index,
24
+ };
25
+ }
26
+ }
27
+ exports.AppError = AppError;
28
+ class HttpError extends AppError {
29
+ status;
30
+ url;
31
+ constructor(status, url, message, opts) {
32
+ super(`HTTP_${String(status)}`, message, opts ?? { retryable: status >= 500 || status === 429 });
33
+ this.name = "HttpError";
34
+ this.status = status;
35
+ this.url = url;
36
+ }
37
+ }
38
+ exports.HttpError = HttpError;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getHttpClient = getHttpClient;
4
+ exports.getRuntimeHttpClient = getRuntimeHttpClient;
5
+ exports.resetHttpClient = resetHttpClient;
6
+ exports.setHttpClient = setHttpClient;
7
+ exports.setRuntimeHttpClient = setRuntimeHttpClient;
8
+ exports.applyCanaryHeader = applyCanaryHeader;
9
+ const http_client_1 = require("@lark-apaas/http-client");
10
+ let adminClient;
11
+ let runtimeClient;
12
+ /**
13
+ * 获取单例 HttpClient(默认:管理端 innerapi)。
14
+ *
15
+ * 管理端链路仅适用于妙搭开发态——baseURL 从 `MIAODA_DEV_INNER_DOMAIN` 读取,
16
+ * 每次请求自动从 `MIAODA_AUTHN_CODE` 读取用户凭证并注入 `X-Miaoda-Client-Token`。
17
+ * AK/SK 的 `Authorization` / `x-api-key` 照旧叠加。
18
+ *
19
+ * 访问运行端 innerapi(`/api/v1/studio/innerapi/...`)请使用 {@link getRuntimeHttpClient}。
20
+ */
21
+ function getHttpClient() {
22
+ adminClient ??= createClient({ adminInnerApi: true });
23
+ return adminClient;
24
+ }
25
+ /** 运行端 innerapi 单例。baseURL 来自 `FORCE_AUTHN_INNERAPI_DOMAIN`。 */
26
+ function getRuntimeHttpClient() {
27
+ runtimeClient ??= createClient({ adminInnerApi: false });
28
+ return runtimeClient;
29
+ }
30
+ /** 重建管理端与运行端客户端(测试/配置变更后调用)。 */
31
+ function resetHttpClient() {
32
+ adminClient = undefined;
33
+ runtimeClient = undefined;
34
+ }
35
+ /** 替换管理端 HttpClient 实例(用于测试 mock)。 */
36
+ function setHttpClient(custom) {
37
+ adminClient = custom;
38
+ }
39
+ /** 替换运行端 HttpClient 实例(用于测试 mock)。 */
40
+ function setRuntimeHttpClient(custom) {
41
+ runtimeClient = custom;
42
+ }
43
+ function createClient(opts) {
44
+ const config = {
45
+ timeout: 30_000,
46
+ platform: {
47
+ enabled: true,
48
+ adminInnerApi: opts.adminInnerApi,
49
+ },
50
+ security: {
51
+ strictMode: true,
52
+ },
53
+ };
54
+ const instance = new http_client_1.HttpClient(config);
55
+ instance.interceptors.request.use(applyCanaryHeader);
56
+ return instance;
57
+ }
58
+ /** 若 MIAODA_CANARY_HEADER 已设置,注入 x-tt-env 小流量头 */
59
+ function applyCanaryHeader(reqConfig) {
60
+ const canary = process.env.MIAODA_CANARY_HEADER;
61
+ if (!canary)
62
+ return reqConfig;
63
+ const headers = new Headers(reqConfig.headers);
64
+ headers.set("x-tt-env", canary);
65
+ reqConfig.headers = headers;
66
+ return reqConfig;
67
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setRuntimeHttpClient = exports.setHttpClient = exports.resetHttpClient = exports.getRuntimeHttpClient = exports.getHttpClient = exports.log = exports.debug = exports.isJsonMode = exports.emitError = exports.emit = exports.getLogId = exports.generateLogId = exports.initConfigFromOpts = exports.resetConfig = exports.setConfig = exports.getConfig = exports.HttpError = exports.AppError = void 0;
4
+ var error_1 = require("./error");
5
+ Object.defineProperty(exports, "AppError", { enumerable: true, get: function () { return error_1.AppError; } });
6
+ Object.defineProperty(exports, "HttpError", { enumerable: true, get: function () { return error_1.HttpError; } });
7
+ var config_1 = require("./config");
8
+ Object.defineProperty(exports, "getConfig", { enumerable: true, get: function () { return config_1.getConfig; } });
9
+ Object.defineProperty(exports, "setConfig", { enumerable: true, get: function () { return config_1.setConfig; } });
10
+ Object.defineProperty(exports, "resetConfig", { enumerable: true, get: function () { return config_1.resetConfig; } });
11
+ Object.defineProperty(exports, "initConfigFromOpts", { enumerable: true, get: function () { return config_1.initConfigFromOpts; } });
12
+ var log_id_1 = require("./log_id");
13
+ Object.defineProperty(exports, "generateLogId", { enumerable: true, get: function () { return log_id_1.generateLogId; } });
14
+ Object.defineProperty(exports, "getLogId", { enumerable: true, get: function () { return log_id_1.getLogId; } });
15
+ var output_1 = require("./output");
16
+ Object.defineProperty(exports, "emit", { enumerable: true, get: function () { return output_1.emit; } });
17
+ Object.defineProperty(exports, "emitError", { enumerable: true, get: function () { return output_1.emitError; } });
18
+ Object.defineProperty(exports, "isJsonMode", { enumerable: true, get: function () { return output_1.isJsonMode; } });
19
+ var logger_1 = require("./logger");
20
+ Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return logger_1.debug; } });
21
+ Object.defineProperty(exports, "log", { enumerable: true, get: function () { return logger_1.log; } });
22
+ var http_1 = require("./http");
23
+ Object.defineProperty(exports, "getHttpClient", { enumerable: true, get: function () { return http_1.getHttpClient; } });
24
+ Object.defineProperty(exports, "getRuntimeHttpClient", { enumerable: true, get: function () { return http_1.getRuntimeHttpClient; } });
25
+ Object.defineProperty(exports, "resetHttpClient", { enumerable: true, get: function () { return http_1.resetHttpClient; } });
26
+ Object.defineProperty(exports, "setHttpClient", { enumerable: true, get: function () { return http_1.setHttpClient; } });
27
+ Object.defineProperty(exports, "setRuntimeHttpClient", { enumerable: true, get: function () { return http_1.setRuntimeHttpClient; } });
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateLogId = generateLogId;
4
+ exports.getLogId = getLogId;
5
+ const node_crypto_1 = require("node:crypto");
6
+ let current;
7
+ function generateLogId() {
8
+ current = (0, node_crypto_1.randomUUID)();
9
+ return current;
10
+ }
11
+ function getLogId() {
12
+ return current ?? generateLogId();
13
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.debug = debug;
4
+ exports.log = log;
5
+ const config_1 = require("./config");
6
+ /** verbose 模式下输出 debug 日志到 stderr */
7
+ function debug(msg) {
8
+ if ((0, config_1.getConfig)().verbose) {
9
+ process.stderr.write(`[debug] ${msg}\n`);
10
+ }
11
+ }
12
+ /** 始终输出到 stderr 的进度日志 */
13
+ function log(prefix, msg) {
14
+ process.stderr.write(`[${prefix}] ${msg}\n`);
15
+ }
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isJsonMode = isJsonMode;
4
+ exports.emit = emit;
5
+ exports.emitError = emitError;
6
+ exports.emitOk = emitOk;
7
+ exports.emitPaged = emitPaged;
8
+ const config_1 = require("./config");
9
+ const error_1 = require("./error");
10
+ function isJsonMode() {
11
+ const cfg = (0, config_1.getConfig)();
12
+ return cfg.output === "json" || Boolean(cfg.json);
13
+ }
14
+ /**
15
+ * 输出数据
16
+ * - pretty 模式:格式化文本到 stdout
17
+ * - json 模式:裸 JSON 到 stdout
18
+ */
19
+ function emit(data) {
20
+ if (isJsonMode()) {
21
+ process.stdout.write(JSON.stringify(data) + "\n");
22
+ }
23
+ else {
24
+ if (typeof data === "string") {
25
+ process.stdout.write(data + "\n");
26
+ }
27
+ else {
28
+ process.stdout.write(JSON.stringify(data, null, 2) + "\n");
29
+ }
30
+ }
31
+ }
32
+ /**
33
+ * 输出错误(写入 stderr)
34
+ * - pretty 模式:Error: message\n hint: ...
35
+ * - json 模式:PRD 约定 envelope 为 `{error: {code, message, hint?}}`
36
+ */
37
+ function emitError(err) {
38
+ const info = toErrorInfo(err);
39
+ if (isJsonMode()) {
40
+ const errObj = {
41
+ code: info.code,
42
+ message: info.message,
43
+ };
44
+ if (info.next_actions && info.next_actions.length > 0) {
45
+ errObj.hint = info.next_actions.join(" ");
46
+ }
47
+ if (typeof info.statement_index === "number") {
48
+ errObj.statement_index = info.statement_index;
49
+ }
50
+ process.stderr.write(JSON.stringify({ error: errObj }) + "\n");
51
+ }
52
+ else {
53
+ process.stderr.write(`Error: ${info.message}\n`);
54
+ if (info.next_actions && info.next_actions.length > 0) {
55
+ process.stderr.write(` hint: ${info.next_actions.join(" ")}\n`);
56
+ }
57
+ }
58
+ }
59
+ /** 发送成功 JSON envelope(非分页):`{ data }`。 */
60
+ function emitOk(data) {
61
+ emit({ data });
62
+ }
63
+ /** 发送成功 JSON envelope(分页信封:`{ data, next_cursor, has_more }`)。 */
64
+ function emitPaged(items, nextCursor, hasMore) {
65
+ emit({ data: items, next_cursor: nextCursor, has_more: hasMore });
66
+ }
67
+ function toErrorInfo(err) {
68
+ if (err instanceof error_1.AppError)
69
+ return err.toJSON();
70
+ const message = err instanceof Error ? err.message : String(err);
71
+ return { code: "UNKNOWN", message, retryable: false, next_actions: [] };
72
+ }
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ /**
3
+ * CLI 渲染 / 解析工具:跨域共用的格式化、表格渲染、字符串解析。
4
+ *
5
+ * 按 AGENTS.md §2.2,跨域共享逻辑统一下沉到 `src/utils/*`。
6
+ * 这里聚合 db / file 域 handler 共用的纯函数:
7
+ * - 格式化:formatSize / formatTime
8
+ * - 表格渲染:renderAlignedTable / renderTsv / renderKeyValue
9
+ * - 终端探测:isStdoutTty
10
+ * - 字符串解析:parseDuration / parseSize
11
+ *
12
+ * JSON envelope 输出(emit / emitOk / emitPaged / emitError)见 ./output.ts。
13
+ */
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.formatSize = formatSize;
16
+ exports.formatTime = formatTime;
17
+ exports.visibleWidth = visibleWidth;
18
+ exports.renderAlignedTable = renderAlignedTable;
19
+ exports.renderTsv = renderTsv;
20
+ exports.renderKeyValue = renderKeyValue;
21
+ exports.isStdoutTty = isStdoutTty;
22
+ exports.parseDuration = parseDuration;
23
+ exports.parseSize = parseSize;
24
+ /** 将字节数格式化为人类可读(`24 KB` / `2.1 MB` / `1.5 GB`)。 */
25
+ function formatSize(bytes) {
26
+ if (!Number.isFinite(bytes) || bytes < 0)
27
+ return "—";
28
+ if (bytes >= 1 << 30)
29
+ return `${(bytes / (1 << 30)).toFixed(1)} GB`;
30
+ if (bytes >= 1 << 20)
31
+ return `${(bytes / (1 << 20)).toFixed(1)} MB`;
32
+ if (bytes >= 1 << 10)
33
+ return `${(bytes / (1 << 10)).toFixed(0)} KB`;
34
+ return `${String(bytes)} bytes`;
35
+ }
36
+ /** 将 ISO 时间转为 TTY 下更友好的相对时间;non-TTY 保持 ISO 原值。 */
37
+ function formatTime(iso, isTty) {
38
+ if (!iso)
39
+ return "—";
40
+ if (!isTty)
41
+ return iso;
42
+ const ts = Date.parse(iso);
43
+ if (Number.isNaN(ts))
44
+ return iso;
45
+ const diff = Date.now() - ts;
46
+ const minute = 60_000;
47
+ const hour = 60 * minute;
48
+ const day = 24 * hour;
49
+ if (diff < hour) {
50
+ const m = Math.max(1, Math.round(diff / minute));
51
+ return `${String(m)}m ago`;
52
+ }
53
+ if (diff < day) {
54
+ const h = Math.round(diff / hour);
55
+ return `${String(h)}h ago`;
56
+ }
57
+ if (diff < 7 * day) {
58
+ const d = Math.round(diff / day);
59
+ return `${String(d)}d ago`;
60
+ }
61
+ // 超过 7 天,用 YYYY-MM-DD
62
+ const date = new Date(ts);
63
+ const y = String(date.getFullYear());
64
+ const mo = String(date.getMonth() + 1).padStart(2, "0");
65
+ const da = String(date.getDate()).padStart(2, "0");
66
+ return `${y}-${mo}-${da}`;
67
+ }
68
+ /**
69
+ * SGR (Select Graphic Rendition) ANSI 转义匹配:`ESC[<digits;...>m`。
70
+ * 用于在列宽计算时剥离不显示的控制字符——否则带 ANSI 颜色的 cell 会按 .length
71
+ * 算进列宽(比如 dim "NULL" 实际占 4 字符但 .length=11),导致表格对齐错位。
72
+ */
73
+ const ANSI_SGR_RE = /\[[0-9;]*m/g;
74
+ /**
75
+ * 单字符的终端列宽:CJK 全角字符(汉字/假名/全角符号等)占 2 列,其它占 1 列。
76
+ * 对齐 Unicode East Asian Width 的 W/F 类,等价于 wcwidth 简化版。
77
+ * 不实现合字 / 零宽字符(ZWJ / 变体选择符)等极端情况,CLI 表格场景够用。
78
+ */
79
+ function charWidth(cp) {
80
+ if ((cp >= 0x1100 && cp <= 0x115F) || // Hangul Jamo
81
+ cp === 0x2329 || cp === 0x232A ||
82
+ (cp >= 0x2E80 && cp <= 0x303E) || // CJK Radicals / Punctuation
83
+ (cp >= 0x3041 && cp <= 0x33FF) || // Hiragana / Katakana / CJK Symbols
84
+ (cp >= 0x3400 && cp <= 0x4DBF) || // CJK Ext A
85
+ (cp >= 0x4E00 && cp <= 0x9FFF) || // CJK Unified
86
+ (cp >= 0xA000 && cp <= 0xA4CF) || // Yi
87
+ (cp >= 0xAC00 && cp <= 0xD7A3) || // Hangul Syllables
88
+ (cp >= 0xF900 && cp <= 0xFAFF) || // CJK Compat Ideographs
89
+ (cp >= 0xFE30 && cp <= 0xFE4F) || // CJK Compat Forms
90
+ (cp >= 0xFF00 && cp <= 0xFF60) || // Fullwidth Forms
91
+ (cp >= 0xFFE0 && cp <= 0xFFE6) ||
92
+ (cp >= 0x20000 && cp <= 0x2FFFD) || // CJK Ext B-F
93
+ (cp >= 0x30000 && cp <= 0x3FFFD) // CJK Ext G-H
94
+ ) {
95
+ return 2;
96
+ }
97
+ return 1;
98
+ }
99
+ /** cell 可见字符宽度:剥离 ANSI 转义后按终端列宽逐字符累加(CJK 算 2 列)。 */
100
+ function visibleWidth(s) {
101
+ const stripped = s.replace(ANSI_SGR_RE, "");
102
+ let w = 0;
103
+ for (const c of stripped) {
104
+ w += charWidth(c.codePointAt(0) ?? 0);
105
+ }
106
+ return w;
107
+ }
108
+ function padVisibleEnd(s, targetWidth) {
109
+ const w = visibleWidth(s);
110
+ return w >= targetWidth ? s : s + " ".repeat(targetWidth - w);
111
+ }
112
+ /** 渲染 TTY 对齐表格(多行,列宽按最长内容;ANSI 转义不计入列宽)。 */
113
+ function renderAlignedTable(headers, rows) {
114
+ const colWidths = headers.map((h, i) => {
115
+ let w = visibleWidth(h);
116
+ for (const row of rows) {
117
+ const cw = visibleWidth(row[i] || "");
118
+ if (cw > w)
119
+ w = cw;
120
+ }
121
+ return w;
122
+ });
123
+ const lines = [];
124
+ lines.push(headers.map((h, i) => padVisibleEnd(h, colWidths[i])).join(" ").trimEnd());
125
+ for (const row of rows) {
126
+ lines.push(row.map((cell, i) => padVisibleEnd(cell || "", colWidths[i])).join(" ").trimEnd());
127
+ }
128
+ return lines.join("\n");
129
+ }
130
+ /** 渲染 non-TTY tab 分隔(字段原值,ISO 时间)。 */
131
+ function renderTsv(headers, rows) {
132
+ const lines = [];
133
+ lines.push(headers.join("\t"));
134
+ for (const row of rows) {
135
+ lines.push(row.join("\t"));
136
+ }
137
+ return lines.join("\n");
138
+ }
139
+ /** 渲染 key-value 多行(用于 stat 等单条详情)。key 右对齐。 */
140
+ function renderKeyValue(pairs, isTty) {
141
+ if (pairs.length === 0)
142
+ return "";
143
+ if (!isTty) {
144
+ return pairs.map(([k, v]) => `${k}\t${v}`).join("\n");
145
+ }
146
+ const keyWidth = Math.max(...pairs.map(([k]) => k.length));
147
+ return pairs
148
+ .map(([k, v]) => `${k.padStart(keyWidth)}: ${v}`)
149
+ .join("\n");
150
+ }
151
+ /** 通用 isTTY 判定(stdout 是否交互终端)。Node 运行时 isTTY 为 true 或 undefined;TS 类型上 tty.WriteStream 定义为固定 true,绕开做运行时判断。 */
152
+ function isStdoutTty() {
153
+ const isTTY = process.stdout.isTTY;
154
+ return isTTY === true;
155
+ }
156
+ /** 解析时长字符串 `7d` / `24h` / `30m` → 秒。无后缀按秒。 */
157
+ function parseDuration(input) {
158
+ const m = /^(\d+)([smhd]?)$/.exec(input.trim());
159
+ if (!m) {
160
+ throw new Error(`Invalid duration: ${input}`);
161
+ }
162
+ const n = Number(m[1]);
163
+ const unit = m[2] || "s";
164
+ switch (unit) {
165
+ case "s": return n;
166
+ case "m": return n * 60;
167
+ case "h": return n * 3600;
168
+ case "d": return n * 86400;
169
+ default: return n;
170
+ }
171
+ }
172
+ /** 解析 size 字符串 `1MB` / `500KB` / `1GB` → 字节。 */
173
+ function parseSize(input) {
174
+ const m = /^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB)?$/i.exec(input.trim());
175
+ if (!m) {
176
+ throw new Error(`Invalid size: ${input}`);
177
+ }
178
+ const n = Number(m[1]);
179
+ const unit = (m[2] || "B").toUpperCase();
180
+ switch (unit) {
181
+ case "B": return Math.round(n);
182
+ case "KB": return Math.round(n * 1024);
183
+ case "MB": return Math.round(n * 1024 * 1024);
184
+ case "GB": return Math.round(n * 1024 * 1024 * 1024);
185
+ default: return Math.round(n);
186
+ }
187
+ }
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@lark-apaas/miaoda-cli",
3
+ "version": "0.1.0-alpha.08508f4",
4
+ "description": "Miaoda 平台命令行工具,面向 Agent 调用",
5
+ "type": "commonjs",
6
+ "bin": {
7
+ "miaoda": "bin/miaoda.js"
8
+ },
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "files": [
13
+ "bin",
14
+ "dist",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "keywords": [
19
+ "miaoda",
20
+ "cli",
21
+ "agent"
22
+ ],
23
+ "license": "MIT",
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "dependencies": {
28
+ "@lark-apaas/http-client": "^0.1.4",
29
+ "commander": "^13.1.0"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^22.15.3",
33
+ "@typescript-eslint/eslint-plugin": "^8.58.2",
34
+ "@typescript-eslint/parser": "^8.58.2",
35
+ "@vitest/coverage-v8": "^4.1.4",
36
+ "eslint": "^9.25.1",
37
+ "husky": "^9.1.7",
38
+ "tsc-alias": "^1.8.11",
39
+ "tsx": "^4.19.4",
40
+ "typescript": "^5.8.3",
41
+ "vitest": "^4.1.4",
42
+ "xml2js": "^0.6.2"
43
+ },
44
+ "scripts": {
45
+ "build": "bash scripts/build.sh",
46
+ "typecheck": "tsc --noEmit -p tsconfig.json",
47
+ "lint": "eslint src/ --max-warnings 0",
48
+ "test": "vitest run",
49
+ "test:watch": "vitest",
50
+ "test:integration": "vitest run --config vitest.integration.config.ts",
51
+ "dev": "node --import tsx src/main.ts"
52
+ }
53
+ }