@lark-apaas/miaoda-cli 0.1.1 → 0.1.2-alpha.2e37617

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 (37) hide show
  1. package/dist/api/db/api.js +333 -12
  2. package/dist/api/db/client.js +76 -29
  3. package/dist/api/db/index.js +9 -1
  4. package/dist/api/db/parsers.js +33 -20
  5. package/dist/api/db/sql-keywords.js +123 -0
  6. package/dist/api/file/api.js +93 -24
  7. package/dist/api/file/client.js +1 -5
  8. package/dist/api/file/index.js +2 -1
  9. package/dist/api/file/parsers.js +1 -5
  10. package/dist/api/plugin/api.js +8 -3
  11. package/dist/cli/commands/db/index.js +138 -0
  12. package/dist/cli/commands/file/index.js +7 -0
  13. package/dist/cli/commands/plugin/index.js +18 -6
  14. package/dist/cli/commands/shared.js +1 -3
  15. package/dist/cli/handlers/db/audit.js +285 -0
  16. package/dist/cli/handlers/db/changelog.js +117 -0
  17. package/dist/cli/handlers/db/data.js +23 -3
  18. package/dist/cli/handlers/db/index.js +17 -1
  19. package/dist/cli/handlers/db/migration.js +147 -0
  20. package/dist/cli/handlers/db/quota.js +68 -0
  21. package/dist/cli/handlers/db/recovery.js +188 -0
  22. package/dist/cli/handlers/db/schema.js +22 -8
  23. package/dist/cli/handlers/db/sql.js +304 -16
  24. package/dist/cli/handlers/file/cp.js +39 -17
  25. package/dist/cli/handlers/file/index.js +3 -1
  26. package/dist/cli/handlers/file/ls.js +1 -3
  27. package/dist/cli/handlers/file/quota.js +66 -0
  28. package/dist/cli/handlers/file/rm.js +4 -3
  29. package/dist/cli/handlers/plugin/plugin-local.js +23 -9
  30. package/dist/cli/handlers/plugin/plugin.js +21 -7
  31. package/dist/cli/help.js +5 -2
  32. package/dist/utils/colors.js +98 -0
  33. package/dist/utils/error.js +11 -0
  34. package/dist/utils/fuzzy-match.js +91 -0
  35. package/dist/utils/output.js +81 -5
  36. package/dist/utils/render.js +61 -41
  37. package/package.json +10 -2
@@ -44,7 +44,10 @@ const config_1 = require("../../../utils/config");
44
44
  const logger_1 = require("../../../utils/logger");
45
45
  const shared_1 = require("../../../cli/commands/shared");
46
46
  const render_1 = require("../../../utils/render");
47
+ const colors_1 = require("../../../utils/colors");
48
+ const fuzzy_match_1 = require("../../../utils/fuzzy-match");
47
49
  const index_1 = require("../../../api/db/index");
50
+ const sql_keywords_1 = require("../../../api/db/sql-keywords");
48
51
  const node_child_process_1 = require("node:child_process");
49
52
  const node_fs_1 = require("node:fs");
50
53
  const node_path_1 = __importDefault(require("node:path"));
