@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/manifest-apply.mjs
CHANGED
|
@@ -1,1600 +1,1650 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `wkgeo content apply` —— 把一份 `site-manifest.json` 一次落成「栏目 / 自定义字段 / 内容 / 单页 / 素材 / 导航」。
|
|
3
|
-
*
|
|
4
|
-
* ## 它在整个分工里的位置(老板 2026-10-30 定)
|
|
5
|
-
* A(agent)负责**理解来源、产出内容成品**;平台负责「**契约 + 校验 + 落库 + 发布**」。
|
|
6
|
-
* 所以这条命令**不做**任何抓取 / 解析 / 合成 —— 它只认契约:
|
|
7
|
-
* `docs/规范-建站内容契约.md`(人读)+ `cli/lib/manifest-schema.json`(机读)+ `cli/lib/manifest.mjs`(判定)
|
|
8
|
-
*
|
|
9
|
-
* ## 三条硬口径
|
|
10
|
-
* 1. **默认 dry-run**:不加 `--confirm` 一个字都不写库,只打逐项计划(将建 / 将改 / 将跳过 / 将失败)。
|
|
11
|
-
* 2. **幂等**:连跑两次,第二次全部 `skipped`。匹配口径:栏目按 slug、内容按 slug(缺省标题)、
|
|
12
|
-
* 素材按显示名;正文 HTML 不比原文(服务端 `sanitizeByEnv` 会改写),而是比 apply 记在
|
|
13
|
-
* 内容 `ext._wk_manifest.body_sha256` 里的指纹。
|
|
14
|
-
* 3. **失败不静默**:单条失败进 `failed[{item, reason}]`、退出码 1、**已成功的项保留**,
|
|
15
|
-
* 修完 manifest 重跑即可(失败项会重试,成功项会 skipped)。
|
|
16
|
-
*
|
|
17
|
-
* ## 退出码(与全仓口径一致)
|
|
18
|
-
* 0 全部成功(含全 skipped)| 1 有单项失败 | 2 manifest 校验 / 用法错误 | 3 未登录 / 授权过期
|
|
19
|
-
*
|
|
20
|
-
* ## 明确不做的事
|
|
21
|
-
* * ❌ 不建站点、不装模板、不改站名 / 语言(那是 `wkgeo sites create --template` / 后台的事);
|
|
22
|
-
* * ❌ 不碰账号、钱包、套餐、域名;
|
|
23
|
-
* * ❌ 不删任何东西(栏目 / 内容 / 素材删除都走各自命令,且要老板点头);
|
|
24
|
-
* * ❌ 不直连数据库、不直传 OSS 绕开 `assets` 三步(`assetSign` → OSS → `assetRegister` 就是官方通道)。
|
|
25
|
-
*
|
|
26
|
-
* ## 与 `--manifest` 的关系(重要,2026-10-30 task-178 已拆开)
|
|
27
|
-
* 模板内容契约那条线(`--manifest` + `sections[]`/`pages[]`,实现在 `cli/lib/content-apply.mjs`)
|
|
28
|
-
* 现在是**独立命令 `wkgeo content fill`**。本文件只负责**建站内容契约**(`--file` + `site-manifest.json`)。
|
|
29
|
-
* `run()` 里**不再**动态 import `content-apply.mjs` 兜底:`--manifest` 传进来直接报用法错
|
|
30
|
-
* (exit 2,提示改用 `wkgeo content fill`),避免两条契约线在同一命令里悄悄互相顶替。
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
import crypto from 'node:crypto';
|
|
34
|
-
import fs from 'node:fs';
|
|
35
|
-
import path from 'node:path';
|
|
36
|
-
|
|
37
|
-
import { uploadToOss } from './cms.mjs';
|
|
38
|
-
import { formatBytes, looksLikeUuid, matchSiteRow } from './commands/cms.mjs';
|
|
39
|
-
import { CliError, UsageError } from './errors.mjs';
|
|
40
|
-
import {
|
|
41
|
-
KIND_TO_CATEGORY_TYPE,
|
|
42
|
-
MANIFEST_FORMAT,
|
|
43
|
-
LIMITS,
|
|
44
|
-
flattenMenus,
|
|
45
|
-
manifestJsonSchema,
|
|
46
|
-
normalizePath,
|
|
47
|
-
sortCategoriesForApply,
|
|
48
|
-
summarizeManifest,
|
|
49
|
-
validateManifest,
|
|
50
|
-
} from './manifest.mjs';
|
|
51
|
-
|
|
52
|
-
/** 发布轮询间隔(毫秒) */
|
|
53
|
-
const PUBLISH_POLL_MS = 2000;
|
|
54
|
-
/** 素材列表翻页上限(每页 100 × N 桶) */
|
|
55
|
-
const ASSET_PAGE_CAP = 5;
|
|
56
|
-
/** 首页栏目 slug(与后台 `isHomeCategorySlug` 同一口径:首页永远进导航) */
|
|
57
|
-
const HOME_SLUGS = new Set(['home', 'index']);
|
|
58
|
-
|
|
59
|
-
/** task-95:建站模式(与三条云上链路同一口径:默认 optimize) */
|
|
60
|
-
const BUILD_MODES = ['optimize', 'origin_copy'];
|
|
61
|
-
const BUILD_MODE_LABEL = { optimize: '优化修改', origin_copy: '原样复制' };
|
|
62
|
-
function normalizeBuildMode(raw) {
|
|
63
|
-
const s = String(raw == null ? '' : raw).trim();
|
|
64
|
-
return BUILD_MODES.includes(s) ? s : 'optimize';
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 建站映射契约(`aliyun/fc/wkgeo-site-builder/fn/src/site-mapping-contract.mjs`)。
|
|
69
|
-
* 仓库内跑:直接加载,与网址仿站 / 资料建站共用同一份判据;
|
|
70
|
-
* npm 发布包(files 只含 cli/**)里没有 aliyun 目录 ⇒ 加载失败,退回 `manifest.mjs` 原判据,
|
|
71
|
-
* 行为与改造前一致(接缝见 docs/规范-建站-栏目与版块映射.md §5)。
|
|
72
|
-
*/
|
|
73
|
-
let siteMappingContractPromise = null;
|
|
74
|
-
function loadSiteMappingContract() {
|
|
75
|
-
if (!siteMappingContractPromise) {
|
|
76
|
-
const url = new URL('../../aliyun/fc/wkgeo-site-builder/fn/src/site-mapping-contract.mjs', import.meta.url);
|
|
77
|
-
siteMappingContractPromise = import(url.href).catch(() => null);
|
|
78
|
-
}
|
|
79
|
-
return siteMappingContractPromise;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 模板绑定契约(`aliyun/fc/_shared/template-binding-contract.js`,与后台 `categorySave`、
|
|
84
|
-
* 建站三条链路的自动绑定**同一份**)。
|
|
85
|
-
*
|
|
86
|
-
* 对话链路(`content apply`)里客户 manifest 可以指定 `categories[].templateFile`,
|
|
87
|
-
* 提交前先按这份判据试算一次:不合法 ⇒ **不提交 `template_path`**(回落前台按类型自动选)
|
|
88
|
-
* 并回一条 `⚠`,绝不静默错绑。npm 包里没有 aliyun 目录时加载失败 ⇒ 退回旧行为(照提交)。
|
|
89
|
-
*/
|
|
90
|
-
let templateBindingContractPromise = null;
|
|
91
|
-
function loadTemplateBindingContract() {
|
|
92
|
-
if (!templateBindingContractPromise) {
|
|
93
|
-
const url = new URL('../../aliyun/fc/_shared/template-binding-contract.js', import.meta.url);
|
|
94
|
-
templateBindingContractPromise = import(url.href).then(m => m.default || m).catch(() => null);
|
|
95
|
-
}
|
|
96
|
-
return templateBindingContractPromise;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/* ------------------------------------------------------------------ 命令定义 */
|
|
100
|
-
|
|
101
|
-
export const contentApplyCommand = {
|
|
102
|
-
name: 'content apply',
|
|
103
|
-
summary: '按 site-manifest.json 一次落站(契约校验 → dry-run 计划 → `--confirm` 幂等写库)',
|
|
104
|
-
usage:
|
|
105
|
-
'wkgeo content apply --file <site-manifest.json> [--site <站点ID|前缀>] [--confirm] [--publish] [--json]\n' +
|
|
106
|
-
' wkgeo content apply --schema # 打印本版契约的 JSON Schema',
|
|
107
|
-
args: [],
|
|
108
|
-
flags: {
|
|
109
|
-
file: { type: 'string', desc: '建站 manifest 路径(契约:docs/规范-建站内容契约.md)' },
|
|
110
|
-
site: { type: 'string', desc: '站点 ID / ID 前缀 / 域名 / 站名(缺省用 `wkgeo site use` 选中的站)' },
|
|
111
|
-
confirm: { type: 'boolean', desc: '真的写库;不加就是 dry-run(只打计划)' },
|
|
112
|
-
mode: {
|
|
113
|
-
type: 'string',
|
|
114
|
-
desc: '建站模式:optimize 优化修改(默认)/ origin_copy 原样复制;落站点,不改变写哪些内容',
|
|
115
|
-
},
|
|
116
|
-
'dry-run': { type: 'boolean', desc: '只打计划(默认行为,写上更明确)' },
|
|
117
|
-
publish: { type: 'boolean', desc: '落库成功后顺手提交一次发布(等价 `wkgeo publish --confirm`)' },
|
|
118
|
-
wait: { type: 'boolean', desc: '`--publish` 时等发布跑完(默认等;`--no-wait` 只提交)' },
|
|
119
|
-
'wait-timeout': { type: 'string', desc: '`--publish` 最多等多少秒(默认 600)' },
|
|
120
|
-
'allow-name-mismatch': {
|
|
121
|
-
type: 'boolean',
|
|
122
|
-
desc: '站点真名与 manifest.site.name 不一致时仍然执行(改名后 / 接手老站时用)',
|
|
123
|
-
},
|
|
124
|
-
schema: { type: 'boolean', desc: '打印 JSON Schema 后退出(不需要 --file,不碰网络)' },
|
|
125
|
-
/**
|
|
126
|
-
* ⚠️ 已迁走的参数:只保留一层拦截(隐藏、不进 `--help` 参数表)。
|
|
127
|
-
* 模板内容契约(`sections[]`/`pages[]`)现在是独立命令 `wkgeo content fill`;
|
|
128
|
-
* 这里留着这个 key 是为了把「拿旧命令写模板清单」的人挡在门前并给出正确命令名,
|
|
129
|
-
* 而不是抛一句「不认识的参数:--manifest」让人自己猜。
|
|
130
|
-
*/
|
|
131
|
-
manifest: {
|
|
132
|
-
type: 'string',
|
|
133
|
-
hidden: true,
|
|
134
|
-
desc: '(已迁走)模板内容清单改用 `wkgeo content fill --manifest`',
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
examples: [
|
|
138
|
-
'wkgeo content apply --schema > site-manifest.schema.json # 把契约给 A / 编辑器用',
|
|
139
|
-
'wkgeo content apply --file site-manifest.json # dry-run:逐项计划,不写库',
|
|
140
|
-
'wkgeo content apply --file site-manifest.json --confirm # 真写(幂等,重跑只 skipped)',
|
|
141
|
-
'wkgeo content apply --file site-manifest.json --site 8f3c1a2e --confirm --publish',
|
|
142
|
-
],
|
|
143
|
-
notes: [
|
|
144
|
-
'**先 dry-run 再 --confirm**:dry-run 会把「将建 / 将改 / 将跳过 / 将失败」逐条列出来,老板看一遍再落库。',
|
|
145
|
-
'**幂等**:栏目按 slug、内容按 slug(缺省标题)、素材按显示名认领已有对象;连跑两次第二次全 `skipped`。',
|
|
146
|
-
'**单条失败不静默**:`failed[{item, reason}]` + 退出码 1,已成功项保留;修完 manifest 重跑即可续上。',
|
|
147
|
-
'**契约必填 `unfilled`**:没填的项逐条写清 `{item, reason}`(来源缺失 / 需老板确认 / 版权不明 / 平台不支持),全填齐写 `[]`。',
|
|
148
|
-
'**不含**建站 / 装模板 / 删数据:装模板走 `wkgeo sites create --template`,删除走 `categories|contents|assets delete`。',
|
|
149
|
-
'写的内容 `ext` 里会留一条 `_wk_manifest`(正文指纹 + 来源),这是幂等标记,别手工删。',
|
|
150
|
-
],
|
|
151
|
-
async run(ctx) {
|
|
152
|
-
/**
|
|
153
|
-
* 两条契约线已拆成两条命令(task-178):
|
|
154
|
-
* `content apply` = 建站清单(`--file site-manifest.json`)
|
|
155
|
-
* `content fill` = 模板内容契约(`--manifest`)
|
|
156
|
-
* 这里只做拦截 —— **不再**动态 import `content-apply.mjs` 兜底,
|
|
157
|
-
* 否则「拿旧命令写模板清单」会静默走另一条线,CLI 表面又少一条命令。
|
|
158
|
-
*/
|
|
159
|
-
if (ctx.flags.manifest) {
|
|
160
|
-
throw new UsageError(
|
|
161
|
-
'这是模板内容契约命令,请用 `wkgeo content fill --manifest <文件>`(`--manifest` 已从 `content apply` 拆出;' +
|
|
162
|
-
'`content apply` 只收 `--file <site-manifest.json>`)',
|
|
163
|
-
{ hint: '契约见 docs/规范-模板内容契约.md §5;建站清单契约见 docs/规范-建站内容契约.md' },
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
return runApply(ctx);
|
|
167
|
-
},
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
/* ------------------------------------------------------------------ 主流程 */
|
|
171
|
-
|
|
172
|
-
async function runApply(ctx) {
|
|
173
|
-
const { out } = ctx;
|
|
174
|
-
|
|
175
|
-
/* ── 0. `--schema`:不碰网络、不读 manifest */
|
|
176
|
-
if (ctx.flags.schema) {
|
|
177
|
-
const schema = manifestJsonSchema();
|
|
178
|
-
const data = { format: MANIFEST_FORMAT, schema };
|
|
179
|
-
if (out.json) ctx.done(data);
|
|
180
|
-
else out.raw(JSON.stringify(schema, null, 2));
|
|
181
|
-
return data;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/* ── 1. 读文件 + 契约校验(一切写库动作之前) */
|
|
185
|
-
const fileFlag = String(ctx.flags.file || '').trim();
|
|
186
|
-
if (!fileFlag) {
|
|
187
|
-
throw new UsageError('缺少 --file <site-manifest.json>', {
|
|
188
|
-
hint: '契约见 docs/规范-建站内容契约.md;`wkgeo content apply --schema` 可打印 JSON Schema。',
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
const abs = path.resolve(process.cwd(), fileFlag);
|
|
192
|
-
let stat = null;
|
|
193
|
-
try {
|
|
194
|
-
stat = fs.statSync(abs);
|
|
195
|
-
} catch {
|
|
196
|
-
stat = null;
|
|
197
|
-
}
|
|
198
|
-
if (!stat || !stat.isFile()) throw new UsageError(`manifest 文件不存在:${abs}`);
|
|
199
|
-
|
|
200
|
-
let doc = null;
|
|
201
|
-
try {
|
|
202
|
-
doc = JSON.parse(fs.readFileSync(abs, 'utf8'));
|
|
203
|
-
} catch (err) {
|
|
204
|
-
throw new UsageError(`manifest 不是合法 JSON:${(err && err.message) || err}`, {
|
|
205
|
-
hint: `检查 ${abs}(BOM / 尾逗号 / 注释都会让 JSON.parse 失败)。`,
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const report = validateManifest(doc, { baseDir: path.dirname(abs) });
|
|
210
|
-
const { manifest, errors, itemErrors, warnings } = report;
|
|
211
|
-
|
|
212
|
-
/* ── 1.5 建站模式(task-95):`--mode` 优先,其次 manifest.buildMode,默认 optimize */
|
|
213
|
-
const modeFlag = String(ctx.flags.mode == null ? '' : ctx.flags.mode).trim();
|
|
214
|
-
if (modeFlag && !BUILD_MODES.includes(modeFlag)) {
|
|
215
|
-
throw new UsageError(`--mode 只认 optimize / origin_copy:收到 "${modeFlag}"`, {
|
|
216
|
-
hint: 'optimize=优化修改(默认);origin_copy=尽量保持原站结构视觉。',
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
const buildMode = normalizeBuildMode(modeFlag || manifest.site.buildMode);
|
|
220
|
-
|
|
221
|
-
if (!out.json) printValidation(out, { file: abs, errors, itemErrors, warnings });
|
|
222
|
-
if (errors.length) {
|
|
223
|
-
const err = new UsageError(`manifest 契约校验未通过:${errors.length} 处致命问题(一条都没写库)`);
|
|
224
|
-
err.details = { file: abs, errors, itemErrors, warnings };
|
|
225
|
-
throw err;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/* ── 2. 登录态 + 站点(顺序:先授权再解析站,未登录时不猜站) */
|
|
229
|
-
await ctx.requireAuth();
|
|
230
|
-
const site = await resolveSiteForApply(ctx);
|
|
231
|
-
const siteRow = await siteRowOf(ctx, site);
|
|
232
|
-
const actualName = String((siteRow && siteRow.name) || site.label || '').trim();
|
|
233
|
-
|
|
234
|
-
/* ── 3. 站名核对:指错站比写错内容严重得多 */
|
|
235
|
-
if (manifest.site.name && actualName && manifest.site.name !== actualName && !ctx.flags.allowNameMismatch) {
|
|
236
|
-
throw new UsageError(
|
|
237
|
-
`站点名不一致:manifest.site.name = "${manifest.site.name}",而 --site 指到的是 "${actualName}"`,
|
|
238
|
-
{ hint: '确认 --site 指对了站;站点确实改过名就同步 manifest.site.name,或加 `--allow-name-mismatch`。' },
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/* ── 4. 执行(dry-run 只算计划) */
|
|
243
|
-
const dryRun = !ctx.flags.confirm;
|
|
244
|
-
const engine = createApplyEngine({ ctx, site, manifest, itemErrors, warnings, dryRun, file: abs });
|
|
245
|
-
const result = await engine.run();
|
|
246
|
-
|
|
247
|
-
const data = {
|
|
248
|
-
dry_run: dryRun,
|
|
249
|
-
file: abs,
|
|
250
|
-
format: MANIFEST_FORMAT,
|
|
251
|
-
build_mode: buildMode,
|
|
252
|
-
build_mode_label: BUILD_MODE_LABEL[buildMode],
|
|
253
|
-
site: { id: site.id, name: actualName, source: site.source },
|
|
254
|
-
manifest: manifest.counts,
|
|
255
|
-
notes: result.notes,
|
|
256
|
-
warnings,
|
|
257
|
-
counts: result.counts,
|
|
258
|
-
created: result.created,
|
|
259
|
-
updated: result.updated,
|
|
260
|
-
skipped: result.skipped,
|
|
261
|
-
failed: result.failed,
|
|
262
|
-
items: result.items,
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
/* ── 4.5 把建站模式记到站点上(task-95;best-effort,dry-run 不写) */
|
|
266
|
-
data.build_mode_saved = false;
|
|
267
|
-
if (!dryRun) {
|
|
268
|
-
try {
|
|
269
|
-
if (typeof ctx.siteCoreCall === 'function') {
|
|
270
|
-
await ctx.siteCoreCall('siteBuildModeSave', { buildMode }, { siteId: site.id });
|
|
271
|
-
data.build_mode_saved = true;
|
|
272
|
-
}
|
|
273
|
-
} catch (e) {
|
|
274
|
-
warnings.push({
|
|
275
|
-
item: 'buildMode',
|
|
276
|
-
reason: `建站模式没记到站点(不影响内容落库):${(e && e.message) || e}`,
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/* ── 5. 可选发布 */
|
|
282
|
-
if (ctx.flags.publish && dryRun) {
|
|
283
|
-
data.publish = { skipped: true, reason: 'dry-run 不发布' };
|
|
284
|
-
} else if (ctx.flags.publish && result.failed.length === 0) {
|
|
285
|
-
const publish = await publishAfterApply(ctx, site, {
|
|
286
|
-
wait: ctx.flags.wait !== false,
|
|
287
|
-
timeoutSec: Number(ctx.flags.waitTimeout) || 600,
|
|
288
|
-
});
|
|
289
|
-
data.publish = publish;
|
|
290
|
-
if (publish && publish.ok === false) {
|
|
291
|
-
result.failed.push({ item: 'publish', reason: publish.reason || '发布未成功', stage: 'publish' });
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (!out.json) printApplyReport(out, data, manifest);
|
|
296
|
-
|
|
297
|
-
/* ── 6. 退出码:0 全成功 / 1 有单项失败(2、3 由抛错决定) */
|
|
298
|
-
if (result.failed.length) {
|
|
299
|
-
const err = new CliError(`apply 完成,但有 ${result.failed.length} 项失败`, {
|
|
300
|
-
kind: 'partial',
|
|
301
|
-
hint: '已成功项保留;改完 manifest 重跑即可(幂等,成功项会 skipped)。',
|
|
302
|
-
});
|
|
303
|
-
err.details = data;
|
|
304
|
-
throw err;
|
|
305
|
-
}
|
|
306
|
-
ctx.done(data);
|
|
307
|
-
return data;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/* ------------------------------------------------------------------ 站解析 */
|
|
311
|
-
|
|
312
|
-
async function resolveSiteForApply(ctx) {
|
|
313
|
-
const wanted = String(ctx.flags.site || '').trim();
|
|
314
|
-
if (!wanted) {
|
|
315
|
-
const site = ctx.cmsSite();
|
|
316
|
-
if (!site || !site.id) {
|
|
317
|
-
throw new UsageError('没选站点:加 `--site <站点ID>` 或先 `wkgeo site use <站点ID>`', {
|
|
318
|
-
hint: 'apply 会往这个站写栏目和内容,绝不替你猜站。',
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
return { id: String(site.id), label: site.label || '', source: site.source || 'local', row: null };
|
|
322
|
-
}
|
|
323
|
-
if (looksLikeUuid(wanted)) return { id: wanted, label: '', source: 'flag', row: null };
|
|
324
|
-
|
|
325
|
-
const res = await ctx.callData('listSites', { limit: 200 });
|
|
326
|
-
const rows = (res && Array.isArray(res.rows) ? res.rows : []).map((r) => r || {});
|
|
327
|
-
const needle = wanted.toLowerCase();
|
|
328
|
-
const byPrefix = rows.filter((r) => String(r.id || '').toLowerCase().startsWith(needle));
|
|
329
|
-
if (byPrefix.length === 1) {
|
|
330
|
-
const row = byPrefix[0];
|
|
331
|
-
return { id: String(row.id), label: String(row.name || ''), source: 'flag', row };
|
|
332
|
-
}
|
|
333
|
-
if (byPrefix.length > 1) {
|
|
334
|
-
throw new UsageError(`站点前缀「${wanted}」匹配到 ${byPrefix.length} 个站`, {
|
|
335
|
-
hint: `给长一点的前缀:${byPrefix.slice(0, 5).map((r) => `${r.name || '(未命名)'}=${r.id}`).join(';')}`,
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
const hit = matchSiteRow(rows, wanted);
|
|
339
|
-
if (hit) return { id: String(hit.id), label: String(hit.label || hit.name || ''), source: 'flag', row: null };
|
|
340
|
-
throw new CliError(`没有找到站点「${wanted}」(按 ID 前缀 / 域名 / 站名都没匹配上)`, {
|
|
341
|
-
kind: 'not_found',
|
|
342
|
-
code: 404,
|
|
343
|
-
hint: rows.length
|
|
344
|
-
? `你名下的站:${rows.slice(0, 10).map((r) => `${r.name || '(未命名)'}=${r.id}`).join(';')}`
|
|
345
|
-
: '当前账号名下没有站点(协作站不在列表里)—— 请直接用站点 ID。',
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/** 站点那一行(`listSites` 结果里按 id 找;找不到就 null) */
|
|
350
|
-
async function siteRowOf(ctx, site) {
|
|
351
|
-
if (site && site.row) return site.row;
|
|
352
|
-
try {
|
|
353
|
-
const res = await ctx.callData('listSites', { limit: 200 });
|
|
354
|
-
const rows = (res && Array.isArray(res.rows) ? res.rows : []).map((r) => r || {});
|
|
355
|
-
return rows.find((r) => String(r.id || '') === String(site.id)) || null;
|
|
356
|
-
} catch {
|
|
357
|
-
return null;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/* ------------------------------------------------------------------ 校验输出 */
|
|
362
|
-
|
|
363
|
-
function printValidation(out, { file, errors, itemErrors, warnings }) {
|
|
364
|
-
out.head(`建站内容契约校验:${oneLine(file, 90)}`);
|
|
365
|
-
for (const w of warnings) out.warn(` ⚠ ${w.path} ${w.message}`);
|
|
366
|
-
if (itemErrors.length) {
|
|
367
|
-
out.warn(` 单条问题 ${itemErrors.length} 处(这些条目会记 failed,其余照常执行):`);
|
|
368
|
-
for (const one of itemErrors) out.line(` · ${one.item} ${one.reason}`);
|
|
369
|
-
}
|
|
370
|
-
if (errors.length) {
|
|
371
|
-
out.err(` 致命问题 ${errors.length} 处:`);
|
|
372
|
-
for (const e of errors) out.err(` ✗ ${e.path} ${e.message}`);
|
|
373
|
-
out.line('');
|
|
374
|
-
out.info(' 契约:docs/规范-建站内容契约.md | Schema:`wkgeo content apply --schema`');
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/* ------------------------------------------------------------------ 执行引擎 */
|
|
379
|
-
|
|
380
|
-
function createApplyEngine({ ctx, site, manifest, itemErrors, warnings, dryRun }) {
|
|
381
|
-
const siteId = String(site.id);
|
|
382
|
-
const call = (action, payload) => ctx.contentCall(action, payload || {}, { siteId });
|
|
383
|
-
const siteCore = (action, payload) => ctx.siteCoreCall(action, payload || {}, { siteId });
|
|
384
|
-
|
|
385
|
-
/** 本站模板说明(`template.json`);一次运行只拉一次,失败 / 没有 ⇒ `null`(不拦,退回旧行为) */
|
|
386
|
-
let templateMetaPromise = null;
|
|
387
|
-
function loadSiteTemplateMeta() {
|
|
388
|
-
if (!templateMetaPromise) {
|
|
389
|
-
templateMetaPromise = (async () => {
|
|
390
|
-
try {
|
|
391
|
-
const got = await call('templateGet', {});
|
|
392
|
-
const files = arrayOf((got && got.files) || []);
|
|
393
|
-
const row = files.find(one => String((one && one.path) || '').toLowerCase() === 'template.json');
|
|
394
|
-
if (!row || !row.content) return null;
|
|
395
|
-
const parsed = JSON.parse(String(row.content));
|
|
396
|
-
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
397
|
-
} catch {
|
|
398
|
-
return null;
|
|
399
|
-
}
|
|
400
|
-
})();
|
|
401
|
-
}
|
|
402
|
-
return templateMetaPromise;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* **模板能不能绑这个栏目**(task-52,判据与后台保存 / 建站链路同一份)。
|
|
407
|
-
*
|
|
408
|
-
* @returns {Promise<null|{ok:boolean,code:string,message:string}>} `null` = 判不了(无契约 / 无模板说明)
|
|
409
|
-
*/
|
|
410
|
-
async function templateBindingVerdict(column, templatePath) {
|
|
411
|
-
if (!templatePath) return null;
|
|
412
|
-
const bindingContract = await loadTemplateBindingContract();
|
|
413
|
-
if (!bindingContract || typeof bindingContract.validateBinding !== 'function') return null;
|
|
414
|
-
const meta = await loadSiteTemplateMeta();
|
|
415
|
-
if (!meta) return null;
|
|
416
|
-
try {
|
|
417
|
-
return bindingContract.validateBinding(meta, { ...column, templatePath });
|
|
418
|
-
} catch {
|
|
419
|
-
return null;
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
const items = [];
|
|
424
|
-
const failed = [];
|
|
425
|
-
const notes = [];
|
|
426
|
-
const blocked = new Set();
|
|
427
|
-
for (const one of itemErrors) {
|
|
428
|
-
blocked.add(one.item);
|
|
429
|
-
failed.push({ item: one.item, reason: one.reason, stage: 'validate' });
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
const state = {
|
|
433
|
-
cats: [],
|
|
434
|
-
catBySlug: new Map(),
|
|
435
|
-
catByPath: new Map(),
|
|
436
|
-
catIdBySlug: new Map(), // manifest slug → {id, path, existed, type, kind}
|
|
437
|
-
assets: [],
|
|
438
|
-
assetIdByKey: new Map(),
|
|
439
|
-
contentsByCat: new Map(),
|
|
440
|
-
siteInfo: null,
|
|
441
|
-
};
|
|
442
|
-
|
|
443
|
-
const push = (entry) => {
|
|
444
|
-
const row = {
|
|
445
|
-
kind: entry.kind,
|
|
446
|
-
item: entry.item,
|
|
447
|
-
action: dryRun ? entry.planned : entry.action,
|
|
448
|
-
label: entry.label || '',
|
|
449
|
-
detail: entry.detail || '',
|
|
450
|
-
};
|
|
451
|
-
items.push(row);
|
|
452
|
-
return row;
|
|
453
|
-
};
|
|
454
|
-
|
|
455
|
-
/** 逐条执行(真跑);失败就改记 fail,不打断后面的条目 */
|
|
456
|
-
async function exec(entries) {
|
|
457
|
-
for (const entry of entries) {
|
|
458
|
-
const row = push(entry);
|
|
459
|
-
if (dryRun || typeof entry.run !== 'function') continue;
|
|
460
|
-
try {
|
|
461
|
-
await entry.run(row);
|
|
462
|
-
row.action = entry.planned;
|
|
463
|
-
row.detail = entry.after || entry.detail || row.detail;
|
|
464
|
-
} catch (err) {
|
|
465
|
-
row.action = 'fail';
|
|
466
|
-
row.detail = errorText(err);
|
|
467
|
-
failed.push({ item: entry.item, reason: row.detail, stage: entry.kind });
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
const markFail = (entry, reason) => {
|
|
473
|
-
entry.planned = 'fail';
|
|
474
|
-
entry.action = 'fail';
|
|
475
|
-
entry.detail = reason;
|
|
476
|
-
failed.push({ item: entry.item, reason, stage: entry.kind });
|
|
477
|
-
return entry;
|
|
478
|
-
};
|
|
479
|
-
|
|
480
|
-
/* ── 载入现状 ─────────────────────────────────────────────── */
|
|
481
|
-
async function loadExisting() {
|
|
482
|
-
state.cats = arrayOf(await call('categoryList'));
|
|
483
|
-
for (const row of state.cats) {
|
|
484
|
-
const slug = String(row.slug || '').toLowerCase();
|
|
485
|
-
if (slug) state.catBySlug.set(slug, row);
|
|
486
|
-
const p = String(row.path || '');
|
|
487
|
-
if (p) state.catByPath.set(normalizePath(p), row);
|
|
488
|
-
}
|
|
489
|
-
state.assets = await loadAllAssets();
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
async function loadAllAssets() {
|
|
493
|
-
const rows = [];
|
|
494
|
-
for (let page = 1; page <= ASSET_PAGE_CAP; page += 1) {
|
|
495
|
-
const res = await call('assetList', { status: 'normal', kind: '', page, pageSize: 100 });
|
|
496
|
-
const batch = (res && Array.isArray(res.rows) ? res.rows : []).map((r) => r || {});
|
|
497
|
-
rows.push(...batch);
|
|
498
|
-
const total = Number(res && res.total) || rows.length;
|
|
499
|
-
if (batch.length < 100 || rows.length >= total) break;
|
|
500
|
-
}
|
|
501
|
-
return rows;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
/* ── 预检:跨引用必须在写任何东西之前就有结论(exit 2) ────── */
|
|
505
|
-
function preflight() {
|
|
506
|
-
const problems = [];
|
|
507
|
-
const knowCat = (slug) => state.catBySlug.has(slug) || manifest.categories.some((c) => c.slug === slug);
|
|
508
|
-
const typeOf = (slug) => {
|
|
509
|
-
const row = state.catBySlug.get(slug);
|
|
510
|
-
if (row) return KIND_TO_CATEGORY_TYPE[String(row.kind || '')] || String(row.kind || '');
|
|
511
|
-
const cat = manifest.categories.find((c) => c.slug === slug);
|
|
512
|
-
return cat ? cat.type : '';
|
|
513
|
-
};
|
|
514
|
-
for (const c of manifest.categories) {
|
|
515
|
-
if (c.parent && !knowCat(c.parent)) {
|
|
516
|
-
problems.push(`categories 里「${c.slug}」的 parent「${c.parent}」在 manifest 和站点上都找不到`);
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
for (const c of manifest.categories) {
|
|
520
|
-
if (!c.path) continue;
|
|
521
|
-
const hit = state.catByPath.get(c.path);
|
|
522
|
-
if (hit && String(hit.slug || '').toLowerCase() !== c.slug) {
|
|
523
|
-
problems.push(`categories 里「${c.slug}」声明的 path "${c.path}" 已是栏目「${hit.slug}」的路径`);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
for (const c of manifest.contents) {
|
|
527
|
-
if (!knowCat(c.categorySlug)) {
|
|
528
|
-
problems.push(`contents 里「${c.item}」挂的栏目「${c.categorySlug}」在 manifest 和站点上都找不到`);
|
|
529
|
-
} else if (typeOf(c.categorySlug) === 'single') {
|
|
530
|
-
problems.push(`contents 里「${c.item}」挂到了单页栏目「${c.categorySlug}」—— 单页内容请写进 singles[]`);
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
for (const one of manifest.singles) {
|
|
534
|
-
if (!knowCat(one.categorySlug)) {
|
|
535
|
-
problems.push(`singles 里「${one.item}」绑的栏目「${one.categorySlug}」在 manifest 和站点上都找不到`);
|
|
536
|
-
} else if (typeOf(one.categorySlug) !== 'single') {
|
|
537
|
-
problems.push(`singles 里「${one.item}」绑的栏目「${one.categorySlug}」不是单页(类型 ${typeOf(one.categorySlug) || '未知'})`);
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
for (const menu of flattenMenus(manifest.menus)) {
|
|
541
|
-
if (!menu.path) continue;
|
|
542
|
-
const hit = state.catByPath.get(menu.path);
|
|
543
|
-
const declared = manifest.categories.find((c) => c.path === menu.path);
|
|
544
|
-
if (!hit && !declared) problems.push(`menus 里「${menu.label}」的 path "${menu.path}" 没有对应栏目`);
|
|
545
|
-
}
|
|
546
|
-
if (problems.length) {
|
|
547
|
-
const err = new UsageError(`manifest 跨引用校验未通过:${problems.length} 处(一条都没写库)`, {
|
|
548
|
-
hint: '先把栏目建出来(同一个 manifest 的 categories[] 里写上),或者修正引用。',
|
|
549
|
-
});
|
|
550
|
-
err.details = { problems };
|
|
551
|
-
if (!ctx.out.json) for (const p of problems) ctx.out.err(` ✗ ${p}`);
|
|
552
|
-
throw err;
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
/* ── 素材 ─────────────────────────────────────────────────── */
|
|
557
|
-
async function stageAssets() {
|
|
558
|
-
const entries = [];
|
|
559
|
-
/** 一条素材可能有多个 key(同一份文件的别名):都指向同一个素材 ID */
|
|
560
|
-
const rememberId = (a, id) => {
|
|
561
|
-
state.assetIdByKey.set(a.key, id);
|
|
562
|
-
for (const alias of a.aliases || []) state.assetIdByKey.set(alias, id);
|
|
563
|
-
};
|
|
564
|
-
for (const a of manifest.assets) {
|
|
565
|
-
const item = `asset:${a.key}`;
|
|
566
|
-
if (blocked.has(item)) continue;
|
|
567
|
-
const role = a.role || 'file';
|
|
568
|
-
const hit = state.assets.find((r) => String(r.name || '').trim().toLowerCase() === a.name.toLowerCase());
|
|
569
|
-
if (hit) {
|
|
570
|
-
if (!dryRun) rememberId(a, String(hit.id));
|
|
571
|
-
entries.push({
|
|
572
|
-
kind: 'asset',
|
|
573
|
-
item,
|
|
574
|
-
planned: 'skip',
|
|
575
|
-
action: 'skip',
|
|
576
|
-
label: a.name,
|
|
577
|
-
detail: `已有素材 ${hit.id}(${formatBytes(Number(hit.size) || a.bytes)})`,
|
|
578
|
-
});
|
|
579
|
-
continue;
|
|
580
|
-
}
|
|
581
|
-
const entry = {
|
|
582
|
-
kind: 'asset',
|
|
583
|
-
item,
|
|
584
|
-
planned: 'create',
|
|
585
|
-
action: 'create',
|
|
586
|
-
label: a.name,
|
|
587
|
-
detail: `上传 ${formatBytes(a.bytes)} → ${a.use}/${role}`,
|
|
588
|
-
};
|
|
589
|
-
entry.run = async (row) => {
|
|
590
|
-
const fileName = path.basename(a.path);
|
|
591
|
-
/**
|
|
592
|
-
* `role` 一并透传(task-46):服务端 `assetSign` / `assetRegister` 现在**不认**这个字段,
|
|
593
|
-
* 多传会被静默忽略、不会报错;等它补了 `asset.role` 入参这边就自动生效。
|
|
594
|
-
*/
|
|
595
|
-
const ticket = await call('assetSign', { fileName, bytes: a.bytes, role });
|
|
596
|
-
if (!ticket || !ticket.key) {
|
|
597
|
-
throw new CliError('服务端没有返回直传票据(assetSign)', { hint: '稍后重试;票据 10 分钟内有效' });
|
|
598
|
-
}
|
|
599
|
-
const buffer = fs.readFileSync(a.path);
|
|
600
|
-
await uploadToOss({ ticket, buffer, fileName, fetchImpl: ctx.fetchImpl });
|
|
601
|
-
const asset = await call('assetRegister', { key: ticket.key, name: a.name, role });
|
|
602
|
-
const id = String((asset && (asset.id || asset.assetId)) || '');
|
|
603
|
-
if (!id) throw new CliError('素材登记成功但没回素材 ID(assetRegister)', { hint: '去后台素材库确认一下' });
|
|
604
|
-
rememberId(a, id);
|
|
605
|
-
row.detail = `已登记素材 ${id}(${a.use}/${role})`;
|
|
606
|
-
};
|
|
607
|
-
entries.push(entry);
|
|
608
|
-
}
|
|
609
|
-
return entries;
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
/**
|
|
613
|
-
* **manifest 里手写的 `templateFile` 先过一遍契约**(task-52)。
|
|
614
|
-
*
|
|
615
|
-
* 对不上 ⇒ 返回 `''`(不提交 `template_path`,前台回落"按类型自动选")并回一条 `⚠`;
|
|
616
|
-
* 对不上还硬提交的话,后台 `categorySave` 会 400 把整条栏目卡掉 —— 那才是更糟的结果。
|
|
617
|
-
* 判不了(无契约 / 模板没说明)⇒ 原样返回,行为与改造前一致。
|
|
618
|
-
*/
|
|
619
|
-
async function resolveManifestTemplateFile(cat, kind) {
|
|
620
|
-
const raw = String(cat.templateFile || '').trim();
|
|
621
|
-
if (!raw) return '';
|
|
622
|
-
const verdict = await templateBindingVerdict(
|
|
623
|
-
{
|
|
624
|
-
key: cat.slug,
|
|
625
|
-
name: cat.name,
|
|
626
|
-
kind,
|
|
627
|
-
fieldsEnabled: null,
|
|
628
|
-
customFields: (cat.fields || []).map(one => String((one && one.key) || one || '')),
|
|
629
|
-
},
|
|
630
|
-
raw,
|
|
631
|
-
);
|
|
632
|
-
if (!verdict || verdict.ok !== false) return raw;
|
|
633
|
-
warnings.push({
|
|
634
|
-
path: `categories.${cat.slug}.templateFile`,
|
|
635
|
-
message: `模板 ${raw} 与栏目类型不匹配,已跳过绑定(前台按类型自动选):${verdict.message}`,
|
|
636
|
-
});
|
|
637
|
-
return '';
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
}
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
const
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
const
|
|
1107
|
-
const
|
|
1108
|
-
if (!
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
}
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
const
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
const
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
if (
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
}
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
.
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
const
|
|
1393
|
-
const
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
changes.push(
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
}
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
const
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
const
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
}
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
}
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
if (!
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1
|
+
/**
|
|
2
|
+
* `wkgeo content apply` —— 把一份 `site-manifest.json` 一次落成「栏目 / 自定义字段 / 内容 / 单页 / 素材 / 导航」。
|
|
3
|
+
*
|
|
4
|
+
* ## 它在整个分工里的位置(老板 2026-10-30 定)
|
|
5
|
+
* A(agent)负责**理解来源、产出内容成品**;平台负责「**契约 + 校验 + 落库 + 发布**」。
|
|
6
|
+
* 所以这条命令**不做**任何抓取 / 解析 / 合成 —— 它只认契约:
|
|
7
|
+
* `docs/规范-建站内容契约.md`(人读)+ `cli/lib/manifest-schema.json`(机读)+ `cli/lib/manifest.mjs`(判定)
|
|
8
|
+
*
|
|
9
|
+
* ## 三条硬口径
|
|
10
|
+
* 1. **默认 dry-run**:不加 `--confirm` 一个字都不写库,只打逐项计划(将建 / 将改 / 将跳过 / 将失败)。
|
|
11
|
+
* 2. **幂等**:连跑两次,第二次全部 `skipped`。匹配口径:栏目按 slug、内容按 slug(缺省标题)、
|
|
12
|
+
* 素材按显示名;正文 HTML 不比原文(服务端 `sanitizeByEnv` 会改写),而是比 apply 记在
|
|
13
|
+
* 内容 `ext._wk_manifest.body_sha256` 里的指纹。
|
|
14
|
+
* 3. **失败不静默**:单条失败进 `failed[{item, reason}]`、退出码 1、**已成功的项保留**,
|
|
15
|
+
* 修完 manifest 重跑即可(失败项会重试,成功项会 skipped)。
|
|
16
|
+
*
|
|
17
|
+
* ## 退出码(与全仓口径一致)
|
|
18
|
+
* 0 全部成功(含全 skipped)| 1 有单项失败 | 2 manifest 校验 / 用法错误 | 3 未登录 / 授权过期
|
|
19
|
+
*
|
|
20
|
+
* ## 明确不做的事
|
|
21
|
+
* * ❌ 不建站点、不装模板、不改站名 / 语言(那是 `wkgeo sites create --template` / 后台的事);
|
|
22
|
+
* * ❌ 不碰账号、钱包、套餐、域名;
|
|
23
|
+
* * ❌ 不删任何东西(栏目 / 内容 / 素材删除都走各自命令,且要老板点头);
|
|
24
|
+
* * ❌ 不直连数据库、不直传 OSS 绕开 `assets` 三步(`assetSign` → OSS → `assetRegister` 就是官方通道)。
|
|
25
|
+
*
|
|
26
|
+
* ## 与 `--manifest` 的关系(重要,2026-10-30 task-178 已拆开)
|
|
27
|
+
* 模板内容契约那条线(`--manifest` + `sections[]`/`pages[]`,实现在 `cli/lib/content-apply.mjs`)
|
|
28
|
+
* 现在是**独立命令 `wkgeo content fill`**。本文件只负责**建站内容契约**(`--file` + `site-manifest.json`)。
|
|
29
|
+
* `run()` 里**不再**动态 import `content-apply.mjs` 兜底:`--manifest` 传进来直接报用法错
|
|
30
|
+
* (exit 2,提示改用 `wkgeo content fill`),避免两条契约线在同一命令里悄悄互相顶替。
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import crypto from 'node:crypto';
|
|
34
|
+
import fs from 'node:fs';
|
|
35
|
+
import path from 'node:path';
|
|
36
|
+
|
|
37
|
+
import { uploadToOss } from './cms.mjs';
|
|
38
|
+
import { formatBytes, looksLikeUuid, matchSiteRow } from './commands/cms.mjs';
|
|
39
|
+
import { CliError, UsageError } from './errors.mjs';
|
|
40
|
+
import {
|
|
41
|
+
KIND_TO_CATEGORY_TYPE,
|
|
42
|
+
MANIFEST_FORMAT,
|
|
43
|
+
LIMITS,
|
|
44
|
+
flattenMenus,
|
|
45
|
+
manifestJsonSchema,
|
|
46
|
+
normalizePath,
|
|
47
|
+
sortCategoriesForApply,
|
|
48
|
+
summarizeManifest,
|
|
49
|
+
validateManifest,
|
|
50
|
+
} from './manifest.mjs';
|
|
51
|
+
|
|
52
|
+
/** 发布轮询间隔(毫秒) */
|
|
53
|
+
const PUBLISH_POLL_MS = 2000;
|
|
54
|
+
/** 素材列表翻页上限(每页 100 × N 桶) */
|
|
55
|
+
const ASSET_PAGE_CAP = 5;
|
|
56
|
+
/** 首页栏目 slug(与后台 `isHomeCategorySlug` 同一口径:首页永远进导航) */
|
|
57
|
+
const HOME_SLUGS = new Set(['home', 'index']);
|
|
58
|
+
|
|
59
|
+
/** task-95:建站模式(与三条云上链路同一口径:默认 optimize) */
|
|
60
|
+
const BUILD_MODES = ['optimize', 'origin_copy'];
|
|
61
|
+
const BUILD_MODE_LABEL = { optimize: '优化修改', origin_copy: '原样复制' };
|
|
62
|
+
function normalizeBuildMode(raw) {
|
|
63
|
+
const s = String(raw == null ? '' : raw).trim();
|
|
64
|
+
return BUILD_MODES.includes(s) ? s : 'optimize';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 建站映射契约(`aliyun/fc/wkgeo-site-builder/fn/src/site-mapping-contract.mjs`)。
|
|
69
|
+
* 仓库内跑:直接加载,与网址仿站 / 资料建站共用同一份判据;
|
|
70
|
+
* npm 发布包(files 只含 cli/**)里没有 aliyun 目录 ⇒ 加载失败,退回 `manifest.mjs` 原判据,
|
|
71
|
+
* 行为与改造前一致(接缝见 docs/规范-建站-栏目与版块映射.md §5)。
|
|
72
|
+
*/
|
|
73
|
+
let siteMappingContractPromise = null;
|
|
74
|
+
function loadSiteMappingContract() {
|
|
75
|
+
if (!siteMappingContractPromise) {
|
|
76
|
+
const url = new URL('../../aliyun/fc/wkgeo-site-builder/fn/src/site-mapping-contract.mjs', import.meta.url);
|
|
77
|
+
siteMappingContractPromise = import(url.href).catch(() => null);
|
|
78
|
+
}
|
|
79
|
+
return siteMappingContractPromise;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 模板绑定契约(`aliyun/fc/_shared/template-binding-contract.js`,与后台 `categorySave`、
|
|
84
|
+
* 建站三条链路的自动绑定**同一份**)。
|
|
85
|
+
*
|
|
86
|
+
* 对话链路(`content apply`)里客户 manifest 可以指定 `categories[].templateFile`,
|
|
87
|
+
* 提交前先按这份判据试算一次:不合法 ⇒ **不提交 `template_path`**(回落前台按类型自动选)
|
|
88
|
+
* 并回一条 `⚠`,绝不静默错绑。npm 包里没有 aliyun 目录时加载失败 ⇒ 退回旧行为(照提交)。
|
|
89
|
+
*/
|
|
90
|
+
let templateBindingContractPromise = null;
|
|
91
|
+
function loadTemplateBindingContract() {
|
|
92
|
+
if (!templateBindingContractPromise) {
|
|
93
|
+
const url = new URL('../../aliyun/fc/_shared/template-binding-contract.js', import.meta.url);
|
|
94
|
+
templateBindingContractPromise = import(url.href).then(m => m.default || m).catch(() => null);
|
|
95
|
+
}
|
|
96
|
+
return templateBindingContractPromise;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* ------------------------------------------------------------------ 命令定义 */
|
|
100
|
+
|
|
101
|
+
export const contentApplyCommand = {
|
|
102
|
+
name: 'content apply',
|
|
103
|
+
summary: '按 site-manifest.json 一次落站(契约校验 → dry-run 计划 → `--confirm` 幂等写库)',
|
|
104
|
+
usage:
|
|
105
|
+
'wkgeo content apply --file <site-manifest.json> [--site <站点ID|前缀>] [--confirm] [--publish] [--json]\n' +
|
|
106
|
+
' wkgeo content apply --schema # 打印本版契约的 JSON Schema',
|
|
107
|
+
args: [],
|
|
108
|
+
flags: {
|
|
109
|
+
file: { type: 'string', desc: '建站 manifest 路径(契约:docs/规范-建站内容契约.md)' },
|
|
110
|
+
site: { type: 'string', desc: '站点 ID / ID 前缀 / 域名 / 站名(缺省用 `wkgeo site use` 选中的站)' },
|
|
111
|
+
confirm: { type: 'boolean', desc: '真的写库;不加就是 dry-run(只打计划)' },
|
|
112
|
+
mode: {
|
|
113
|
+
type: 'string',
|
|
114
|
+
desc: '建站模式:optimize 优化修改(默认)/ origin_copy 原样复制;落站点,不改变写哪些内容',
|
|
115
|
+
},
|
|
116
|
+
'dry-run': { type: 'boolean', desc: '只打计划(默认行为,写上更明确)' },
|
|
117
|
+
publish: { type: 'boolean', desc: '落库成功后顺手提交一次发布(等价 `wkgeo publish --confirm`)' },
|
|
118
|
+
wait: { type: 'boolean', desc: '`--publish` 时等发布跑完(默认等;`--no-wait` 只提交)' },
|
|
119
|
+
'wait-timeout': { type: 'string', desc: '`--publish` 最多等多少秒(默认 600)' },
|
|
120
|
+
'allow-name-mismatch': {
|
|
121
|
+
type: 'boolean',
|
|
122
|
+
desc: '站点真名与 manifest.site.name 不一致时仍然执行(改名后 / 接手老站时用)',
|
|
123
|
+
},
|
|
124
|
+
schema: { type: 'boolean', desc: '打印 JSON Schema 后退出(不需要 --file,不碰网络)' },
|
|
125
|
+
/**
|
|
126
|
+
* ⚠️ 已迁走的参数:只保留一层拦截(隐藏、不进 `--help` 参数表)。
|
|
127
|
+
* 模板内容契约(`sections[]`/`pages[]`)现在是独立命令 `wkgeo content fill`;
|
|
128
|
+
* 这里留着这个 key 是为了把「拿旧命令写模板清单」的人挡在门前并给出正确命令名,
|
|
129
|
+
* 而不是抛一句「不认识的参数:--manifest」让人自己猜。
|
|
130
|
+
*/
|
|
131
|
+
manifest: {
|
|
132
|
+
type: 'string',
|
|
133
|
+
hidden: true,
|
|
134
|
+
desc: '(已迁走)模板内容清单改用 `wkgeo content fill --manifest`',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
examples: [
|
|
138
|
+
'wkgeo content apply --schema > site-manifest.schema.json # 把契约给 A / 编辑器用',
|
|
139
|
+
'wkgeo content apply --file site-manifest.json # dry-run:逐项计划,不写库',
|
|
140
|
+
'wkgeo content apply --file site-manifest.json --confirm # 真写(幂等,重跑只 skipped)',
|
|
141
|
+
'wkgeo content apply --file site-manifest.json --site 8f3c1a2e --confirm --publish',
|
|
142
|
+
],
|
|
143
|
+
notes: [
|
|
144
|
+
'**先 dry-run 再 --confirm**:dry-run 会把「将建 / 将改 / 将跳过 / 将失败」逐条列出来,老板看一遍再落库。',
|
|
145
|
+
'**幂等**:栏目按 slug、内容按 slug(缺省标题)、素材按显示名认领已有对象;连跑两次第二次全 `skipped`。',
|
|
146
|
+
'**单条失败不静默**:`failed[{item, reason}]` + 退出码 1,已成功项保留;修完 manifest 重跑即可续上。',
|
|
147
|
+
'**契约必填 `unfilled`**:没填的项逐条写清 `{item, reason}`(来源缺失 / 需老板确认 / 版权不明 / 平台不支持),全填齐写 `[]`。',
|
|
148
|
+
'**不含**建站 / 装模板 / 删数据:装模板走 `wkgeo sites create --template`,删除走 `categories|contents|assets delete`。',
|
|
149
|
+
'写的内容 `ext` 里会留一条 `_wk_manifest`(正文指纹 + 来源),这是幂等标记,别手工删。',
|
|
150
|
+
],
|
|
151
|
+
async run(ctx) {
|
|
152
|
+
/**
|
|
153
|
+
* 两条契约线已拆成两条命令(task-178):
|
|
154
|
+
* `content apply` = 建站清单(`--file site-manifest.json`)
|
|
155
|
+
* `content fill` = 模板内容契约(`--manifest`)
|
|
156
|
+
* 这里只做拦截 —— **不再**动态 import `content-apply.mjs` 兜底,
|
|
157
|
+
* 否则「拿旧命令写模板清单」会静默走另一条线,CLI 表面又少一条命令。
|
|
158
|
+
*/
|
|
159
|
+
if (ctx.flags.manifest) {
|
|
160
|
+
throw new UsageError(
|
|
161
|
+
'这是模板内容契约命令,请用 `wkgeo content fill --manifest <文件>`(`--manifest` 已从 `content apply` 拆出;' +
|
|
162
|
+
'`content apply` 只收 `--file <site-manifest.json>`)',
|
|
163
|
+
{ hint: '契约见 docs/规范-模板内容契约.md §5;建站清单契约见 docs/规范-建站内容契约.md' },
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
return runApply(ctx);
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/* ------------------------------------------------------------------ 主流程 */
|
|
171
|
+
|
|
172
|
+
async function runApply(ctx) {
|
|
173
|
+
const { out } = ctx;
|
|
174
|
+
|
|
175
|
+
/* ── 0. `--schema`:不碰网络、不读 manifest */
|
|
176
|
+
if (ctx.flags.schema) {
|
|
177
|
+
const schema = manifestJsonSchema();
|
|
178
|
+
const data = { format: MANIFEST_FORMAT, schema };
|
|
179
|
+
if (out.json) ctx.done(data);
|
|
180
|
+
else out.raw(JSON.stringify(schema, null, 2));
|
|
181
|
+
return data;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/* ── 1. 读文件 + 契约校验(一切写库动作之前) */
|
|
185
|
+
const fileFlag = String(ctx.flags.file || '').trim();
|
|
186
|
+
if (!fileFlag) {
|
|
187
|
+
throw new UsageError('缺少 --file <site-manifest.json>', {
|
|
188
|
+
hint: '契约见 docs/规范-建站内容契约.md;`wkgeo content apply --schema` 可打印 JSON Schema。',
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
const abs = path.resolve(process.cwd(), fileFlag);
|
|
192
|
+
let stat = null;
|
|
193
|
+
try {
|
|
194
|
+
stat = fs.statSync(abs);
|
|
195
|
+
} catch {
|
|
196
|
+
stat = null;
|
|
197
|
+
}
|
|
198
|
+
if (!stat || !stat.isFile()) throw new UsageError(`manifest 文件不存在:${abs}`);
|
|
199
|
+
|
|
200
|
+
let doc = null;
|
|
201
|
+
try {
|
|
202
|
+
doc = JSON.parse(fs.readFileSync(abs, 'utf8'));
|
|
203
|
+
} catch (err) {
|
|
204
|
+
throw new UsageError(`manifest 不是合法 JSON:${(err && err.message) || err}`, {
|
|
205
|
+
hint: `检查 ${abs}(BOM / 尾逗号 / 注释都会让 JSON.parse 失败)。`,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const report = validateManifest(doc, { baseDir: path.dirname(abs) });
|
|
210
|
+
const { manifest, errors, itemErrors, warnings } = report;
|
|
211
|
+
|
|
212
|
+
/* ── 1.5 建站模式(task-95):`--mode` 优先,其次 manifest.buildMode,默认 optimize */
|
|
213
|
+
const modeFlag = String(ctx.flags.mode == null ? '' : ctx.flags.mode).trim();
|
|
214
|
+
if (modeFlag && !BUILD_MODES.includes(modeFlag)) {
|
|
215
|
+
throw new UsageError(`--mode 只认 optimize / origin_copy:收到 "${modeFlag}"`, {
|
|
216
|
+
hint: 'optimize=优化修改(默认);origin_copy=尽量保持原站结构视觉。',
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
const buildMode = normalizeBuildMode(modeFlag || manifest.site.buildMode);
|
|
220
|
+
|
|
221
|
+
if (!out.json) printValidation(out, { file: abs, errors, itemErrors, warnings });
|
|
222
|
+
if (errors.length) {
|
|
223
|
+
const err = new UsageError(`manifest 契约校验未通过:${errors.length} 处致命问题(一条都没写库)`);
|
|
224
|
+
err.details = { file: abs, errors, itemErrors, warnings };
|
|
225
|
+
throw err;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/* ── 2. 登录态 + 站点(顺序:先授权再解析站,未登录时不猜站) */
|
|
229
|
+
await ctx.requireAuth();
|
|
230
|
+
const site = await resolveSiteForApply(ctx);
|
|
231
|
+
const siteRow = await siteRowOf(ctx, site);
|
|
232
|
+
const actualName = String((siteRow && siteRow.name) || site.label || '').trim();
|
|
233
|
+
|
|
234
|
+
/* ── 3. 站名核对:指错站比写错内容严重得多 */
|
|
235
|
+
if (manifest.site.name && actualName && manifest.site.name !== actualName && !ctx.flags.allowNameMismatch) {
|
|
236
|
+
throw new UsageError(
|
|
237
|
+
`站点名不一致:manifest.site.name = "${manifest.site.name}",而 --site 指到的是 "${actualName}"`,
|
|
238
|
+
{ hint: '确认 --site 指对了站;站点确实改过名就同步 manifest.site.name,或加 `--allow-name-mismatch`。' },
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/* ── 4. 执行(dry-run 只算计划) */
|
|
243
|
+
const dryRun = !ctx.flags.confirm;
|
|
244
|
+
const engine = createApplyEngine({ ctx, site, manifest, itemErrors, warnings, dryRun, file: abs });
|
|
245
|
+
const result = await engine.run();
|
|
246
|
+
|
|
247
|
+
const data = {
|
|
248
|
+
dry_run: dryRun,
|
|
249
|
+
file: abs,
|
|
250
|
+
format: MANIFEST_FORMAT,
|
|
251
|
+
build_mode: buildMode,
|
|
252
|
+
build_mode_label: BUILD_MODE_LABEL[buildMode],
|
|
253
|
+
site: { id: site.id, name: actualName, source: site.source },
|
|
254
|
+
manifest: manifest.counts,
|
|
255
|
+
notes: result.notes,
|
|
256
|
+
warnings,
|
|
257
|
+
counts: result.counts,
|
|
258
|
+
created: result.created,
|
|
259
|
+
updated: result.updated,
|
|
260
|
+
skipped: result.skipped,
|
|
261
|
+
failed: result.failed,
|
|
262
|
+
items: result.items,
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/* ── 4.5 把建站模式记到站点上(task-95;best-effort,dry-run 不写) */
|
|
266
|
+
data.build_mode_saved = false;
|
|
267
|
+
if (!dryRun) {
|
|
268
|
+
try {
|
|
269
|
+
if (typeof ctx.siteCoreCall === 'function') {
|
|
270
|
+
await ctx.siteCoreCall('siteBuildModeSave', { buildMode }, { siteId: site.id });
|
|
271
|
+
data.build_mode_saved = true;
|
|
272
|
+
}
|
|
273
|
+
} catch (e) {
|
|
274
|
+
warnings.push({
|
|
275
|
+
item: 'buildMode',
|
|
276
|
+
reason: `建站模式没记到站点(不影响内容落库):${(e && e.message) || e}`,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/* ── 5. 可选发布 */
|
|
282
|
+
if (ctx.flags.publish && dryRun) {
|
|
283
|
+
data.publish = { skipped: true, reason: 'dry-run 不发布' };
|
|
284
|
+
} else if (ctx.flags.publish && result.failed.length === 0) {
|
|
285
|
+
const publish = await publishAfterApply(ctx, site, {
|
|
286
|
+
wait: ctx.flags.wait !== false,
|
|
287
|
+
timeoutSec: Number(ctx.flags.waitTimeout) || 600,
|
|
288
|
+
});
|
|
289
|
+
data.publish = publish;
|
|
290
|
+
if (publish && publish.ok === false) {
|
|
291
|
+
result.failed.push({ item: 'publish', reason: publish.reason || '发布未成功', stage: 'publish' });
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (!out.json) printApplyReport(out, data, manifest);
|
|
296
|
+
|
|
297
|
+
/* ── 6. 退出码:0 全成功 / 1 有单项失败(2、3 由抛错决定) */
|
|
298
|
+
if (result.failed.length) {
|
|
299
|
+
const err = new CliError(`apply 完成,但有 ${result.failed.length} 项失败`, {
|
|
300
|
+
kind: 'partial',
|
|
301
|
+
hint: '已成功项保留;改完 manifest 重跑即可(幂等,成功项会 skipped)。',
|
|
302
|
+
});
|
|
303
|
+
err.details = data;
|
|
304
|
+
throw err;
|
|
305
|
+
}
|
|
306
|
+
ctx.done(data);
|
|
307
|
+
return data;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/* ------------------------------------------------------------------ 站解析 */
|
|
311
|
+
|
|
312
|
+
async function resolveSiteForApply(ctx) {
|
|
313
|
+
const wanted = String(ctx.flags.site || '').trim();
|
|
314
|
+
if (!wanted) {
|
|
315
|
+
const site = ctx.cmsSite();
|
|
316
|
+
if (!site || !site.id) {
|
|
317
|
+
throw new UsageError('没选站点:加 `--site <站点ID>` 或先 `wkgeo site use <站点ID>`', {
|
|
318
|
+
hint: 'apply 会往这个站写栏目和内容,绝不替你猜站。',
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
return { id: String(site.id), label: site.label || '', source: site.source || 'local', row: null };
|
|
322
|
+
}
|
|
323
|
+
if (looksLikeUuid(wanted)) return { id: wanted, label: '', source: 'flag', row: null };
|
|
324
|
+
|
|
325
|
+
const res = await ctx.callData('listSites', { limit: 200 });
|
|
326
|
+
const rows = (res && Array.isArray(res.rows) ? res.rows : []).map((r) => r || {});
|
|
327
|
+
const needle = wanted.toLowerCase();
|
|
328
|
+
const byPrefix = rows.filter((r) => String(r.id || '').toLowerCase().startsWith(needle));
|
|
329
|
+
if (byPrefix.length === 1) {
|
|
330
|
+
const row = byPrefix[0];
|
|
331
|
+
return { id: String(row.id), label: String(row.name || ''), source: 'flag', row };
|
|
332
|
+
}
|
|
333
|
+
if (byPrefix.length > 1) {
|
|
334
|
+
throw new UsageError(`站点前缀「${wanted}」匹配到 ${byPrefix.length} 个站`, {
|
|
335
|
+
hint: `给长一点的前缀:${byPrefix.slice(0, 5).map((r) => `${r.name || '(未命名)'}=${r.id}`).join(';')}`,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
const hit = matchSiteRow(rows, wanted);
|
|
339
|
+
if (hit) return { id: String(hit.id), label: String(hit.label || hit.name || ''), source: 'flag', row: null };
|
|
340
|
+
throw new CliError(`没有找到站点「${wanted}」(按 ID 前缀 / 域名 / 站名都没匹配上)`, {
|
|
341
|
+
kind: 'not_found',
|
|
342
|
+
code: 404,
|
|
343
|
+
hint: rows.length
|
|
344
|
+
? `你名下的站:${rows.slice(0, 10).map((r) => `${r.name || '(未命名)'}=${r.id}`).join(';')}`
|
|
345
|
+
: '当前账号名下没有站点(协作站不在列表里)—— 请直接用站点 ID。',
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** 站点那一行(`listSites` 结果里按 id 找;找不到就 null) */
|
|
350
|
+
async function siteRowOf(ctx, site) {
|
|
351
|
+
if (site && site.row) return site.row;
|
|
352
|
+
try {
|
|
353
|
+
const res = await ctx.callData('listSites', { limit: 200 });
|
|
354
|
+
const rows = (res && Array.isArray(res.rows) ? res.rows : []).map((r) => r || {});
|
|
355
|
+
return rows.find((r) => String(r.id || '') === String(site.id)) || null;
|
|
356
|
+
} catch {
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/* ------------------------------------------------------------------ 校验输出 */
|
|
362
|
+
|
|
363
|
+
function printValidation(out, { file, errors, itemErrors, warnings }) {
|
|
364
|
+
out.head(`建站内容契约校验:${oneLine(file, 90)}`);
|
|
365
|
+
for (const w of warnings) out.warn(` ⚠ ${w.path} ${w.message}`);
|
|
366
|
+
if (itemErrors.length) {
|
|
367
|
+
out.warn(` 单条问题 ${itemErrors.length} 处(这些条目会记 failed,其余照常执行):`);
|
|
368
|
+
for (const one of itemErrors) out.line(` · ${one.item} ${one.reason}`);
|
|
369
|
+
}
|
|
370
|
+
if (errors.length) {
|
|
371
|
+
out.err(` 致命问题 ${errors.length} 处:`);
|
|
372
|
+
for (const e of errors) out.err(` ✗ ${e.path} ${e.message}`);
|
|
373
|
+
out.line('');
|
|
374
|
+
out.info(' 契约:docs/规范-建站内容契约.md | Schema:`wkgeo content apply --schema`');
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/* ------------------------------------------------------------------ 执行引擎 */
|
|
379
|
+
|
|
380
|
+
function createApplyEngine({ ctx, site, manifest, itemErrors, warnings, dryRun }) {
|
|
381
|
+
const siteId = String(site.id);
|
|
382
|
+
const call = (action, payload) => ctx.contentCall(action, payload || {}, { siteId });
|
|
383
|
+
const siteCore = (action, payload) => ctx.siteCoreCall(action, payload || {}, { siteId });
|
|
384
|
+
|
|
385
|
+
/** 本站模板说明(`template.json`);一次运行只拉一次,失败 / 没有 ⇒ `null`(不拦,退回旧行为) */
|
|
386
|
+
let templateMetaPromise = null;
|
|
387
|
+
function loadSiteTemplateMeta() {
|
|
388
|
+
if (!templateMetaPromise) {
|
|
389
|
+
templateMetaPromise = (async () => {
|
|
390
|
+
try {
|
|
391
|
+
const got = await call('templateGet', {});
|
|
392
|
+
const files = arrayOf((got && got.files) || []);
|
|
393
|
+
const row = files.find(one => String((one && one.path) || '').toLowerCase() === 'template.json');
|
|
394
|
+
if (!row || !row.content) return null;
|
|
395
|
+
const parsed = JSON.parse(String(row.content));
|
|
396
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
397
|
+
} catch {
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
})();
|
|
401
|
+
}
|
|
402
|
+
return templateMetaPromise;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* **模板能不能绑这个栏目**(task-52,判据与后台保存 / 建站链路同一份)。
|
|
407
|
+
*
|
|
408
|
+
* @returns {Promise<null|{ok:boolean,code:string,message:string}>} `null` = 判不了(无契约 / 无模板说明)
|
|
409
|
+
*/
|
|
410
|
+
async function templateBindingVerdict(column, templatePath) {
|
|
411
|
+
if (!templatePath) return null;
|
|
412
|
+
const bindingContract = await loadTemplateBindingContract();
|
|
413
|
+
if (!bindingContract || typeof bindingContract.validateBinding !== 'function') return null;
|
|
414
|
+
const meta = await loadSiteTemplateMeta();
|
|
415
|
+
if (!meta) return null;
|
|
416
|
+
try {
|
|
417
|
+
return bindingContract.validateBinding(meta, { ...column, templatePath });
|
|
418
|
+
} catch {
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const items = [];
|
|
424
|
+
const failed = [];
|
|
425
|
+
const notes = [];
|
|
426
|
+
const blocked = new Set();
|
|
427
|
+
for (const one of itemErrors) {
|
|
428
|
+
blocked.add(one.item);
|
|
429
|
+
failed.push({ item: one.item, reason: one.reason, stage: 'validate' });
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const state = {
|
|
433
|
+
cats: [],
|
|
434
|
+
catBySlug: new Map(),
|
|
435
|
+
catByPath: new Map(),
|
|
436
|
+
catIdBySlug: new Map(), // manifest slug → {id, path, existed, type, kind}
|
|
437
|
+
assets: [],
|
|
438
|
+
assetIdByKey: new Map(),
|
|
439
|
+
contentsByCat: new Map(),
|
|
440
|
+
siteInfo: null,
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
const push = (entry) => {
|
|
444
|
+
const row = {
|
|
445
|
+
kind: entry.kind,
|
|
446
|
+
item: entry.item,
|
|
447
|
+
action: dryRun ? entry.planned : entry.action,
|
|
448
|
+
label: entry.label || '',
|
|
449
|
+
detail: entry.detail || '',
|
|
450
|
+
};
|
|
451
|
+
items.push(row);
|
|
452
|
+
return row;
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
/** 逐条执行(真跑);失败就改记 fail,不打断后面的条目 */
|
|
456
|
+
async function exec(entries) {
|
|
457
|
+
for (const entry of entries) {
|
|
458
|
+
const row = push(entry);
|
|
459
|
+
if (dryRun || typeof entry.run !== 'function') continue;
|
|
460
|
+
try {
|
|
461
|
+
await entry.run(row);
|
|
462
|
+
row.action = entry.planned;
|
|
463
|
+
row.detail = entry.after || entry.detail || row.detail;
|
|
464
|
+
} catch (err) {
|
|
465
|
+
row.action = 'fail';
|
|
466
|
+
row.detail = errorText(err);
|
|
467
|
+
failed.push({ item: entry.item, reason: row.detail, stage: entry.kind });
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const markFail = (entry, reason) => {
|
|
473
|
+
entry.planned = 'fail';
|
|
474
|
+
entry.action = 'fail';
|
|
475
|
+
entry.detail = reason;
|
|
476
|
+
failed.push({ item: entry.item, reason, stage: entry.kind });
|
|
477
|
+
return entry;
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
/* ── 载入现状 ─────────────────────────────────────────────── */
|
|
481
|
+
async function loadExisting() {
|
|
482
|
+
state.cats = arrayOf(await call('categoryList'));
|
|
483
|
+
for (const row of state.cats) {
|
|
484
|
+
const slug = String(row.slug || '').toLowerCase();
|
|
485
|
+
if (slug) state.catBySlug.set(slug, row);
|
|
486
|
+
const p = String(row.path || '');
|
|
487
|
+
if (p) state.catByPath.set(normalizePath(p), row);
|
|
488
|
+
}
|
|
489
|
+
state.assets = await loadAllAssets();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
async function loadAllAssets() {
|
|
493
|
+
const rows = [];
|
|
494
|
+
for (let page = 1; page <= ASSET_PAGE_CAP; page += 1) {
|
|
495
|
+
const res = await call('assetList', { status: 'normal', kind: '', page, pageSize: 100 });
|
|
496
|
+
const batch = (res && Array.isArray(res.rows) ? res.rows : []).map((r) => r || {});
|
|
497
|
+
rows.push(...batch);
|
|
498
|
+
const total = Number(res && res.total) || rows.length;
|
|
499
|
+
if (batch.length < 100 || rows.length >= total) break;
|
|
500
|
+
}
|
|
501
|
+
return rows;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/* ── 预检:跨引用必须在写任何东西之前就有结论(exit 2) ────── */
|
|
505
|
+
function preflight() {
|
|
506
|
+
const problems = [];
|
|
507
|
+
const knowCat = (slug) => state.catBySlug.has(slug) || manifest.categories.some((c) => c.slug === slug);
|
|
508
|
+
const typeOf = (slug) => {
|
|
509
|
+
const row = state.catBySlug.get(slug);
|
|
510
|
+
if (row) return KIND_TO_CATEGORY_TYPE[String(row.kind || '')] || String(row.kind || '');
|
|
511
|
+
const cat = manifest.categories.find((c) => c.slug === slug);
|
|
512
|
+
return cat ? cat.type : '';
|
|
513
|
+
};
|
|
514
|
+
for (const c of manifest.categories) {
|
|
515
|
+
if (c.parent && !knowCat(c.parent)) {
|
|
516
|
+
problems.push(`categories 里「${c.slug}」的 parent「${c.parent}」在 manifest 和站点上都找不到`);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
for (const c of manifest.categories) {
|
|
520
|
+
if (!c.path) continue;
|
|
521
|
+
const hit = state.catByPath.get(c.path);
|
|
522
|
+
if (hit && String(hit.slug || '').toLowerCase() !== c.slug) {
|
|
523
|
+
problems.push(`categories 里「${c.slug}」声明的 path "${c.path}" 已是栏目「${hit.slug}」的路径`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
for (const c of manifest.contents) {
|
|
527
|
+
if (!knowCat(c.categorySlug)) {
|
|
528
|
+
problems.push(`contents 里「${c.item}」挂的栏目「${c.categorySlug}」在 manifest 和站点上都找不到`);
|
|
529
|
+
} else if (typeOf(c.categorySlug) === 'single') {
|
|
530
|
+
problems.push(`contents 里「${c.item}」挂到了单页栏目「${c.categorySlug}」—— 单页内容请写进 singles[]`);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
for (const one of manifest.singles) {
|
|
534
|
+
if (!knowCat(one.categorySlug)) {
|
|
535
|
+
problems.push(`singles 里「${one.item}」绑的栏目「${one.categorySlug}」在 manifest 和站点上都找不到`);
|
|
536
|
+
} else if (typeOf(one.categorySlug) !== 'single') {
|
|
537
|
+
problems.push(`singles 里「${one.item}」绑的栏目「${one.categorySlug}」不是单页(类型 ${typeOf(one.categorySlug) || '未知'})`);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
for (const menu of flattenMenus(manifest.menus)) {
|
|
541
|
+
if (!menu.path) continue;
|
|
542
|
+
const hit = state.catByPath.get(menu.path);
|
|
543
|
+
const declared = manifest.categories.find((c) => c.path === menu.path);
|
|
544
|
+
if (!hit && !declared) problems.push(`menus 里「${menu.label}」的 path "${menu.path}" 没有对应栏目`);
|
|
545
|
+
}
|
|
546
|
+
if (problems.length) {
|
|
547
|
+
const err = new UsageError(`manifest 跨引用校验未通过:${problems.length} 处(一条都没写库)`, {
|
|
548
|
+
hint: '先把栏目建出来(同一个 manifest 的 categories[] 里写上),或者修正引用。',
|
|
549
|
+
});
|
|
550
|
+
err.details = { problems };
|
|
551
|
+
if (!ctx.out.json) for (const p of problems) ctx.out.err(` ✗ ${p}`);
|
|
552
|
+
throw err;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/* ── 素材 ─────────────────────────────────────────────────── */
|
|
557
|
+
async function stageAssets() {
|
|
558
|
+
const entries = [];
|
|
559
|
+
/** 一条素材可能有多个 key(同一份文件的别名):都指向同一个素材 ID */
|
|
560
|
+
const rememberId = (a, id) => {
|
|
561
|
+
state.assetIdByKey.set(a.key, id);
|
|
562
|
+
for (const alias of a.aliases || []) state.assetIdByKey.set(alias, id);
|
|
563
|
+
};
|
|
564
|
+
for (const a of manifest.assets) {
|
|
565
|
+
const item = `asset:${a.key}`;
|
|
566
|
+
if (blocked.has(item)) continue;
|
|
567
|
+
const role = a.role || 'file';
|
|
568
|
+
const hit = state.assets.find((r) => String(r.name || '').trim().toLowerCase() === a.name.toLowerCase());
|
|
569
|
+
if (hit) {
|
|
570
|
+
if (!dryRun) rememberId(a, String(hit.id));
|
|
571
|
+
entries.push({
|
|
572
|
+
kind: 'asset',
|
|
573
|
+
item,
|
|
574
|
+
planned: 'skip',
|
|
575
|
+
action: 'skip',
|
|
576
|
+
label: a.name,
|
|
577
|
+
detail: `已有素材 ${hit.id}(${formatBytes(Number(hit.size) || a.bytes)})`,
|
|
578
|
+
});
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
const entry = {
|
|
582
|
+
kind: 'asset',
|
|
583
|
+
item,
|
|
584
|
+
planned: 'create',
|
|
585
|
+
action: 'create',
|
|
586
|
+
label: a.name,
|
|
587
|
+
detail: `上传 ${formatBytes(a.bytes)} → ${a.use}/${role}`,
|
|
588
|
+
};
|
|
589
|
+
entry.run = async (row) => {
|
|
590
|
+
const fileName = path.basename(a.path);
|
|
591
|
+
/**
|
|
592
|
+
* `role` 一并透传(task-46):服务端 `assetSign` / `assetRegister` 现在**不认**这个字段,
|
|
593
|
+
* 多传会被静默忽略、不会报错;等它补了 `asset.role` 入参这边就自动生效。
|
|
594
|
+
*/
|
|
595
|
+
const ticket = await call('assetSign', { fileName, bytes: a.bytes, role });
|
|
596
|
+
if (!ticket || !ticket.key) {
|
|
597
|
+
throw new CliError('服务端没有返回直传票据(assetSign)', { hint: '稍后重试;票据 10 分钟内有效' });
|
|
598
|
+
}
|
|
599
|
+
const buffer = fs.readFileSync(a.path);
|
|
600
|
+
await uploadToOss({ ticket, buffer, fileName, fetchImpl: ctx.fetchImpl });
|
|
601
|
+
const asset = await call('assetRegister', { key: ticket.key, name: a.name, role });
|
|
602
|
+
const id = String((asset && (asset.id || asset.assetId)) || '');
|
|
603
|
+
if (!id) throw new CliError('素材登记成功但没回素材 ID(assetRegister)', { hint: '去后台素材库确认一下' });
|
|
604
|
+
rememberId(a, id);
|
|
605
|
+
row.detail = `已登记素材 ${id}(${a.use}/${role})`;
|
|
606
|
+
};
|
|
607
|
+
entries.push(entry);
|
|
608
|
+
}
|
|
609
|
+
return entries;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* **manifest 里手写的 `templateFile` 先过一遍契约**(task-52)。
|
|
614
|
+
*
|
|
615
|
+
* 对不上 ⇒ 返回 `''`(不提交 `template_path`,前台回落"按类型自动选")并回一条 `⚠`;
|
|
616
|
+
* 对不上还硬提交的话,后台 `categorySave` 会 400 把整条栏目卡掉 —— 那才是更糟的结果。
|
|
617
|
+
* 判不了(无契约 / 模板没说明)⇒ 原样返回,行为与改造前一致。
|
|
618
|
+
*/
|
|
619
|
+
async function resolveManifestTemplateFile(cat, kind) {
|
|
620
|
+
const raw = String(cat.templateFile || '').trim();
|
|
621
|
+
if (!raw) return '';
|
|
622
|
+
const verdict = await templateBindingVerdict(
|
|
623
|
+
{
|
|
624
|
+
key: cat.slug,
|
|
625
|
+
name: cat.name,
|
|
626
|
+
kind,
|
|
627
|
+
fieldsEnabled: null,
|
|
628
|
+
customFields: (cat.fields || []).map(one => String((one && one.key) || one || '')),
|
|
629
|
+
},
|
|
630
|
+
raw,
|
|
631
|
+
);
|
|
632
|
+
if (!verdict || verdict.ok !== false) return raw;
|
|
633
|
+
warnings.push({
|
|
634
|
+
path: `categories.${cat.slug}.templateFile`,
|
|
635
|
+
message: `模板 ${raw} 与栏目类型不匹配,已跳过绑定(前台按类型自动选):${verdict.message}`,
|
|
636
|
+
});
|
|
637
|
+
return '';
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* **这个栏目自己那一页**(老板 2026-11-21):「尽量每一个栏目制作一个模版 …… 复制一个模版绑定
|
|
642
|
+
* 当前的栏目,这样就方便后期修改」。
|
|
643
|
+
*
|
|
644
|
+
* 口径:
|
|
645
|
+
* * **清单没写 `templateFile`** 才算(写了就听清单的,上面 `resolveManifestTemplateFile` 已校验);
|
|
646
|
+
* * **只给新建的栏目用**(存量那行一律不碰 —— 客户自己挑过的模板页不能被一次 `content apply` 覆盖);
|
|
647
|
+
* * 模板里**声明了** `pages/c-<slug>.html` 才绑(判据走契约的 `templatePageBindings`)——
|
|
648
|
+
* base 模板 / `wkgeo template init` 出来的模板会给每个栏目做一份;
|
|
649
|
+
* 没做 ⇒ 空串 = 不绑(前台按类型自动选,兜底一个字没改)。
|
|
650
|
+
*
|
|
651
|
+
* @returns {Promise<string>} 要绑的模板页;`''` = 不绑
|
|
652
|
+
*/
|
|
653
|
+
async function resolveOwnPerCategoryPage(cat, kind) {
|
|
654
|
+
const bindingContract = await loadTemplateBindingContract();
|
|
655
|
+
if (!bindingContract || typeof bindingContract.perCategoryPagePath !== 'function') return '';
|
|
656
|
+
const meta = await loadSiteTemplateMeta();
|
|
657
|
+
if (!meta) return '';
|
|
658
|
+
const own = bindingContract.perCategoryPagePath(String(cat.slug || cat.name || ''));
|
|
659
|
+
if (!own) return '';
|
|
660
|
+
const declared = new Set(
|
|
661
|
+
(typeof bindingContract.templatePageBindings === 'function' ? bindingContract.templatePageBindings(meta) : [])
|
|
662
|
+
.map(one => String((one && one.path) || '')),
|
|
663
|
+
);
|
|
664
|
+
if (!declared.has(own)) return '';
|
|
665
|
+
const verdict = await templateBindingVerdict(
|
|
666
|
+
{
|
|
667
|
+
key: cat.slug,
|
|
668
|
+
name: cat.name,
|
|
669
|
+
kind,
|
|
670
|
+
fieldsEnabled: null,
|
|
671
|
+
customFields: (cat.fields || []).map(one => String((one && one.key) || one || '')),
|
|
672
|
+
},
|
|
673
|
+
own,
|
|
674
|
+
);
|
|
675
|
+
if (verdict && verdict.ok === false) {
|
|
676
|
+
warnings.push({
|
|
677
|
+
path: `categories.${cat.slug}.templateFile`,
|
|
678
|
+
message: `模板里有 ${own},但与栏目类型不匹配,已跳过绑定(前台按类型自动选):${verdict.message}`,
|
|
679
|
+
});
|
|
680
|
+
return '';
|
|
681
|
+
}
|
|
682
|
+
return own;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/* ── 栏目 ─────────────────────────────────────────────────── */
|
|
686
|
+
|
|
687
|
+
/** 本次清单 `menus[]` 声明的导航 path(懒算一次;`menus[]` 是导航唯一真相) */
|
|
688
|
+
let navPathsMemo = null;
|
|
689
|
+
function navPaths() {
|
|
690
|
+
if (!navPathsMemo) {
|
|
691
|
+
navPathsMemo = new Set();
|
|
692
|
+
for (const one of flattenMenus(manifest.menus)) {
|
|
693
|
+
const p = normalizePath(one.path);
|
|
694
|
+
if (p) navPathsMemo.add(p);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return navPathsMemo;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* 这个栏目该不该进前台导航(老板 2026-11-13 口径:「首页板块一律用隐藏栏目承载」)。
|
|
702
|
+
*
|
|
703
|
+
* @returns {true|false|undefined} `undefined` = 本次**不动**导航字段(老清单:没写 `inMenu`、
|
|
704
|
+
* 也没声明 `menus[]` ⇒ 服务端默认 true,行为与改造前完全一致)。
|
|
705
|
+
*
|
|
706
|
+
* 判定顺序(`menus[]` 是导航唯一真相,`categories[].inMenu` 只是补充):
|
|
707
|
+
* 1. 在 `menus[]` 里 ⇒ `true`(老的显式导航行为不变);同时又写 `inMenu:false` ⇒ 按导航为准并告警;
|
|
708
|
+
* 2. 不在 `menus[]` 里、显式 `inMenu:false` ⇒ `false`(首页板块 / 隐藏栏目);
|
|
709
|
+
* 3. 不在 `menus[]` 里、显式 `inMenu:true` ⇒ `true`(清单没声明导航时也能手动进导航);
|
|
710
|
+
* 4. 没写 `inMenu`、但清单**声明了**导航 ⇒ `false`(没被导航引用就不进导航);
|
|
711
|
+
* 5. 没写 `inMenu`、清单也没有 `menus[]` ⇒ `undefined`(老行为,不碰)。
|
|
712
|
+
*/
|
|
713
|
+
function navStateOf(cat, catPath) {
|
|
714
|
+
const nav = navPaths();
|
|
715
|
+
const p = normalizePath(catPath);
|
|
716
|
+
const inNav = nav.size > 0 && Boolean(p) && nav.has(p);
|
|
717
|
+
if (inNav) {
|
|
718
|
+
if (cat.inMenu === false) {
|
|
719
|
+
warnings.push({
|
|
720
|
+
path: `categories.${cat.slug}.inMenu`,
|
|
721
|
+
message: '写了 inMenu:false,但它出现在 menus[] 里 —— 按导航为准(进导航);要隐藏就从 menus[] 里去掉',
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
return true;
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* 首页永远进导航:`menus[].path` 的契约是 `/xxx/`,**写不出 `/`**
|
|
728
|
+
* (JSON Schema `^/[a-z0-9]...` + 校验都拦),所以「不在 menus[] 里」对首页没有意义。
|
|
729
|
+
* 判据与后台一致:slug = `home` / `index`(服务端 `isHomeCategorySlug`)或 path 为 `/`。
|
|
730
|
+
*/
|
|
731
|
+
if (nav.size > 0 && (p === '/' || HOME_SLUGS.has(String(cat.slug || '').toLowerCase()))) return true;
|
|
732
|
+
if (cat.inMenu === false) return false;
|
|
733
|
+
if (cat.inMenu === true) return true;
|
|
734
|
+
return nav.size > 0 ? false : undefined;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
async function stageCategories() {
|
|
738
|
+
const entries = [];
|
|
739
|
+
/**
|
|
740
|
+
* 映射判据(`site-mapping-contract.mjs`):manifest 的 `type` 落成哪个 `kind` 由它一处说了算。
|
|
741
|
+
* 加载不到(npm 包里没有 `aliyun/` 目录)⇒ `null`,退回 `cat.kind`,行为与改造前一致。
|
|
742
|
+
*
|
|
743
|
+
* ⚠️ 原来这里只有 `const mapped = contract ? … : null`,**`contract` 从没赋值过** ⇒ 只要有
|
|
744
|
+
* `categories[]` 就抛 `ReferenceError: contract is not defined`(`content apply` 一直是坏的;
|
|
745
|
+
* 之前没有用例覆盖「apply 带栏目」,所以没被发现)。这一行是补上的加载点。
|
|
746
|
+
*/
|
|
747
|
+
const contract = await loadSiteMappingContract();
|
|
748
|
+
/** 本次按口径落成隐藏的栏目(集中一条告警,免得刷屏) */
|
|
749
|
+
const hidden = [];
|
|
750
|
+
for (const cat of sortCategoriesForApply(manifest.categories)) {
|
|
751
|
+
const item = `category:${cat.slug}`;
|
|
752
|
+
if (blocked.has(item)) continue;
|
|
753
|
+
const existing = state.catBySlug.get(cat.slug) || null;
|
|
754
|
+
|
|
755
|
+
let parentId = null;
|
|
756
|
+
let parentPath = '/';
|
|
757
|
+
if (cat.parent) {
|
|
758
|
+
const p = state.catIdBySlug.get(cat.parent);
|
|
759
|
+
if (p) {
|
|
760
|
+
parentId = p.id || null;
|
|
761
|
+
parentPath = p.path || '/';
|
|
762
|
+
} else {
|
|
763
|
+
const row = state.catBySlug.get(cat.parent);
|
|
764
|
+
if (row) {
|
|
765
|
+
parentId = String(row.id);
|
|
766
|
+
parentPath = normalizePath(row.path);
|
|
767
|
+
} else {
|
|
768
|
+
entries.push(
|
|
769
|
+
markFail(
|
|
770
|
+
{ kind: 'category', item, planned: 'create', action: 'create', label: cat.name, detail: '' },
|
|
771
|
+
`父栏目「${cat.parent}」不存在(预检应已拦住,属内部不一致)`,
|
|
772
|
+
),
|
|
773
|
+
);
|
|
774
|
+
continue;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
const wantPath = cat.path || (parentPath === '/' ? `/${cat.slug}/` : `${normalizePath(parentPath)}${cat.slug}/`);
|
|
779
|
+
const label = `${cat.name} ${wantPath} [${cat.type}]`.trim();
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* 映射判据收敛到契约(老板 2026-11-04 口径):manifest 的 type(list|single|channel)
|
|
783
|
+
* 该怎么落成 kind,由 `site-mapping-contract.mjs` 一处说了算;缺契约时用原 `cat.kind`。
|
|
784
|
+
*/
|
|
785
|
+
const mapped = contract
|
|
786
|
+
? contract.classifySection(
|
|
787
|
+
{
|
|
788
|
+
key: cat.slug,
|
|
789
|
+
name: cat.name,
|
|
790
|
+
path: wantPath,
|
|
791
|
+
slug: cat.slug,
|
|
792
|
+
type: cat.type,
|
|
793
|
+
level: cat.level,
|
|
794
|
+
inMenu: false,
|
|
795
|
+
},
|
|
796
|
+
{ levelZeroChannel: false },
|
|
797
|
+
)
|
|
798
|
+
: null;
|
|
799
|
+
const kind = mapped ? mapped.kind : cat.kind;
|
|
800
|
+
/** 模板绑定先过契约(计划期就定下来:不合法 ⇒ 空串 + 一条 ⚠,创建 / 更新两条分支共用) */
|
|
801
|
+
const templateFile = await resolveManifestTemplateFile(cat, kind);
|
|
802
|
+
/**
|
|
803
|
+
* ★ 2026-11-21(每栏目一个模板页):**新建**栏目、且清单没指定模板页时,
|
|
804
|
+
* 绑它自己那一页(模板里声明了 `pages/c-<slug>.html` 才算)。存量栏目一律不碰。
|
|
805
|
+
*/
|
|
806
|
+
const boundTemplate = templateFile || (existing ? '' : await resolveOwnPerCategoryPage(cat, kind));
|
|
807
|
+
/** 导航归属:true / false / undefined(undefined = 本次不动,老行为) */
|
|
808
|
+
const inMenu = navStateOf(cat, wantPath);
|
|
809
|
+
if (inMenu === false) hidden.push(cat.name);
|
|
810
|
+
|
|
811
|
+
if (!existing) {
|
|
812
|
+
const entry = {
|
|
813
|
+
kind: 'category',
|
|
814
|
+
item,
|
|
815
|
+
planned: 'create',
|
|
816
|
+
action: 'create',
|
|
817
|
+
label,
|
|
818
|
+
detail: `新建栏目(${kind}${inMenu === false ? ',不进导航' : ''})${boundTemplate ? ` 模板 ${boundTemplate}` : ''}`,
|
|
819
|
+
};
|
|
820
|
+
entry.run = async (row) => {
|
|
821
|
+
const payload = { name: cat.name, slug: cat.slug, kind, sort: cat.sort };
|
|
822
|
+
/* 父栏目可能是同一份 manifest 里刚建的:执行时才拿 ID(建计划时父还没有 ID) */
|
|
823
|
+
const liveParentId = cat.parent ? (state.catIdBySlug.get(cat.parent) || {}).id || '' : '';
|
|
824
|
+
const effectiveParentId = liveParentId || parentId;
|
|
825
|
+
if (effectiveParentId) payload.parent_id = effectiveParentId;
|
|
826
|
+
if (boundTemplate) payload.template_path = boundTemplate;
|
|
827
|
+
if (cat.description) payload.description = cat.description;
|
|
828
|
+
if (cat.seoDescription) payload.seo_description = cat.seoDescription;
|
|
829
|
+
if (cat.seoKeywords) payload.seo_keywords = cat.seoKeywords;
|
|
830
|
+
/**
|
|
831
|
+
* 关键:**建的时候就带上导航归属**。以前这里没有 `in_menu`,服务端默认 true,
|
|
832
|
+
* 于是「首页·XXX」建完就挂在导航里,只能事后手动 `categories update --no-in-menu` 补。
|
|
833
|
+
*/
|
|
834
|
+
if (inMenu !== undefined) payload.in_menu = inMenu;
|
|
835
|
+
const saved = await call('categorySave', payload);
|
|
836
|
+
let id = String((saved && (saved.id || saved.categoryId)) || '');
|
|
837
|
+
if (!id) {
|
|
838
|
+
const list = arrayOf(await call('categoryList'));
|
|
839
|
+
const found = list.find((r) => String(r.slug || '').toLowerCase() === cat.slug);
|
|
840
|
+
id = String((found && found.id) || '');
|
|
841
|
+
}
|
|
842
|
+
if (!id) throw new CliError('栏目已提交但没回栏目 ID', { hint: '去后台栏目管理看一眼是否建出来了' });
|
|
843
|
+
state.catIdBySlug.set(cat.slug, { id, path: wantPath, existed: false, type: cat.type, kind });
|
|
844
|
+
row.detail = `已建栏目 ${id}${cat.fields.length ? `(字段 ${cat.fields.length} 个随后写)` : ''}`;
|
|
845
|
+
};
|
|
846
|
+
entries.push(entry);
|
|
847
|
+
/* 真跑时 entry.run 会把真实 ID 写回来;这里先垫一条,让子栏目 / 内容 / 单页能算路径(dry-run 也走这里) */
|
|
848
|
+
state.catIdBySlug.set(cat.slug, {
|
|
849
|
+
id: dryRun ? '' : state.catIdBySlug.get(cat.slug)?.id || '',
|
|
850
|
+
path: wantPath,
|
|
851
|
+
existed: false,
|
|
852
|
+
type: cat.type,
|
|
853
|
+
kind,
|
|
854
|
+
});
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/* 已存在:只比 manifest 关心的字段(没写的字段一律不碰,避免误清空后台配置) */
|
|
859
|
+
const { payload, changes } = diffCategory(existing, {
|
|
860
|
+
name: cat.name,
|
|
861
|
+
parentId,
|
|
862
|
+
kind: cat.level === 1 ? cat.kind : '',
|
|
863
|
+
sort: cat.sort,
|
|
864
|
+
templateFile: templateFile,
|
|
865
|
+
description: cat.description,
|
|
866
|
+
seoDescription: cat.seoDescription,
|
|
867
|
+
seoKeywords: cat.seoKeywords,
|
|
868
|
+
/** 已有栏目也按同一口径纠正导航归属(以前只摘「不在清单里」的,清单内的永远留在导航) */
|
|
869
|
+
inMenu,
|
|
870
|
+
});
|
|
871
|
+
state.catIdBySlug.set(cat.slug, {
|
|
872
|
+
id: String(existing.id),
|
|
873
|
+
path: normalizePath(existing.path || wantPath),
|
|
874
|
+
existed: true,
|
|
875
|
+
type: KIND_TO_CATEGORY_TYPE[String(existing.kind || '')] || cat.type,
|
|
876
|
+
kind: String(existing.kind || cat.kind),
|
|
877
|
+
});
|
|
878
|
+
if (!changes.length) {
|
|
879
|
+
entries.push({ kind: 'category', item, planned: 'skip', action: 'skip', label, detail: '已存在且一致' });
|
|
880
|
+
continue;
|
|
881
|
+
}
|
|
882
|
+
const entry = { kind: 'category', item, planned: 'update', action: 'update', label, detail: changes.join(';') };
|
|
883
|
+
entry.after = `已更新:${changes.join(';')}`;
|
|
884
|
+
entry.run = async () => {
|
|
885
|
+
await call('categorySave', { id: String(existing.id), ...payload });
|
|
886
|
+
};
|
|
887
|
+
entries.push(entry);
|
|
888
|
+
}
|
|
889
|
+
if (hidden.length) {
|
|
890
|
+
warnings.push({
|
|
891
|
+
path: 'categories',
|
|
892
|
+
message:
|
|
893
|
+
`${hidden.length} 个栏目不在 menus[] 里,按口径落成隐藏栏目(in_menu=false):${hidden.join('、')}` +
|
|
894
|
+
' —— 要进导航就把它们写进 menus[]',
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
return entries;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/* ── 自定义字段(依赖栏目 ID) ─────────────────────────────── */
|
|
901
|
+
async function stageFields() {
|
|
902
|
+
const entries = [];
|
|
903
|
+
for (const cat of manifest.categories) {
|
|
904
|
+
if (!cat.fields.length) continue;
|
|
905
|
+
const got = state.catIdBySlug.get(cat.slug);
|
|
906
|
+
const categoryId = got && got.id;
|
|
907
|
+
if (!categoryId) {
|
|
908
|
+
for (const f of cat.fields) {
|
|
909
|
+
const entry = {
|
|
910
|
+
kind: 'field',
|
|
911
|
+
item: `field:${cat.slug}.${f.key}`,
|
|
912
|
+
planned: 'create',
|
|
913
|
+
action: 'create',
|
|
914
|
+
label: `${cat.slug}.${f.key}`,
|
|
915
|
+
detail: dryRun ? `新建字段(${f.type}${f.required ? ',必填' : ''})` : '',
|
|
916
|
+
};
|
|
917
|
+
if (!dryRun) markFail(entry, '所在栏目没建成(见上面那条栏目失败)');
|
|
918
|
+
entries.push(entry);
|
|
919
|
+
}
|
|
920
|
+
continue;
|
|
921
|
+
}
|
|
922
|
+
const list = arrayOf(await call('fieldList', { scope: 'content', categoryId }));
|
|
923
|
+
for (const f of cat.fields) {
|
|
924
|
+
const item = `field:${cat.slug}.${f.key}`;
|
|
925
|
+
const label = `${cat.slug}.${f.key}`;
|
|
926
|
+
const own = list.find((r) => String(r.category_id || '') === String(categoryId) && String(r.key || '') === f.key);
|
|
927
|
+
if (own) {
|
|
928
|
+
const changes = [];
|
|
929
|
+
if (String(own.label || '') !== f.label) changes.push(`label:${own.label || '(空)'} → ${f.label}`);
|
|
930
|
+
if (String(own.type || '') !== f.type) changes.push(`type:${own.type} → ${f.type}`);
|
|
931
|
+
if (Boolean(own.required) !== Boolean(f.required)) changes.push(`required:${Boolean(own.required)} → ${Boolean(f.required)}`);
|
|
932
|
+
if (Number(own.sort || 0) !== Number(f.sort)) changes.push(`sort:${own.sort} → ${f.sort}`);
|
|
933
|
+
if (f.options && jsonOf(own.options) !== jsonOf(f.options)) changes.push('options 有变');
|
|
934
|
+
if (!changes.length) {
|
|
935
|
+
entries.push({ kind: 'field', item, planned: 'skip', action: 'skip', label, detail: '字段已存在且一致' });
|
|
936
|
+
continue;
|
|
937
|
+
}
|
|
938
|
+
const entry = { kind: 'field', item, planned: 'update', action: 'update', label, detail: changes.join(';') };
|
|
939
|
+
entry.after = `已更新字段:${changes.join(';')}`;
|
|
940
|
+
entry.run = async () => {
|
|
941
|
+
await call('fieldSave', {
|
|
942
|
+
id: String(own.id),
|
|
943
|
+
scope: 'content',
|
|
944
|
+
categoryId,
|
|
945
|
+
key: f.key,
|
|
946
|
+
label: f.label,
|
|
947
|
+
type: f.type,
|
|
948
|
+
required: Boolean(f.required),
|
|
949
|
+
options: f.options || undefined,
|
|
950
|
+
sort: f.sort,
|
|
951
|
+
});
|
|
952
|
+
};
|
|
953
|
+
entries.push(entry);
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
const entry = {
|
|
957
|
+
kind: 'field',
|
|
958
|
+
item,
|
|
959
|
+
planned: 'create',
|
|
960
|
+
action: 'create',
|
|
961
|
+
label,
|
|
962
|
+
detail: `新建字段(${f.type}${f.required ? ',必填' : ''})`,
|
|
963
|
+
};
|
|
964
|
+
entry.after = `已建字段 ${f.key}`;
|
|
965
|
+
entry.run = async () => {
|
|
966
|
+
await call('fieldSave', {
|
|
967
|
+
scope: 'content',
|
|
968
|
+
categoryId,
|
|
969
|
+
key: f.key,
|
|
970
|
+
label: f.label,
|
|
971
|
+
type: f.type,
|
|
972
|
+
required: Boolean(f.required),
|
|
973
|
+
options: f.options || undefined,
|
|
974
|
+
sort: f.sort,
|
|
975
|
+
});
|
|
976
|
+
};
|
|
977
|
+
entries.push(entry);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
return entries;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/* ── 内容 ─────────────────────────────────────────────────── */
|
|
984
|
+
async function stageContents() {
|
|
985
|
+
const entries = [];
|
|
986
|
+
for (const c of manifest.contents) {
|
|
987
|
+
const item = c.item;
|
|
988
|
+
if (blocked.has(item)) continue;
|
|
989
|
+
const got = state.catIdBySlug.get(c.categorySlug);
|
|
990
|
+
const categoryId = got && got.id;
|
|
991
|
+
const label = `${c.title}(${c.categorySlug})`;
|
|
992
|
+
if (!categoryId) {
|
|
993
|
+
const entry = {
|
|
994
|
+
kind: 'content',
|
|
995
|
+
item,
|
|
996
|
+
planned: 'create',
|
|
997
|
+
action: 'create',
|
|
998
|
+
label,
|
|
999
|
+
detail: dryRun ? '新建内容(栏目这次也会新建)' : '',
|
|
1000
|
+
};
|
|
1001
|
+
if (!dryRun) markFail(entry, `所在栏目「${c.categorySlug}」没建成`);
|
|
1002
|
+
entries.push(entry);
|
|
1003
|
+
continue;
|
|
1004
|
+
}
|
|
1005
|
+
const rows = await contentsOfCategory(categoryId);
|
|
1006
|
+
const existing = c.slug
|
|
1007
|
+
? rows.find((r) => String(r.slug || '').toLowerCase() === c.slug)
|
|
1008
|
+
: rows.find((r) => String(r.title || '').trim() === c.title.trim());
|
|
1009
|
+
const bodyText = readBodyText(c);
|
|
1010
|
+
if (!existing) {
|
|
1011
|
+
const entry = { kind: 'content', item, planned: 'create', action: 'create', label, detail: `新建内容(${c.status})` };
|
|
1012
|
+
entry.after = `已建内容(${c.status})`;
|
|
1013
|
+
entry.run = async (row) => {
|
|
1014
|
+
const payload = {
|
|
1015
|
+
category_id: categoryId,
|
|
1016
|
+
title: c.title,
|
|
1017
|
+
status: c.status,
|
|
1018
|
+
sort: c.sort,
|
|
1019
|
+
pinned: c.pinned,
|
|
1020
|
+
body: bodyText,
|
|
1021
|
+
ext: freshExt(c, bodyText, 'manifest'),
|
|
1022
|
+
};
|
|
1023
|
+
if (c.slug) payload.slug = c.slug;
|
|
1024
|
+
if (c.description) payload.description = c.description;
|
|
1025
|
+
if (c.keywords) payload.keywords = c.keywords;
|
|
1026
|
+
if (c.cover) payload.cover_asset_id = state.assetIdByKey.get(c.cover) || (looksLikeUuid(c.cover) ? c.cover : undefined);
|
|
1027
|
+
const ids = attachmentIds(c, state.assetIdByKey);
|
|
1028
|
+
if (ids.length) payload.attachments = ids;
|
|
1029
|
+
const saved = await call('contentSave', payload);
|
|
1030
|
+
const id = String((saved && (saved.id || saved.contentId)) || '');
|
|
1031
|
+
if (!id) throw new CliError('内容已提交但没回内容 ID', { hint: '去后台内容列表看一眼是否建出来了' });
|
|
1032
|
+
row.detail = `已建内容 ${id}(${c.status})`;
|
|
1033
|
+
};
|
|
1034
|
+
entries.push(entry);
|
|
1035
|
+
continue;
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
const one = await call('contentGet', { id: String(existing.id) });
|
|
1039
|
+
const { payload, changes } = diffContent(one || existing, c, bodyText, state.assetIdByKey, 'manifest');
|
|
1040
|
+
if (!changes.length) {
|
|
1041
|
+
entries.push({
|
|
1042
|
+
kind: 'content',
|
|
1043
|
+
item,
|
|
1044
|
+
planned: 'skip',
|
|
1045
|
+
action: 'skip',
|
|
1046
|
+
label,
|
|
1047
|
+
detail: `已存在且一致(${String(existing.id).slice(0, 8)}…)`,
|
|
1048
|
+
});
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
const entry = { kind: 'content', item, planned: 'update', action: 'update', label, detail: changes.join(';') };
|
|
1052
|
+
entry.after = `已更新:${changes.join(';')}`;
|
|
1053
|
+
entry.run = async () => {
|
|
1054
|
+
await call('contentSave', { id: String(existing.id), ...payload });
|
|
1055
|
+
};
|
|
1056
|
+
entries.push(entry);
|
|
1057
|
+
}
|
|
1058
|
+
return entries;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
/* ── 单页 ─────────────────────────────────────────────────── */
|
|
1062
|
+
async function stageSingles() {
|
|
1063
|
+
const entries = [];
|
|
1064
|
+
for (const one of manifest.singles) {
|
|
1065
|
+
const item = one.item;
|
|
1066
|
+
if (blocked.has(item)) continue;
|
|
1067
|
+
const got = state.catIdBySlug.get(one.categorySlug);
|
|
1068
|
+
const categoryId = got && got.id;
|
|
1069
|
+
const label = `${one.title}(单页 ${one.categorySlug})`;
|
|
1070
|
+
if (!categoryId) {
|
|
1071
|
+
const entry = {
|
|
1072
|
+
kind: 'single',
|
|
1073
|
+
item,
|
|
1074
|
+
planned: 'create',
|
|
1075
|
+
action: 'create',
|
|
1076
|
+
label,
|
|
1077
|
+
detail: dryRun ? '写单页正文(栏目这次也会新建)' : '',
|
|
1078
|
+
};
|
|
1079
|
+
if (!dryRun) markFail(entry, `所在栏目「${one.categorySlug}」没建成`);
|
|
1080
|
+
entries.push(entry);
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
const rows = arrayOf(await call('contentList', { categoryId, ownerPage: true, pageSize: 20 }));
|
|
1084
|
+
const existing = rows[0] || null;
|
|
1085
|
+
const bodyText = readBodyText(one);
|
|
1086
|
+
if (!existing) {
|
|
1087
|
+
const entry = { kind: 'single', item, planned: 'create', action: 'create', label, detail: '写单页正文(服务端没自动建页)' };
|
|
1088
|
+
entry.run = async (row) => {
|
|
1089
|
+
const payload = {
|
|
1090
|
+
category_id: categoryId,
|
|
1091
|
+
title: one.title,
|
|
1092
|
+
status: one.status,
|
|
1093
|
+
body: bodyText,
|
|
1094
|
+
ext: freshExt(one, bodyText, 'manifest'),
|
|
1095
|
+
};
|
|
1096
|
+
if (one.description) payload.description = one.description;
|
|
1097
|
+
if (one.keywords) payload.keywords = one.keywords;
|
|
1098
|
+
const ids = attachmentIds(one, state.assetIdByKey);
|
|
1099
|
+
if (ids.length) payload.attachments = ids;
|
|
1100
|
+
const saved = await call('contentSave', payload);
|
|
1101
|
+
row.detail = `已建单页内容 ${String((saved && saved.id) || '')}`;
|
|
1102
|
+
};
|
|
1103
|
+
entries.push(entry);
|
|
1104
|
+
continue;
|
|
1105
|
+
}
|
|
1106
|
+
const current = await call('contentGet', { id: String(existing.id) });
|
|
1107
|
+
const { payload, changes } = diffContent(current || existing, one, bodyText, state.assetIdByKey, 'manifest');
|
|
1108
|
+
if (!changes.length) {
|
|
1109
|
+
entries.push({ kind: 'single', item, planned: 'skip', action: 'skip', label, detail: '单页已存在且一致' });
|
|
1110
|
+
continue;
|
|
1111
|
+
}
|
|
1112
|
+
const entry = { kind: 'single', item, planned: 'update', action: 'update', label, detail: changes.join(';') };
|
|
1113
|
+
entry.after = `已更新单页:${changes.join(';')}`;
|
|
1114
|
+
entry.run = async () => {
|
|
1115
|
+
await call('contentSave', { id: String(existing.id), ...payload });
|
|
1116
|
+
};
|
|
1117
|
+
entries.push(entry);
|
|
1118
|
+
}
|
|
1119
|
+
return entries;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
/* ── 导航(menus[] 是全站导航唯一真相) ───────────────────── */
|
|
1123
|
+
async function stageMenus() {
|
|
1124
|
+
const entries = [];
|
|
1125
|
+
const flat = flattenMenus(manifest.menus);
|
|
1126
|
+
/**
|
|
1127
|
+
* 清单没写 `menus[]` ⇒ **本次不动导航**(老清单行为不变)。
|
|
1128
|
+
*
|
|
1129
|
+
* 注意这里**不再**兜底「把清单里的栏目摘出导航」:清单内的导航归属在 `stageCategories`
|
|
1130
|
+
* 里按 `menus[]` 一次定好(见 `navStateOf`:没被引用 ⇒ 建的时候就是 `in_menu=false`)。
|
|
1131
|
+
* 如果在这里再摘一遍,没有 `menus[]` 的老清单会连整站导航一起被摘空(以前正是这么绕开的)。
|
|
1132
|
+
*/
|
|
1133
|
+
if (!flat.length) return entries;
|
|
1134
|
+
if (!dryRun) {
|
|
1135
|
+
/* 栏目阶段可能刚建出一批:落库后以服务端为准重建 path 索引(服务端算 path,本地不猜) */
|
|
1136
|
+
state.cats = arrayOf(await call('categoryList'));
|
|
1137
|
+
state.catByPath = new Map();
|
|
1138
|
+
for (const row of state.cats) {
|
|
1139
|
+
const p = normalizePath(row.path);
|
|
1140
|
+
if (p) state.catByPath.set(p, row);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
/* 兜底:服务端列表里还没有的(例如刚建、列表缓存未更新),按 manifest 声明的 path 认领 */
|
|
1144
|
+
const byDeclaredPath = new Map();
|
|
1145
|
+
for (const cat of manifest.categories) {
|
|
1146
|
+
const got = state.catIdBySlug.get(cat.slug);
|
|
1147
|
+
if (!got || !got.id) continue;
|
|
1148
|
+
const p = normalizePath(got.path || cat.declaredPath);
|
|
1149
|
+
if (p) byDeclaredPath.set(p, got);
|
|
1150
|
+
}
|
|
1151
|
+
const whitelist = new Set();
|
|
1152
|
+
for (const menu of flat) {
|
|
1153
|
+
if (!menu.path) continue;
|
|
1154
|
+
whitelist.add(menu.path);
|
|
1155
|
+
const item = `menu:${menu.path}`;
|
|
1156
|
+
const row = state.catByPath.get(menu.path) || byDeclaredPath.get(menu.path) || null;
|
|
1157
|
+
const label = `${' '.repeat(Math.max(0, menu.depth - 1))}${menu.label} → ${menu.path}`;
|
|
1158
|
+
if (!row) {
|
|
1159
|
+
if (!dryRun) {
|
|
1160
|
+
entries.push(
|
|
1161
|
+
markFail({ kind: 'menu', item, planned: 'update', action: 'update', label, detail: '' }, `找不到 path 为 "${menu.path}" 的栏目`),
|
|
1162
|
+
);
|
|
1163
|
+
} else {
|
|
1164
|
+
entries.push({ kind: 'menu', item, planned: 'skip', action: 'skip', label, detail: '栏目本次新建,导航随建' });
|
|
1165
|
+
}
|
|
1166
|
+
continue;
|
|
1167
|
+
}
|
|
1168
|
+
const needSort = Number(row.sort || 0) !== Number(menu.sort);
|
|
1169
|
+
const needShow = row.in_menu !== true;
|
|
1170
|
+
if (!needSort && !needShow) {
|
|
1171
|
+
entries.push({ kind: 'menu', item, planned: 'skip', action: 'skip', label, detail: '导航已一致' });
|
|
1172
|
+
continue;
|
|
1173
|
+
}
|
|
1174
|
+
const detail = [needShow ? '加入导航' : '', needSort ? `sort:${row.sort} → ${menu.sort}` : ''].filter(Boolean).join(';');
|
|
1175
|
+
const entry = { kind: 'menu', item, planned: 'update', action: 'update', label, detail };
|
|
1176
|
+
entry.run = async () => {
|
|
1177
|
+
await call('categorySave', { id: String(row.id), in_menu: true, sort: menu.sort });
|
|
1178
|
+
};
|
|
1179
|
+
entries.push(entry);
|
|
1180
|
+
}
|
|
1181
|
+
/* 清单外的栏目:从导航里摘掉(只动非 builtin、当前在导航里的) */
|
|
1182
|
+
const manifestCatIds = new Set();
|
|
1183
|
+
for (const got of state.catIdBySlug.values()) if (got && got.id) manifestCatIds.add(String(got.id));
|
|
1184
|
+
for (const row of state.cats) {
|
|
1185
|
+
if (row.builtin === true) continue;
|
|
1186
|
+
if (row.in_menu !== true) continue;
|
|
1187
|
+
/**
|
|
1188
|
+
* 清单里的栏目:导航归属已由 `stageCategories` 按 `menus[]` 决定(建/改时带 `in_menu`),
|
|
1189
|
+
* 这里再摘一次只会多出一条重复条目。只跳过**清单里**的,清单外的照样按老逻辑摘。
|
|
1190
|
+
* 认 ID 不认 path(服务端 path 可能与 manifest 写法不同)。
|
|
1191
|
+
*/
|
|
1192
|
+
if (manifestCatIds.has(String(row.id))) continue;
|
|
1193
|
+
const p = normalizePath(row.path);
|
|
1194
|
+
if (!p || whitelist.has(p)) continue;
|
|
1195
|
+
/* 首页(`/`)不在 menus[] 里是因为契约写不出 `/`,不是"没被引用"—— 永远留在导航 */
|
|
1196
|
+
if (p === '/') continue;
|
|
1197
|
+
const item = `menu:${p}`;
|
|
1198
|
+
const entry = {
|
|
1199
|
+
kind: 'menu',
|
|
1200
|
+
item,
|
|
1201
|
+
plan: 'update',
|
|
1202
|
+
planned: 'update',
|
|
1203
|
+
action: 'update',
|
|
1204
|
+
label: `${row.name || row.slug} 不在 menus[] 里`,
|
|
1205
|
+
detail: '从导航移除(in_menu=false)',
|
|
1206
|
+
};
|
|
1207
|
+
entry.run = async () => {
|
|
1208
|
+
await call('categorySave', { id: String(row.id), in_menu: false });
|
|
1209
|
+
};
|
|
1210
|
+
entries.push(entry);
|
|
1211
|
+
}
|
|
1212
|
+
return entries;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/* ── 站点信息:logo ───────────────────────────────────────── */
|
|
1216
|
+
async function stageSiteLogo() {
|
|
1217
|
+
const entries = [];
|
|
1218
|
+
const logo = manifest.assets.find((a) => a.use === 'logo');
|
|
1219
|
+
if (!logo) return entries;
|
|
1220
|
+
const item = 'site:logo';
|
|
1221
|
+
const assetId = state.assetIdByKey.get(logo.key) || '';
|
|
1222
|
+
if (!assetId) {
|
|
1223
|
+
if (dryRun) {
|
|
1224
|
+
entries.push({
|
|
1225
|
+
kind: 'site',
|
|
1226
|
+
item,
|
|
1227
|
+
planned: 'update',
|
|
1228
|
+
action: 'update',
|
|
1229
|
+
label: '站点 logo',
|
|
1230
|
+
detail: '写入站点信息 logo(素材这次会新建)',
|
|
1231
|
+
});
|
|
1232
|
+
} else {
|
|
1233
|
+
entries.push(
|
|
1234
|
+
markFail({ kind: 'site', item, planned: 'update', action: 'update', label: '站点 logo', detail: '' }, 'logo 素材没上传成功'),
|
|
1235
|
+
);
|
|
1236
|
+
}
|
|
1237
|
+
return entries;
|
|
1238
|
+
}
|
|
1239
|
+
let current = '';
|
|
1240
|
+
try {
|
|
1241
|
+
if (!state.siteInfo) state.siteInfo = await siteCore('siteInfoGet', {});
|
|
1242
|
+
const values = (state.siteInfo && (state.siteInfo.values || state.siteInfo)) || {};
|
|
1243
|
+
current = valueId(values.logo);
|
|
1244
|
+
} catch (err) {
|
|
1245
|
+
notes.push(`读站点信息失败(${errorText(err)}),logo 未核对`);
|
|
1246
|
+
return entries;
|
|
1247
|
+
}
|
|
1248
|
+
if (current === assetId) {
|
|
1249
|
+
entries.push({ kind: 'site', item, planned: 'skip', action: 'skip', label: '站点 logo', detail: `已是 ${assetId}` });
|
|
1250
|
+
return entries;
|
|
1251
|
+
}
|
|
1252
|
+
/* 公司信息还没初始化时保存会被必填项挡住(服务端 400)—— 那样每次重跑都"要写",破坏幂等;
|
|
1253
|
+
先在读侧判定,没初始化就明确 skip 并记一条 note,让人去后台补公司信息后再跑。 */
|
|
1254
|
+
const values = (state.siteInfo && (state.siteInfo.values || state.siteInfo)) || {};
|
|
1255
|
+
const initialized = Object.keys(values).some((k) => /^company_/.test(k) && values[k]);
|
|
1256
|
+
if (!initialized) {
|
|
1257
|
+
entries.push({
|
|
1258
|
+
kind: 'site',
|
|
1259
|
+
item,
|
|
1260
|
+
planned: 'skip',
|
|
1261
|
+
action: 'skip',
|
|
1262
|
+
label: '站点 logo',
|
|
1263
|
+
detail: '公司信息未初始化,logo 暂不写(后台补 company_* 后重跑即可)',
|
|
1264
|
+
});
|
|
1265
|
+
notes.push('站点「公司信息」还没初始化:logo 需后台补一次,或先补全 company_* 再重跑');
|
|
1266
|
+
return entries;
|
|
1267
|
+
}
|
|
1268
|
+
const entry = {
|
|
1269
|
+
kind: 'site',
|
|
1270
|
+
item,
|
|
1271
|
+
planned: 'update',
|
|
1272
|
+
action: 'update',
|
|
1273
|
+
label: '站点 logo',
|
|
1274
|
+
detail: current ? `${current} → ${assetId}` : `写入 ${assetId}`,
|
|
1275
|
+
};
|
|
1276
|
+
entry.run = async (row) => {
|
|
1277
|
+
try {
|
|
1278
|
+
await siteCore('siteInfoSave', { values: { logo: assetId } });
|
|
1279
|
+
row.detail = `已写入站点信息 logo ${assetId}`;
|
|
1280
|
+
} catch (err) {
|
|
1281
|
+
/* 公司信息还没初始化时保存会被必填项挡住 —— 不该挡住建站,记一条 note 让人去后台补 */
|
|
1282
|
+
if (isNotReady(err)) {
|
|
1283
|
+
row.action = 'skip';
|
|
1284
|
+
row.detail = `公司信息未初始化,logo 未写入(${errorText(err)})`;
|
|
1285
|
+
notes.push('站点「公司信息」必填项还没填,logo 需后台补一次(或补全 company_* 后重跑)');
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
throw err;
|
|
1289
|
+
}
|
|
1290
|
+
};
|
|
1291
|
+
entries.push(entry);
|
|
1292
|
+
return entries;
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
/* ── 工具 ─────────────────────────────────────────────────── */
|
|
1296
|
+
async function contentsOfCategory(categoryId) {
|
|
1297
|
+
const key = String(categoryId);
|
|
1298
|
+
if (state.contentsByCat.has(key)) return state.contentsByCat.get(key);
|
|
1299
|
+
const rows = [];
|
|
1300
|
+
for (let page = 1; page <= 5; page += 1) {
|
|
1301
|
+
const res = await call('contentList', { categoryId, page, pageSize: 100 });
|
|
1302
|
+
const batch = arrayOf(res);
|
|
1303
|
+
rows.push(...batch);
|
|
1304
|
+
const total = Number(res && res.total) || rows.length;
|
|
1305
|
+
if (batch.length < 100 || rows.length >= total) break;
|
|
1306
|
+
}
|
|
1307
|
+
state.contentsByCat.set(key, rows);
|
|
1308
|
+
return rows;
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
/* ── 主流程 ───────────────────────────────────────────────── */
|
|
1312
|
+
async function run() {
|
|
1313
|
+
await loadExisting();
|
|
1314
|
+
preflight();
|
|
1315
|
+
|
|
1316
|
+
await exec(await stageAssets());
|
|
1317
|
+
await exec(await stageCategories());
|
|
1318
|
+
await exec(await stageFields());
|
|
1319
|
+
await exec(await stageContents());
|
|
1320
|
+
await exec(await stageSingles());
|
|
1321
|
+
await exec(await stageMenus());
|
|
1322
|
+
await exec(await stageSiteLogo());
|
|
1323
|
+
|
|
1324
|
+
const counts = { create: 0, update: 0, skip: 0, fail: 0 };
|
|
1325
|
+
for (const row of items) counts[row.action] = (counts[row.action] || 0) + 1;
|
|
1326
|
+
/* 校验期就判死的条目不会进 items(压根没执行)——单独加回来,汇总行不能显示「失败 0」 */
|
|
1327
|
+
counts.fail += failed.filter((one) => one.stage === 'validate').length;
|
|
1328
|
+
counts.failed_total = failed.length;
|
|
1329
|
+
|
|
1330
|
+
return {
|
|
1331
|
+
items,
|
|
1332
|
+
counts,
|
|
1333
|
+
notes,
|
|
1334
|
+
created: items.filter((r) => r.action === 'create').map((r) => r.item),
|
|
1335
|
+
updated: items.filter((r) => r.action === 'update').map((r) => r.item),
|
|
1336
|
+
skipped: items.filter((r) => r.action === 'skip').map((r) => r.item),
|
|
1337
|
+
failed,
|
|
1338
|
+
};
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
return { run };
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
/* ------------------------------------------------------------------ diff */
|
|
1345
|
+
|
|
1346
|
+
/** 栏目 diff:只比 manifest 显式关心的字段;返回 `{payload, changes}` */
|
|
1347
|
+
function diffCategory(row, want) {
|
|
1348
|
+
const payload = {};
|
|
1349
|
+
const changes = [];
|
|
1350
|
+
if (want.name != null && String(row.name || '') !== want.name) {
|
|
1351
|
+
payload.name = want.name;
|
|
1352
|
+
changes.push(`name:${row.name || '(空)'} → ${want.name}`);
|
|
1353
|
+
}
|
|
1354
|
+
if (want.parentId !== undefined && String(row.parent_id || '') !== String(want.parentId || '')) {
|
|
1355
|
+
payload.parent_id = want.parentId || null;
|
|
1356
|
+
changes.push('换父栏目');
|
|
1357
|
+
}
|
|
1358
|
+
if (want.kind && String(row.kind || '') !== want.kind) {
|
|
1359
|
+
payload.kind = want.kind;
|
|
1360
|
+
changes.push(`kind:${row.kind || '(空)'} → ${want.kind}`);
|
|
1361
|
+
}
|
|
1362
|
+
if (want.sort != null && Number(row.sort || 0) !== Number(want.sort)) {
|
|
1363
|
+
payload.sort = want.sort;
|
|
1364
|
+
changes.push(`sort:${row.sort} → ${want.sort}`);
|
|
1365
|
+
}
|
|
1366
|
+
/* 导航归属:`undefined` = 本次不动(老清单);只比布尔值,避免把服务端默认值当成「有变」 */
|
|
1367
|
+
if (typeof want.inMenu === 'boolean' && Boolean(row.in_menu) !== want.inMenu) {
|
|
1368
|
+
payload.in_menu = want.inMenu;
|
|
1369
|
+
changes.push(`导航:${row.in_menu ? '显示' : '隐藏'} → ${want.inMenu ? '显示' : '隐藏'}`);
|
|
1370
|
+
}
|
|
1371
|
+
if (want.templateFile && normTemplate(row.template_path) !== normTemplate(want.templateFile)) {
|
|
1372
|
+
payload.template_path = want.templateFile;
|
|
1373
|
+
changes.push(`模板:${row.template_path || '(空)'} → ${want.templateFile}`);
|
|
1374
|
+
}
|
|
1375
|
+
if (want.description && String(row.description || '') !== want.description) {
|
|
1376
|
+
payload.description = want.description;
|
|
1377
|
+
changes.push('栏目简介有变');
|
|
1378
|
+
}
|
|
1379
|
+
if (want.seoDescription && String(row.seo_description || '') !== want.seoDescription) {
|
|
1380
|
+
payload.seo_description = want.seoDescription;
|
|
1381
|
+
changes.push('seo 描述有变');
|
|
1382
|
+
}
|
|
1383
|
+
if (want.seoKeywords && String(row.seo_keywords || '') !== want.seoKeywords) {
|
|
1384
|
+
payload.seo_keywords = want.seoKeywords;
|
|
1385
|
+
changes.push('seo 关键词有变');
|
|
1386
|
+
}
|
|
1387
|
+
return { payload, changes };
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
/** 内容 / 单页 diff:正文按 `ext._wk_manifest.body_sha256` 指纹比;`ext` 合并写(不清别人的键) */
|
|
1391
|
+
function diffContent(row, want, bodyText, assetIds) {
|
|
1392
|
+
const payload = { id: String(row.id) };
|
|
1393
|
+
const changes = [];
|
|
1394
|
+
if (String(row.title || '') !== want.title) {
|
|
1395
|
+
payload.title = want.title;
|
|
1396
|
+
changes.push(`标题:${row.title || '(空)'} → ${want.title}`);
|
|
1397
|
+
}
|
|
1398
|
+
if (want.slug && String(row.slug || '').toLowerCase() !== want.slug) {
|
|
1399
|
+
payload.slug = want.slug;
|
|
1400
|
+
changes.push(`slug:${row.slug || '(空)'} → ${want.slug}`);
|
|
1401
|
+
}
|
|
1402
|
+
if (want.status && String(row.status || '') !== want.status) {
|
|
1403
|
+
payload.status = want.status;
|
|
1404
|
+
changes.push(`状态:${row.status} → ${want.status}`);
|
|
1405
|
+
}
|
|
1406
|
+
/* 服务端把 description / keywords / cover_asset_id / attachments 收在 ext 里(顶层列没有),两边都认 */
|
|
1407
|
+
const ext0 = row.ext && typeof row.ext === 'object' ? row.ext : {};
|
|
1408
|
+
const haveDescription = String(row.description != null ? row.description : (ext0.description != null ? ext0.description : ''));
|
|
1409
|
+
const haveKeywords = String(row.keywords != null ? row.keywords : (ext0.keywords != null ? ext0.keywords : ''));
|
|
1410
|
+
if (want.description && haveDescription !== want.description) {
|
|
1411
|
+
payload.description = want.description;
|
|
1412
|
+
changes.push('描述有变');
|
|
1413
|
+
}
|
|
1414
|
+
if (want.keywords && haveKeywords !== want.keywords) {
|
|
1415
|
+
payload.keywords = want.keywords;
|
|
1416
|
+
changes.push('关键词有变');
|
|
1417
|
+
}
|
|
1418
|
+
if (want.sort != null && Number(row.sort || 0) !== Number(want.sort)) {
|
|
1419
|
+
payload.sort = want.sort;
|
|
1420
|
+
changes.push(`sort:${row.sort} → ${want.sort}`);
|
|
1421
|
+
}
|
|
1422
|
+
if (Boolean(row.pinned) !== Boolean(want.pinned)) {
|
|
1423
|
+
payload.pinned = Boolean(want.pinned);
|
|
1424
|
+
changes.push(`置顶:${Boolean(row.pinned)} → ${Boolean(want.pinned)}`);
|
|
1425
|
+
}
|
|
1426
|
+
const wantIds = attachmentIds(want, assetIds);
|
|
1427
|
+
const haveRaw = Array.isArray(row.attachments) && row.attachments.length ? row.attachments : ext0.attachments;
|
|
1428
|
+
const haveIds = (Array.isArray(haveRaw) ? haveRaw : [])
|
|
1429
|
+
.map((one) => (typeof one === 'string' ? one : (one && (one.id || one.asset_id)) || ''))
|
|
1430
|
+
.filter(Boolean);
|
|
1431
|
+
if (JSON.stringify(wantIds) !== JSON.stringify(haveIds)) {
|
|
1432
|
+
payload.attachments = wantIds;
|
|
1433
|
+
changes.push(`附件:${haveIds.length} → ${wantIds.length}`);
|
|
1434
|
+
}
|
|
1435
|
+
const wantCover = want.cover ? assetIds.get(want.cover) || (looksLikeUuid(want.cover) ? want.cover : '') : '';
|
|
1436
|
+
const haveCover = String(row.cover_asset_id || ext0.cover_asset_id || '');
|
|
1437
|
+
if (wantCover && haveCover !== wantCover) {
|
|
1438
|
+
payload.cover_asset_id = wantCover;
|
|
1439
|
+
changes.push('封面有变');
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
const ext = ext0;
|
|
1443
|
+
const marker = ext._wk_manifest || {};
|
|
1444
|
+
const bodyHash = sha256(bodyText || '');
|
|
1445
|
+
const customChanged = Object.entries(want.customFields || {}).some(([k, v]) => jsonOf(ext[k]) !== jsonOf(v));
|
|
1446
|
+
const markerChanged =
|
|
1447
|
+
String(marker.format || '') !== MANIFEST_FORMAT ||
|
|
1448
|
+
String(marker.body_sha256 || '') !== bodyHash ||
|
|
1449
|
+
String(marker.source || '') !== 'manifest';
|
|
1450
|
+
if (markerChanged) {
|
|
1451
|
+
payload.body = bodyText;
|
|
1452
|
+
changes.push(marker.body_sha256 ? '正文有变' : '首次写入正文');
|
|
1453
|
+
} else if (customChanged) {
|
|
1454
|
+
changes.push('自定义字段有变');
|
|
1455
|
+
}
|
|
1456
|
+
if (markerChanged || customChanged) {
|
|
1457
|
+
payload.ext = {
|
|
1458
|
+
...ext,
|
|
1459
|
+
...(want.customFields || {}),
|
|
1460
|
+
_wk_manifest: {
|
|
1461
|
+
format: MANIFEST_FORMAT,
|
|
1462
|
+
body_sha256: bodyHash,
|
|
1463
|
+
source: 'manifest',
|
|
1464
|
+
applied_at: new Date().toISOString(),
|
|
1465
|
+
},
|
|
1466
|
+
};
|
|
1467
|
+
}
|
|
1468
|
+
return { payload, changes };
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
/** 新建内容时写进 ext 的初始指纹 */
|
|
1472
|
+
function freshExt(want, bodyText) {
|
|
1473
|
+
return {
|
|
1474
|
+
...(want.customFields || {}),
|
|
1475
|
+
_wk_manifest: {
|
|
1476
|
+
format: MANIFEST_FORMAT,
|
|
1477
|
+
body_sha256: sha256(bodyText || ''),
|
|
1478
|
+
source: 'manifest',
|
|
1479
|
+
applied_at: new Date().toISOString(),
|
|
1480
|
+
},
|
|
1481
|
+
};
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
/* ------------------------------------------------------------------ 发布 */
|
|
1485
|
+
|
|
1486
|
+
async function publishAfterApply(ctx, site, opts) {
|
|
1487
|
+
const siteId = String(site.id);
|
|
1488
|
+
const started = (await ctx.publishCall('cmsSitePublishStart', { siteId }, { siteId })) || {};
|
|
1489
|
+
const jobId = String(started.jobId || '');
|
|
1490
|
+
if (!ctx.out.json) {
|
|
1491
|
+
ctx.out.ok(`发布任务已提交:${jobId || '(后端没回 jobId)'}(${started.status || 'queued'},共 ${started.total || '?'} 页)`);
|
|
1492
|
+
}
|
|
1493
|
+
if (!opts.wait || !jobId) return { job_id: jobId, status: started.status || 'queued', waited: false, ok: true };
|
|
1494
|
+
|
|
1495
|
+
const deadline = Date.now() + Math.max(30, opts.timeoutSec) * 1000;
|
|
1496
|
+
let job = started;
|
|
1497
|
+
const pending = new Set(['queued', 'running', 'pending', 'idle', '']);
|
|
1498
|
+
while (Date.now() < deadline) {
|
|
1499
|
+
await ctx.sleep(PUBLISH_POLL_MS);
|
|
1500
|
+
try {
|
|
1501
|
+
job = (await ctx.publishCall('cmsSitePublishStatus', { jobId }, { siteId })) || job;
|
|
1502
|
+
} catch {
|
|
1503
|
+
/* 轮询失败不致命:下次再看 */
|
|
1504
|
+
}
|
|
1505
|
+
if (!pending.has(String(job.status || ''))) break;
|
|
1506
|
+
}
|
|
1507
|
+
const status = String(job.status || 'unknown');
|
|
1508
|
+
if (!ctx.out.json) ctx.out.line(` 发布状态:${status}(${job.done || 0}/${job.total || '?'} 页)`);
|
|
1509
|
+
if (status === 'success') return { job_id: jobId, status, waited: true, ok: true };
|
|
1510
|
+
if (status === 'partial') {
|
|
1511
|
+
return { job_id: jobId, status, waited: true, ok: false, reason: '发布部分页面失败(看 `wkgeo publish status`)' };
|
|
1512
|
+
}
|
|
1513
|
+
if (status === 'failed' || status === 'canceled') {
|
|
1514
|
+
return { job_id: jobId, status, waited: true, ok: false, reason: `发布任务 ${status}` };
|
|
1515
|
+
}
|
|
1516
|
+
return {
|
|
1517
|
+
job_id: jobId,
|
|
1518
|
+
status,
|
|
1519
|
+
waited: true,
|
|
1520
|
+
ok: false,
|
|
1521
|
+
reason: `发布任务超时未结束(${status})—— 用 \`wkgeo publish status\` 再看`,
|
|
1522
|
+
};
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
/* ------------------------------------------------------------------ 输出 */
|
|
1526
|
+
|
|
1527
|
+
function printApplyReport(out, data, manifest) {
|
|
1528
|
+
const dry = data.dry_run;
|
|
1529
|
+
out.head(
|
|
1530
|
+
dry
|
|
1531
|
+
? `建站内容落地计划(dry-run,一个字都不写库)—— 站点 ${data.site.name || data.site.id}`
|
|
1532
|
+
: `建站内容落地 —— 站点 ${data.site.name || data.site.id}`,
|
|
1533
|
+
);
|
|
1534
|
+
out.line(` manifest:${oneLine(data.file, 100)}`);
|
|
1535
|
+
out.line(` 契约内容:${summarizeManifest(manifest)}`);
|
|
1536
|
+
out.line(` 站点来源:${data.site.source === 'flag' ? '--site' : data.site.source === 'env' ? 'WKONGAI_SITE' : 'site use'}`);
|
|
1537
|
+
for (const w of data.warnings || []) out.warn(` ⚠ ${w.path} ${w.message}`);
|
|
1538
|
+
|
|
1539
|
+
const group = (kind, title) => {
|
|
1540
|
+
const rows = data.items.filter((one) => one.kind === kind);
|
|
1541
|
+
if (!rows.length) return;
|
|
1542
|
+
out.line('');
|
|
1543
|
+
out.line(` ${title}`);
|
|
1544
|
+
for (const row of rows) {
|
|
1545
|
+
const mark = row.action === 'create' ? '+' : row.action === 'update' ? '~' : row.action === 'skip' ? '=' : '✗';
|
|
1546
|
+
out.line(` ${mark} ${row.label}${row.detail ? ` ${oneLine(row.detail, 120)}` : ''}`);
|
|
1547
|
+
}
|
|
1548
|
+
};
|
|
1549
|
+
group('asset', '素材');
|
|
1550
|
+
group('category', '栏目');
|
|
1551
|
+
group('field', '自定义字段');
|
|
1552
|
+
group('content', '内容');
|
|
1553
|
+
group('single', '单页');
|
|
1554
|
+
group('menu', '导航');
|
|
1555
|
+
group('site', '站点信息');
|
|
1556
|
+
|
|
1557
|
+
if (manifest.unfilled && manifest.unfilled.length) {
|
|
1558
|
+
out.line('');
|
|
1559
|
+
out.line(` 未填充(${manifest.unfilled.length}):`);
|
|
1560
|
+
for (const one of manifest.unfilled) {
|
|
1561
|
+
out.line(` · ${one.item} —— ${one.reason}${one.detail ? `:${one.detail}` : ''}`);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
for (const note of data.notes || []) out.warn(` ⚠ ${note}`);
|
|
1565
|
+
|
|
1566
|
+
const c = data.counts || {};
|
|
1567
|
+
out.line('');
|
|
1568
|
+
const verb = dry ? '将' : '已';
|
|
1569
|
+
out.line(
|
|
1570
|
+
` 汇总:${verb}建 ${c.create || 0} | ${verb}改 ${c.update || 0} | ${verb}跳过 ${c.skip || 0} | 失败 ${c.fail || 0}`,
|
|
1571
|
+
);
|
|
1572
|
+
if (dry) out.info(' 确认无误就加 `--confirm` 真写(幂等,重跑只 skipped)。');
|
|
1573
|
+
else if (!c.fail) out.ok(' 全部落库完成。');
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
/* ------------------------------------------------------------------ 小工具 */
|
|
1577
|
+
|
|
1578
|
+
function arrayOf(res) {
|
|
1579
|
+
if (Array.isArray(res)) return res.map((r) => r || {});
|
|
1580
|
+
if (res && Array.isArray(res.rows)) return res.rows.map((r) => r || {});
|
|
1581
|
+
return [];
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
function attachmentIds(want, assetIds) {
|
|
1585
|
+
const out = [];
|
|
1586
|
+
for (const one of want.attachments || []) {
|
|
1587
|
+
const id = assetIds.get(one.key) || (one.mode === 'site' ? one.key : '');
|
|
1588
|
+
if (id && !out.includes(id)) out.push(id);
|
|
1589
|
+
}
|
|
1590
|
+
return out;
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
function readBodyText(want) {
|
|
1594
|
+
if (want.bodyInline) return want.bodyInline;
|
|
1595
|
+
if (want.bodyPath) {
|
|
1596
|
+
try {
|
|
1597
|
+
return fs.readFileSync(want.bodyPath, 'utf8');
|
|
1598
|
+
} catch {
|
|
1599
|
+
return '';
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
return '';
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
function normTemplate(v) {
|
|
1606
|
+
return String(v || '').replace(/^\.?\//, '');
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
function sha256(text) {
|
|
1610
|
+
return crypto.createHash('sha256').update(String(text || ''), 'utf8').digest('hex');
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
function jsonOf(v) {
|
|
1614
|
+
if (v === undefined) return '';
|
|
1615
|
+
try {
|
|
1616
|
+
return JSON.stringify(v);
|
|
1617
|
+
} catch {
|
|
1618
|
+
return String(v);
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
function valueId(v) {
|
|
1623
|
+
if (!v) return '';
|
|
1624
|
+
if (typeof v === 'string') return v;
|
|
1625
|
+
if (typeof v === 'object') return String(v.id || v.assetId || v.url || '');
|
|
1626
|
+
return '';
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
function isNotReady(err) {
|
|
1630
|
+
const kind = String((err && err.kind) || '');
|
|
1631
|
+
const code = Number((err && err.code) || 0);
|
|
1632
|
+
return kind === 'bad_request' || kind === 'not_found' || code === 400 || code === 404;
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
function errorText(err) {
|
|
1636
|
+
if (!err) return '未知错误';
|
|
1637
|
+
const msg = String(err.message || err);
|
|
1638
|
+
const hint = err.hint ? `(${err.hint})` : '';
|
|
1639
|
+
return oneLine(`${msg}${hint}`, 300);
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
function oneLine(v, max = 160) {
|
|
1643
|
+
const s = String(v == null ? '' : v)
|
|
1644
|
+
.replace(/\s+/g, ' ')
|
|
1645
|
+
.trim();
|
|
1646
|
+
return s.length > max ? `${s.slice(0, max)}…` : s;
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
/** 给别的模块用(文档示例 / 单测):本命令接受的上限快照 */
|
|
1650
|
+
export const CONTENT_APPLY_LIMITS = LIMITS;
|