@lark-apaas/miaoda-cli 0.1.1 → 0.1.2

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.
@@ -1,4 +1,14 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatSize = formatSize;
4
+ exports.formatTime = formatTime;
5
+ exports.visibleWidth = visibleWidth;
6
+ exports.renderAlignedTable = renderAlignedTable;
7
+ exports.renderTsv = renderTsv;
8
+ exports.renderKeyValue = renderKeyValue;
9
+ exports.isStdoutTty = isStdoutTty;
10
+ exports.parseDuration = parseDuration;
11
+ exports.parseSize = parseSize;
2
12
  /**
3
13
  * CLI 渲染 / 解析工具:跨域共用的格式化、表格渲染、字符串解析。
4
14
  *
@@ -9,18 +19,12 @@
9
19
  * - 终端探测:isStdoutTty
10
20
  * - 字符串解析:parseDuration / parseSize
11
21
  *
22
+ * 彩色高亮的语义层封装见 ./colors.ts。表头 / key 标签等结构性元素由本文件
23
+ * 主动调用 colors.c 染色;业务文案的染色(成功/失败 prefix 等)由 handler 自治。
24
+ *
12
25
  * JSON envelope 输出(emit / emitOk / emitPaged / emitError)见 ./output.ts。
13
26
  */
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;
27
+ const colors_1 = require("./colors");
24
28
  /** 将字节数格式化为人类可读(`24 KB` / `2.1 MB` / `1.5 GB`)。 */