@@ -64,7 +67,20 @@ async function handleDbSql(query, opts) {
64
67
  if (!sql.trim()) {
65
68
  throw new error_1.AppError("ARGS_INVALID", "Empty SQL (no inline query and stdin is empty)");
66
69
  }
67
- const results = await api.db.execSql({ appId, sql, dbBranch: opts.env });
70
+ let results;
71
+ try {
72
+ results = await api.db.execSql({ appId, sql, dbBranch: opts.env });
73
+ }
74
+ catch (err) {
75
+ // 错误抛出前富化 next_actions:识别"语法错误"/"表不存在"/"列不存在"
76
+ // 三类常见 PG 错误,做 fuzzy match 给 did-you-mean 提示。
77
+ // 富化失败任何环节都静默吞掉,不破坏原始错误信息。
78
+ await enrichSqlError(err, { appId, env: opts.env, sql });
79
+ // PRD 多语句失败:把服务端给的 partial_results 转成 `completed`,推断 `rolled_back`,
80
+ // 并把 pretty 模式的 `Statement N: ✓ ... / Statement K: ✗ ...` 立即打到 stderr。
81
+ enrichMultiStatementError(err, sql);
82
+ throw err;
83
+ }
68
84
  if (results.length === 0) {
69
85
  // 后端未返回任何结果,通常不会发生
70
86
  if ((0, output_1.isJsonMode)()) {
@@ -225,19 +241,19 @@ function renderSingle(raw) {
225
241
  return;
226
242
  }
227
243
  const cols = collectColumns(parsed.rows);
228
- const rows = parsed.rows.map((r) => cols.map((c) => formatCell(r[c], tty)));
244
+ const rows = parsed.rows.map((r) => cols.map((col) => formatCell(r[col], tty)));
229
245
  (0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(cols, rows) : (0, render_1.renderTsv)(cols, rows));
230
246
  return;
231
247
  }
232
248
  if (parsed.kind === "dml") {
233
249
  const verb = dmlVerb(parsed.sqlType);
234
250
  (0, output_1.emit)(tty
235
- ? `✓ ${String(parsed.affectedRows)} row${parsed.affectedRows === 1 ? "" : "s"} ${verb}`
251
+ ? colors_1.c.success(`✓ ${String(parsed.affectedRows)} row${parsed.affectedRows === 1 ? "" : "s"} ${verb}`)
236
252
  : `OK ${String(parsed.affectedRows)} rows ${verb}`);
237
253
  return;
238
254
  }
239
255
  // DDL
240
- (0, output_1.emit)(tty ? "✓ DDL executed" : "OK DDL executed");
256
+ (0, output_1.emit)(tty ? colors_1.c.success("✓ DDL executed") : "OK DDL executed");
241
257
  }
242
258
  function toJson(parsed) {
243
259
  if (parsed.kind === "select") {
@@ -293,7 +309,10 @@ function parseJsonFields() {
293
309
  const v = (0, config_1.getConfig)().json;
294
310
  if (typeof v !== "string" || v === "")
295
311
  return null;
296
- return v.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
312
+ return v
313
+ .split(",")
314
+ .map((s) => s.trim())
315
+ .filter((s) => s.length > 0);
297
316
  }
298
317
  /** 多语句 pretty:逐条 `Statement N: ...` + 末尾 `✓ N statements executed`。 */
299
318
  function renderMultiPretty(results) {
@@ -307,7 +326,7 @@ function renderMultiPretty(results) {
307
326
  lines.push(`Statement ${String(idx)}: SELECT (${String(n)} row${n === 1 ? "" : "s"})`);
308
327
  if (n > 0) {
309
328
  const cols = collectColumns(parsed.rows);
310
- const tbl = parsed.rows.map((r) => cols.map((c) => formatCell(r[c], tty)));
329
+ const tbl = parsed.rows.map((r) => cols.map((col) => formatCell(r[col], tty)));
311
330
  lines.push(tty ? (0, render_1.renderAlignedTable)(cols, tbl) : (0, render_1.renderTsv)(cols, tbl));
312
331
  }
313
332
  // 块间空行(最后一条不留)
@@ -318,25 +337,31 @@ function renderMultiPretty(results) {
318
337
  if (parsed.kind === "dml") {
319
338
  const verb = dmlVerb(parsed.sqlType);
320
339
  const n = parsed.affectedRows;
321
- lines.push(`Statement ${String(idx)}: ${tty ? "✓ " : ""}${String(n)} row${n === 1 ? "" : "s"} ${verb}`);
340
+ const body = `${String(n)} row${n === 1 ? "" : "s"} ${verb}`;
341
+ lines.push(`Statement ${String(idx)}: ${tty ? colors_1.c.success("✓ " + body) : body}`);
322
342
  continue;
323
343
  }
324
344
  // DDL
325
- lines.push(`Statement ${String(idx)}: ${tty ? "✓ " : ""}DDL executed`);
345
+ lines.push(`Statement ${String(idx)}: ${tty ? colors_1.c.success("✓ DDL executed") : "DDL executed"}`);
326
346
  }
327
347
  // 汇总行:所有 statement 都跑完了
328
348
  lines.push(tty
329
- ? `✓ ${String(results.length)} statements executed`
349
+ ? colors_1.c.success(`✓ ${String(results.length)} statements executed`)
330
350
  : `OK ${String(results.length)} statements executed`);
331
351
  (0, output_1.emit)(lines.join("\n"));
332
352
  }
333
353
  function dmlVerb(type) {
334
354
  switch (type) {
335
- case "INSERT": return "inserted";
336
- case "UPDATE": return "updated";
337
- case "DELETE": return "deleted";
338
- case "MERGE": return "merged";
339
- case "DML": return "affected"; // 未识别子类的兜底
355
+ case "INSERT":
356
+ return "inserted";
357
+ case "UPDATE":
358
+ return "updated";
359
+ case "DELETE":
360
+ return "deleted";
361
+ case "MERGE":
362
+ return "merged";
363
+ case "DML":
364
+ return "affected"; // 未识别子类的兜底
340
365
  }
341
366
  }
342
367
  /** 从第一行收集列顺序;缺失列保留空白(兼容稀疏行)。 */
@@ -356,12 +381,275 @@ function collectColumns(rows) {
356
381
  */
357
382
  function formatCell(v, tty) {
358
383
  if (v === null || v === undefined) {
359
- return tty ? "\u001b[2;90mNULL\u001b[0m" : "NULL";
384
+ return tty ? colors_1.c.muted("NULL") : "NULL";
360
385
  }
361
- if (typeof v === "string")
386
+ if (typeof v === "string") {
387
+ // TTY 下检测到 ISO 8601 时间字符串 → 转成相对时间("3h ago" / "2d ago" /
388
+ // "2026-03-15"),方便 _created_at / _updated_at 等列直观可读
389
+ if (tty && ISO_TIMESTAMP_RE.test(v) && !Number.isNaN(Date.parse(v))) {
390
+ return (0, render_1.formatTime)(v, tty);
391
+ }
362
392
  return v;
393
+ }
363
394
  if (typeof v === "number" || typeof v === "boolean")
364
395
  return String(v);
365
396
  // object / array → JSON
366
397
  return JSON.stringify(v);
367
398
  }
399
+ /**
400
+ * 匹配 PG / ISO 8601 形态的日期时间字符串:
401
+ * 2026-04-29
402
+ * 2026-04-29 19:07:43 (PG 默认 timestamp 形态)
403
+ * 2026-04-29T19:07:43
404
+ * 2026-04-29T19:07:43.882+08:00
405
+ * 2026-04-29T19:07:43.882Z
406
+ * 时间部分 / 毫秒 / 时区可选。
407
+ */
408
+ const ISO_TIMESTAMP_RE = /^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
409
+ // ──────────────────────────────────────────────────────────────────────
410
+ // db sql 错误富化:识别 PG 常见报错形态 → fuzzy match → 加 did-you-mean hint
411
+ // ──────────────────────────────────────────────────────────────────────
412
+ const SYNTAX_RE = /syntax error at or near "([^"]+)"/i;
413
+ const RELATION_RE = /relation "([^"]+)" does not exist/i;
414
+ const COLUMN_RE = /column "([^"]+)"(?: of relation "([^"]+)")? does not exist/i;
415
+ /**
416
+ * 在错误抛出前富化 next_actions(最多 1 条 hint),按 PG 报错形态分发:
417
+ *
418
+ * 表 / 列不存在路径:
419
+ * - `column "X" of relation "Y" does not exist` → 在 Y 表的列里 fuzzy
420
+ * - `column "X" does not exist`(未指定表) → 跨表所有列合集 fuzzy
421
+ * - `relation "X" does not exist` → 在所有表名里 fuzzy
422
+ *
423
+ * 语法错误路径(`syntax error at or near "X"`)三条互斥子路径:
424
+ * a. X 字面就是保留字(用户拿 keyword 当标识符,如 `FROM order`,
425
+ * 但 `order` 是 PG 保留字)→ 当作"想引用的表名",远程 schema fuzzy。
426
+ * **不要建议 keyword 自己**,那只是把用户输入大写返还,毫无价值。
427
+ * b. X 接近某 keyword(典型拼写错误,如 SELCT)→ suggest keyword
428
+ * c. X 是数字 / 标点(如 `LIMITT 1` 报错点到 `1`)→ fallback 扫整段
429
+ * SQL 找看似拼错的 keyword token
430
+ *
431
+ * 任一环节失败(pattern 不匹配 / 远程拉表失败 / 没 fuzzy 候选)都静默吞掉,
432
+ * 不破坏原始错误信息——原则:"只在我们高置信能帮上忙时才插嘴"。
433
+ */
434
+ async function enrichSqlError(err, ctx) {
435
+ if (!(err instanceof error_1.AppError))
436
+ return;
437
+ const msg = err.message;
438
+ // ── 表 / 列不存在路径 ──
439
+ // 注意:列报错 'column "X" of relation "Y"' 也含 'relation "Y"' 子串,
440
+ // 会同时命中 RELATION_RE。先 colMatch(更精确),命中后不再走 relation 分支。
441
+ const colMatch = COLUMN_RE.exec(msg);
442
+ const relMatch = colMatch ? null : RELATION_RE.exec(msg);
443
+ if (relMatch || colMatch) {
444
+ const tableMap = await loadTableMap(ctx);
445
+ if (!tableMap)
446
+ return;
447
+ if (relMatch) {
448
+ enrichRelationNotExist(err, relMatch[1], tableMap);
449
+ }
450
+ else if (colMatch) {
451
+ enrichColumnNotExist(err, colMatch, tableMap);
452
+ }
453
+ return;
454
+ }
455
+ // ── 语法错误路径 ──
456
+ const kwMatch = SYNTAX_RE.exec(msg);
457
+ if (!kwMatch)
458
+ return;
459
+ const token = kwMatch[1];
460
+ const upper = token.toUpperCase();
461
+ // 子路径 a:token 字面就是 keyword → 用户拿保留字当标识符
462
+ if (sql_keywords_1.SQL_KEYWORDS.includes(upper)) {
463
+ const tableMap = await loadTableMap(ctx);
464
+ if (!tableMap)
465
+ return;
466
+ const guess = (0, fuzzy_match_1.suggest)(token, Object.keys(tableMap));
467
+ if (guess) {
468
+ err.next_actions.push(`Did you mean table '${guess}'? '${token}' is a reserved keyword; ` +
469
+ `quote it as "${token}" if you really mean a table named '${token}'.`);
470
+ }
471
+ return;
472
+ }
473
+ // 子路径 b:token 接近 keyword(拼写错误)
474
+ const guess = (0, fuzzy_match_1.suggest)(upper, sql_keywords_1.SQL_KEYWORDS);
475
+ if (guess && guess !== upper) {
476
+ err.next_actions.push(`Check SQL keyword spelling. Did you mean "${guess}"?`);
477
+ return;
478
+ }
479
+ // 子路径 c:PG 错误位置点到了下一个 token(如 `LIMITT 1` → 报错指向 "1"),
480
+ // 扫整段 SQL 找看似拼错的 keyword
481
+ const typo = findKeywordTypo(ctx.sql);
482
+ if (typo) {
483
+ err.next_actions.push(`Check SQL keyword spelling. '${typo.input}' looks like '${typo.suggest}'.`);
484
+ }
485
+ }
486
+ /** 远程拉一次 schema 并扁平化为 `tableName → fieldName[]`。失败返 null(调用方静默不加 hint)。 */
487
+ async function loadTableMap(ctx) {
488
+ try {
489
+ const resp = await api.db.getSchema({
490
+ appId: ctx.appId,
491
+ format: "schema",
492
+ dbBranch: ctx.env,
493
+ });
494
+ return extractTableFieldMap(resp.schema);
495
+ }
496
+ catch {
497
+ return null;
498
+ }
499
+ }
500
+ function enrichRelationNotExist(err, relation, tableMap) {
501
+ const guess = (0, fuzzy_match_1.suggest)(relation, Object.keys(tableMap));
502
+ err.next_actions.push(guess
503
+ ? `Did you mean '${guess}'? Run \`miaoda db schema list\` to see all tables.`
504
+ : "Run `miaoda db schema list` to see all tables.");
505
+ }
506
+ function enrichColumnNotExist(err, colMatch, tableMap) {
507
+ const colName = colMatch[1];
508
+ // TS RegExpMatchArray 数字索引类型 = string,但 optional capture group `(...)?`
509
+ // 没匹配时运行时返 undefined;as cast 把这个事实告诉类型系统。
510
+ const relation = colMatch[2];
511
+ const relationCols = relation !== undefined ? tableMap[relation] : undefined;
512
+ if (relation !== undefined && relationCols !== undefined) {
513
+ // 指定了表 → 在该表的列里找
514
+ const guess = (0, fuzzy_match_1.suggest)(colName, relationCols);
515
+ err.next_actions.push(guess
516
+ ? `Did you mean column '${guess}' in table '${relation}'?`
517
+ : `Run \`miaoda db schema get ${relation}\` to see all columns.`);
518
+ return;
519
+ }
520
+ // 没指定表(CTE / 别名 / 子查询 / 单表 SELECT 但 PG 报错没带 relation 等场景)
521
+ // → 跨表所有列合集 fuzzy;没命中也给一句通用 hint,让用户知道下一步去哪查。
522
+ const allCols = Array.from(new Set(Object.values(tableMap).flat()));
523
+ const guess = (0, fuzzy_match_1.suggest)(colName, allCols);
524
+ err.next_actions.push(guess
525
+ ? `Did you mean '${guess}'? Run \`miaoda db schema get <table>\` to see columns.`
526
+ : "Run `miaoda db schema get <table>` to see all columns.");
527
+ }
528
+ /**
529
+ * 扫整段 SQL 找一个看似"拼错的关键字"token:纯字母 + 长度 ≥ 3 + 不是已知 keyword
530
+ * + fuzzy 命中某个 keyword(且距离 > 0)。返第一个命中的 token。
531
+ *
532
+ * 用于 `syntax error at or near "<X>"` 的 X 是数字 / 标点的兜底场景——例如
533
+ * `... LIMITT 1` 报错点到 `1`,但用户真正的拼写错误在 LIMITT。
534
+ *
535
+ * 阈值复用 suggest(),候选词越短匹配越严,避免把 `userid` / `usrname` 这种
536
+ * 用户标识符误抓为关键字 typo。
537
+ */
538
+ function findKeywordTypo(sql) {
539
+ const tokens = sql.split(/[\s,;()'"`*=<>+\-./]+/).filter((t) => /^[A-Za-z_]{3,}$/.test(t));
540
+ for (const tok of tokens) {
541
+ const upper = tok.toUpperCase();
542
+ if (sql_keywords_1.SQL_KEYWORDS.includes(upper))
543
+ continue; // 写对的关键字
544
+ const guess = (0, fuzzy_match_1.suggest)(upper, sql_keywords_1.SQL_KEYWORDS);
545
+ if (guess && guess !== upper) {
546
+ return { input: tok, suggest: guess };
547
+ }
548
+ }
549
+ return null;
550
+ }
551
+ /** 从 InnerSchemaRespVO 提取 `tableName → fieldName[]` 映射,扁平化 tables/views/mvs 三池。 */
552
+ function extractTableFieldMap(s) {
553
+ const out = {};
554
+ if (!s)
555
+ return out;
556
+ const pools = [s.tables?.data, s.views?.data, s.materializedViews?.data];
557
+ for (const pool of pools) {
558
+ if (!pool)
559
+ continue;
560
+ for (const t of pool) {
561
+ out[t.tableName] = (t.fields ?? []).map((f) => f.fieldName);
562
+ }
563
+ }
564
+ return out;
565
+ }
566
+ /**
567
+ * 多语句 SQL 失败时把服务端透传的 partial_results 包装成 PRD 期望形态:
568
+ * - 用现有 toMultiElement 把每条 SQLExecuteResult 转成 `{command, ...}` 元素,
569
+ * 与正常 multi-statement 成功路径的 data[] 结构完全一致
570
+ * - 推断 rolled_back:扫 completed 数组里 BEGIN/COMMIT/ROLLBACK 计数;失败时
571
+ * 还在 user tx 内 ⇒ 服务端 closeUserTxIfOpen 已发 ROLLBACK,标 true
572
+ * - rolled_back=true 时给 next_actions 追加一句 spec 文案
573
+ * - pretty 模式(非 JSON)把 `Statement N: ✓ ... / K: ✗ ...` 立即打到 stderr,
574
+ * 与 emitError 的 Error/hint 行连成一段 PRD 期望的 multi-statement 报告
575
+ */
576
+ function enrichMultiStatementError(err, sql) {
577
+ if (!(err instanceof error_1.AppError))
578
+ return;
579
+ // 用 SQL 文本判断是否多语句,而不是 partial.length——第一条就失败时
580
+ // partial_results=[],但仍需输出统一的多语句 envelope 字段保持格式一致。
581
+ const total = countStatements(sql);
582
+ if (total <= 1)
583
+ return;
584
+ const partial = Array.isArray(err.partial_results)
585
+ ? err.partial_results
586
+ : [];
587
+ const completed = partial.map((r) => toMultiElement((0, index_1.parseSqlResult)(r)));
588
+ err.completed = completed;
589
+ err.rolled_back = inferRolledBack(completed);
590
+ err.total_statements = total;
591
+ // pretty 模式(非 JSON)打 per-statement breakdown 到 stderr
592
+ if (!(0, output_1.isJsonMode)()) {
593
+ writeMultiStatementBreakdown(err, completed);
594
+ }
595
+ // rolled_back=true 时追加 spec hint:"Transaction rolled back; no changes persisted."
596
+ if (err.rolled_back) {
597
+ err.next_actions.push("Transaction rolled back; no changes persisted.");
598
+ }
599
+ }
600
+ /**
601
+ * 推断本次多语句执行最终是否处于"事务被回滚"状态:
602
+ * 遍历 completed[] 数组,BEGIN +1 / COMMIT|ROLLBACK -1;
603
+ * 失败时 depth > 0 → 用户事务还开着 → 服务端 closeUserTxIfOpen 发了 ROLLBACK
604
+ * 失败时 depth = 0 → 失败语句在 autocommit 模式 → 已成功的 statement 真实落库
605
+ */
606
+ function inferRolledBack(completed) {
607
+ let depth = 0;
608
+ for (const e of completed) {
609
+ if (e.command === "BEGIN")
610
+ depth++;
611
+ else if (e.command === "COMMIT" || e.command === "ROLLBACK")
612
+ depth--;
613
+ }
614
+ return depth > 0;
615
+ }
616
+ /**
617
+ * 数 SQL 里有几条独立语句:先去掉单 / 双引号字面量(防止 'a;b' 里的分号被算成
618
+ * 分隔符),再按分号 split,过滤空条。CLI 用户场景几乎都是简单 SQL,不处理
619
+ * dollar-quoted($$..$$)等高级形态——估错 ±1 不影响 hint 可读性。
620
+ */
621
+ function countStatements(sql) {
622
+ const stripped = sql.replace(/'(?:''|[^'])*'/g, "''").replace(/"(?:""|[^"])*"/g, '""');
623
+ return stripped.split(/;+/).filter((s) => s.trim().length > 0).length;
624
+ }
625
+ /**
626
+ * 写 `Statement N: ✓ ... / Statement K: ✗ ...` 到 stderr,对齐 PRD spec 的
627
+ * 多语句失败 pretty 输出形态。
628
+ */
629
+ function writeMultiStatementBreakdown(err, completed) {
630
+ const tty = (0, render_1.isStdoutTty)();
631
+ const lines = [];
632
+ for (let i = 0; i < completed.length; i++) {
633
+ const e = completed[i];
634
+ const verb = e.command === "BEGIN" || e.command === "COMMIT" || e.command === "ROLLBACK"
635
+ ? e.command
636
+ : describeCompleted(e);
637
+ lines.push(`Statement ${String(i + 1)}: ${tty ? colors_1.c.success("✓ ") : "✓ "}${verb}`);
638
+ }
639
+ // 失败那条
640
+ const failedIdx = err.statement_index ?? completed.length;
641
+ lines.push(`Statement ${String(failedIdx + 1)}: ${tty ? colors_1.c.fail("✗ ") : "✗ "}${err.message}`);
642
+ process.stderr.write(lines.join("\n") + "\n\n");
643
+ }
644
+ /** completed 单条结果的人类可读描述(用于 stderr breakdown 行尾)。 */
645
+ function describeCompleted(e) {
646
+ const r = e;
647
+ if (r.command === "SELECT" && Array.isArray(r.rows)) {
648
+ return `SELECT (${String(r.rows.length)} row${r.rows.length === 1 ? "" : "s"})`;
649
+ }
650
+ if (typeof r.rows_affected === "number") {
651
+ const verb = dmlVerb(r.command);
652
+ return `${String(r.rows_affected)} row${r.rows_affected === 1 ? "" : "s"} ${verb}`;
653
+ }
654
+ return `${r.command} executed`;
655
+ }
@@ -45,15 +45,23 @@ const output_1 = require("../../../utils/output");
45
45
  const render_1 = require("../../../utils/render");
46
46
  const error_1 = require("../../../utils/error");
47
47
  const shared_1 = require("../../../cli/commands/shared");
48
+ const colors_1 = require("../../../utils/colors");
48
49
  const MAX_UPLOAD_BYTES = 100 * 1024 * 1024;
49
50
  /**
50
- * 判断 src 是本地文件还是远程引用:
51
- * - src 本地 fs 可访问 upload(dst remote)
52
- * - 其他 → download(src 是 /path 或 file_name,由 resolveRemotePath 解析)
53
- *
54
- * `cp` 语义要求 src 必须存在;本地不存在就认为用户指向远程。不需要给 dst 猜方向。
51
+ * 判断 src 是本地路径还是远程引用——按 PRD `miaoda file cp` 规则的优先级:
52
+ * 1. `./` / `../` / `~/` 开头 + 裸文件名(不含 `/`)→ 本地路径(即使不存在)
53
+ * handleUpload 把"不存在"准确抛成 FILE_SRC_NOT_FOUND,
54
+ * 避免回退到远程 download 输出 `FILE_NOT_FOUND` + "Run miaoda file ls"
55
+ * 这种与用户意图(上传)背离的引导。
56
+ * 2. `/` 开头:fs.existsSync 兜底——存在即本地(绝对路径上传),
57
+ * 不存在即远程 path(download,由 resolveRemotePath 处理)。
55
58
  */
56
59
  function isLocalSrc(src) {
60
+ if (src.startsWith("./") || src.startsWith("../") || src.startsWith("~/"))
61
+ return true;
62
+ if (!src.includes("/"))
63
+ return true;
64
+ // `/` 开头:可能是本地绝对路径,也可能是远程 path;交给 fs 探测。
57
65
  const expanded = expandHome(src);
58
66
  try {
59
67
  return node_fs_1.default.existsSync(expanded);
@@ -104,23 +112,37 @@ async function resolveRemotePath(appId, input) {
104
112
  async function handleUpload(appId, localRaw, remoteRaw, rename) {
105
113
  const localPath = expandHome(localRaw);
106
114
  if (!node_fs_1.default.existsSync(localPath) || !node_fs_1.default.statSync(localPath).isFile()) {
107
- throw new error_1.AppError("FILE_SRC_NOT_FOUND", `Local file '${localRaw}' does not exist`);
115
+ throw new error_1.AppError("FILE_SRC_NOT_FOUND", `Local file '${localRaw}' does not exist`, {
116
+ // 引导本地路径自检;远程下载请用 `/path` 形态,避免裸名/相对路径误入上传分支
117
+ next_actions: ["Check the local file path; use `/path` prefix for remote download."],
118
+ });
108
119
  }
109
120
  const stat = node_fs_1.default.statSync(localPath);
110
121
  if (stat.size > MAX_UPLOAD_BYTES) {
111
122
  throw new error_1.AppError("FILE_SIZE_EXCEEDED", `File size ${(0, render_1.formatSize)(stat.size)} exceeds the 100 MB upload limit`, {
112
- next_actions: [
113
- "Split the file, or use the web console for large uploads.",
114
- ],
123
+ next_actions: ["Split the file, or use the web console for large uploads."],
115
124
  });
116
125
  }
117
- const fileName = rename ?? node_path_1.default.basename(localPath);
118
- let remotePath = remoteRaw;
119
- if (remoteRaw.endsWith("/")) {
120
- remotePath = remoteRaw + fileName;
126
+ // PRD:path 末段**始终**由平台生成 16 ID 确保唯一,不受 dst 形态或
127
+ // --rename 影响;用户指定的 file_name 仅作为显示名(同目录下允许重名)。
128
+ // 因此 CLI 端把 dst 拆成"目录前缀 + file_name"两段:
129
+ // - dst / 结尾或为空 → 整段当目录前缀,file_name 用 --rename 或本地 basename
130
+ // 例:cp ./logo.png /imgs/ → path=/imgs/<GID>.png, file_name=logo.png
131
+ // - dst 不以 / 结尾 → 末段当 file_name,前段(含尾 /)当目录前缀
132
+ // 例:cp ./1.jpg /post-covers/cover.jpg → path=/post-covers/<GID>.jpg, file_name=cover.jpg
133
+ // 这样无论用户怎么写 dst,都走服务端目录前缀模式(PreUpload `filePath`
134
+ // 末尾带 /),保证 path 全局唯一性,不会因用户指定了完整路径而退化成
135
+ // 完整对象 key 模式。--rename 始终覆盖 file_name 推断结果(PRD 优先级)。
136
+ let fileName;
137
+ let remotePath;
138
+ if (remoteRaw === "" || remoteRaw.endsWith("/")) {
139
+ fileName = rename ?? node_path_1.default.basename(localPath);
140
+ remotePath = remoteRaw;
121
141
  }
122
- else if (remoteRaw === "/") {
123
- remotePath = "/" + fileName;
142
+ else {
143
+ const lastSlash = remoteRaw.lastIndexOf("/");
144
+ fileName = rename ?? remoteRaw.slice(lastSlash + 1);
145
+ remotePath = remoteRaw.slice(0, lastSlash + 1);
124
146
  }
125
147
  const contentType = detectMime(localPath);
126
148
  const result = await api.file.uploadFile({
@@ -147,7 +169,7 @@ async function handleUpload(appId, localRaw, remoteRaw, rename) {
147
169
  const tty = (0, render_1.isStdoutTty)();
148
170
  const lines = [];
149
171
  if (tty) {
150
- lines.push(`✓ Uploaded ${node_path_1.default.basename(localPath)} → ${result.path}`);
172
+ lines.push(colors_1.c.success(`✓ Uploaded ${node_path_1.default.basename(localPath)} → ${result.path}`));
151
173
  lines.push(` file_name: ${result.file_name}`);
152
174
  lines.push(` path: ${result.path}`);
153
175
  lines.push(` size: ${(0, render_1.formatSize)(result.size)} (${String(result.size)} bytes)`);
@@ -204,7 +226,7 @@ async function handleDownload(appId, remoteRaw, localRaw) {
204
226
  }
205
227
  const tty = (0, render_1.isStdoutTty)();
206
228
  if (tty) {
207
- (0, output_1.emit)(`✓ Downloaded ${baseName} → ${localTarget} (${(0, render_1.formatSize)(writtenBytes)})`);
229
+ (0, output_1.emit)(colors_1.c.success(`✓ Downloaded ${baseName} → ${localTarget} (${(0, render_1.formatSize)(writtenBytes)})`));
208
230
  }
209
231
  else {
210
232
  (0, output_1.emit)(`OK Downloaded ${baseName} -> ${localTarget} (${String(writtenBytes)} bytes)`);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleFileSign = exports.handleFileRm = exports.handleFileCp = exports.handleFileStat = exports.handleFileLs = void 0;
3
+ exports.handleFileQuota = exports.handleFileSign = exports.handleFileRm = exports.handleFileCp = exports.handleFileStat = exports.handleFileLs = void 0;
4
4
  var ls_1 = require("./ls");
5
5
  Object.defineProperty(exports, "handleFileLs", { enumerable: true, get: function () { return ls_1.handleFileLs; } });
6
6
  var stat_1 = require("./stat");
@@ -11,3 +11,5 @@ var rm_1 = require("./rm");
11
11
  Object.defineProperty(exports, "handleFileRm", { enumerable: true, get: function () { return rm_1.handleFileRm; } });
12
12
  var sign_1 = require("./sign");
13
13
  Object.defineProperty(exports, "handleFileSign", { enumerable: true, get: function () { return sign_1.handleFileSign; } });
14
+ var quota_1 = require("./quota");
15
+ Object.defineProperty(exports, "handleFileQuota", { enumerable: true, get: function () { return quota_1.handleFileQuota; } });
@@ -101,9 +101,7 @@ async function handleFileLs(opts) {
101
101
  info.type,
102
102
  (0, render_1.formatTime)(info.uploaded_at, tty),
103
103
  ]);
104
- const table = tty
105
- ? (0, render_1.renderAlignedTable)(headers, rows)
106
- : (0, render_1.renderTsv)(headers, rows);
104
+ const table = tty ? (0, render_1.renderAlignedTable)(headers, rows) : (0, render_1.renderTsv)(headers, rows);
107
105
  const hint = result.has_more && result.next_cursor
108
106
  ? `\n— ${String(result.items.length)} results. Next: --cursor ${result.next_cursor}`
109
107
  : "";
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.handleFileQuota = handleFileQuota;
37
+ const api = __importStar(require("../../../api/index"));
38
+ const shared_1 = require("../../../cli/commands/shared");
39
+ const output_1 = require("../../../utils/output");
40
+ const render_1 = require("../../../utils/render");
41
+ async function handleFileQuota(opts) {
42
+ const appId = (0, shared_1.resolveAppId)(opts);
43
+ const data = await api.file.getStorageQuota({ appId });
44
+ if ((0, output_1.isJsonMode)()) {
45
+ // 配额未对接(storageQuotaBytes=0)时,quota / usage_percent 字段都不输出
46
+ const out = {
47
+ storageUsedBytes: data.storageUsedBytes,
48
+ files: data.files,
49
+ };
50
+ if (data.storageQuotaBytes > 0) {
51
+ out.storageQuotaBytes = data.storageQuotaBytes;
52
+ out.usagePercent = data.usagePercent;
53
+ }
54
+ (0, output_1.emitOk)((0, output_1.snakeCaseKeys)(out));
55
+ return;
56
+ }
57
+ // PRD:单行 "Storage: 150 MB / 1 GB (15%)";配额未对接时只显示 used
58
+ const tty = (0, render_1.isStdoutTty)();
59
+ const storageLine = data.storageQuotaBytes > 0
60
+ ? `${(0, render_1.formatSize)(data.storageUsedBytes)} / ${(0, render_1.formatSize)(data.storageQuotaBytes)} (${data.usagePercent.toFixed(1)}%)`
61
+ : (0, render_1.formatSize)(data.storageUsedBytes);
62
+ (0, output_1.emit)((0, render_1.renderKeyValue)([
63
+ ["Storage", storageLine],
64
+ ["Files", String(data.files)],
65
+ ], tty));
66
+ }
@@ -43,6 +43,7 @@ const error_1 = require("../../../utils/error");
43
43
  const shared_1 = require("../../../cli/commands/shared");
44
44
  const index_1 = require("../../../api/file/index");
45
45
  const render_1 = require("../../../utils/render");
46
+ const colors_1 = require("../../../utils/colors");
46
47
  const node_readline_1 = __importDefault(require("node:readline"));
47
48
  const MAX_BATCH = 100;
48
49
  /**
@@ -169,7 +170,7 @@ async function handleFileRm(paths, opts) {
169
170
  results.push({
170
171
  status: "ok",
171
172
  input: entry?.input ?? p,
172
- file_name: entry?.file_name ?? (p.split("/").pop() ?? p),
173
+ file_name: entry?.file_name ?? p.split("/").pop() ?? p,
173
174
  path: p,
174
175
  });
175
176
  }
@@ -228,9 +229,9 @@ async function handleFileRm(paths, opts) {
228
229
  const lines = [];
229
230
  for (const r of results) {
230
231
  if (r.status === "ok")
231
- lines.push(`✓ Deleted ${r.input}`);
232
+ lines.push(colors_1.c.success(`✓ Deleted ${r.input}`));
232
233
  else
233
- lines.push(`✗ ${r.input}: ${r.error.message}`);
234
+ lines.push(colors_1.c.fail(`✗ ${r.input}: ${r.error.message}`));
234
235
  }
235
236
  if (failCount === 0) {
236
237
  lines.push(`Deleted ${String(okCount)} of ${String(totalCount)} files`);