@lark-apaas/miaoda-cli 0.1.0-alpha.097a394

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 (48) 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 +160 -0
  5. package/dist/api/db/client.js +108 -0
  6. package/dist/api/db/index.js +16 -0
  7. package/dist/api/db/parsers.js +157 -0
  8. package/dist/api/db/types.js +10 -0
  9. package/dist/api/file/api.js +420 -0
  10. package/dist/api/file/client.js +161 -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 +73 -0
  20. package/dist/cli/commands/file/index.js +67 -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 +53 -0
  24. package/dist/cli/handlers/db/data.js +167 -0
  25. package/dist/cli/handlers/db/index.js +11 -0
  26. package/dist/cli/handlers/db/schema.js +161 -0
  27. package/dist/cli/handlers/db/sql.js +173 -0
  28. package/dist/cli/handlers/file/cp.js +220 -0
  29. package/dist/cli/handlers/file/helpers.js +16 -0
  30. package/dist/cli/handlers/file/index.js +13 -0
  31. package/dist/cli/handlers/file/ls.js +109 -0
  32. package/dist/cli/handlers/file/rm.js +243 -0
  33. package/dist/cli/handlers/file/sign.js +96 -0
  34. package/dist/cli/handlers/file/stat.js +97 -0
  35. package/dist/cli/handlers/index.js +19 -0
  36. package/dist/cli/handlers/plugin/index.js +10 -0
  37. package/dist/cli/handlers/plugin/plugin-local.js +382 -0
  38. package/dist/cli/handlers/plugin/plugin.js +308 -0
  39. package/dist/cli/handlers/shared.js +139 -0
  40. package/dist/main.js +31 -0
  41. package/dist/utils/config.js +31 -0
  42. package/dist/utils/error.js +35 -0
  43. package/dist/utils/http.js +46 -0
  44. package/dist/utils/index.js +25 -0
  45. package/dist/utils/log_id.js +13 -0
  46. package/dist/utils/logger.js +15 -0
  47. package/dist/utils/output.js +59 -0
  48. package/package.json +53 -0
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatSize = formatSize;
4
+ exports.formatTime = formatTime;
5
+ exports.renderAlignedTable = renderAlignedTable;
6
+ exports.renderTsv = renderTsv;
7
+ exports.renderKeyValue = renderKeyValue;
8
+ exports.emitOk = emitOk;
9
+ exports.emitPaged = emitPaged;
10
+ exports.isStdoutTty = isStdoutTty;
11
+ exports.parseDuration = parseDuration;
12
+ exports.parseSize = parseSize;
13
+ const output_1 = require("../../utils/output");
14
+ /** 将字节数格式化为人类可读(`24 KB` / `2.1 MB` / `1.5 GB`)。 */
15
+ function formatSize(bytes) {
16
+ if (!Number.isFinite(bytes) || bytes < 0)
17
+ return "—";
18
+ if (bytes >= 1 << 30)
19
+ return `${(bytes / (1 << 30)).toFixed(1)} GB`;
20
+ if (bytes >= 1 << 20)
21
+ return `${(bytes / (1 << 20)).toFixed(1)} MB`;
22
+ if (bytes >= 1 << 10)
23
+ return `${(bytes / (1 << 10)).toFixed(0)} KB`;
24
+ return `${String(bytes)} bytes`;
25
+ }
26
+ /** 将 ISO 时间转为 TTY 下更友好的相对时间;non-TTY 保持 ISO 原值。 */
27
+ function formatTime(iso, isTty) {
28
+ if (!iso)
29
+ return "—";
30
+ if (!isTty)
31
+ return iso;
32
+ const ts = Date.parse(iso);
33
+ if (Number.isNaN(ts))
34
+ return iso;
35
+ const diff = Date.now() - ts;
36
+ const minute = 60_000;
37
+ const hour = 60 * minute;
38
+ const day = 24 * hour;
39
+ if (diff < hour) {
40
+ const m = Math.max(1, Math.round(diff / minute));
41
+ return `${String(m)}m ago`;
42
+ }
43
+ if (diff < day) {
44
+ const h = Math.round(diff / hour);
45
+ return `${String(h)}h ago`;
46
+ }
47
+ if (diff < 7 * day) {
48
+ const d = Math.round(diff / day);
49
+ return `${String(d)}d ago`;
50
+ }
51
+ // 超过 7 天,用 YYYY-MM-DD
52
+ const date = new Date(ts);
53
+ const y = String(date.getFullYear());
54
+ const mo = String(date.getMonth() + 1).padStart(2, "0");
55
+ const da = String(date.getDate()).padStart(2, "0");
56
+ return `${y}-${mo}-${da}`;
57
+ }
58
+ /** 渲染 TTY 对齐表格(多行,列宽按最长内容)。 */
59
+ function renderAlignedTable(headers, rows) {
60
+ const colWidths = headers.map((h, i) => {
61
+ let w = h.length;
62
+ for (const row of rows) {
63
+ const cell = row[i] || "";
64
+ if (cell.length > w)
65
+ w = cell.length;
66
+ }
67
+ return w;
68
+ });
69
+ const lines = [];
70
+ lines.push(headers.map((h, i) => h.padEnd(colWidths[i])).join(" ").trimEnd());
71
+ for (const row of rows) {
72
+ lines.push(row.map((cell, i) => (cell || "").padEnd(colWidths[i])).join(" ").trimEnd());
73
+ }
74
+ return lines.join("\n");
75
+ }
76
+ /** 渲染 non-TTY tab 分隔(字段原值,ISO 时间)。 */
77
+ function renderTsv(headers, rows) {
78
+ const lines = [];
79
+ lines.push(headers.join("\t"));
80
+ for (const row of rows) {
81
+ lines.push(row.join("\t"));
82
+ }
83
+ return lines.join("\n");
84
+ }
85
+ /** 渲染 key-value 多行(用于 stat 等单条详情)。key 右对齐。 */
86
+ function renderKeyValue(pairs, isTty) {
87
+ if (!isTty) {
88
+ return pairs.map(([k, v]) => `${k}\t${v}`).join("\n");
89
+ }
90
+ const keyWidth = Math.max(...pairs.map(([k]) => k.length));
91
+ return pairs
92
+ .map(([k, v]) => `${k.padStart(keyWidth)}: ${v}`)
93
+ .join("\n");
94
+ }
95
+ /** 发送成功 JSON envelope(非分页)。 */
96
+ function emitOk(data) {
97
+ (0, output_1.emit)({ data });
98
+ }
99
+ /** 发送成功 JSON envelope(分页信封:data + next_cursor + has_more)。 */
100
+ function emitPaged(items, nextCursor, hasMore) {
101
+ (0, output_1.emit)({ data: items, next_cursor: nextCursor, has_more: hasMore });
102
+ }
103
+ /** 通用 isTTY 判定(stdout 是否交互终端)。Node 运行时 isTTY 为 true 或 undefined;TS 类型上 tty.WriteStream 定义为固定 true,绕开做运行时判断。 */
104
+ function isStdoutTty() {
105
+ const isTTY = process.stdout.isTTY;
106
+ return isTTY === true;
107
+ }
108
+ /** 解析时长字符串 `7d` / `24h` / `30m` → 秒。无后缀按秒。 */
109
+ function parseDuration(input) {
110
+ const m = /^(\d+)([smhd]?)$/.exec(input.trim());
111
+ if (!m) {
112
+ throw new Error(`Invalid duration: ${input}`);
113
+ }
114
+ const n = Number(m[1]);
115
+ const unit = m[2] || "s";
116
+ switch (unit) {
117
+ case "s": return n;
118
+ case "m": return n * 60;
119
+ case "h": return n * 3600;
120
+ case "d": return n * 86400;
121
+ default: return n;
122
+ }
123
+ }
124
+ /** 解析 size 字符串 `1MB` / `500KB` / `1GB` → 字节。 */
125
+ function parseSize(input) {
126
+ const m = /^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB)?$/i.exec(input.trim());
127
+ if (!m) {
128
+ throw new Error(`Invalid size: ${input}`);
129
+ }
130
+ const n = Number(m[1]);
131
+ const unit = (m[2] || "B").toUpperCase();
132
+ switch (unit) {
133
+ case "B": return Math.round(n);
134
+ case "KB": return Math.round(n * 1024);
135
+ case "MB": return Math.round(n * 1024 * 1024);
136
+ case "GB": return Math.round(n * 1024 * 1024 * 1024);
137
+ default: return Math.round(n);
138
+ }
139
+ }
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,35 @@
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
+ constructor(code, message, opts) {
9
+ super(message);
10
+ this.name = "AppError";
11
+ this.code = code;
12
+ this.retryable = opts?.retryable ?? false;
13
+ this.next_actions = opts?.next_actions ?? [];
14
+ }
15
+ toJSON() {
16
+ return {
17
+ code: this.code,
18
+ message: this.message,
19
+ retryable: this.retryable,
20
+ next_actions: this.next_actions,
21
+ };
22
+ }
23
+ }
24
+ exports.AppError = AppError;
25
+ class HttpError extends AppError {
26
+ status;
27
+ url;
28
+ constructor(status, url, message, opts) {
29
+ super(`HTTP_${String(status)}`, message, opts ?? { retryable: status >= 500 || status === 429 });
30
+ this.name = "HttpError";
31
+ this.status = status;
32
+ this.url = url;
33
+ }
34
+ }
35
+ exports.HttpError = HttpError;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getHttpClient = getHttpClient;
4
+ exports.resetHttpClient = resetHttpClient;
5
+ exports.setHttpClient = setHttpClient;
6
+ exports.applyCanaryHeader = applyCanaryHeader;
7
+ const http_client_1 = require("@lark-apaas/http-client");
8
+ let client;
9
+ /** 获取单例 HttpClient,首次调用时初始化 */
10
+ function getHttpClient() {
11
+ client ??= createClient();
12
+ return client;
13
+ }
14
+ /** 重建客户端 */
15
+ function resetHttpClient() {
16
+ client = undefined;
17
+ }
18
+ /** 替换为自定义 HttpClient 实例(用于测试 mock) */
19
+ function setHttpClient(custom) {
20
+ client = custom;
21
+ }
22
+ function createClient() {
23
+ const config = {
24
+ timeout: 30_000,
25
+ platform: {
26
+ enabled: true,
27
+ tokenProvider: { type: "file" },
28
+ },
29
+ security: {
30
+ strictMode: true,
31
+ },
32
+ };
33
+ const instance = new http_client_1.HttpClient(config);
34
+ instance.interceptors.request.use(applyCanaryHeader);
35
+ return instance;
36
+ }
37
+ /** 若 MIAODA_CANARY_HEADER 已设置,注入 x-tt-env 小流量头 */
38
+ function applyCanaryHeader(reqConfig) {
39
+ const canary = process.env.MIAODA_CANARY_HEADER;
40
+ if (!canary)
41
+ return reqConfig;
42
+ const headers = new Headers(reqConfig.headers);
43
+ headers.set("x-tt-env", canary);
44
+ reqConfig.headers = headers;
45
+ return reqConfig;
46
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setHttpClient = exports.resetHttpClient = 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, "resetHttpClient", { enumerable: true, get: function () { return http_1.resetHttpClient; } });
25
+ Object.defineProperty(exports, "setHttpClient", { enumerable: true, get: function () { return http_1.setHttpClient; } });
@@ -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,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isJsonMode = isJsonMode;
4
+ exports.emit = emit;
5
+ exports.emitError = emitError;
6
+ const config_1 = require("./config");
7
+ const error_1 = require("./error");
8
+ function isJsonMode() {
9
+ const cfg = (0, config_1.getConfig)();
10
+ return cfg.output === "json" || Boolean(cfg.json);
11
+ }
12
+ /**
13
+ * 输出数据
14
+ * - pretty 模式:格式化文本到 stdout
15
+ * - json 模式:裸 JSON 到 stdout
16
+ */
17
+ function emit(data) {
18
+ if (isJsonMode()) {
19
+ process.stdout.write(JSON.stringify(data) + "\n");
20
+ }
21
+ else {
22
+ if (typeof data === "string") {
23
+ process.stdout.write(data + "\n");
24
+ }
25
+ else {
26
+ process.stdout.write(JSON.stringify(data, null, 2) + "\n");
27
+ }
28
+ }
29
+ }
30
+ /**
31
+ * 输出错误(写入 stderr)
32
+ * - pretty 模式:Error: message\n hint: ...
33
+ * - json 模式:{"error_code": "...", "message": "...", "hint": "..."}
34
+ */
35
+ function emitError(err) {
36
+ const info = toErrorInfo(err);
37
+ if (isJsonMode()) {
38
+ const jsonErr = {
39
+ error_code: info.code,
40
+ message: info.message,
41
+ };
42
+ if (info.next_actions && info.next_actions.length > 0) {
43
+ jsonErr.hint = info.next_actions.join(" ");
44
+ }
45
+ process.stderr.write(JSON.stringify(jsonErr) + "\n");
46
+ }
47
+ else {
48
+ process.stderr.write(`Error: ${info.message}\n`);
49
+ if (info.next_actions && info.next_actions.length > 0) {
50
+ process.stderr.write(` hint: ${info.next_actions.join(" ")}\n`);
51
+ }
52
+ }
53
+ }
54
+ function toErrorInfo(err) {
55
+ if (err instanceof error_1.AppError)
56
+ return err.toJSON();
57
+ const message = err instanceof Error ? err.message : String(err);
58
+ return { code: "UNKNOWN", message, retryable: false, next_actions: [] };
59
+ }
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@lark-apaas/miaoda-cli",
3
+ "version": "0.1.0-alpha.097a394",
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.3",
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
+ }