25
29
  function formatSize(bytes) {
26
30
  if (!Number.isFinite(bytes) || bytes < 0)
@@ -77,20 +81,21 @@ const ANSI_SGR_RE = /\[[0-9;]*m/g;
77
81
  * 不实现合字 / 零宽字符(ZWJ / 变体选择符)等极端情况,CLI 表格场景够用。
78
82
  */
79
83
  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
84
+ if ((cp >= 0x1100 && cp <= 0x115f) || // Hangul Jamo
85
+ cp === 0x2329 ||
86
+ cp === 0x232a ||
87
+ (cp >= 0x2e80 && cp <= 0x303e) || // CJK Radicals / Punctuation
88
+ (cp >= 0x3041 && cp <= 0x33ff) || // Hiragana / Katakana / CJK Symbols
89
+ (cp >= 0x3400 && cp <= 0x4dbf) || // CJK Ext A
90
+ (cp >= 0x4e00 && cp <= 0x9fff) || // CJK Unified
91
+ (cp >= 0xa000 && cp <= 0xa4cf) || // Yi
92
+ (cp >= 0xac00 && cp <= 0xd7a3) || // Hangul Syllables
93
+ (cp >= 0xf900 && cp <= 0xfaff) || // CJK Compat Ideographs
94
+ (cp >= 0xfe30 && cp <= 0xfe4f) || // CJK Compat Forms
95
+ (cp >= 0xff00 && cp <= 0xff60) || // Fullwidth Forms
96
+ (cp >= 0xffe0 && cp <= 0xffe6) ||
97
+ (cp >= 0x20000 && cp <= 0x2fffd) || // CJK Ext B-F
98
+ (cp >= 0x30000 && cp <= 0x3fffd) // CJK Ext G-H
94
99
  ) {
95
100
  return 2;
96
101
  }
@@ -109,7 +114,8 @@ function padVisibleEnd(s, targetWidth) {
109
114
  const w = visibleWidth(s);
110
115
  return w >= targetWidth ? s : s + " ".repeat(targetWidth - w);
111
116
  }
112
- /** 渲染 TTY 对齐表格(多行,列宽按最长内容;ANSI 转义不计入列宽)。 */
117
+ /** 渲染 TTY 对齐表格(多行,列宽按最长内容;ANSI 转义不计入列宽)。
118
+ * 表头按 spec 用 bold + cyan 染色;ANSI 序列由 visibleWidth 剥离不影响列宽。 */
113
119
  function renderAlignedTable(headers, rows) {
114
120
  const colWidths = headers.map((h, i) => {
115
121
  let w = visibleWidth(h);
@@ -121,9 +127,15 @@ function renderAlignedTable(headers, rows) {
121
127
  return w;
122
128
  });
123
129
  const lines = [];
124
- lines.push(headers.map((h, i) => padVisibleEnd(h, colWidths[i])).join(" ").trimEnd());
130
+ lines.push(headers
131
+ .map((h, i) => colors_1.c.header(padVisibleEnd(h, colWidths[i])))
132
+ .join(" ")
133
+ .trimEnd());
125
134
  for (const row of rows) {
126
- lines.push(row.map((cell, i) => padVisibleEnd(cell || "", colWidths[i])).join(" ").trimEnd());
135
+ lines.push(row
136
+ .map((cell, i) => padVisibleEnd(cell || "", colWidths[i]))
137
+ .join(" ")
138
+ .trimEnd());
127
139
  }
128
140
  return lines.join("\n");
129
141
  }
@@ -136,7 +148,7 @@ function renderTsv(headers, rows) {
136
148
  }
137
149
  return lines.join("\n");
138
150
  }
139
- /** 渲染 key-value 多行(用于 stat 等单条详情)。key 右对齐。 */
151
+ /** 渲染 key-value 多行(用于 stat 等单条详情)。key 右对齐 + bold cyan 染色。 */
140
152
  function renderKeyValue(pairs, isTty) {
141
153
  if (pairs.length === 0)
142
154
  return "";
@@ -144,9 +156,7 @@ function renderKeyValue(pairs, isTty) {
144
156
  return pairs.map(([k, v]) => `${k}\t${v}`).join("\n");
145
157
  }
146
158
  const keyWidth = Math.max(...pairs.map(([k]) => k.length));
147
- return pairs
148
- .map(([k, v]) => `${k.padStart(keyWidth)}: ${v}`)
149
- .join("\n");
159
+ return pairs.map(([k, v]) => `${colors_1.c.header(k.padStart(keyWidth))}: ${v}`).join("\n");
150
160
  }
151
161
  /** 通用 isTTY 判定(stdout 是否交互终端)。Node 运行时 isTTY 为 true 或 undefined;TS 类型上 tty.WriteStream 定义为固定 true,绕开做运行时判断。 */
152
162
  function isStdoutTty() {
@@ -162,11 +172,16 @@ function parseDuration(input) {
162
172
  const n = Number(m[1]);
163
173
  const unit = m[2] || "s";
164
174
  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;
175
+ case "s":
176
+ return n;
177
+ case "m":
178
+ return n * 60;
179
+ case "h":
180
+ return n * 3600;
181
+ case "d":
182
+ return n * 86400;
183
+ default:
184
+ return n;
170
185
  }
171
186
  }
172
187
  /** 解析 size 字符串 `1MB` / `500KB` / `1GB` → 字节。 */
@@ -178,10 +193,15 @@ function parseSize(input) {
178
193
  const n = Number(m[1]);
179
194
  const unit = (m[2] || "B").toUpperCase();
180
195
  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);
196
+ case "B":
197
+ return Math.round(n);
198
+ case "KB":
199
+ return Math.round(n * 1024);
200
+ case "MB":
201
+ return Math.round(n * 1024 * 1024);
202
+ case "GB":
203
+ return Math.round(n * 1024 * 1024 * 1024);
204
+ default:
205
+ return Math.round(n);
186
206
  }
187
207
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/miaoda-cli",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Miaoda 平台命令行工具,面向 Agent 调用",
5
5
  "type": "commonjs",
6
6
  "bin": {
@@ -26,7 +26,8 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@lark-apaas/http-client": "^0.1.5",
29
- "commander": "^13.1.0"
29
+ "commander": "^13.1.0",
30
+ "picocolors": "^1.1.1"
30
31
  },
31
32
  "devDependencies": {
32
33
  "@types/node": "^22.15.3",
@@ -34,7 +35,12 @@
34
35
  "@typescript-eslint/parser": "^8.58.2",
35
36
  "@vitest/coverage-v8": "^4.1.4",
36
37
  "eslint": "^9.25.1",
38
+ "eslint-config-prettier": "^10.1.8",
39
+ "eslint-import-resolver-typescript": "^4.4.4",
40
+ "eslint-plugin-boundaries": "^6.0.2",
41
+ "eslint-plugin-import": "^2.32.0",
37
42
  "husky": "^9.1.7",
43
+ "prettier": "^3.8.3",
38
44
  "tsc-alias": "^1.8.11",
39
45
  "tsx": "^4.19.4",
40
46
  "typescript": "^5.8.3",
@@ -45,6 +51,8 @@
45
51
  "build": "bash scripts/build.sh",
46
52
  "typecheck": "tsc --noEmit -p tsconfig.json",
47
53
  "lint": "eslint src/ --max-warnings 0",
54
+ "format": "prettier --write src/",
55
+ "format:check": "prettier --check src/",
48
56
  "test": "vitest run",
49
57
  "test:watch": "vitest",
50
58
  "test:integration": "vitest run --config vitest.integration.config.ts",