@wkgeo/cli 2.0.2 → 2.1.0
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/CHANGELOG.md +10 -2
- package/README.md +28 -7
- package/SKILL.md +1038 -1015
- package/lib/api.mjs +351 -351
- package/lib/args.mjs +235 -235
- package/lib/build.mjs +1047 -0
- package/lib/cms.mjs +328 -328
- package/lib/commands/auth.mjs +1280 -1280
- package/lib/commands/data.mjs +694 -694
- package/lib/commands/index.mjs +33 -1
- package/lib/commands/init.mjs +111 -0
- package/lib/commands/orders.mjs +691 -691
- package/lib/commands/site.mjs +82 -0
- package/lib/commands/siteops.mjs +248 -0
- package/lib/commands/sites.mjs +968 -801
- package/lib/commands/spec.mjs +72 -0
- package/lib/commands/template.mjs +1484 -1484
- package/lib/config.mjs +57 -4
- package/lib/errors.mjs +446 -336
- package/lib/manifest-apply.mjs +1650 -1600
- package/lib/manifest-schema.json +480 -480
- package/lib/manifest.mjs +1106 -1106
- package/lib/runtime.mjs +767 -654
- package/lib/site-create.mjs +537 -537
- package/lib/site-credentials.mjs +157 -0
- package/lib/site-meta.mjs +474 -0
- package/lib/site-spec.mjs +302 -0
- package/lib/site-template.mjs +405 -0
- package/lib/template-gen.mjs +1354 -1247
- package/lib/template-publish.mjs +696 -696
- package/lib/templates-sync.mjs +676 -644
- package/lib/workspace-sync.mjs +386 -0
- package/package.json +56 -55
- package/spec/SITE-SPEC.md +198 -0
package/lib/templates-sync.mjs
CHANGED
|
@@ -1,644 +1,676 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 模板「拉到本地改 → 推回线上」(2026-11-01 老板单)。
|
|
3
|
-
*
|
|
4
|
-
* ## 老板原话(派发单 m03286)
|
|
5
|
-
* > 「我不需要在线编辑那一块,那一块功能可以取消。如果我编辑的话,我就在我的 CLI 里面去编辑,
|
|
6
|
-
* > 本地电脑绑定客户端 + CLI。」
|
|
7
|
-
*
|
|
8
|
-
* ⇒ CMS 里的 Monaco 在线编辑器入口下线(只隐藏入口,服务端 `templateFileSave` 保留),
|
|
9
|
-
* 模板真源以后是**本地目录**:`wkgeo templates pull` 拉整包下来改,`wkgeo templates push` 推回。
|
|
10
|
-
*
|
|
11
|
-
* ## 为什么单独一个模块(不塞进 commands/template.mjs)
|
|
12
|
-
* 1. 拉 / 推是**纯文件逻辑**(落盘、sha256 对账、路径穿越防护),跟 CLI 的打印 / 参数无关,
|
|
13
|
-
* 放独立模块才能被单测直接喂假 `api`(不需要起 CLI、不需要网络);
|
|
14
|
-
* 2. 命令层只做三件事:解析参数 → 调这里 → 打印计划/结果。
|
|
15
|
-
*
|
|
16
|
-
* ## `api` 适配器(由命令层从 `ctx` 造,见 commands/template.mjs)
|
|
17
|
-
* * `get(templateId?)` → `Promise<{id,name,key,files:[{path,content,encoding?}]}>`
|
|
18
|
-
* = 服务端动作 `templateGet`(不传 id = 当前启用那套);
|
|
19
|
-
* * `list()` → `Promise<{templateId,templates:[{id,key,name}]}>` = `templateList`(只在按 key 找模板时用);
|
|
20
|
-
* * `save({path,content,encoding?,templateId?})` → `templateFileSave`。
|
|
21
|
-
*
|
|
22
|
-
* ⚠️ **服务端能力边界(本单不新增动作,这是已知缺口)**:
|
|
23
|
-
* `templateFileSave` 只收文本(`.html / .css / .js / .json / .min.css / .min.js`,≤40 万字符),
|
|
24
|
-
* 图片扩展名会被 400「模板里不放图片,图片请传到素材库」拒掉,而 actionTable 里**没有**
|
|
25
|
-
* "删模板文件"的动作。所以:
|
|
26
|
-
* * 拉(pull)支持二进制(服务端哪天返回 `encoding:'base64'` 就能按真文件还原,见 `entryBytes`);
|
|
27
|
-
* * 推(push)遇到二进制 / 服务端不收的扩展名,**逐个文件报错并跳过**(不静默、不假装成功);
|
|
28
|
-
* * `--prune` 只把"本地已删、远端还在"的文件**列出来**,不会真删(没有删除动作)。
|
|
29
|
-
* 这三条都写进 `docs/规范-模板存储与编辑.md` 的「CLI 拉取/推送用法」一节。
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
import crypto from 'node:crypto';
|
|
33
|
-
import fs from 'node:fs';
|
|
34
|
-
import path from 'node:path';
|
|
35
|
-
|
|
36
|
-
import { CliError, UsageError } from './errors.mjs';
|
|
37
|
-
|
|
38
|
-
/** manifest 文件名(放模板目录根,push 时会被跳过) */
|
|
39
|
-
export const MANIFEST_FILE = 'template-manifest.json';
|
|
40
|
-
export const MANIFEST_VERSION = 1;
|
|
41
|
-
|
|
42
|
-
/** 服务端 `templateMaxBytes = 400 * 1024`(**按字符**算);二进制按字节同口径兜住 */
|
|
43
|
-
export const MAX_FILE_CHARS = 400 * 1024;
|
|
44
|
-
export const MAX_FILE_BYTES = 400 * 1024;
|
|
45
|
-
/**
|
|
46
|
-
* 超限文件 = **不该进站点模板副本的大体积演示资产**(three.min.js / 大图等)。
|
|
47
|
-
*
|
|
48
|
-
* ⚠️ 2026-11-14 task-107:以前拉 / 推遇到超限文件**整包直接失败**,结果是
|
|
49
|
-
* 「库里本来就有的大文件把整站的拉/推全堵死」——base 站 `assets/vendor/three-0.147.0/three.min.js`
|
|
50
|
-
* (607784 字符)与闵航站 `home.jpg`(538619 字节)就是这样堵住的,其余几十个小文件一个都推不上去。
|
|
51
|
-
* 现在改成:**跳过这一个文件 + 告警**,其余文件照常拉 / 推;上限本身**不调大**(避免整站模板过大)。
|
|
52
|
-
*/
|
|
53
|
-
export function oversizeOf(filePath, buf) {
|
|
54
|
-
const rel = String(filePath || '');
|
|
55
|
-
const text = Buffer.isBuffer(buf) ? buf : Buffer.from(String(buf === undefined || buf === null ? '' : buf), 'utf8');
|
|
56
|
-
if (isBinaryPath(rel)) {
|
|
57
|
-
if (text.length > MAX_FILE_BYTES) return { reason: 'binary-bytes', measured: text.length, limit: MAX_FILE_BYTES };
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
const chars = text.toString('utf8').length;
|
|
61
|
-
if (chars > MAX_FILE_CHARS) return { reason: 'text-chars', measured: chars, limit: MAX_FILE_CHARS };
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** 超限告警文案(拉 / 推共用;不打印正文) */
|
|
66
|
-
export function oversizeWarning(rel, one) {
|
|
67
|
-
const unit = one.reason === 'binary-bytes' ? '字节' : '字符';
|
|
68
|
-
return (
|
|
69
|
-
`跳过超限文件 ${rel}(${one.measured} ${unit},上限 ${one.limit}):大体积演示资产不进站点模板副本,` +
|
|
70
|
-
'其余文件照常同步;要放它请走素材库 / 母版引用'
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** 路径上限:对齐 `template-files.js` 的 `TEMPLATE_FILE_MAX_PATH_CHARS` / 每段 64 字符 */
|
|
75
|
-
export const MAX_PATH_CHARS = 200;
|
|
76
|
-
export const MAX_SEGMENT_CHARS = 64;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 二进制扩展名(派发单点名的几种 + 常见同类)。
|
|
80
|
-
*
|
|
81
|
-
* `svg` **故意不算二进制**:它是 XML 文本,线上模板里常直接内联 / 手改。
|
|
82
|
-
*/
|
|
83
|
-
export const BINARY_EXTS = new Set([
|
|
84
|
-
'png',
|
|
85
|
-
'jpg',
|
|
86
|
-
'jpeg',
|
|
87
|
-
'webp',
|
|
88
|
-
'gif',
|
|
89
|
-
'ico',
|
|
90
|
-
'bmp',
|
|
91
|
-
'avif',
|
|
92
|
-
'woff',
|
|
93
|
-
'woff2',
|
|
94
|
-
'ttf',
|
|
95
|
-
'otf',
|
|
96
|
-
'eot',
|
|
97
|
-
'mp4',
|
|
98
|
-
'webm',
|
|
99
|
-
'mp3',
|
|
100
|
-
'wav',
|
|
101
|
-
'pdf',
|
|
102
|
-
'zip',
|
|
103
|
-
'wasm',
|
|
104
|
-
]);
|
|
105
|
-
|
|
106
|
-
/** 扫描本地目录时**不进**的目录(模板包不该有它们;进了只会把整仓 node_modules 拉进来) */
|
|
107
|
-
const SKIP_DIRS = new Set(['.git', 'node_modules', '.tmp-build']);
|
|
108
|
-
|
|
109
|
-
/** sha256(UTF-8 字节口径)—— 与服务端 `template-files.js#sha256` 对齐 */
|
|
110
|
-
export function sha256Hex(input) {
|
|
111
|
-
const buf = Buffer.isBuffer(input) ? input : Buffer.from(String(input === undefined || input === null ? '' : input), 'utf8');
|
|
112
|
-
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** 扩展名(小写,不带点);没有扩展名返回 '' */
|
|
116
|
-
export function extOf(filePath) {
|
|
117
|
-
const base = String(filePath || '').split('/').pop() || '';
|
|
118
|
-
const at = base.lastIndexOf('.');
|
|
119
|
-
if (at <= 0 || at === base.length - 1) return '';
|
|
120
|
-
return base.slice(at + 1).toLowerCase();
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function isBinaryPath(filePath) {
|
|
124
|
-
return BINARY_EXTS.has(extOf(filePath));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* 路径安全校验 —— **拉 / 推 / 读 manifest 三处都走同一个函数**,别各写一套。
|
|
129
|
-
*
|
|
130
|
-
* 拒(派发单要求):绝对路径、盘符、反斜杠、`..`、`.`、空段、`%` 编码绕过、控制字符、
|
|
131
|
-
* 超长(整条 >200 / 单段 >64)、路径段里的怪字符。
|
|
132
|
-
*
|
|
133
|
-
* @returns {string} 规范化后的**正斜杠**相对路径
|
|
134
|
-
* @throws {UsageError}
|
|
135
|
-
*/
|
|
136
|
-
export function assertSafePath(raw) {
|
|
137
|
-
const p = String(raw === undefined || raw === null ? '' : raw);
|
|
138
|
-
const bad = (why) =>
|
|
139
|
-
new UsageError(
|
|
140
|
-
`模板文件路径不安全(${why}):${p || '(空)'}\n` +
|
|
141
|
-
'只接受模板里的相对路径,例如 pages/index.html / partials/header.html / assets/style.css;' +
|
|
142
|
-
'不许 ..、绝对路径、盘符、反斜杠、百分号编码',
|
|
143
|
-
);
|
|
144
|
-
if (!p) throw bad('空路径');
|
|
145
|
-
if (p.length > MAX_PATH_CHARS) throw bad(`超过 ${MAX_PATH_CHARS} 字符`);
|
|
146
|
-
if (/^[A-Za-z]:/.test(p)) throw bad('不能带盘符(C: 之类)');
|
|
147
|
-
if (p.startsWith('/') || p.startsWith('\\')) throw bad('不能是绝对路径');
|
|
148
|
-
if (p.includes('\\')) throw bad('不许反斜杠(一律用 /)');
|
|
149
|
-
if (p.includes('%')) throw bad('不许百分号编码(%2e 之类)');
|
|
150
|
-
// eslint-disable-next-line no-control-regex
|
|
151
|
-
if (/[\u0000-\u001f]/.test(p)) throw bad('含控制字符');
|
|
152
|
-
if (p.endsWith('/')) throw bad('不能以 / 结尾');
|
|
153
|
-
for (const seg of p.split('/')) {
|
|
154
|
-
if (!seg) throw bad('出现空路径段(//)');
|
|
155
|
-
if (seg === '.' || seg === '..') throw bad('不许 . 或 .. 路径段');
|
|
156
|
-
if (seg.length > MAX_SEGMENT_CHARS) throw bad(`单段超过 ${MAX_SEGMENT_CHARS} 字符`);
|
|
157
|
-
if (!/^[A-Za-z0-9._-]+$/.test(seg)) throw bad(`路径段只许字母/数字/点/下划线/中划线:「${seg}」`);
|
|
158
|
-
}
|
|
159
|
-
return p.split('/').join('/');
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/** 把相对路径落到根目录下,并**再确认一次**结果没跑到根目录外面(双保险) */
|
|
163
|
-
function resolveInside(rootDir, relPath) {
|
|
164
|
-
const root = path.resolve(rootDir);
|
|
165
|
-
const target = path.resolve(root, relPath);
|
|
166
|
-
if (target !== root && !target.startsWith(root + path.sep)) {
|
|
167
|
-
throw new UsageError(`模板文件路径跑出目标目录了:${relPath}(写到 ${target})`);
|
|
168
|
-
}
|
|
169
|
-
return target;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* 取服务端返回的一个文件条目的**原始字节**。
|
|
174
|
-
*
|
|
175
|
-
* 认得三种形态(服务端另一单在改 OSS 真源,返回形态可能变,所以三种都收):
|
|
176
|
-
* * `{path, content, encoding:'base64'}` —— 显式 base64;
|
|
177
|
-
* * `{path, content}` + 二进制扩展名 —— 按 base64 解;
|
|
178
|
-
* * `{path, content}` + 文本扩展名 —— 按 UTF-8。
|
|
179
|
-
*/
|
|
180
|
-
export function entryBytes(file = {}) {
|
|
181
|
-
const declared = String(file.encoding || '').toLowerCase();
|
|
182
|
-
const rel = String(file.path || '');
|
|
183
|
-
const isBase64 = declared === 'base64' || declared === 'b64' || (!declared && isBinaryPath(rel));
|
|
184
|
-
if (isBase64) {
|
|
185
|
-
const b64 = String(file.content === undefined || file.content === null ? file.base64 || '' : file.content);
|
|
186
|
-
return Buffer.from(b64.replace(/\s+/g, ''), 'base64');
|
|
187
|
-
}
|
|
188
|
-
const text = file.content === undefined || file.content === null ? '' : String(file.content);
|
|
189
|
-
return Buffer.from(text, 'utf8');
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/** 远端文件 → `{path, bytes, sha256, encoding}`(不保存正文,避免 manifest 里塞源码) */
|
|
193
|
-
function describeRemoteFile(file) {
|
|
194
|
-
const rel = assertSafePath(file.path);
|
|
195
|
-
const buf = entryBytes({ ...file, path: rel });
|
|
196
|
-
return {
|
|
197
|
-
path: rel,
|
|
198
|
-
bytes: buf.length,
|
|
199
|
-
sha256: sha256Hex(buf),
|
|
200
|
-
encoding: isBinaryPath(rel) ? 'base64' : 'utf8',
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/** 整套文件的 contentHash:对**按路径排序**的 `path\0sha256\n` 再 sha256 */
|
|
205
|
-
export function contentHashOf(files = []) {
|
|
206
|
-
const sorted = files.slice().sort((a, b) => String(a.path).localeCompare(String(b.path)));
|
|
207
|
-
return sha256Hex(sorted.map((f) => `${f.path}\u0000${f.sha256}\n`).join(''));
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const UUID_RE = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
211
|
-
|
|
212
|
-
/** `--template` 的解析:给 id 直接取;给 key / 名字先查列表再取;不给 = 当前启用那套 */
|
|
213
|
-
async function resolveTemplateSource(api, templateKeyOrId) {
|
|
214
|
-
if (typeof api.get !== 'function') throw new CliError('内部错误:模板接口缺少 get()');
|
|
215
|
-
const wanted = String(templateKeyOrId || '').trim();
|
|
216
|
-
if (!wanted) {
|
|
217
|
-
const tpl = (await api.get(undefined)) || {};
|
|
218
|
-
return { template: tpl, pickedBy: 'active', key: tpl.key || null };
|
|
219
|
-
}
|
|
220
|
-
if (UUID_RE.test(wanted)) {
|
|
221
|
-
const tpl = (await api.get(wanted)) || {};
|
|
222
|
-
return { template: tpl, pickedBy: 'id', key: tpl.key || null };
|
|
223
|
-
}
|
|
224
|
-
if (typeof api.list !== 'function') {
|
|
225
|
-
throw new UsageError(`--template 只认得模板 id(uuid);按 key 找需要接口支持列表:${wanted}`);
|
|
226
|
-
}
|
|
227
|
-
const list = (await api.list()) || {};
|
|
228
|
-
const rows = Array.isArray(list.templates) ? list.templates : [];
|
|
229
|
-
const hit =
|
|
230
|
-
rows.find((t) => String(t.key || '') === wanted) ||
|
|
231
|
-
rows.find((t) => String(t.id || '') === wanted) ||
|
|
232
|
-
rows.find((t) => String(t.name || '') === wanted);
|
|
233
|
-
if (!hit) {
|
|
234
|
-
const known = rows.map((t) => `${t.key || t.id}(${t.name || '-'})`).join(' / ');
|
|
235
|
-
throw new CliError(`本站没找到模板「${wanted}」`, {
|
|
236
|
-
kind: 'not_found',
|
|
237
|
-
code: 404,
|
|
238
|
-
hint: known ? `本站现有:${known}\n不带 --template = 当前启用那套` : '本站还没有模板,先在 CMS 模板页换一套(或 `wkgeo templates list` 触发服务端 seed)',
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
const tpl = (await api.get(String(hit.id))) || {};
|
|
242
|
-
return { template: tpl, pickedBy: 'key', key: tpl.key || hit.key || null };
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function manifestOf({ siteId, templateId, key, files, at }) {
|
|
246
|
-
const sorted = files.slice().sort((a, b) => String(a.path).localeCompare(String(b.path)));
|
|
247
|
-
return {
|
|
248
|
-
version: MANIFEST_VERSION,
|
|
249
|
-
siteId: siteId || null,
|
|
250
|
-
templateId: templateId || null,
|
|
251
|
-
key: key || null,
|
|
252
|
-
contentHash: contentHashOf(sorted),
|
|
253
|
-
pulledAt: at,
|
|
254
|
-
files: sorted.map((f) => ({ path: f.path, bytes: f.bytes, sha256: f.sha256, encoding: f.encoding })),
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function writeManifest(dir, manifest) {
|
|
259
|
-
const file = path.join(dir, MANIFEST_FILE);
|
|
260
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
261
|
-
fs.writeFileSync(file, `${JSON.stringify(manifest, null, 2)}\n`, 'utf8');
|
|
262
|
-
return file;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
if (
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
const
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
const
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
);
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
const
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 模板「拉到本地改 → 推回线上」(2026-11-01 老板单)。
|
|
3
|
+
*
|
|
4
|
+
* ## 老板原话(派发单 m03286)
|
|
5
|
+
* > 「我不需要在线编辑那一块,那一块功能可以取消。如果我编辑的话,我就在我的 CLI 里面去编辑,
|
|
6
|
+
* > 本地电脑绑定客户端 + CLI。」
|
|
7
|
+
*
|
|
8
|
+
* ⇒ CMS 里的 Monaco 在线编辑器入口下线(只隐藏入口,服务端 `templateFileSave` 保留),
|
|
9
|
+
* 模板真源以后是**本地目录**:`wkgeo templates pull` 拉整包下来改,`wkgeo templates push` 推回。
|
|
10
|
+
*
|
|
11
|
+
* ## 为什么单独一个模块(不塞进 commands/template.mjs)
|
|
12
|
+
* 1. 拉 / 推是**纯文件逻辑**(落盘、sha256 对账、路径穿越防护),跟 CLI 的打印 / 参数无关,
|
|
13
|
+
* 放独立模块才能被单测直接喂假 `api`(不需要起 CLI、不需要网络);
|
|
14
|
+
* 2. 命令层只做三件事:解析参数 → 调这里 → 打印计划/结果。
|
|
15
|
+
*
|
|
16
|
+
* ## `api` 适配器(由命令层从 `ctx` 造,见 commands/template.mjs)
|
|
17
|
+
* * `get(templateId?)` → `Promise<{id,name,key,files:[{path,content,encoding?}]}>`
|
|
18
|
+
* = 服务端动作 `templateGet`(不传 id = 当前启用那套);
|
|
19
|
+
* * `list()` → `Promise<{templateId,templates:[{id,key,name}]}>` = `templateList`(只在按 key 找模板时用);
|
|
20
|
+
* * `save({path,content,encoding?,templateId?})` → `templateFileSave`。
|
|
21
|
+
*
|
|
22
|
+
* ⚠️ **服务端能力边界(本单不新增动作,这是已知缺口)**:
|
|
23
|
+
* `templateFileSave` 只收文本(`.html / .css / .js / .json / .min.css / .min.js`,≤40 万字符),
|
|
24
|
+
* 图片扩展名会被 400「模板里不放图片,图片请传到素材库」拒掉,而 actionTable 里**没有**
|
|
25
|
+
* "删模板文件"的动作。所以:
|
|
26
|
+
* * 拉(pull)支持二进制(服务端哪天返回 `encoding:'base64'` 就能按真文件还原,见 `entryBytes`);
|
|
27
|
+
* * 推(push)遇到二进制 / 服务端不收的扩展名,**逐个文件报错并跳过**(不静默、不假装成功);
|
|
28
|
+
* * `--prune` 只把"本地已删、远端还在"的文件**列出来**,不会真删(没有删除动作)。
|
|
29
|
+
* 这三条都写进 `docs/规范-模板存储与编辑.md` 的「CLI 拉取/推送用法」一节。
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import crypto from 'node:crypto';
|
|
33
|
+
import fs from 'node:fs';
|
|
34
|
+
import path from 'node:path';
|
|
35
|
+
|
|
36
|
+
import { CliError, UsageError } from './errors.mjs';
|
|
37
|
+
|
|
38
|
+
/** manifest 文件名(放模板目录根,push 时会被跳过) */
|
|
39
|
+
export const MANIFEST_FILE = 'template-manifest.json';
|
|
40
|
+
export const MANIFEST_VERSION = 1;
|
|
41
|
+
|
|
42
|
+
/** 服务端 `templateMaxBytes = 400 * 1024`(**按字符**算);二进制按字节同口径兜住 */
|
|
43
|
+
export const MAX_FILE_CHARS = 400 * 1024;
|
|
44
|
+
export const MAX_FILE_BYTES = 400 * 1024;
|
|
45
|
+
/**
|
|
46
|
+
* 超限文件 = **不该进站点模板副本的大体积演示资产**(three.min.js / 大图等)。
|
|
47
|
+
*
|
|
48
|
+
* ⚠️ 2026-11-14 task-107:以前拉 / 推遇到超限文件**整包直接失败**,结果是
|
|
49
|
+
* 「库里本来就有的大文件把整站的拉/推全堵死」——base 站 `assets/vendor/three-0.147.0/three.min.js`
|
|
50
|
+
* (607784 字符)与闵航站 `home.jpg`(538619 字节)就是这样堵住的,其余几十个小文件一个都推不上去。
|
|
51
|
+
* 现在改成:**跳过这一个文件 + 告警**,其余文件照常拉 / 推;上限本身**不调大**(避免整站模板过大)。
|
|
52
|
+
*/
|
|
53
|
+
export function oversizeOf(filePath, buf) {
|
|
54
|
+
const rel = String(filePath || '');
|
|
55
|
+
const text = Buffer.isBuffer(buf) ? buf : Buffer.from(String(buf === undefined || buf === null ? '' : buf), 'utf8');
|
|
56
|
+
if (isBinaryPath(rel)) {
|
|
57
|
+
if (text.length > MAX_FILE_BYTES) return { reason: 'binary-bytes', measured: text.length, limit: MAX_FILE_BYTES };
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const chars = text.toString('utf8').length;
|
|
61
|
+
if (chars > MAX_FILE_CHARS) return { reason: 'text-chars', measured: chars, limit: MAX_FILE_CHARS };
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 超限告警文案(拉 / 推共用;不打印正文) */
|
|
66
|
+
export function oversizeWarning(rel, one) {
|
|
67
|
+
const unit = one.reason === 'binary-bytes' ? '字节' : '字符';
|
|
68
|
+
return (
|
|
69
|
+
`跳过超限文件 ${rel}(${one.measured} ${unit},上限 ${one.limit}):大体积演示资产不进站点模板副本,` +
|
|
70
|
+
'其余文件照常同步;要放它请走素材库 / 母版引用'
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 路径上限:对齐 `template-files.js` 的 `TEMPLATE_FILE_MAX_PATH_CHARS` / 每段 64 字符 */
|
|
75
|
+
export const MAX_PATH_CHARS = 200;
|
|
76
|
+
export const MAX_SEGMENT_CHARS = 64;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 二进制扩展名(派发单点名的几种 + 常见同类)。
|
|
80
|
+
*
|
|
81
|
+
* `svg` **故意不算二进制**:它是 XML 文本,线上模板里常直接内联 / 手改。
|
|
82
|
+
*/
|
|
83
|
+
export const BINARY_EXTS = new Set([
|
|
84
|
+
'png',
|
|
85
|
+
'jpg',
|
|
86
|
+
'jpeg',
|
|
87
|
+
'webp',
|
|
88
|
+
'gif',
|
|
89
|
+
'ico',
|
|
90
|
+
'bmp',
|
|
91
|
+
'avif',
|
|
92
|
+
'woff',
|
|
93
|
+
'woff2',
|
|
94
|
+
'ttf',
|
|
95
|
+
'otf',
|
|
96
|
+
'eot',
|
|
97
|
+
'mp4',
|
|
98
|
+
'webm',
|
|
99
|
+
'mp3',
|
|
100
|
+
'wav',
|
|
101
|
+
'pdf',
|
|
102
|
+
'zip',
|
|
103
|
+
'wasm',
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
/** 扫描本地目录时**不进**的目录(模板包不该有它们;进了只会把整仓 node_modules 拉进来) */
|
|
107
|
+
const SKIP_DIRS = new Set(['.git', 'node_modules', '.tmp-build']);
|
|
108
|
+
|
|
109
|
+
/** sha256(UTF-8 字节口径)—— 与服务端 `template-files.js#sha256` 对齐 */
|
|
110
|
+
export function sha256Hex(input) {
|
|
111
|
+
const buf = Buffer.isBuffer(input) ? input : Buffer.from(String(input === undefined || input === null ? '' : input), 'utf8');
|
|
112
|
+
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 扩展名(小写,不带点);没有扩展名返回 '' */
|
|
116
|
+
export function extOf(filePath) {
|
|
117
|
+
const base = String(filePath || '').split('/').pop() || '';
|
|
118
|
+
const at = base.lastIndexOf('.');
|
|
119
|
+
if (at <= 0 || at === base.length - 1) return '';
|
|
120
|
+
return base.slice(at + 1).toLowerCase();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function isBinaryPath(filePath) {
|
|
124
|
+
return BINARY_EXTS.has(extOf(filePath));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 路径安全校验 —— **拉 / 推 / 读 manifest 三处都走同一个函数**,别各写一套。
|
|
129
|
+
*
|
|
130
|
+
* 拒(派发单要求):绝对路径、盘符、反斜杠、`..`、`.`、空段、`%` 编码绕过、控制字符、
|
|
131
|
+
* 超长(整条 >200 / 单段 >64)、路径段里的怪字符。
|
|
132
|
+
*
|
|
133
|
+
* @returns {string} 规范化后的**正斜杠**相对路径
|
|
134
|
+
* @throws {UsageError}
|
|
135
|
+
*/
|
|
136
|
+
export function assertSafePath(raw) {
|
|
137
|
+
const p = String(raw === undefined || raw === null ? '' : raw);
|
|
138
|
+
const bad = (why) =>
|
|
139
|
+
new UsageError(
|
|
140
|
+
`模板文件路径不安全(${why}):${p || '(空)'}\n` +
|
|
141
|
+
'只接受模板里的相对路径,例如 pages/index.html / partials/header.html / assets/style.css;' +
|
|
142
|
+
'不许 ..、绝对路径、盘符、反斜杠、百分号编码',
|
|
143
|
+
);
|
|
144
|
+
if (!p) throw bad('空路径');
|
|
145
|
+
if (p.length > MAX_PATH_CHARS) throw bad(`超过 ${MAX_PATH_CHARS} 字符`);
|
|
146
|
+
if (/^[A-Za-z]:/.test(p)) throw bad('不能带盘符(C: 之类)');
|
|
147
|
+
if (p.startsWith('/') || p.startsWith('\\')) throw bad('不能是绝对路径');
|
|
148
|
+
if (p.includes('\\')) throw bad('不许反斜杠(一律用 /)');
|
|
149
|
+
if (p.includes('%')) throw bad('不许百分号编码(%2e 之类)');
|
|
150
|
+
// eslint-disable-next-line no-control-regex
|
|
151
|
+
if (/[\u0000-\u001f]/.test(p)) throw bad('含控制字符');
|
|
152
|
+
if (p.endsWith('/')) throw bad('不能以 / 结尾');
|
|
153
|
+
for (const seg of p.split('/')) {
|
|
154
|
+
if (!seg) throw bad('出现空路径段(//)');
|
|
155
|
+
if (seg === '.' || seg === '..') throw bad('不许 . 或 .. 路径段');
|
|
156
|
+
if (seg.length > MAX_SEGMENT_CHARS) throw bad(`单段超过 ${MAX_SEGMENT_CHARS} 字符`);
|
|
157
|
+
if (!/^[A-Za-z0-9._-]+$/.test(seg)) throw bad(`路径段只许字母/数字/点/下划线/中划线:「${seg}」`);
|
|
158
|
+
}
|
|
159
|
+
return p.split('/').join('/');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** 把相对路径落到根目录下,并**再确认一次**结果没跑到根目录外面(双保险) */
|
|
163
|
+
function resolveInside(rootDir, relPath) {
|
|
164
|
+
const root = path.resolve(rootDir);
|
|
165
|
+
const target = path.resolve(root, relPath);
|
|
166
|
+
if (target !== root && !target.startsWith(root + path.sep)) {
|
|
167
|
+
throw new UsageError(`模板文件路径跑出目标目录了:${relPath}(写到 ${target})`);
|
|
168
|
+
}
|
|
169
|
+
return target;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 取服务端返回的一个文件条目的**原始字节**。
|
|
174
|
+
*
|
|
175
|
+
* 认得三种形态(服务端另一单在改 OSS 真源,返回形态可能变,所以三种都收):
|
|
176
|
+
* * `{path, content, encoding:'base64'}` —— 显式 base64;
|
|
177
|
+
* * `{path, content}` + 二进制扩展名 —— 按 base64 解;
|
|
178
|
+
* * `{path, content}` + 文本扩展名 —— 按 UTF-8。
|
|
179
|
+
*/
|
|
180
|
+
export function entryBytes(file = {}) {
|
|
181
|
+
const declared = String(file.encoding || '').toLowerCase();
|
|
182
|
+
const rel = String(file.path || '');
|
|
183
|
+
const isBase64 = declared === 'base64' || declared === 'b64' || (!declared && isBinaryPath(rel));
|
|
184
|
+
if (isBase64) {
|
|
185
|
+
const b64 = String(file.content === undefined || file.content === null ? file.base64 || '' : file.content);
|
|
186
|
+
return Buffer.from(b64.replace(/\s+/g, ''), 'base64');
|
|
187
|
+
}
|
|
188
|
+
const text = file.content === undefined || file.content === null ? '' : String(file.content);
|
|
189
|
+
return Buffer.from(text, 'utf8');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** 远端文件 → `{path, bytes, sha256, encoding}`(不保存正文,避免 manifest 里塞源码) */
|
|
193
|
+
function describeRemoteFile(file) {
|
|
194
|
+
const rel = assertSafePath(file.path);
|
|
195
|
+
const buf = entryBytes({ ...file, path: rel });
|
|
196
|
+
return {
|
|
197
|
+
path: rel,
|
|
198
|
+
bytes: buf.length,
|
|
199
|
+
sha256: sha256Hex(buf),
|
|
200
|
+
encoding: isBinaryPath(rel) ? 'base64' : 'utf8',
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** 整套文件的 contentHash:对**按路径排序**的 `path\0sha256\n` 再 sha256 */
|
|
205
|
+
export function contentHashOf(files = []) {
|
|
206
|
+
const sorted = files.slice().sort((a, b) => String(a.path).localeCompare(String(b.path)));
|
|
207
|
+
return sha256Hex(sorted.map((f) => `${f.path}\u0000${f.sha256}\n`).join(''));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const UUID_RE = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
211
|
+
|
|
212
|
+
/** `--template` 的解析:给 id 直接取;给 key / 名字先查列表再取;不给 = 当前启用那套 */
|
|
213
|
+
async function resolveTemplateSource(api, templateKeyOrId) {
|
|
214
|
+
if (typeof api.get !== 'function') throw new CliError('内部错误:模板接口缺少 get()');
|
|
215
|
+
const wanted = String(templateKeyOrId || '').trim();
|
|
216
|
+
if (!wanted) {
|
|
217
|
+
const tpl = (await api.get(undefined)) || {};
|
|
218
|
+
return { template: tpl, pickedBy: 'active', key: tpl.key || null };
|
|
219
|
+
}
|
|
220
|
+
if (UUID_RE.test(wanted)) {
|
|
221
|
+
const tpl = (await api.get(wanted)) || {};
|
|
222
|
+
return { template: tpl, pickedBy: 'id', key: tpl.key || null };
|
|
223
|
+
}
|
|
224
|
+
if (typeof api.list !== 'function') {
|
|
225
|
+
throw new UsageError(`--template 只认得模板 id(uuid);按 key 找需要接口支持列表:${wanted}`);
|
|
226
|
+
}
|
|
227
|
+
const list = (await api.list()) || {};
|
|
228
|
+
const rows = Array.isArray(list.templates) ? list.templates : [];
|
|
229
|
+
const hit =
|
|
230
|
+
rows.find((t) => String(t.key || '') === wanted) ||
|
|
231
|
+
rows.find((t) => String(t.id || '') === wanted) ||
|
|
232
|
+
rows.find((t) => String(t.name || '') === wanted);
|
|
233
|
+
if (!hit) {
|
|
234
|
+
const known = rows.map((t) => `${t.key || t.id}(${t.name || '-'})`).join(' / ');
|
|
235
|
+
throw new CliError(`本站没找到模板「${wanted}」`, {
|
|
236
|
+
kind: 'not_found',
|
|
237
|
+
code: 404,
|
|
238
|
+
hint: known ? `本站现有:${known}\n不带 --template = 当前启用那套` : '本站还没有模板,先在 CMS 模板页换一套(或 `wkgeo templates list` 触发服务端 seed)',
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
const tpl = (await api.get(String(hit.id))) || {};
|
|
242
|
+
return { template: tpl, pickedBy: 'key', key: tpl.key || hit.key || null };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function manifestOf({ siteId, templateId, key, files, at }) {
|
|
246
|
+
const sorted = files.slice().sort((a, b) => String(a.path).localeCompare(String(b.path)));
|
|
247
|
+
return {
|
|
248
|
+
version: MANIFEST_VERSION,
|
|
249
|
+
siteId: siteId || null,
|
|
250
|
+
templateId: templateId || null,
|
|
251
|
+
key: key || null,
|
|
252
|
+
contentHash: contentHashOf(sorted),
|
|
253
|
+
pulledAt: at,
|
|
254
|
+
files: sorted.map((f) => ({ path: f.path, bytes: f.bytes, sha256: f.sha256, encoding: f.encoding })),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function writeManifest(dir, manifest) {
|
|
259
|
+
const file = path.join(dir, MANIFEST_FILE);
|
|
260
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
261
|
+
fs.writeFileSync(file, `${JSON.stringify(manifest, null, 2)}\n`, 'utf8');
|
|
262
|
+
return file;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** `site build` 写进 `template/` 的资产包文件名(`site-<hash>.css` / `site-<hash>.js`)。 */
|
|
266
|
+
const BUILD_BUNDLE_FILE_RE = /^site-[0-9a-f]{6,}\.(?:css|js)$/i;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* 目录里是不是**只有 build 派生的资产包**(`css/site-<hash>.css`、`js/site-<hash>.js`)。
|
|
270
|
+
* 是 ⇒ 视为空目录,`templates pull` 不必加 `--force`(用户没往里面放模板源,不会被覆盖)。
|
|
271
|
+
*/
|
|
272
|
+
function onlyBuildArtifacts(root) {
|
|
273
|
+
let entries = [];
|
|
274
|
+
try {
|
|
275
|
+
entries = fs.readdirSync(root, { withFileTypes: true });
|
|
276
|
+
} catch (_err) {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
if (!entries.length) return false;
|
|
280
|
+
for (const entry of entries) {
|
|
281
|
+
if (!entry.isDirectory() || (entry.name !== 'css' && entry.name !== 'js')) return false;
|
|
282
|
+
let files = [];
|
|
283
|
+
try {
|
|
284
|
+
files = fs.readdirSync(path.join(root, entry.name), { withFileTypes: true });
|
|
285
|
+
} catch (_err) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
if (!files.length) return false;
|
|
289
|
+
const onlyBundle = files.every((file) => file.isFile() && BUILD_BUNDLE_FILE_RE.test(file.name));
|
|
290
|
+
if (!onlyBundle) return false;
|
|
291
|
+
}
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* `wkgeo templates pull` 的本体:取一套模板的全部文件 → 按模板内相对路径落盘 → 写 manifest。
|
|
297
|
+
*
|
|
298
|
+
* @param {object} opts
|
|
299
|
+
* @param {string} [opts.siteId] 站点 id(只写进 manifest,供 push 时对账)
|
|
300
|
+
* @param {string} [opts.templateKeyOrId] 模板 key / id;不给 = 当前启用那套
|
|
301
|
+
* @param {string} opts.dir 落盘目录(相对 cwd 解析;不存在就建)
|
|
302
|
+
* @param {object} opts.api 适配器(见文件头)
|
|
303
|
+
* @param {boolean} [opts.force] 目录非空时是否允许覆盖(默认否)
|
|
304
|
+
* @param {string} [opts.cwd] 相对路径基准(默认 process.cwd())
|
|
305
|
+
* @param {() => Date} [opts.now] 时钟注入(单测固定 pulledAt)
|
|
306
|
+
* @returns {Promise<object>}
|
|
307
|
+
*/
|
|
308
|
+
export async function pullTemplate({
|
|
309
|
+
siteId = '',
|
|
310
|
+
templateKeyOrId = '',
|
|
311
|
+
dir = '',
|
|
312
|
+
api = null,
|
|
313
|
+
force = false,
|
|
314
|
+
cwd = process.cwd(),
|
|
315
|
+
now = () => new Date(),
|
|
316
|
+
} = {}) {
|
|
317
|
+
if (!String(dir || '').trim()) {
|
|
318
|
+
throw new UsageError('必须给 --dir <目录>(拉到哪个本地目录)\n例:wkgeo templates pull --dir ./my-template');
|
|
319
|
+
}
|
|
320
|
+
if (!api || typeof api.get !== 'function') throw new CliError('内部错误:pullTemplate 缺少 api.get');
|
|
321
|
+
|
|
322
|
+
const root = path.resolve(cwd, String(dir));
|
|
323
|
+
const { template, pickedBy, key: keyFromList } = await resolveTemplateSource(api, templateKeyOrId);
|
|
324
|
+
const remoteFiles = Array.isArray(template.files) ? template.files : [];
|
|
325
|
+
if (!remoteFiles.length) {
|
|
326
|
+
throw new CliError(`模板《${template.name || template.id || '?'}》里还没有任何文件`, {
|
|
327
|
+
kind: 'not_found',
|
|
328
|
+
code: 404,
|
|
329
|
+
hint: '先在 CMS 模板页换一套模板(会 seed 默认文件),或用 `wkgeo templates list` 看有没有文件',
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (fs.existsSync(root)) {
|
|
334
|
+
if (!fs.statSync(root).isDirectory()) throw new UsageError(`--dir 指向的是个文件,不是目录:${root}`);
|
|
335
|
+
const entries = fs.readdirSync(root);
|
|
336
|
+
/* ★ rev4:`site build` 会把站内资产包写进 `template/css|js`(页面引用 `/template/…`)。
|
|
337
|
+
这些是**派生产物**,不是用户的模板源;只因为它们存在就拒绝拉取,会让「先 build 后拉模板」永远卡住。 */
|
|
338
|
+
if (entries.length && !force && !onlyBuildArtifacts(root)) {
|
|
339
|
+
throw new UsageError(
|
|
340
|
+
`目录里已经有东西,拉取会覆盖同名文件:${root}\n` +
|
|
341
|
+
`换个空目录,或加 --force 明确覆盖(已有的 ${entries.length} 项:${entries.slice(0, 5).join(' / ')}${entries.length > 5 ? ' …' : ''})`,
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
fs.mkdirSync(root, { recursive: true });
|
|
346
|
+
|
|
347
|
+
const at = now().toISOString();
|
|
348
|
+
const files = [];
|
|
349
|
+
const skipped = [];
|
|
350
|
+
let binaryCount = 0;
|
|
351
|
+
let totalBytes = 0;
|
|
352
|
+
for (const raw of remoteFiles) {
|
|
353
|
+
const rel = assertSafePath(raw.path);
|
|
354
|
+
const buf = entryBytes({ ...raw, path: rel });
|
|
355
|
+
const binary = isBinaryPath(rel);
|
|
356
|
+
const oversize = oversizeOf(rel, buf);
|
|
357
|
+
if (oversize) {
|
|
358
|
+
skipped.push({ path: rel, bytes: buf.length, measured: oversize.measured, reason: oversize.reason });
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const target = resolveInside(root, rel);
|
|
362
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
363
|
+
fs.writeFileSync(target, buf);
|
|
364
|
+
files.push({
|
|
365
|
+
path: rel,
|
|
366
|
+
bytes: buf.length,
|
|
367
|
+
sha256: sha256Hex(buf),
|
|
368
|
+
encoding: binary ? 'base64' : 'utf8',
|
|
369
|
+
});
|
|
370
|
+
if (binary) binaryCount += 1;
|
|
371
|
+
totalBytes += buf.length;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const manifest = manifestOf({
|
|
375
|
+
siteId: siteId || '',
|
|
376
|
+
templateId: template.id || '',
|
|
377
|
+
key: keyFromList || template.key || '',
|
|
378
|
+
files,
|
|
379
|
+
at,
|
|
380
|
+
});
|
|
381
|
+
const manifestPath = writeManifest(root, manifest);
|
|
382
|
+
|
|
383
|
+
return {
|
|
384
|
+
dir: root,
|
|
385
|
+
manifestPath,
|
|
386
|
+
picked_by: pickedBy,
|
|
387
|
+
site_id: siteId || '',
|
|
388
|
+
template: {
|
|
389
|
+
id: template.id || null,
|
|
390
|
+
name: template.name || '',
|
|
391
|
+
key: manifest.key,
|
|
392
|
+
},
|
|
393
|
+
fileCount: files.length,
|
|
394
|
+
binaryCount,
|
|
395
|
+
bytes: totalBytes,
|
|
396
|
+
contentHash: manifest.contentHash,
|
|
397
|
+
files,
|
|
398
|
+
skipped,
|
|
399
|
+
warnings: skipped.map((one) => oversizeWarning(one.path, { reason: one.reason, measured: one.measured, limit: one.reason === 'binary-bytes' ? MAX_FILE_BYTES : MAX_FILE_CHARS })),
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* 读 `dir/template-manifest.json`(没有 / 坏了都给出可照做的下一步)。
|
|
405
|
+
* @returns {{manifest:object, file:string}}
|
|
406
|
+
*/
|
|
407
|
+
export function readManifest(dir) {
|
|
408
|
+
const root = path.resolve(dir);
|
|
409
|
+
const file = path.join(root, MANIFEST_FILE);
|
|
410
|
+
if (!fs.existsSync(file)) {
|
|
411
|
+
throw new UsageError(
|
|
412
|
+
`这个目录里没有 ${MANIFEST_FILE}:${root}\n` +
|
|
413
|
+
'先 `wkgeo templates pull --dir <目录>` 拉一套模板下来;manifest 会记住站点 / 模板 id / 每个文件的 sha256,push 靠它对账',
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
let json;
|
|
417
|
+
try {
|
|
418
|
+
json = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
419
|
+
} catch (err) {
|
|
420
|
+
throw new CliError(`${MANIFEST_FILE} 不是合法 JSON:${(err && err.message) || err}`, {
|
|
421
|
+
kind: 'bad_manifest',
|
|
422
|
+
hint: '重新 `templates pull` 覆盖一份(或手工修好 JSON 再 push)',
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
if (!json || typeof json !== 'object' || json.version !== MANIFEST_VERSION) {
|
|
426
|
+
throw new CliError(`${MANIFEST_FILE} 版本不认识(要 version:${MANIFEST_VERSION},实际 ${json && json.version})`, {
|
|
427
|
+
kind: 'bad_manifest',
|
|
428
|
+
hint: '用新版本 CLI 重新 `templates pull` 一份',
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
if (!Array.isArray(json.files)) {
|
|
432
|
+
throw new CliError(`${MANIFEST_FILE} 里的 files 不是数组`, { kind: 'bad_manifest' });
|
|
433
|
+
}
|
|
434
|
+
return { manifest: json, file };
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** 递归扫本地模板目录(跳过 manifest / .git / node_modules;不跟符号链接) */
|
|
438
|
+
function scanLocal(dir) {
|
|
439
|
+
const root = path.resolve(dir);
|
|
440
|
+
const absPaths = [];
|
|
441
|
+
const walk = (current) => {
|
|
442
|
+
let entries = [];
|
|
443
|
+
try {
|
|
444
|
+
entries = fs.readdirSync(current, { withFileTypes: true });
|
|
445
|
+
} catch (err) {
|
|
446
|
+
throw new CliError(`读目录失败:${current}——${(err && err.message) || err}`, { kind: 'fs_error' });
|
|
447
|
+
}
|
|
448
|
+
for (const ent of entries) {
|
|
449
|
+
const abs = path.join(current, ent.name);
|
|
450
|
+
if (ent.isDirectory()) {
|
|
451
|
+
if (SKIP_DIRS.has(ent.name)) continue;
|
|
452
|
+
walk(abs);
|
|
453
|
+
} else if (ent.isFile()) {
|
|
454
|
+
absPaths.push(abs);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
walk(root);
|
|
459
|
+
const files = [];
|
|
460
|
+
const skipped = [];
|
|
461
|
+
for (const abs of absPaths) {
|
|
462
|
+
const rel = path.relative(root, abs).split(path.sep).join('/');
|
|
463
|
+
if (rel === MANIFEST_FILE) continue;
|
|
464
|
+
const safe = assertSafePath(rel);
|
|
465
|
+
const buf = fs.readFileSync(abs);
|
|
466
|
+
const binary = isBinaryPath(safe);
|
|
467
|
+
const oversize = oversizeOf(safe, buf);
|
|
468
|
+
if (oversize) {
|
|
469
|
+
skipped.push({ path: safe, bytes: buf.length, measured: oversize.measured, reason: oversize.reason });
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
files.push({
|
|
473
|
+
path: safe,
|
|
474
|
+
abs,
|
|
475
|
+
bytes: buf.length,
|
|
476
|
+
sha256: sha256Hex(buf),
|
|
477
|
+
encoding: binary ? 'base64' : 'utf8',
|
|
478
|
+
binary,
|
|
479
|
+
text: binary ? null : buf.toString('utf8'),
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
return { files: files.sort((a, b) => a.path.localeCompare(b.path)), skipped };
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** 远端当前文件(按 manifest 里那套模板取;不传 id = 当前启用那套) */
|
|
486
|
+
async function fetchRemote(api, manifest) {
|
|
487
|
+
const wanted = manifest && manifest.templateId ? String(manifest.templateId) : undefined;
|
|
488
|
+
const tpl = (await api.get(wanted)) || {};
|
|
489
|
+
const list = Array.isArray(tpl.files) ? tpl.files : [];
|
|
490
|
+
const files = list.map(describeRemoteFile);
|
|
491
|
+
const warnings = [];
|
|
492
|
+
if (wanted && tpl.id && String(tpl.id) !== wanted) {
|
|
493
|
+
warnings.push(
|
|
494
|
+
`远端返回的是另一套模板(id ${tpl.id},manifest 记的是 ${wanted})——那套可能已被删/换过,push 会写到自己站的当前模板上,先看清楚再推`,
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
return { template: tpl, files, warnings };
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* 防跨站写:manifest 记着"这套模板是从哪个站拉的",push 时当前站必须是同一个。
|
|
502
|
+
*
|
|
503
|
+
* 为什么必须有这道闸:`templateFileSave` 不传 templateId 时写的是**当前启用**那套,
|
|
504
|
+
* 传了也只会校验"这个模板属于某站";一旦用户在别处 `site use` 换了站就 push,
|
|
505
|
+
* 最好的情况是 404、最坏的情况是**把 A 站的模板写进 B 站的启用模板**。宁可直接拒绝。
|
|
506
|
+
*
|
|
507
|
+
* 适配器没提供 `currentSiteId()`(单测的假 api 常常不提供)时跳过,不做半吊子判断。
|
|
508
|
+
*/
|
|
509
|
+
function assertSameSite(manifest, api) {
|
|
510
|
+
if (!api || typeof api.currentSiteId !== 'function') return;
|
|
511
|
+
const here = String(api.currentSiteId() || '').trim();
|
|
512
|
+
const from = String((manifest && manifest.siteId) || '').trim();
|
|
513
|
+
if (!here || !from || here === from) return;
|
|
514
|
+
throw new UsageError(
|
|
515
|
+
`这个目录是从**别的站**拉的(manifest.siteId=${from},当前站=${here})——拒绝跨站推送。\n` +
|
|
516
|
+
`先切回那个站:wkgeo site use ${from}(或给命令加 --site ${from}),再 push。`,
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* `planPush` 的本体:manifest + 本地扫描 + 远端 sha256 → 四类差异。
|
|
522
|
+
*
|
|
523
|
+
* ⚠️ 删远端**默认不做**:`willDelete` 只有 `prune:true` 才填(而服务端目前也没有删除动作)。
|
|
524
|
+
*
|
|
525
|
+
* @param {object} opts
|
|
526
|
+
* @param {string} opts.dir 模板目录
|
|
527
|
+
* @param {object} opts.api 适配器
|
|
528
|
+
* @param {boolean} [opts.prune] 是否把 removed 列进"待删"
|
|
529
|
+
*/
|
|
530
|
+
export async function planPush({ dir = '', api = null, prune = false, cwd = process.cwd() } = {}) {
|
|
531
|
+
if (!String(dir || '').trim()) throw new UsageError('必须给 --dir <目录>\n例:wkgeo templates push --dir ./my-template --dry-run');
|
|
532
|
+
if (!api || typeof api.get !== 'function') throw new CliError('内部错误:planPush 缺少 api.get');
|
|
533
|
+
const root = path.resolve(cwd, String(dir));
|
|
534
|
+
if (!fs.existsSync(root)) throw new UsageError(`模板目录不存在:${root}`);
|
|
535
|
+
|
|
536
|
+
const { manifest, file: manifestPath } = readManifest(root);
|
|
537
|
+
assertSameSite(manifest, api);
|
|
538
|
+
const scanned = scanLocal(root);
|
|
539
|
+
const local = scanned.files;
|
|
540
|
+
const skipped = scanned.skipped;
|
|
541
|
+
const remote = await fetchRemote(api, manifest);
|
|
542
|
+
const localMap = new Map(local.map((f) => [f.path, f]));
|
|
543
|
+
const remoteMap = new Map(remote.files.map((f) => [f.path, f]));
|
|
544
|
+
|
|
545
|
+
const plan = { added: [], changed: [], removed: [], unchanged: [], willDelete: [], prune: Boolean(prune) };
|
|
546
|
+
for (const f of local) {
|
|
547
|
+
const r = remoteMap.get(f.path);
|
|
548
|
+
const brief = { path: f.path, bytes: f.bytes, sha256: f.sha256, encoding: f.encoding, binary: f.binary };
|
|
549
|
+
if (!r) plan.added.push(brief);
|
|
550
|
+
else if (r.sha256 !== f.sha256) plan.changed.push({ ...brief, remote_sha256: r.sha256, remote_bytes: r.bytes });
|
|
551
|
+
else plan.unchanged.push(brief);
|
|
552
|
+
}
|
|
553
|
+
for (const r of remote.files) {
|
|
554
|
+
if (localMap.has(r.path)) continue;
|
|
555
|
+
plan.removed.push({ path: r.path, bytes: r.bytes, sha256: r.sha256, encoding: r.encoding, binary: isBinaryPath(r.path) });
|
|
556
|
+
}
|
|
557
|
+
if (plan.prune) plan.willDelete = plan.removed.slice();
|
|
558
|
+
|
|
559
|
+
return {
|
|
560
|
+
dir: root,
|
|
561
|
+
manifestPath,
|
|
562
|
+
manifest,
|
|
563
|
+
plan,
|
|
564
|
+
local,
|
|
565
|
+
skipped,
|
|
566
|
+
remote: remote.files,
|
|
567
|
+
warnings: [
|
|
568
|
+
...remote.warnings,
|
|
569
|
+
...skipped.map((one) => oversizeWarning(one.path, { reason: one.reason, measured: one.measured, limit: one.reason === 'binary-bytes' ? MAX_FILE_BYTES : MAX_FILE_CHARS })),
|
|
570
|
+
],
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* `pushTemplate` 的本体:先在本地算计划;`confirm !== true` 时**零写请求**,只把计划交回去。
|
|
576
|
+
*
|
|
577
|
+
* 确认后逐文件 `api.save()`(= `templateFileSave`)写入,全部成功才回写 manifest 的
|
|
578
|
+
* `contentHash` / `pushedAt`;有失败就**不写 manifest**(下次 push 仍然能算对差异)。
|
|
579
|
+
*
|
|
580
|
+
* @param {object} opts
|
|
581
|
+
* @param {string} opts.dir
|
|
582
|
+
* @param {object} opts.api
|
|
583
|
+
* @param {boolean} [opts.confirm]
|
|
584
|
+
* @param {boolean} [opts.prune]
|
|
585
|
+
*/
|
|
586
|
+
export async function pushTemplate({
|
|
587
|
+
dir = '',
|
|
588
|
+
api = null,
|
|
589
|
+
confirm = false,
|
|
590
|
+
prune = false,
|
|
591
|
+
cwd = process.cwd(),
|
|
592
|
+
now = () => new Date(),
|
|
593
|
+
} = {}) {
|
|
594
|
+
const planned = await planPush({ dir, api, prune, cwd });
|
|
595
|
+
const { plan } = planned;
|
|
596
|
+
const base = {
|
|
597
|
+
dir: planned.dir,
|
|
598
|
+
manifestPath: planned.manifestPath,
|
|
599
|
+
site_id: planned.manifest.siteId || '',
|
|
600
|
+
template: { id: planned.manifest.templateId || null, key: planned.manifest.key || null },
|
|
601
|
+
plan,
|
|
602
|
+
warnings: planned.warnings.slice(),
|
|
603
|
+
skipped: planned.skipped || [],
|
|
604
|
+
written: [],
|
|
605
|
+
failed: [],
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
if (!confirm) {
|
|
609
|
+
base.applied = false;
|
|
610
|
+
return base;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
const targets = [...plan.added, ...plan.changed];
|
|
614
|
+
let index = 0;
|
|
615
|
+
for (const one of targets) {
|
|
616
|
+
index += 1;
|
|
617
|
+
const file = planned.local.find((f) => f.path === one.path);
|
|
618
|
+
try {
|
|
619
|
+
if (!file) throw new CliError('本地文件在扫描后消失了(改完再跑一次)');
|
|
620
|
+
if (file.binary) {
|
|
621
|
+
throw new CliError(
|
|
622
|
+
`服务端现在只收文本模板文件(.html/.css/.js/.json),二进制(${extOf(one.path) || '未知类型'})写不进去`,
|
|
623
|
+
{ hint: '图片/字体请走素材库:`wkgeo assets upload <文件>`,模板里用 /assets/... 引用' },
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
await api.save({
|
|
627
|
+
path: one.path,
|
|
628
|
+
content: file.text,
|
|
629
|
+
encoding: 'utf8',
|
|
630
|
+
templateId: planned.manifest.templateId || undefined,
|
|
631
|
+
});
|
|
632
|
+
base.written.push({ path: one.path, bytes: file.bytes, sha256: file.sha256 });
|
|
633
|
+
} catch (err) {
|
|
634
|
+
base.failed.push({
|
|
635
|
+
index,
|
|
636
|
+
path: one.path,
|
|
637
|
+
message: (err && err.message) || String(err),
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
if (plan.prune && plan.removed.length) {
|
|
643
|
+
base.warnings.push(
|
|
644
|
+
`--prune 列出了 ${plan.removed.length} 个"本地已删、远端还在"的文件,但服务端**没有删除模板文件的动作**,本次没有删(列在 plan.willDelete 里)`,
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
if (base.failed.length) {
|
|
649
|
+
const first = base.failed[0];
|
|
650
|
+
const more = base.failed.length > 1 ? `(另有 ${base.failed.length - 1} 个也失败,见 details.failed)` : '';
|
|
651
|
+
throw new CliError(
|
|
652
|
+
`push 失败:共 ${targets.length} 个文件要写,${base.written.length} 个成功、${base.failed.length} 个失败。` +
|
|
653
|
+
`第 ${first.index} 个文件 ${first.path}:${first.message}${more}`,
|
|
654
|
+
{
|
|
655
|
+
kind: 'push_failed',
|
|
656
|
+
details: { written: base.written, failed: base.failed, plan },
|
|
657
|
+
hint: '修好失败的文件再跑一次(已写进去的不会重复写);要看清单加 --dry-run',
|
|
658
|
+
},
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const next = manifestOf({
|
|
663
|
+
siteId: planned.manifest.siteId || '',
|
|
664
|
+
templateId: planned.manifest.templateId || '',
|
|
665
|
+
key: planned.manifest.key || '',
|
|
666
|
+
files: planned.local.map((f) => ({ path: f.path, bytes: f.bytes, sha256: f.sha256, encoding: f.encoding })),
|
|
667
|
+
at: planned.manifest.pulledAt || now().toISOString(),
|
|
668
|
+
});
|
|
669
|
+
next.pushedAt = now().toISOString();
|
|
670
|
+
writeManifest(planned.dir, next);
|
|
671
|
+
|
|
672
|
+
base.applied = true;
|
|
673
|
+
base.written_at = next.pushedAt;
|
|
674
|
+
base.contentHash = next.contentHash;
|
|
675
|
+
return base;
|
|
676
|
+
}
|