@lark-apaas/miaoda-cli 0.1.32 → 0.1.33-alpha.021d268
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.
- package/README.md +23 -0
- package/dist/cli/commands/app/index.js +81 -0
- package/dist/cli/commands/deploy/modern.js +6 -5
- package/dist/cli/handlers/app/index.js +6 -1
- package/dist/cli/handlers/app/pack.js +256 -0
- package/dist/services/app/pack/archive.js +35 -0
- package/dist/services/app/pack/copy-tree.js +51 -0
- package/dist/services/app/pack/download.js +52 -0
- package/dist/services/app/pack/index.js +27 -0
- package/dist/services/app/pack/inline-babel.js +91 -0
- package/dist/services/app/pack/naming.js +65 -0
- package/dist/services/app/pack/rewrite.js +80 -0
- package/dist/services/app/pack/scan.js +72 -0
- package/dist/services/app/pack/strategies.js +264 -0
- package/dist/services/app/pack/upload.js +110 -0
- package/dist/services/deploy/modern/patch/actions.js +6 -4
- package/dist/services/deploy/modern/patch/html-title.js +75 -0
- package/dist/services/deploy/modern/patch/index.js +1 -1
- package/dist/services/deploy/modern/patch/routes.js +13 -3
- package/dist/services/deploy/modern/patch/source-scan.js +3 -40
- package/dist/utils/exclude-patterns.js +49 -0
- package/dist/utils/sandbox-skills.js +13 -1
- package/package.json +1 -1
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/lint.js +43 -7
package/README.md
CHANGED
|
@@ -48,9 +48,32 @@ miaoda file ls --output json
|
|
|
48
48
|
| `miaoda observability ...` | 线上日志、链路、前端源码堆栈反查、监控指标、运营指标 |
|
|
49
49
|
| `miaoda comment ...` | 应用评论:查看评论列表、解决评论(仅 design-html 场景开放) |
|
|
50
50
|
| `miaoda registry ...` | 组件 registry:列出 / copy-in SDK 文件(本地执行) |
|
|
51
|
+
| `miaoda app ...` | 应用生命周期:元数据、初始化、平台同步、源码导出、静态产物导出 |
|
|
51
52
|
|
|
52
53
|
完整命令通过 `miaoda --help` 或 `miaoda <domain> --help` 查看。
|
|
53
54
|
|
|
55
|
+
## 打包可本地访问的静态产物
|
|
56
|
+
|
|
57
|
+
`miaoda app pack`(默认 `--mode standalone`)把当前应用构建成一份自包含静态产物,可脱离妙搭平台本地打开。
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
miaoda app pack # → dist/standalone.zip(中间目录打完即删)
|
|
61
|
+
miaoda app pack --out dist/share
|
|
62
|
+
miaoda app pack --json
|
|
63
|
+
miaoda app pack --upload-url 'https://tos.example.com/x.zip?sign=...'
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
支持 `design-html` / `html` / `vite-react` / `nestjs-react-fullstack`。页面与代码里字面以
|
|
67
|
+
`/spark/app` 开头的存储图片会经沙箱网关(localhost:8080)下载到 `images/` 并改写成相对路径。
|
|
68
|
+
|
|
69
|
+
默认输出落在 `dist/` 下是刻意的:`dist` 已在部署扫描的 EXCLUDES 与各模板 `.gitignore` 中,
|
|
70
|
+
产物不会污染部署包、不会被误提交;代价是下一次构建会清掉它。
|
|
71
|
+
|
|
72
|
+
**只保证纯静态可看** —— 接口调用会失败,不做数据快照。多路由 SPA 在 `file://` 下不可用
|
|
73
|
+
(`BrowserRouter` 匹配不上路径),需用本地 HTTP server 打开,命令会在 `warnings` 里提示。
|
|
74
|
+
|
|
75
|
+
> 与 `miaoda app export` **语义无关**:后者拉的是**其他应用**的源码归档(创意应用升级链路的只读参考物)。
|
|
76
|
+
|
|
54
77
|
## 组件 registry
|
|
55
78
|
|
|
56
79
|
`miaoda registry` 提供 shadcn 式的「组件 registry」:把 SDK 文件**拷进**当前项目(copy-in),而非作为依赖安装。registry 是独立 npm 包,按运行时分发(design-html / html → buildless runtime → `@lark-apaas/coding-registry-buildless`)。
|
|
@@ -22,6 +22,7 @@ function registerAppCommands(program, opts = {}) {
|
|
|
22
22
|
registerAppGet(appCmd);
|
|
23
23
|
registerAppUpdate(appCmd);
|
|
24
24
|
registerAppExport(appCmd);
|
|
25
|
+
registerAppPack(appCmd);
|
|
25
26
|
if (opts.includeInit) {
|
|
26
27
|
registerAppInit(appCmd);
|
|
27
28
|
registerAppSync(appCmd);
|
|
@@ -127,6 +128,10 @@ function registerAppExport(parent) {
|
|
|
127
128
|
.option('--out <dir>', '输出目录(相对当前目录)', 'source_package/creative')
|
|
128
129
|
.option('--checkpoint <n>', '按检查点版本导出;缺省为最新提交(HEAD)')
|
|
129
130
|
.addHelpText('after', `
|
|
131
|
+
与 'miaoda app pack' 的区别(**两个命令语义无关,别选错**)
|
|
132
|
+
app export 拉「其他应用」的源码归档(本命令)
|
|
133
|
+
app pack 把「当前应用」打包成产物(默认 standalone 形态)
|
|
134
|
+
|
|
130
135
|
说明
|
|
131
136
|
从服务端导出源应用(可以不是当前应用)的源码归档并解压到 --out 目录,导出完成后写
|
|
132
137
|
--out/.upgrade-manifest.json(含 sourceAppId / 导出时间 / 文件数),供升级链路幂等
|
|
@@ -173,6 +178,82 @@ JSON 输出
|
|
|
173
178
|
});
|
|
174
179
|
}));
|
|
175
180
|
}
|
|
181
|
+
function registerAppPack(parent) {
|
|
182
|
+
const cmd = parent
|
|
183
|
+
.command('pack')
|
|
184
|
+
.description('打包应用产物。默认 standalone 模式:自包含静态产物(目录 + zip),可脱离妙搭平台本地访问')
|
|
185
|
+
.option('--dir <path>', '项目目录,默认 cwd(需含 .spark/meta.json)')
|
|
186
|
+
.option('--mode <mode>', `打包形态(${index_1.PACK_MODES.join(' / ')})`, index_1.DEFAULT_PACK_MODE)
|
|
187
|
+
.option('--out <dir>', '产物路径(相对项目根),实际产出 <out>.zip', index_1.DEFAULT_PACK_OUT)
|
|
188
|
+
.option('--upload-url <url>', '打包后把 zip 以 PUT 上传到该预签名 URL(http/https);上传成功后清掉本地产物')
|
|
189
|
+
.addHelpText('after', `
|
|
190
|
+
与 'miaoda app export' 的区别(**两个命令语义无关,别选错**)
|
|
191
|
+
app export 拉「其他应用」的源码归档(创意应用升级链路的只读参考物)
|
|
192
|
+
app pack 把「当前应用」打包成产物(本命令)
|
|
193
|
+
|
|
194
|
+
打包形态(--mode)
|
|
195
|
+
standalone(默认) 自包含静态产物,可脱离妙搭平台本地访问
|
|
196
|
+
后续新增形态时在此列出;不传 --mode 的行为永远等同 standalone。
|
|
197
|
+
|
|
198
|
+
支持的 stack
|
|
199
|
+
design-html / html / vite-react / nestjs-react-fullstack
|
|
200
|
+
|
|
201
|
+
产物
|
|
202
|
+
<out>.zip **唯一交付物**。内含 index.html + assets/ + images/
|
|
203
|
+
(+ static/,vite 系有 shared/static 时)
|
|
204
|
+
<out>/ 打包用的中间目录,**打完包即删除**——它与 zip 内容重复,
|
|
205
|
+
留着占双份磁盘。要预览请先解压。
|
|
206
|
+
传了 --upload-url 时,zip 会以 PUT 裸 body(Content-Type: application/zip)
|
|
207
|
+
上传到该地址——预签名 URL 的标准形态,鉴权由 URL 自带签名承担,不加额外 header。
|
|
208
|
+
**上传成功后本地 zip 也会被清掉**(目录本就已删),交付物以远端为准,
|
|
209
|
+
JSON 里 localRemoved=true。上传失败则保留 zip,便于修正后重跑或手动上传。
|
|
210
|
+
默认 <out> 是 dist/standalone —— 落在 dist/ 下是刻意的:dist 已在部署扫描的
|
|
211
|
+
EXCLUDES 与各模板 .gitignore 中,产物不会污染部署包、不会被误提交。
|
|
212
|
+
代价:下一次构建会 rm -rf dist,导出产物是易失的。
|
|
213
|
+
|
|
214
|
+
图片本地化
|
|
215
|
+
只处理页面/代码里字面以 /spark/app 开头的 URL,经沙箱网关(localhost:8080)下载到
|
|
216
|
+
<out>/images/ 并改写成相对路径。其它挂载前缀(/spark/p/、/app/<id>、反代)不处理。
|
|
217
|
+
接口运行时返回的 download_url 属动态 URL,静态扫描覆盖不到。
|
|
218
|
+
|
|
219
|
+
已知限制
|
|
220
|
+
只保证纯静态可看——接口调用会失败,不做数据快照。
|
|
221
|
+
多路由 SPA 在 file:// 下不可用(BrowserRouter 匹配不上路径、pushState 抛 SecurityError),
|
|
222
|
+
请用本地 HTTP server 打开;检测到时会在 warnings 里给 SPA_ROUTER_FILE_PROTOCOL。
|
|
223
|
+
design-html 的 cd-elements SDK 是 CDN 外链,断网打开自定义元素不渲染。
|
|
224
|
+
|
|
225
|
+
JSON 输出
|
|
226
|
+
{"data": {"appId": "...", "stack": "...", "out": "dist/standalone", "zip": "dist/standalone.zip",
|
|
227
|
+
"fileCount": N, "bytes": N,
|
|
228
|
+
"images": {"matched": N, "downloaded": N, "failed": [{"url": "...", "reason": "..."}]},
|
|
229
|
+
"localRemoved": false,
|
|
230
|
+
"rewrittenFiles": [...],
|
|
231
|
+
"warnings": [{"code": "...", "message": "..."}]}}
|
|
232
|
+
|
|
233
|
+
失败
|
|
234
|
+
单张图片下载失败 → 跳过并计入 images.failed + warnings,退出码 0。
|
|
235
|
+
命中 > 0 但全部失败 → PACK_IMAGE_ALL_FAILED,退出码 1(系统性问题)。
|
|
236
|
+
stack 不支持 → PACK_STACK_UNSUPPORTED。
|
|
237
|
+
构建失败 → PACK_BUILD_FAILED。
|
|
238
|
+
上传失败(非 2xx / 网络异常)→ PACK_UPLOAD_FAILED,退出码 1。
|
|
239
|
+
本地产物保留,可修正 URL 后重跑或手动上传。
|
|
240
|
+
--upload-url 非 http/https → ARGS_INVALID,退出码 2。
|
|
241
|
+
|
|
242
|
+
示例
|
|
243
|
+
$ miaoda app pack
|
|
244
|
+
$ miaoda app pack --out dist/share
|
|
245
|
+
$ miaoda app pack --json
|
|
246
|
+
$ miaoda app pack --upload-url 'https://tos.example.com/x.zip?sign=...'
|
|
247
|
+
`);
|
|
248
|
+
cmd.action((0, shared_1.withHelp)(cmd, async (rawOpts) => {
|
|
249
|
+
await (0, index_1.handleAppPack)({
|
|
250
|
+
dir: rawOpts.dir,
|
|
251
|
+
out: rawOpts.out,
|
|
252
|
+
uploadUrl: rawOpts.uploadUrl,
|
|
253
|
+
mode: rawOpts.mode,
|
|
254
|
+
});
|
|
255
|
+
}));
|
|
256
|
+
}
|
|
176
257
|
function registerAppGet(parent) {
|
|
177
258
|
const cmd = parent
|
|
178
259
|
.command('get')
|
|
@@ -72,7 +72,7 @@ JSON 输出(stdout)
|
|
|
72
72
|
}));
|
|
73
73
|
const patchCmd = deployCmd
|
|
74
74
|
.command('patch')
|
|
75
|
-
.description('design-html 增量发布:按文件 create/update/delete,.html
|
|
75
|
+
.description('design-html 增量发布:按文件 create/update/delete,.html 变动时同步 routes.json')
|
|
76
76
|
.option('--create <relpath>', '新增文件(可重复)', shared_1.collectRepeatedOption, [])
|
|
77
77
|
.option('--update <relpath>', '覆盖已有文件(可重复)', shared_1.collectRepeatedOption, [])
|
|
78
78
|
.option('--delete <relpath>', '删除文件(可重复)', shared_1.collectRepeatedOption, [])
|
|
@@ -80,10 +80,11 @@ JSON 输出(stdout)
|
|
|
80
80
|
.addHelpText('after', `
|
|
81
81
|
--dir 为项目目录(与 deploy 同名参数,默认当前目录)。
|
|
82
82
|
仅支持 design-html(design_local_deploy)。路径为工程根相对路径(= 服务端 latest 下 key),
|
|
83
|
-
禁绝对路径与 ".."。--create/--update
|
|
84
|
-
时自动重算并随包更新 routes.json
|
|
85
|
-
routes.json 每项为 {path, file}:path 为相对路径(不含 base 前缀,由消费侧拼接),
|
|
86
|
-
|
|
83
|
+
禁绝对路径与 ".."。--create/--update 的本地文件必须存在。改动里含任意 .html(增 / 改 / 删)
|
|
84
|
+
时自动重算并随包更新 routes.json;纯非 .html 改动不动 routes.json。
|
|
85
|
+
routes.json 每项为 {path, file, name?}:path 为相对路径(不含 base 前缀,由消费侧拼接),
|
|
86
|
+
file 为对应源文件(如 / → index.html),name 为该 HTML 的 <title> 文本(方案名,可缺省,
|
|
87
|
+
缺省时由消费侧 fallback 到 path)。
|
|
87
88
|
|
|
88
89
|
JSON 输出
|
|
89
90
|
{"data": {"upsertCount": <n>, "deleteCount": <n>, "actionsSent": <n>, "routesRegenerated": <bool>}}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SUPPORTED_STACKS = exports.handleAppMigrate = exports.UPGRADE_MANIFEST_FILENAME = exports.DEFAULT_EXPORT_OUT = exports.handleAppExport = exports.handleAppUpgrade = exports.handleAppSync = exports.handleAppInit = exports.handleAppUpdate = exports.handleAppGet = void 0;
|
|
3
|
+
exports.PACK_MODES = exports.DEFAULT_PACK_MODE = exports.DEFAULT_PACK_OUT = exports.handleAppPack = exports.SUPPORTED_STACKS = exports.handleAppMigrate = exports.UPGRADE_MANIFEST_FILENAME = exports.DEFAULT_EXPORT_OUT = exports.handleAppExport = exports.handleAppUpgrade = exports.handleAppSync = exports.handleAppInit = exports.handleAppUpdate = exports.handleAppGet = void 0;
|
|
4
4
|
var get_1 = require("./get");
|
|
5
5
|
Object.defineProperty(exports, "handleAppGet", { enumerable: true, get: function () { return get_1.handleAppGet; } });
|
|
6
6
|
var update_1 = require("./update");
|
|
@@ -19,3 +19,8 @@ Object.defineProperty(exports, "handleAppMigrate", { enumerable: true, get: func
|
|
|
19
19
|
// commands 层渲染 help 时需要这份枚举;从 handler barrel 转发,避免 commands → services 越界
|
|
20
20
|
var index_1 = require("../../../services/app/init/index");
|
|
21
21
|
Object.defineProperty(exports, "SUPPORTED_STACKS", { enumerable: true, get: function () { return index_1.SUPPORTED_STACKS; } });
|
|
22
|
+
var pack_1 = require("./pack");
|
|
23
|
+
Object.defineProperty(exports, "handleAppPack", { enumerable: true, get: function () { return pack_1.handleAppPack; } });
|
|
24
|
+
Object.defineProperty(exports, "DEFAULT_PACK_OUT", { enumerable: true, get: function () { return pack_1.DEFAULT_PACK_OUT; } });
|
|
25
|
+
Object.defineProperty(exports, "DEFAULT_PACK_MODE", { enumerable: true, get: function () { return pack_1.DEFAULT_PACK_MODE; } });
|
|
26
|
+
Object.defineProperty(exports, "PACK_MODES", { enumerable: true, get: function () { return pack_1.PACK_MODES; } });
|
|
@@ -0,0 +1,256 @@
|
|
|
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
|
+
exports.DEFAULT_PACK_OUT = exports.DEFAULT_PACK_MODE = exports.PACK_MODES = void 0;
|
|
7
|
+
exports.handleAppPack = handleAppPack;
|
|
8
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const exclude_patterns_1 = require("../../../utils/exclude-patterns");
|
|
11
|
+
const spark_meta_1 = require("../../../utils/spark-meta");
|
|
12
|
+
const error_1 = require("../../../utils/error");
|
|
13
|
+
const output_1 = require("../../../utils/output");
|
|
14
|
+
const logger_1 = require("../../../utils/logger");
|
|
15
|
+
const index_1 = require("../../../services/app/pack/index");
|
|
16
|
+
/**
|
|
17
|
+
* 支持的打包形态。当前只有 `standalone`;留成枚举是为了让扩展点显式可见 ——
|
|
18
|
+
* 新增形态时只需往这里加,命令 help 会自动列出。
|
|
19
|
+
*
|
|
20
|
+
* ⚠️ 不传 `--mode` 的行为永远等同 `standalone`,这是对调用方的稳定契约:
|
|
21
|
+
* 将来加了新形态也不能改默认值,否则存量调用会静默换语义。
|
|
22
|
+
*/
|
|
23
|
+
exports.PACK_MODES = ['standalone'];
|
|
24
|
+
exports.DEFAULT_PACK_MODE = 'standalone';
|
|
25
|
+
/** standalone 形态的默认输出目录。新增形态时各自用自己的目录名。 */
|
|
26
|
+
exports.DEFAULT_PACK_OUT = 'dist/standalone';
|
|
27
|
+
/** 校验相对输出路径:禁绝对路径、禁含 ".." 段(与 app export 的 normalizeOutDir 同口径)。 */
|
|
28
|
+
function normalizeOutDir(rel) {
|
|
29
|
+
if (node_path_1.default.isAbsolute(rel) || rel.startsWith('/')) {
|
|
30
|
+
throw new error_1.AppError('ARGS_INVALID', `输出目录不能是绝对路径:${rel}`);
|
|
31
|
+
}
|
|
32
|
+
const posix = rel.split(node_path_1.default.sep).join('/').replace(/^\.\//, '');
|
|
33
|
+
if (posix.split('/').includes('..')) {
|
|
34
|
+
throw new error_1.AppError('ARGS_INVALID', `输出目录不能包含 "..":${rel}`);
|
|
35
|
+
}
|
|
36
|
+
return posix;
|
|
37
|
+
}
|
|
38
|
+
/** 沙箱网关地址:它才是代理 `/spark/app` 的那个服务(响应头 `Server: openresty`)。 */
|
|
39
|
+
const SANDBOX_GATEWAY_BASE = 'http://localhost:8080';
|
|
40
|
+
/** 遍历产物树里的文本文件,返回 posix 相对路径。 */
|
|
41
|
+
function* walkText(dir, rel = '') {
|
|
42
|
+
for (const entry of node_fs_1.default.readdirSync(dir, { withFileTypes: true })) {
|
|
43
|
+
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
44
|
+
const abs = node_path_1.default.join(dir, entry.name);
|
|
45
|
+
if (entry.isDirectory())
|
|
46
|
+
yield* walkText(abs, childRel);
|
|
47
|
+
else if (index_1.TEXT_EXTENSIONS.has(node_path_1.default.extname(entry.name).toLowerCase()))
|
|
48
|
+
yield childRel;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** 统计产物树的文件数与总字节数。 */
|
|
52
|
+
function measure(dir) {
|
|
53
|
+
let fileCount = 0;
|
|
54
|
+
let bytes = 0;
|
|
55
|
+
const walk = (cur) => {
|
|
56
|
+
for (const entry of node_fs_1.default.readdirSync(cur, { withFileTypes: true })) {
|
|
57
|
+
const abs = node_path_1.default.join(cur, entry.name);
|
|
58
|
+
if (entry.isDirectory())
|
|
59
|
+
walk(abs);
|
|
60
|
+
else {
|
|
61
|
+
fileCount += 1;
|
|
62
|
+
bytes += node_fs_1.default.statSync(abs).size;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
walk(dir);
|
|
67
|
+
return { fileCount, bytes };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 扫**项目源码**里是否用了 BrowserRouter。
|
|
71
|
+
*
|
|
72
|
+
* preset 在 standalone 下会把它编译期换成 HashRouter(`standaloneRouterPlugin`),
|
|
73
|
+
* 换完 file:// 下路由完全可用。这里保留检测是为了**覆盖不到时给提示**:插件只替换
|
|
74
|
+
* 用户源码里字面的 `BrowserRouter` / `createBrowserRouter`,碰不到的情况有——
|
|
75
|
+
* 用户二次封装(`export const Router = BrowserRouter`)、非 react-router 的路由库、
|
|
76
|
+
* 代码里硬编码 `window.location.href = '/about'`。
|
|
77
|
+
*
|
|
78
|
+
* 命中时只是提示"若路由异常请用本地 server",不代表一定坏。
|
|
79
|
+
*
|
|
80
|
+
* ⚠️ **必须扫源码,不能扫产物**:编译型 stack 的产物经过 minify,`BrowserRouter`
|
|
81
|
+
* 这个标识符会被改名,扫产物永远命中不了 —— 而那恰恰是最需要这条警告的场景。
|
|
82
|
+
* 真机踩过:vite-react 应用源码里明明有 BrowserRouter、产物首屏渲染成 404,
|
|
83
|
+
* 但因为扫的是产物,warnings 是空的。
|
|
84
|
+
*
|
|
85
|
+
* 扫描面套 EXCLUDES(跳过 node_modules / dist 等),四个 stack 统一按项目根扫。
|
|
86
|
+
*/
|
|
87
|
+
function detectBrowserRouter(projectDir) {
|
|
88
|
+
const walk = (dir) => {
|
|
89
|
+
for (const entry of node_fs_1.default.readdirSync(dir, { withFileTypes: true })) {
|
|
90
|
+
if (exclude_patterns_1.EXCLUDES.has(entry.name))
|
|
91
|
+
continue;
|
|
92
|
+
const abs = node_path_1.default.join(dir, entry.name);
|
|
93
|
+
let isDir;
|
|
94
|
+
try {
|
|
95
|
+
isDir = node_fs_1.default.statSync(abs).isDirectory();
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (isDir) {
|
|
101
|
+
if (walk(abs))
|
|
102
|
+
return true;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (!index_1.TEXT_EXTENSIONS.has(node_path_1.default.extname(entry.name).toLowerCase()))
|
|
106
|
+
continue;
|
|
107
|
+
if (/\b(BrowserRouter|createBrowserRouter)\b/.test(node_fs_1.default.readFileSync(abs, 'utf-8'))) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
};
|
|
113
|
+
return walk(projectDir);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* miaoda app pack [--mode standalone] [--out <dir>]
|
|
117
|
+
*
|
|
118
|
+
* 把当前应用构建成自包含静态产物:按 stack 走各自的 strategy 产出可静态服务的目录树,
|
|
119
|
+
* 再扫出 `/spark/app` 图片经 dev server 代理下载到 `images/` 并改写成相对路径,最后打 zip。
|
|
120
|
+
*
|
|
121
|
+
* 设计文档:docs/superpowers/specs/2026-08-19-app-pack-design.md
|
|
122
|
+
*/
|
|
123
|
+
async function handleAppPack(opts) {
|
|
124
|
+
const mode = opts.mode ?? exports.DEFAULT_PACK_MODE;
|
|
125
|
+
if (!exports.PACK_MODES.includes(mode)) {
|
|
126
|
+
throw new error_1.AppError('ARGS_INVALID', `不支持的打包形态:${mode}`, {
|
|
127
|
+
next_actions: [`可用形态:${exports.PACK_MODES.join(', ')}`],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const projectDir = node_path_1.default.resolve(opts.dir ?? process.cwd());
|
|
131
|
+
const meta = (0, spark_meta_1.readSparkMeta)(projectDir);
|
|
132
|
+
if (!meta.stack) {
|
|
133
|
+
throw new error_1.AppError('ARGS_INVALID', '.spark/meta.json 缺少 stack —— 请先跑 miaoda app init', {
|
|
134
|
+
next_actions: ['cd 到应用根目录后重试,或先执行 miaoda app init'],
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
const strategy = (0, index_1.resolveStrategy)(meta.stack);
|
|
138
|
+
// 参数校验一律前置:URL 写错不该等到构建 + 下载 + 打包跑完才报(那是几十秒)
|
|
139
|
+
if (opts.uploadUrl)
|
|
140
|
+
(0, index_1.assertValidUploadUrl)(opts.uploadUrl);
|
|
141
|
+
const outRel = normalizeOutDir(opts.out ?? exports.DEFAULT_PACK_OUT);
|
|
142
|
+
const outDir = node_path_1.default.join(projectDir, outRel);
|
|
143
|
+
const zipRel = `${outRel}.zip`;
|
|
144
|
+
const zipPath = node_path_1.default.join(projectDir, zipRel);
|
|
145
|
+
// 1. stack-specific:产出可静态服务的目录树
|
|
146
|
+
strategy({ projectDir, outDir });
|
|
147
|
+
if (!node_fs_1.default.existsSync(outDir) || node_fs_1.default.readdirSync(outDir).length === 0) {
|
|
148
|
+
throw new error_1.AppError('PACK_EMPTY_OUTPUT', '构建产物为空', {
|
|
149
|
+
next_actions: ['先手动跑一次该 stack 的构建命令确认工程本身可构建'],
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
// 2. 扫描
|
|
153
|
+
const urls = [];
|
|
154
|
+
const seen = new Set();
|
|
155
|
+
for (const rel of walkText(outDir)) {
|
|
156
|
+
for (const url of (0, index_1.scanSparkAppUrls)(node_fs_1.default.readFileSync(node_path_1.default.join(outDir, rel), 'utf-8'))) {
|
|
157
|
+
if (seen.has(url))
|
|
158
|
+
continue;
|
|
159
|
+
seen.add(url);
|
|
160
|
+
urls.push(url);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// 3. 下载
|
|
164
|
+
const devServerBase = SANDBOX_GATEWAY_BASE;
|
|
165
|
+
(0, logger_1.log)('pack', `Matched ${String(urls.length)} image url(s), fetching via ${devServerBase}`);
|
|
166
|
+
const { mapping, failed } = await (0, index_1.downloadImages)(urls, {
|
|
167
|
+
imagesDir: node_path_1.default.join(outDir, 'images'),
|
|
168
|
+
devServerBase,
|
|
169
|
+
});
|
|
170
|
+
// 命中 > 0 且全部失败 = 系统性问题(代理挂了 / 路径口径错),静默成功会误导调用方
|
|
171
|
+
if (urls.length > 0 && mapping.size === 0) {
|
|
172
|
+
throw new error_1.AppError('PACK_IMAGE_ALL_FAILED', `命中 ${String(urls.length)} 张图片但全部下载失败`, {
|
|
173
|
+
next_actions: [
|
|
174
|
+
'确认 dev server 正在运行且能代理 /spark/app 路径',
|
|
175
|
+
`首个失败:${failed[0]?.url ?? '?'} → ${failed[0]?.reason ?? 'unknown'}`,
|
|
176
|
+
...(failed.length > 1
|
|
177
|
+
? [`其余 ${String(failed.length - 1)} 条见 --verbose(每条请求地址都会打到 stderr)`]
|
|
178
|
+
: []),
|
|
179
|
+
`实际请求前缀:${devServerBase}(沙箱网关)`,
|
|
180
|
+
],
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
// 4. 改写
|
|
184
|
+
const rewrittenFiles = (0, index_1.rewriteTree)(outDir, mapping);
|
|
185
|
+
// 5. 统计。**必须在打包前做**——目录随后就被删掉,那时量不到了。
|
|
186
|
+
const { fileCount, bytes } = measure(outDir);
|
|
187
|
+
// 6. 打包,随后删掉目录。目录是**中间态**,zip 才是交付物;两者内容重复,
|
|
188
|
+
// 留着等于占双份磁盘。预览需先解压。
|
|
189
|
+
(0, index_1.zipDir)(outDir, zipPath);
|
|
190
|
+
node_fs_1.default.rmSync(outDir, { recursive: true, force: true });
|
|
191
|
+
(0, logger_1.log)('pack', `Packed ${zipRel}, staging dir removed`);
|
|
192
|
+
// 7. 上传(可选)。放在 emit 之前——传了 --upload-url 就说明上传是目的,
|
|
193
|
+
// 失败要让整个命令失败(退出码 1),不能先 emit 成功再抛。
|
|
194
|
+
// JSON 输出里**不回显 URL**(凭证类信息),只给状态码与服务端 log_id。
|
|
195
|
+
const upload = opts.uploadUrl ? await (0, index_1.uploadZip)(zipPath, opts.uploadUrl) : undefined;
|
|
196
|
+
const warnings = [];
|
|
197
|
+
// 8. 上传成功后清掉本地产物:交付物已在远端,沙箱磁盘不必留一份目录 + 一份 zip。
|
|
198
|
+
//
|
|
199
|
+
// **只在上传成功后清**。上传失败时产物必须留着——错误提示里明确写了「本地产物已保留,
|
|
200
|
+
// 可修正 --upload-url 后重跑,或手动上传」,清掉会让那句话变成谎话,且用户得重跑
|
|
201
|
+
// 整轮构建。失败路径走 uploadZip 抛错,根本到不了这里。
|
|
202
|
+
//
|
|
203
|
+
// 清理失败不算命令失败:上传已经成功,目的达到了,删不掉只是留了点垃圾 → 降级为 warning。
|
|
204
|
+
let localRemoved = false;
|
|
205
|
+
if (upload) {
|
|
206
|
+
try {
|
|
207
|
+
// 目录在打包后已删,这里只需清 zip
|
|
208
|
+
node_fs_1.default.rmSync(zipPath, { force: true });
|
|
209
|
+
localRemoved = true;
|
|
210
|
+
(0, logger_1.log)('pack', `Uploaded, local zip removed (${zipRel})`);
|
|
211
|
+
}
|
|
212
|
+
catch (err) {
|
|
213
|
+
warnings.push({
|
|
214
|
+
code: 'LOCAL_CLEANUP_FAILED',
|
|
215
|
+
message: `上传成功但本地产物清理失败:${err instanceof Error ? err.message : String(err)}`,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (failed.length > 0) {
|
|
220
|
+
warnings.push({
|
|
221
|
+
code: 'IMAGE_DOWNLOAD_PARTIAL_FAILURE',
|
|
222
|
+
message: `${String(failed.length)} 张图片下载失败,产物中保留原始绝对路径(离线打不开)`,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (detectBrowserRouter(projectDir)) {
|
|
226
|
+
warnings.push({
|
|
227
|
+
code: 'SPA_ROUTER_FILE_PROTOCOL',
|
|
228
|
+
message: '检测到 BrowserRouter:standalone 构建已自动换成 HashRouter,路由形如 index.html#/about。' +
|
|
229
|
+
'若你对 router 做过二次封装或用了非 react-router 的方案,替换可能覆盖不到 —— ' +
|
|
230
|
+
'此时产物在 file:// 下路由会失效(首屏落 404 页),请改用本地 HTTP server 并从 / 访问。',
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
(0, output_1.emit)({
|
|
234
|
+
data: {
|
|
235
|
+
appId: meta.app_id ?? '',
|
|
236
|
+
stack: meta.stack,
|
|
237
|
+
mode,
|
|
238
|
+
out: outRel,
|
|
239
|
+
zip: zipRel,
|
|
240
|
+
// ⚠️ out 指向的**目录在打包后即被删除**(它只是中间态),永远不要去读它。
|
|
241
|
+
// zip 是本地交付物;localRemoved=true 时连 zip 也被清了(已上传到远端)。
|
|
242
|
+
localRemoved,
|
|
243
|
+
fileCount,
|
|
244
|
+
bytes,
|
|
245
|
+
images: {
|
|
246
|
+
matched: urls.length,
|
|
247
|
+
downloaded: mapping.size,
|
|
248
|
+
failed,
|
|
249
|
+
},
|
|
250
|
+
rewrittenFiles,
|
|
251
|
+
// 只在传了 --upload-url 时出现:{ status, logId? }。刻意不含 URL。
|
|
252
|
+
...(upload ? { upload } : {}),
|
|
253
|
+
warnings,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
exports.zipDir = zipDir;
|
|
7
|
+
const node_child_process_1 = require("node:child_process");
|
|
8
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const error_1 = require("../../../utils/error");
|
|
11
|
+
/**
|
|
12
|
+
* 把 `dir` 的**内容**打成 zip(解压后直接是 index.html,不多一层目录)。
|
|
13
|
+
*
|
|
14
|
+
* 走系统 `zip` 而非引 npm 依赖:仓库现有的 `app export` 也是 shell 出去调 `unzip`/`tar`,
|
|
15
|
+
* 保持一致,且避免为一个收尾动作增加运行时依赖。
|
|
16
|
+
*
|
|
17
|
+
* 覆盖语义:`zip` 默认是**增量更新**已存在的归档(会残留上次的文件),
|
|
18
|
+
* 所以先删掉目标文件再打,保证重跑导出幂等。
|
|
19
|
+
*/
|
|
20
|
+
function zipDir(dir, zipPath) {
|
|
21
|
+
if (!node_fs_1.default.existsSync(dir)) {
|
|
22
|
+
throw new error_1.AppError('PACK_ARCHIVE_FAILED', `待打包目录不存在:${dir}`);
|
|
23
|
+
}
|
|
24
|
+
node_fs_1.default.rmSync(zipPath, { force: true });
|
|
25
|
+
node_fs_1.default.mkdirSync(node_path_1.default.dirname(zipPath), { recursive: true });
|
|
26
|
+
try {
|
|
27
|
+
// cwd=dir + '.' → 归档内是目录内容而非目录本身
|
|
28
|
+
(0, node_child_process_1.execFileSync)('zip', ['-qr', zipPath, '.'], { cwd: dir, stdio: 'pipe' });
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
throw new error_1.AppError('PACK_ARCHIVE_FAILED', `zip failed: ${err.message}`, {
|
|
32
|
+
next_actions: ['确认运行环境有 zip 命令(沙箱镜像应自带)'],
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
exports.copyTree = copyTree;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const error_1 = require("../../../utils/error");
|
|
10
|
+
/**
|
|
11
|
+
* 递归拷贝目录树到 `out`(先清空 out)。
|
|
12
|
+
*
|
|
13
|
+
* `design-html` 与 `html` 共用:两者的 standalone 构建都只是纯文件搬运,
|
|
14
|
+
* 差别仅在 `root`(项目根 vs `src/`)与 `excludes`(EXCLUDES vs 无)。
|
|
15
|
+
*
|
|
16
|
+
* 用 `statSync` 判目录以 **follow 符号链接**,与 `source-scan.ts` 的 `listSourceFiles`
|
|
17
|
+
* 同口径(design-html 的 `.claude/skills` 是指向 `.agents/skills` 的软链)。
|
|
18
|
+
*/
|
|
19
|
+
function copyTree(root, out, excludes) {
|
|
20
|
+
if (!node_fs_1.default.existsSync(root)) {
|
|
21
|
+
throw new error_1.AppError('PACK_EMPTY_OUTPUT', `源目录不存在:${root}`, {
|
|
22
|
+
next_actions: ['确认已在应用根目录下执行,且项目结构完整'],
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
node_fs_1.default.rmSync(out, { recursive: true, force: true });
|
|
26
|
+
node_fs_1.default.mkdirSync(out, { recursive: true });
|
|
27
|
+
const walk = (dir, rel) => {
|
|
28
|
+
for (const entry of node_fs_1.default.readdirSync(dir, { withFileTypes: true })) {
|
|
29
|
+
if (excludes?.has(entry.name))
|
|
30
|
+
continue;
|
|
31
|
+
const abs = node_path_1.default.join(dir, entry.name);
|
|
32
|
+
const childRel = rel ? node_path_1.default.join(rel, entry.name) : entry.name;
|
|
33
|
+
let isDir;
|
|
34
|
+
try {
|
|
35
|
+
isDir = node_fs_1.default.statSync(abs).isDirectory();
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
continue; // 断链等无法 stat 的条目跳过
|
|
39
|
+
}
|
|
40
|
+
if (isDir) {
|
|
41
|
+
walk(abs, childRel);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const dest = node_path_1.default.join(out, childRel);
|
|
45
|
+
node_fs_1.default.mkdirSync(node_path_1.default.dirname(dest), { recursive: true });
|
|
46
|
+
node_fs_1.default.copyFileSync(abs, dest);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
walk(root, '');
|
|
51
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
exports.downloadImages = downloadImages;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const logger_1 = require("../../../utils/logger");
|
|
10
|
+
const naming_1 = require("./naming");
|
|
11
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
12
|
+
/**
|
|
13
|
+
* 经沙箱 dev server 代理批量下载图片。
|
|
14
|
+
*
|
|
15
|
+
* **单张失败不抛**——记进 `failed` 继续走。整体导出的成败判定由 handler 依据
|
|
16
|
+
* 「命中数 / 成功数」决定(设计文档「失败口径」:部分失败退 0,全失败退 1)。
|
|
17
|
+
*
|
|
18
|
+
* 串行下载:图片量级通常是个位数到几十,并发收益有限,而串行的日志与失败归因更清晰。
|
|
19
|
+
*/
|
|
20
|
+
async function downloadImages(urls, opts) {
|
|
21
|
+
const mapping = new Map();
|
|
22
|
+
const failed = [];
|
|
23
|
+
if (urls.length === 0)
|
|
24
|
+
return { mapping, failed };
|
|
25
|
+
node_fs_1.default.mkdirSync(opts.imagesDir, { recursive: true });
|
|
26
|
+
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
27
|
+
for (const url of urls) {
|
|
28
|
+
const target = `${opts.devServerBase}${url}`;
|
|
29
|
+
(0, logger_1.debug)(`pack: GET ${target}`);
|
|
30
|
+
try {
|
|
31
|
+
// 刻意串行(见函数注释):图片量级通常个位数到几十,并发收益有限;
|
|
32
|
+
// 串行能让每张图的失败归因清晰,且不对沙箱 dev server 造成并发压力。
|
|
33
|
+
const resp = await fetch(target, { signal: AbortSignal.timeout(timeoutMs) });
|
|
34
|
+
if (!resp.ok) {
|
|
35
|
+
// resp.url 是**跟随重定向后**的最终地址。沙箱代理对存储图返回 302 指向签名 CDN,
|
|
36
|
+
// 失败发生在哪一跳,只看状态码分不出来,必须把最终地址带出来。
|
|
37
|
+
const finalUrl = resp.url && resp.url !== target ? ` (final: ${resp.url})` : '';
|
|
38
|
+
failed.push({ url, reason: `HTTP ${String(resp.status)}${finalUrl}` });
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const buf = Buffer.from(await resp.arrayBuffer());
|
|
42
|
+
const name = (0, naming_1.localImageName)(url, resp.headers.get('content-type') ?? undefined);
|
|
43
|
+
node_fs_1.default.writeFileSync(node_path_1.default.join(opts.imagesDir, name), buf);
|
|
44
|
+
mapping.set(url, name);
|
|
45
|
+
(0, logger_1.debug)(`pack: downloaded ${url} -> images/${name} (${String(buf.length)}B)`);
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
failed.push({ url, reason: err instanceof Error ? err.message : String(err) });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { mapping, failed };
|
|
52
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PACK_SUPPORTED_STACKS = exports.resolveStrategy = exports.buildStandaloneEnv = exports.uploadZip = exports.redactUrl = exports.assertValidUploadUrl = exports.scanSparkAppUrls = exports.TEXT_EXTENSIONS = exports.rewriteTree = exports.localImageName = exports.extFromContentType = exports.inlineBabelScripts = exports.downloadImages = exports.copyTree = exports.zipDir = void 0;
|
|
4
|
+
var archive_1 = require("./archive");
|
|
5
|
+
Object.defineProperty(exports, "zipDir", { enumerable: true, get: function () { return archive_1.zipDir; } });
|
|
6
|
+
var copy_tree_1 = require("./copy-tree");
|
|
7
|
+
Object.defineProperty(exports, "copyTree", { enumerable: true, get: function () { return copy_tree_1.copyTree; } });
|
|
8
|
+
var download_1 = require("./download");
|
|
9
|
+
Object.defineProperty(exports, "downloadImages", { enumerable: true, get: function () { return download_1.downloadImages; } });
|
|
10
|
+
var inline_babel_1 = require("./inline-babel");
|
|
11
|
+
Object.defineProperty(exports, "inlineBabelScripts", { enumerable: true, get: function () { return inline_babel_1.inlineBabelScripts; } });
|
|
12
|
+
var naming_1 = require("./naming");
|
|
13
|
+
Object.defineProperty(exports, "extFromContentType", { enumerable: true, get: function () { return naming_1.extFromContentType; } });
|
|
14
|
+
Object.defineProperty(exports, "localImageName", { enumerable: true, get: function () { return naming_1.localImageName; } });
|
|
15
|
+
var rewrite_1 = require("./rewrite");
|
|
16
|
+
Object.defineProperty(exports, "rewriteTree", { enumerable: true, get: function () { return rewrite_1.rewriteTree; } });
|
|
17
|
+
Object.defineProperty(exports, "TEXT_EXTENSIONS", { enumerable: true, get: function () { return rewrite_1.TEXT_EXTENSIONS; } });
|
|
18
|
+
var scan_1 = require("./scan");
|
|
19
|
+
Object.defineProperty(exports, "scanSparkAppUrls", { enumerable: true, get: function () { return scan_1.scanSparkAppUrls; } });
|
|
20
|
+
var upload_1 = require("./upload");
|
|
21
|
+
Object.defineProperty(exports, "assertValidUploadUrl", { enumerable: true, get: function () { return upload_1.assertValidUploadUrl; } });
|
|
22
|
+
Object.defineProperty(exports, "redactUrl", { enumerable: true, get: function () { return upload_1.redactUrl; } });
|
|
23
|
+
Object.defineProperty(exports, "uploadZip", { enumerable: true, get: function () { return upload_1.uploadZip; } });
|
|
24
|
+
var strategies_1 = require("./strategies");
|
|
25
|
+
Object.defineProperty(exports, "buildStandaloneEnv", { enumerable: true, get: function () { return strategies_1.buildStandaloneEnv; } });
|
|
26
|
+
Object.defineProperty(exports, "resolveStrategy", { enumerable: true, get: function () { return strategies_1.resolveStrategy; } });
|
|
27
|
+
Object.defineProperty(exports, "PACK_SUPPORTED_STACKS", { enumerable: true, get: function () { return strategies_1.PACK_SUPPORTED_STACKS; } });
|