@lark-apaas/miaoda-cli 0.1.0 → 0.1.1

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.
@@ -0,0 +1,367 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.handleDbSql = handleDbSql;
40
+ const api = __importStar(require("../../../api/index"));
41
+ const error_1 = require("../../../utils/error");
42
+ const output_1 = require("../../../utils/output");
43
+ const config_1 = require("../../../utils/config");
44
+ const logger_1 = require("../../../utils/logger");
45
+ const shared_1 = require("../../../cli/commands/shared");
46
+ const render_1 = require("../../../utils/render");
47
+ const index_1 = require("../../../api/db/index");
48
+ const node_child_process_1 = require("node:child_process");
49
+ const node_fs_1 = require("node:fs");
50
+ const node_path_1 = __importDefault(require("node:path"));
51
+ /**
52
+ * miaoda db sql <query> — 执行任意 SQL。
53
+ *
54
+ * - query 为空时读 stdin
55
+ * - --env 透传给后端 admin-inner(dbBranch 参数),不传时由后端按 workspace
56
+ * 多环境状态兜底(多环境 → dev / 单环境 → main);后端检测到环境不存在
57
+ * 会返 k_dl_1600000 + "Invalid DB Branch:...",CLI 侧映射为 MULTI_ENV_NOT_INITIALIZED
58
+ * - 多语句行为对齐 PRD:每条 statement 一个独立结果元素,pretty 逐条 +
59
+ * 末尾汇总,--json 输出 data 数组。
60
+ */
61
+ async function handleDbSql(query, opts) {
62
+ const appId = (0, shared_1.resolveAppId)(opts);
63
+ const sql = await readSql(query);
64
+ if (!sql.trim()) {
65
+ throw new error_1.AppError("ARGS_INVALID", "Empty SQL (no inline query and stdin is empty)");
66
+ }
67
+ const results = await api.db.execSql({ appId, sql, dbBranch: opts.env });
68
+ if (results.length === 0) {
69
+ // 后端未返回任何结果,通常不会发生
70
+ if ((0, output_1.isJsonMode)()) {
71
+ (0, output_1.emit)({ data: [] });
72
+ return;
73
+ }
74
+ (0, output_1.emit)("✓ No results");
75
+ return;
76
+ }
77
+ if (results.length === 1) {
78
+ renderSingle(results[0]);
79
+ await maybeSyncAgentSchema(results);
80
+ return;
81
+ }
82
+ // 多语句:每条 statement 独立结果
83
+ if ((0, output_1.isJsonMode)()) {
84
+ (0, output_1.emit)({ data: results.map((r) => toMultiElement((0, index_1.parseSqlResult)(r))) });
85
+ }
86
+ else {
87
+ renderMultiPretty(results);
88
+ }
89
+ await maybeSyncAgentSchema(results);
90
+ }
91
+ /**
92
+ * 当本次 SQL 包含成功执行的 DDL 时,触发 `npm run gen:db-schema`(实质执行
93
+ * fullstack-cli gen-db-schema → drizzle-kit introspect)同步 agent 项目的
94
+ * schema.ts。**弱依赖**:失败不阻塞主流程退出码。
95
+ *
96
+ * 行为细节:
97
+ * - 仅当 cwd/package.json 定义了 gen:db-schema 脚本时才尝试运行;非 agent
98
+ * 项目静默跳过
99
+ * - 默认静默捕获子进程 stdout/stderr,成功只打一行 "Agent schema synced",
100
+ * 失败时把捕获的输出回放给用户排查;--verbose 实时透传方便看进度
101
+ * - 加 60s 兜底超时:drizzle-kit introspect 通常 < 30s,超时直接 SIGKILL,
102
+ * 避免 DB 慢或网络抖动时把 CLI 挂死
103
+ * - 子进程的 stdout / stderr 都不会进入当前 CLI 的 stdout,--json 输出干净
104
+ *
105
+ * 调用时机:在 SQL 主输出 emit 完成之后;execSql 已经返回意味着所有
106
+ * statement 都执行成功,无需再校验逐条状态。
107
+ */
108
+ /** agent runtime 中固定的项目根目录;放在此处便于以后调整。 */
109
+ const AGENT_PROJECT_ROOT = "/home/gem/workspace/code";
110
+ /** drain 子进程 pipe stream:默认静默模式下消费 chunk 防止缓冲区反压。 */
111
+ function drainChunk(_chunk) {
112
+ // 显式接 chunk 参数让 ESLint 不再当成 empty function;不做任何处理
113
+ }
114
+ async function maybeSyncAgentSchema(results) {
115
+ if (!hasDdl(results))
116
+ return;
117
+ const projectRoot = await resolveAgentProjectRoot();
118
+ if (!projectRoot) {
119
+ (0, logger_1.debug)("[db sql] agent project root not found or missing gen:db-schema script, skip schema sync");
120
+ return;
121
+ }
122
+ const verbose = (0, config_1.getConfig)().verbose;
123
+ // drizzle-kit introspect 通常 < 30s;60s 兜底,超过即认为卡住
124
+ const TIMEOUT_MS = 60_000;
125
+ (0, logger_1.debug)(`[db sql] DDL detected, running \`npm run gen:db-schema\` in ${projectRoot}`);
126
+ try {
127
+ await new Promise((resolve) => {
128
+ const proc = (0, node_child_process_1.spawn)("npm", ["run", "gen:db-schema"], {
129
+ stdio: ["ignore", "pipe", "pipe"],
130
+ cwd: projectRoot,
131
+ });
132
+ // --verbose:实时透传到 stderr 看进度;默认完全静默丢弃。
133
+ // 任何路径下子进程输出都不会进入当前 CLI 的 stdout,--json 模式安全。
134
+ // 注:默认路径必须 attach 监听器,否则 pipe 缓冲区写满会反压子进程。
135
+ if (verbose) {
136
+ proc.stdout.pipe(process.stderr);
137
+ proc.stderr.pipe(process.stderr);
138
+ }
139
+ else {
140
+ proc.stdout.on("data", drainChunk);
141
+ proc.stderr.on("data", drainChunk);
142
+ }
143
+ let timedOut = false;
144
+ const timer = setTimeout(() => {
145
+ timedOut = true;
146
+ proc.kill("SIGKILL");
147
+ }, TIMEOUT_MS);
148
+ proc.on("close", (code) => {
149
+ clearTimeout(timer);
150
+ if (timedOut) {
151
+ (0, logger_1.debug)(`[db sql] gen:db-schema timed out after ${String(TIMEOUT_MS / 1000)}s, killed`);
152
+ }
153
+ else if (code !== 0 && code !== null) {
154
+ (0, logger_1.debug)(`[db sql] gen:db-schema exited with code ${String(code)}`);
155
+ }
156
+ else {
157
+ (0, logger_1.debug)("[db sql] gen:db-schema completed");
158
+ }
159
+ resolve();
160
+ });
161
+ proc.on("error", (err) => {
162
+ clearTimeout(timer);
163
+ (0, logger_1.debug)(`[db sql] gen:db-schema spawn error: ${err.message}`);
164
+ resolve();
165
+ });
166
+ });
167
+ }
168
+ catch (err) {
169
+ (0, logger_1.debug)(`[db sql] gen:db-schema unexpected error: ${String(err)}`);
170
+ }
171
+ }
172
+ /**
173
+ * 校验 AGENT_PROJECT_ROOT 是否是一个有效的 agent 项目(package.json 含
174
+ * `gen:db-schema` 脚本)。命中返回路径;缺失任一条件返 null,让调用方静默跳过。
175
+ *
176
+ * 这里不再从 process.cwd() 向上查找:agent runtime 里项目根是固定路径,
177
+ * 在 CLI 之外(本地手动跑 miaoda db sql)也不应触发同步。
178
+ */
179
+ async function resolveAgentProjectRoot() {
180
+ try {
181
+ const pkgPath = node_path_1.default.join(AGENT_PROJECT_ROOT, "package.json");
182
+ const raw = await node_fs_1.promises.readFile(pkgPath, "utf8");
183
+ const pkg = JSON.parse(raw);
184
+ if (pkg.scripts?.["gen:db-schema"])
185
+ return AGENT_PROJECT_ROOT;
186
+ }
187
+ catch {
188
+ // 路径不存在 / 不可读 / 不是 agent 项目 → 静默跳过
189
+ }
190
+ return null;
191
+ }
192
+ /** 是否有任意一条 statement 是 DDL(CREATE / ALTER / DROP / GRANT / TRUNCATE / COMMENT 等)。 */
193
+ function hasDdl(results) {
194
+ return results.some((r) => (0, index_1.parseSqlResult)(r).kind === "ddl");
195
+ }
196
+ /** 读取 stdin 并返回完整 SQL 文本(stdin 不是 TTY 即认为被 pipe)。 */
197
+ async function readSql(inline) {
198
+ if (inline !== undefined && inline.length > 0)
199
+ return inline;
200
+ const stdin = process.stdin;
201
+ if (stdin.isTTY)
202
+ return "";
203
+ return new Promise((resolve, reject) => {
204
+ let data = "";
205
+ process.stdin.setEncoding("utf8");
206
+ process.stdin.on("data", (chunk) => {
207
+ data += chunk;
208
+ });
209
+ process.stdin.on("end", () => {
210
+ resolve(data);
211
+ });
212
+ process.stdin.on("error", reject);
213
+ });
214
+ }
215
+ function renderSingle(raw) {
216
+ const parsed = (0, index_1.parseSqlResult)(raw);
217
+ const tty = (0, render_1.isStdoutTty)();
218
+ if ((0, output_1.isJsonMode)()) {
219
+ (0, output_1.emit)(toJson(parsed));
220
+ return;
221
+ }
222
+ if (parsed.kind === "select") {
223
+ if (parsed.rows.length === 0) {
224
+ (0, output_1.emit)("(0 rows)");
225
+ return;
226
+ }
227
+ const cols = collectColumns(parsed.rows);
228
+ const rows = parsed.rows.map((r) => cols.map((c) => formatCell(r[c], tty)));
229
+ (0, output_1.emit)(tty ? (0, render_1.renderAlignedTable)(cols, rows) : (0, render_1.renderTsv)(cols, rows));
230
+ return;
231
+ }
232
+ if (parsed.kind === "dml") {
233
+ const verb = dmlVerb(parsed.sqlType);
234
+ (0, output_1.emit)(tty
235
+ ? `✓ ${String(parsed.affectedRows)} row${parsed.affectedRows === 1 ? "" : "s"} ${verb}`
236
+ : `OK ${String(parsed.affectedRows)} rows ${verb}`);
237
+ return;
238
+ }
239
+ // DDL
240
+ (0, output_1.emit)(tty ? "✓ DDL executed" : "OK DDL executed");
241
+ }
242
+ function toJson(parsed) {
243
+ if (parsed.kind === "select") {
244
+ // PRD 单 SELECT:data 直接是行数组(按 --json 字段投影裁剪)
245
+ return { data: projectRows(parsed.rows) };
246
+ }
247
+ if (parsed.kind === "dml") {
248
+ // PRD 单 DML:data = {command, rows_affected}
249
+ return {
250
+ data: {
251
+ command: parsed.sqlType,
252
+ rows_affected: parsed.affectedRows,
253
+ },
254
+ };
255
+ }
256
+ // PRD 单 DDL:data = {command, target?}。command 直接用后端给的细粒度
257
+ // (CREATE_TABLE / DROP_TABLE / ...),target 待后端给对象名后再加。
258
+ return { data: { command: parsed.sqlType } };
259
+ }
260
+ /**
261
+ * 多语句 --json 元素:与单 DDL/DML 形状一致,但 SELECT 包成
262
+ * {command:"SELECT", rows:[...]}(PRD 约定,避免数组里嵌套数组造成歧义)。
263
+ */
264
+ function toMultiElement(parsed) {
265
+ if (parsed.kind === "select") {
266
+ return { command: "SELECT", rows: projectRows(parsed.rows) };
267
+ }
268
+ if (parsed.kind === "dml") {
269
+ return { command: parsed.sqlType, rows_affected: parsed.affectedRows };
270
+ }
271
+ // DDL:用后端给的细粒度 command
272
+ return { command: parsed.sqlType };
273
+ }
274
+ /**
275
+ * PRD:`--json id,name` 字段投影。--json 不带值(boolean true)等价于不裁剪。
276
+ * 字段不存在时按 undefined 处理(JSON.stringify 会忽略 undefined value 的 key),
277
+ * 这样 Agent 拿到的 row 永远只含请求过的列。
278
+ */
279
+ function projectRows(rows) {
280
+ const fields = parseJsonFields();
281
+ if (!fields)
282
+ return rows;
283
+ return rows.map((r) => {
284
+ const out = {};
285
+ for (const f of fields) {
286
+ out[f] = r[f];
287
+ }
288
+ return out;
289
+ });
290
+ }
291
+ /** 读取 --json [fields]:返回字段列表;boolean true 或 undefined 返回 null(不裁剪)。 */
292
+ function parseJsonFields() {
293
+ const v = (0, config_1.getConfig)().json;
294
+ if (typeof v !== "string" || v === "")
295
+ return null;
296
+ return v.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
297
+ }
298
+ /** 多语句 pretty:逐条 `Statement N: ...` + 末尾 `✓ N statements executed`。 */
299
+ function renderMultiPretty(results) {
300
+ const tty = (0, render_1.isStdoutTty)();
301
+ const lines = [];
302
+ for (let i = 0; i < results.length; i++) {
303
+ const parsed = (0, index_1.parseSqlResult)(results[i]);
304
+ const idx = i + 1;
305
+ if (parsed.kind === "select") {
306
+ const n = parsed.rows.length;
307
+ lines.push(`Statement ${String(idx)}: SELECT (${String(n)} row${n === 1 ? "" : "s"})`);
308
+ if (n > 0) {
309
+ const cols = collectColumns(parsed.rows);
310
+ const tbl = parsed.rows.map((r) => cols.map((c) => formatCell(r[c], tty)));
311
+ lines.push(tty ? (0, render_1.renderAlignedTable)(cols, tbl) : (0, render_1.renderTsv)(cols, tbl));
312
+ }
313
+ // 块间空行(最后一条不留)
314
+ if (i < results.length - 1)
315
+ lines.push("");
316
+ continue;
317
+ }
318
+ if (parsed.kind === "dml") {
319
+ const verb = dmlVerb(parsed.sqlType);
320
+ const n = parsed.affectedRows;
321
+ lines.push(`Statement ${String(idx)}: ${tty ? "✓ " : ""}${String(n)} row${n === 1 ? "" : "s"} ${verb}`);
322
+ continue;
323
+ }
324
+ // DDL
325
+ lines.push(`Statement ${String(idx)}: ${tty ? "✓ " : ""}DDL executed`);
326
+ }
327
+ // 汇总行:所有 statement 都跑完了
328
+ lines.push(tty
329
+ ? `✓ ${String(results.length)} statements executed`
330
+ : `OK ${String(results.length)} statements executed`);
331
+ (0, output_1.emit)(lines.join("\n"));
332
+ }
333
+ function dmlVerb(type) {
334
+ 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"; // 未识别子类的兜底
340
+ }
341
+ }
342
+ /** 从第一行收集列顺序;缺失列保留空白(兼容稀疏行)。 */
343
+ function collectColumns(rows) {
344
+ if (rows.length === 0)
345
+ return [];
346
+ return Object.keys(rows[0]);
347
+ }
348
+ /**
349
+ * SQL 单元格 → 字符串:
350
+ * - null / undefined → "NULL"(TTY 下用 dim+gray ANSI 弱化,对齐 mysql/DuckDB;
351
+ * non-TTY 用裸字面量,避免被 cat / 管道吞掉样式;JSON 模式不会走到这里,
352
+ * 走 toJson 直接用 parsed.rows,JSON.stringify 输出 `null` 字面)
353
+ * - string → 原样
354
+ * - number / boolean → toString
355
+ * - object / array → JSON.stringify
356
+ */
357
+ function formatCell(v, tty) {
358
+ if (v === null || v === undefined) {
359
+ return tty ? "\u001b[2;90mNULL\u001b[0m" : "NULL";
360
+ }
361
+ if (typeof v === "string")
362
+ return v;
363
+ if (typeof v === "number" || typeof v === "boolean")
364
+ return String(v);
365
+ // object / array → JSON
366
+ return JSON.stringify(v);
367
+ }
@@ -0,0 +1,220 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.handleFileCp = handleFileCp;
40
+ const node_fs_1 = __importDefault(require("node:fs"));
41
+ const node_path_1 = __importDefault(require("node:path"));
42
+ const node_os_1 = __importDefault(require("node:os"));
43
+ const api = __importStar(require("../../../api/index"));
44
+ const output_1 = require("../../../utils/output");
45
+ const render_1 = require("../../../utils/render");
46
+ const error_1 = require("../../../utils/error");
47
+ const shared_1 = require("../../../cli/commands/shared");
48
+ const MAX_UPLOAD_BYTES = 100 * 1024 * 1024;
49
+ /**
50
+ * 判断 src 是本地文件还是远程引用:
51
+ * - src 本地 fs 可访问 → upload(dst 当 remote)
52
+ * - 其他 → download(src 是 /path 或 file_name,由 resolveRemotePath 解析)
53
+ *
54
+ * `cp` 语义要求 src 必须存在;本地不存在就认为用户指向远程。不需要给 dst 猜方向。
55
+ */
56
+ function isLocalSrc(src) {
57
+ const expanded = expandHome(src);
58
+ try {
59
+ return node_fs_1.default.existsSync(expanded);
60
+ }
61
+ catch {
62
+ return false;
63
+ }
64
+ }
65
+ function expandHome(p) {
66
+ if (p.startsWith("~/"))
67
+ return node_path_1.default.join(node_os_1.default.homedir(), p.slice(2));
68
+ return p;
69
+ }
70
+ function detectMime(filePath) {
71
+ const ext = node_path_1.default.extname(filePath).toLowerCase();
72
+ const map = {
73
+ ".png": "image/png",
74
+ ".jpg": "image/jpeg",
75
+ ".jpeg": "image/jpeg",
76
+ ".gif": "image/gif",
77
+ ".webp": "image/webp",
78
+ ".svg": "image/svg+xml",
79
+ ".pdf": "application/pdf",
80
+ ".json": "application/json",
81
+ ".csv": "text/csv",
82
+ ".txt": "text/plain",
83
+ ".html": "text/html",
84
+ ".zip": "application/zip",
85
+ ".tar": "application/x-tar",
86
+ ".gz": "application/gzip",
87
+ ".mp4": "video/mp4",
88
+ ".mp3": "audio/mpeg",
89
+ };
90
+ return map[ext] ?? "application/octet-stream";
91
+ }
92
+ /** 把 src 的远程形式(`/path` / `file_name`)解析成后端 sign 可用的 remote path。 */
93
+ async function resolveRemotePath(appId, input) {
94
+ if (input.startsWith("/"))
95
+ return input;
96
+ const [resolved] = await api.file.resolveInputs({ appId, inputs: [input] });
97
+ if (resolved.status === "error") {
98
+ throw new error_1.AppError(resolved.error.code, resolved.error.message, {
99
+ next_actions: resolved.error.hint ? [resolved.error.hint] : undefined,
100
+ });
101
+ }
102
+ return resolved.file.path;
103
+ }
104
+ async function handleUpload(appId, localRaw, remoteRaw, rename) {
105
+ const localPath = expandHome(localRaw);
106
+ 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`);
108
+ }
109
+ const stat = node_fs_1.default.statSync(localPath);
110
+ if (stat.size > MAX_UPLOAD_BYTES) {
111
+ 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
+ ],
115
+ });
116
+ }
117
+ const fileName = rename ?? node_path_1.default.basename(localPath);
118
+ let remotePath = remoteRaw;
119
+ if (remoteRaw.endsWith("/")) {
120
+ remotePath = remoteRaw + fileName;
121
+ }
122
+ else if (remoteRaw === "/") {
123
+ remotePath = "/" + fileName;
124
+ }
125
+ const contentType = detectMime(localPath);
126
+ const result = await api.file.uploadFile({
127
+ appId,
128
+ localPath,
129
+ remotePath,
130
+ fileName,
131
+ fileSize: stat.size,
132
+ contentType,
133
+ readFile: async () => node_fs_1.default.promises.readFile(localPath),
134
+ });
135
+ if ((0, output_1.isJsonMode)()) {
136
+ const payload = {
137
+ file_name: result.file_name,
138
+ path: result.path,
139
+ size_bytes: result.size,
140
+ type: result.type,
141
+ };
142
+ if (result.download_url)
143
+ payload.download_url = result.download_url;
144
+ (0, output_1.emitOk)(payload);
145
+ return;
146
+ }
147
+ const tty = (0, render_1.isStdoutTty)();
148
+ const lines = [];
149
+ if (tty) {
150
+ lines.push(`✓ Uploaded ${node_path_1.default.basename(localPath)} → ${result.path}`);
151
+ lines.push(` file_name: ${result.file_name}`);
152
+ lines.push(` path: ${result.path}`);
153
+ lines.push(` size: ${(0, render_1.formatSize)(result.size)} (${String(result.size)} bytes)`);
154
+ lines.push(` type: ${result.type}`);
155
+ if (result.download_url) {
156
+ lines.push(` download_url: ${result.download_url}`);
157
+ }
158
+ }
159
+ else {
160
+ lines.push(`OK Uploaded ${node_path_1.default.basename(localPath)} -> ${result.path}`);
161
+ lines.push(`file_name\t${result.file_name}`);
162
+ lines.push(`path\t${result.path}`);
163
+ lines.push(`size\t${String(result.size)}`);
164
+ lines.push(`type\t${result.type}`);
165
+ if (result.download_url) {
166
+ lines.push(`download_url\t${result.download_url}`);
167
+ }
168
+ }
169
+ (0, output_1.emit)(lines.join("\n"));
170
+ }
171
+ async function handleDownload(appId, remoteRaw, localRaw) {
172
+ const filePath = await resolveRemotePath(appId, remoteRaw);
173
+ const sign = await api.file.signDownload({
174
+ appId,
175
+ filePath,
176
+ expiresIn: 300,
177
+ });
178
+ const baseName = node_path_1.default.basename(filePath);
179
+ let localTarget = expandHome(localRaw);
180
+ if (localTarget === "./" || localTarget === "." || localTarget.endsWith("/")) {
181
+ localTarget = node_path_1.default.join(localTarget, baseName);
182
+ }
183
+ else if (node_fs_1.default.existsSync(localTarget) && node_fs_1.default.statSync(localTarget).isDirectory()) {
184
+ localTarget = node_path_1.default.join(localTarget, baseName);
185
+ }
186
+ let writtenBytes = 0;
187
+ await api.file.downloadFile({
188
+ signedURL: sign.signed_url,
189
+ writeFile: async (buf) => {
190
+ await node_fs_1.default.promises.mkdir(node_path_1.default.dirname(node_path_1.default.resolve(localTarget)), {
191
+ recursive: true,
192
+ });
193
+ await node_fs_1.default.promises.writeFile(localTarget, buf);
194
+ writtenBytes = buf.length;
195
+ },
196
+ });
197
+ if ((0, output_1.isJsonMode)()) {
198
+ (0, output_1.emitOk)({
199
+ path: filePath,
200
+ local_path: localTarget,
201
+ size: writtenBytes,
202
+ });
203
+ return;
204
+ }
205
+ const tty = (0, render_1.isStdoutTty)();
206
+ if (tty) {
207
+ (0, output_1.emit)(`✓ Downloaded ${baseName} → ${localTarget} (${(0, render_1.formatSize)(writtenBytes)})`);
208
+ }
209
+ else {
210
+ (0, output_1.emit)(`OK Downloaded ${baseName} -> ${localTarget} (${String(writtenBytes)} bytes)`);
211
+ }
212
+ }
213
+ async function handleFileCp(src, dst, opts) {
214
+ const appId = (0, shared_1.resolveAppId)(opts);
215
+ // src 本地存在 → upload;其他情况(file_ref / `/path` / 裸文件名)→ download
216
+ if (isLocalSrc(src)) {
217
+ return handleUpload(appId, src, dst, opts.rename);
218
+ }
219
+ return handleDownload(appId, src, dst);
220
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleFileSign = exports.handleFileRm = exports.handleFileCp = exports.handleFileStat = exports.handleFileLs = void 0;
4
+ var ls_1 = require("./ls");
5
+ Object.defineProperty(exports, "handleFileLs", { enumerable: true, get: function () { return ls_1.handleFileLs; } });
6
+ var stat_1 = require("./stat");
7
+ Object.defineProperty(exports, "handleFileStat", { enumerable: true, get: function () { return stat_1.handleFileStat; } });
8
+ var cp_1 = require("./cp");
9
+ Object.defineProperty(exports, "handleFileCp", { enumerable: true, get: function () { return cp_1.handleFileCp; } });
10
+ var rm_1 = require("./rm");
11
+ Object.defineProperty(exports, "handleFileRm", { enumerable: true, get: function () { return rm_1.handleFileRm; } });
12
+ var sign_1 = require("./sign");
13
+ Object.defineProperty(exports, "handleFileSign", { enumerable: true, get: function () { return sign_1.handleFileSign; } });