@wkgeo/cli 1.1.0 → 1.1.2
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 +45 -0
- package/README.md +163 -25
- package/SKILL.md +121 -36
- package/lib/args.mjs +2 -2
- package/lib/asset-role.mjs +68 -0
- package/lib/cms.mjs +2 -2
- package/lib/commands/billing.mjs +56 -10
- package/lib/commands/cms.mjs +49 -13
- package/lib/commands/content.mjs +108 -0
- package/lib/commands/crawl.mjs +219 -0
- package/lib/commands/geo.mjs +1673 -0
- package/lib/commands/index.mjs +640 -541
- package/lib/commands/member.mjs +930 -0
- package/lib/commands/seo.mjs +1386 -0
- package/lib/commands/siteops.mjs +3 -3
- package/lib/commands/sites.mjs +21 -125
- package/lib/commands/template.mjs +1222 -6
- package/lib/config.mjs +16 -13
- package/lib/content-apply.mjs +664 -0
- package/lib/content-check.mjs +887 -0
- package/lib/content-contract.mjs +957 -0
- package/lib/errors.mjs +6 -0
- package/lib/format.mjs +5 -1
- package/lib/login.mjs +15 -3
- package/lib/manifest-apply.mjs +1499 -0
- package/lib/manifest-schema.json +476 -0
- package/lib/manifest.mjs +1082 -0
- package/lib/oauth-urls.mjs +1 -1
- package/lib/runtime.mjs +5 -1
- package/lib/site-create.mjs +536 -0
- package/lib/template-anim.mjs +1338 -0
- package/lib/template-animation.mjs +232 -0
- package/lib/template-capture.mjs +700 -0
- package/lib/template-gen.mjs +1204 -0
- package/lib/template-pack.mjs +206 -0
- package/lib/template-publish.mjs +733 -0
- package/lib/template-score.mjs +865 -0
- package/lib/template-validate.mjs +368 -0
- package/lib/template-verify.mjs +434 -0
- package/lib/templates-sync.mjs +616 -0
- package/package.json +2 -2
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模板校验(9 关)——`wkgeo template validate` 与 `pack` 共用同一套门禁。
|
|
3
|
+
*
|
|
4
|
+
* 口径来源:`templates/_base/README.md` §1(12 种字段类型 / 8 个字段键)、§3(目录约定)、
|
|
5
|
+
* §4(v2 字段表)、§5(禁止事项)。**这里不另创规则**,每条门禁都能指回 README 的哪一节。
|
|
6
|
+
*
|
|
7
|
+
* 9 关:
|
|
8
|
+
* 1. manifest template.json 存在且是合法 JSON
|
|
9
|
+
* 2. format `_format` / key / name / version / demo;key 必须等于目录名(§3)
|
|
10
|
+
* 3. site site.title/logo/theme/seo 齐全,logo 文件真实存在(§4)
|
|
11
|
+
* 4. menus menus.top 非空,项字段与 menu_type/link_type 枚举合法(§1)
|
|
12
|
+
* 5. columns ≥1 个栏目;12 个栏键齐全;fields 8 键齐全且 type ∈ 12 种;items 与 fields 对齐(§1 §4)
|
|
13
|
+
* 6. pages pages/index.html 存在;columns[].page 都有对应文件;每页有 <title>(§3)
|
|
14
|
+
* 7. base assets/base/ 存在,且能找到 `templates/_base` 时逐字节一致(§3)
|
|
15
|
+
* 8. links 页面引用的本地 CSS/JS/图片都在;资源类标签不得走外网(§5.2)
|
|
16
|
+
* 9. design design/desktop-1440.png 与 mobile-375.png 尺寸正确(t0/t1500 帧可选但建议有)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
import fsp from 'node:fs/promises';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
|
|
23
|
+
import { CliError, EXIT } from './errors.mjs';
|
|
24
|
+
import { readPngSize } from './template-capture.mjs';
|
|
25
|
+
import { VIEWPORTS } from './template-capture.mjs';
|
|
26
|
+
import { TEMPLATE_FORMAT, resolveBaseDir } from './template-gen.mjs';
|
|
27
|
+
|
|
28
|
+
export const FIELD_TYPES = ['text', 'textarea', 'richtext', 'image', 'gallery', 'date', 'number', 'select', 'link', 'phone', 'email', 'boolean'];
|
|
29
|
+
export const FIELD_KEYS = ['key', 'label', 'type', 'required', 'options', 'group', 'sort', 'hint'];
|
|
30
|
+
export const COLUMN_KEYS = ['key', 'name', 'slug', 'page', 'in_menu', 'menu_type', 'link_type', 'anchor', 'parent', 'sort', 'seo', 'fields', 'items'];
|
|
31
|
+
const MENU_TYPES = ['page', 'custom'];
|
|
32
|
+
const LINK_TYPES = ['internal', 'anchor', 'external'];
|
|
33
|
+
|
|
34
|
+
const isObj = (v) => v && typeof v === 'object' && !Array.isArray(v);
|
|
35
|
+
const nonEmptyStr = (v) => typeof v === 'string' && v.trim().length > 0;
|
|
36
|
+
const kebab = (s) => typeof s === 'string' && /^[a-z0-9][a-z0-9-]*$/.test(s);
|
|
37
|
+
|
|
38
|
+
async function walkFiles(root, out = [], skip = new Set(['node_modules', '.git'])) {
|
|
39
|
+
const entries = await fsp.readdir(root, { withFileTypes: true }).catch(() => []);
|
|
40
|
+
for (const e of entries) {
|
|
41
|
+
if (skip.has(e.name)) continue;
|
|
42
|
+
const full = path.join(root, e.name);
|
|
43
|
+
if (e.isDirectory()) await walkFiles(full, out, skip);
|
|
44
|
+
else if (e.isFile()) out.push(full);
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 逐字节比较两个目录(相对路径集合 + 每个文件内容) */
|
|
50
|
+
export async function compareTrees(a, b) {
|
|
51
|
+
const fa = await walkFiles(a);
|
|
52
|
+
const fb = await walkFiles(b);
|
|
53
|
+
const rel = (root, list) => new Map(list.map((f) => [path.relative(root, f).replace(/\\/g, '/'), f]));
|
|
54
|
+
const ma = rel(a, fa);
|
|
55
|
+
const mb = rel(b, fb);
|
|
56
|
+
const missing = [...ma.keys()].filter((k) => !mb.has(k));
|
|
57
|
+
const extra = [...mb.keys()].filter((k) => !ma.has(k));
|
|
58
|
+
const different = [];
|
|
59
|
+
for (const [k, pa] of ma) {
|
|
60
|
+
if (!mb.has(k)) continue;
|
|
61
|
+
const [ba, bb] = await Promise.all([fsp.readFile(pa), fsp.readFile(mb.get(k))]);
|
|
62
|
+
if (!ba.equals(bb)) different.push(k);
|
|
63
|
+
}
|
|
64
|
+
return { ok: !missing.length && !extra.length && !different.length, missing, extra, different, count: ma.size };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 从页面 HTML 里取"资源类"引用(不含 <a>) */
|
|
68
|
+
export function resourceRefs(html) {
|
|
69
|
+
const s = String(html || '');
|
|
70
|
+
const refs = [];
|
|
71
|
+
const push = (value, kind) => {
|
|
72
|
+
const v = String(value || '').trim();
|
|
73
|
+
if (!v || /^(data:|#|mailto:|tel:|javascript:)/i.test(v)) return;
|
|
74
|
+
refs.push({ value: v, kind });
|
|
75
|
+
};
|
|
76
|
+
for (const m of s.matchAll(/<link\b[^>]*>/gi)) {
|
|
77
|
+
const rel = (String((m[0].match(/rel\s*=\s*["']([^"']*)["']/i) || [])[1] || '').toLowerCase());
|
|
78
|
+
if (rel.split(/\s+/).includes('stylesheet') || rel.includes('preload') || rel.includes('icon')) push((m[0].match(/href\s*=\s*["']([^"']*)["']/i) || [])[1], 'css');
|
|
79
|
+
}
|
|
80
|
+
for (const m of s.matchAll(/<script\b[^>]*>/gi)) push((m[0].match(/src\s*=\s*["']([^"']*)["']/i) || [])[1], 'js');
|
|
81
|
+
for (const m of s.matchAll(/<(?:img|source|video|audio|iframe)\b[^>]*>/gi)) {
|
|
82
|
+
push((m[0].match(/(?:^|\s)src\s*=\s*["']([^"']*)["']/i) || [])[1], 'img');
|
|
83
|
+
push((m[0].match(/(?:^|\s)poster\s*=\s*["']([^"']*)["']/i) || [])[1], 'img');
|
|
84
|
+
const ss = (m[0].match(/srcset\s*=\s*["']([^"']*)["']/i) || [])[1];
|
|
85
|
+
if (ss) for (const part of ss.split(',')) push(part.trim().split(/\s+/)[0], 'img');
|
|
86
|
+
}
|
|
87
|
+
for (const m of s.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/gi)) {
|
|
88
|
+
for (const u of m[1].matchAll(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi)) push(u[2], 'css');
|
|
89
|
+
}
|
|
90
|
+
for (const m of s.matchAll(/\sstyle\s*=\s*(["'])([\s\S]*?)\1/gi)) {
|
|
91
|
+
for (const u of m[2].matchAll(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi)) push(u[2], 'img');
|
|
92
|
+
}
|
|
93
|
+
return refs;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** CSS 内部 url() 引用 */
|
|
97
|
+
function cssRefs(css) {
|
|
98
|
+
return [...String(css || '').matchAll(/url\(\s*(['"]?)([^'")]+)\1\s*\)/gi)]
|
|
99
|
+
.map((m) => m[2])
|
|
100
|
+
.filter((v) => v && !/^(data:|#|https?:)/i.test(v));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 跑 9 关校验。
|
|
105
|
+
* @returns {Promise<{ok:boolean, dir:string, checks:Array<{id,title,ok,errors:string[],warnings:string[]}>, summary:{failed:string[]}}>}
|
|
106
|
+
*/
|
|
107
|
+
export async function validateTemplate({ dir, base = null, cwd = process.cwd(), requireDesign = true } = {}) {
|
|
108
|
+
const root = path.resolve(cwd, dir || '.');
|
|
109
|
+
const checks = [];
|
|
110
|
+
const add = (id, title, errors = [], warnings = []) => checks.push({ id, title, ok: errors.length === 0, errors, warnings });
|
|
111
|
+
|
|
112
|
+
// ---- 1. manifest ----
|
|
113
|
+
let tpl = null;
|
|
114
|
+
const manifestFile = path.join(root, 'template.json');
|
|
115
|
+
let raw = '';
|
|
116
|
+
try {
|
|
117
|
+
raw = await fsp.readFile(manifestFile, 'utf8');
|
|
118
|
+
tpl = JSON.parse(raw);
|
|
119
|
+
add('manifest', 'template.json 存在且可解析', []);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
add('manifest', 'template.json 存在且可解析', [`读不到或不是合法 JSON:${manifestFile}(${e.code || e.message})`]);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!tpl) {
|
|
125
|
+
// 后面 8 关全部跳过(但保留条目,让报告完整)
|
|
126
|
+
for (const [id, title] of [
|
|
127
|
+
['format', '格式 / key / 版本'],
|
|
128
|
+
['site', 'site / 主题 / SEO'],
|
|
129
|
+
['menus', 'menus 菜单'],
|
|
130
|
+
['columns', 'columns 栏目与字段'],
|
|
131
|
+
['pages', 'pages 页面文件'],
|
|
132
|
+
['base', 'assets/base 基础库副本'],
|
|
133
|
+
['links', '页面资源引用'],
|
|
134
|
+
['design', 'design 设计稿截图'],
|
|
135
|
+
]) add(id, title, ['前置失败:template.json 不可读,跳过']);
|
|
136
|
+
return { ok: false, dir: root, checks, summary: { failed: checks.filter((c) => !c.ok).map((c) => c.id) } };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---- 2. format ----
|
|
140
|
+
{
|
|
141
|
+
const errors = [];
|
|
142
|
+
if (tpl._format !== TEMPLATE_FORMAT) errors.push(`_format 应为 "${TEMPLATE_FORMAT}",实际 ${JSON.stringify(tpl._format)}`);
|
|
143
|
+
if (!kebab(tpl.key)) errors.push(`key 必须是 kebab-case(小写字母/数字/短横线),实际 ${JSON.stringify(tpl.key)}`);
|
|
144
|
+
if (tpl.key !== path.basename(root)) errors.push(`key 必须与目录名一致:key=${JSON.stringify(tpl.key)} 目录=${JSON.stringify(path.basename(root))}`);
|
|
145
|
+
if (!nonEmptyStr(tpl.name)) errors.push('name 不能为空');
|
|
146
|
+
if (!/^\d+\.\d+\.\d+$/.test(String(tpl.version || ''))) errors.push(`version 必须是 x.y.z,实际 ${JSON.stringify(tpl.version)}`);
|
|
147
|
+
if (typeof tpl.demo !== 'boolean') errors.push('demo 必须是布尔值');
|
|
148
|
+
add('format', '格式 / key / 版本', errors);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ---- 3. site ----
|
|
152
|
+
{
|
|
153
|
+
const errors = [];
|
|
154
|
+
const site = tpl.site || {};
|
|
155
|
+
if (!nonEmptyStr(site.title)) errors.push('site.title 不能为空');
|
|
156
|
+
if (!nonEmptyStr(site.logo)) errors.push('site.logo 不能为空');
|
|
157
|
+
else if (!/^https?:/i.test(site.logo) && !fs.existsSync(path.join(root, site.logo))) errors.push(`site.logo 指向的文件不存在:${site.logo}`);
|
|
158
|
+
const theme = site.theme || {};
|
|
159
|
+
if (!nonEmptyStr(theme.primary)) errors.push('site.theme.primary 不能为空');
|
|
160
|
+
if (!nonEmptyStr(theme.font)) errors.push('site.theme.font 不能为空');
|
|
161
|
+
const seo = site.seo || {};
|
|
162
|
+
for (const k of ['title', 'keywords', 'description']) if (typeof seo[k] !== 'string') errors.push(`site.seo.${k} 必须是字符串`);
|
|
163
|
+
add('site', 'site / 主题 / SEO', errors);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ---- 4. menus ----
|
|
167
|
+
{
|
|
168
|
+
const errors = [];
|
|
169
|
+
const menus = tpl.menus || {};
|
|
170
|
+
if (!Array.isArray(menus.top) || !menus.top.length) errors.push('menus.top 必须是非空数组');
|
|
171
|
+
if (!Array.isArray(menus.footer)) errors.push('menus.footer 必须是数组');
|
|
172
|
+
const checkItem = (it, where) => {
|
|
173
|
+
if (!isObj(it)) {
|
|
174
|
+
errors.push(`${where} 不是对象`);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (!nonEmptyStr(it.label)) errors.push(`${where}.label 不能为空`);
|
|
178
|
+
if (!nonEmptyStr(it.href)) errors.push(`${where}.href 不能为空`);
|
|
179
|
+
if (!MENU_TYPES.includes(it.menu_type)) errors.push(`${where}.menu_type 必须是 ${MENU_TYPES.join('/')},实际 ${JSON.stringify(it.menu_type)}`);
|
|
180
|
+
if (!LINK_TYPES.includes(it.link_type)) errors.push(`${where}.link_type 必须是 ${LINK_TYPES.join('/')},实际 ${JSON.stringify(it.link_type)}`);
|
|
181
|
+
if (it.link_type === 'anchor' && !nonEmptyStr(it.anchor)) errors.push(`${where} link_type=anchor 时必须给 anchor`);
|
|
182
|
+
if (it.link_type === 'external' && it.target !== '_blank') errors.push(`${where} 站外链接必须 target="_blank"`);
|
|
183
|
+
if (!['_self', '_blank'].includes(it.target)) errors.push(`${where}.target 必须是 _self/_blank`);
|
|
184
|
+
if (!Array.isArray(it.children)) errors.push(`${where}.children 必须是数组`);
|
|
185
|
+
};
|
|
186
|
+
(Array.isArray(menus.top) ? menus.top : []).forEach((it, i) => checkItem(it, `menus.top[${i}]`));
|
|
187
|
+
(Array.isArray(menus.footer) ? menus.footer : []).forEach((it, i) => checkItem(it, `menus.footer[${i}]`));
|
|
188
|
+
add('menus', 'menus 菜单', errors);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ---- 5. columns ----
|
|
192
|
+
{
|
|
193
|
+
const errors = [];
|
|
194
|
+
const cols = Array.isArray(tpl.columns) ? tpl.columns : [];
|
|
195
|
+
if (!cols.length) errors.push('columns 至少要有 1 个栏目');
|
|
196
|
+
const keys = new Set();
|
|
197
|
+
cols.forEach((c, i) => {
|
|
198
|
+
const where = `columns[${i}]`;
|
|
199
|
+
if (!isObj(c)) {
|
|
200
|
+
errors.push(`${where} 不是对象`);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
for (const k of COLUMN_KEYS) if (!(k in c)) errors.push(`${where} 缺键 ${k}`);
|
|
204
|
+
if (!kebab(c.key)) errors.push(`${where}.key 必须 kebab-case,实际 ${JSON.stringify(c.key)}`);
|
|
205
|
+
if (keys.has(c.key)) errors.push(`${where}.key 重复:${c.key}`);
|
|
206
|
+
keys.add(c.key);
|
|
207
|
+
if (c.slug !== c.key) errors.push(`${where}.slug 必须等于 key(实际 ${JSON.stringify(c.slug)})`);
|
|
208
|
+
if (!nonEmptyStr(c.name)) errors.push(`${where}.name 不能为空`);
|
|
209
|
+
if (!nonEmptyStr(c.page)) errors.push(`${where}.page 不能为空(写页面主名,不带 .html)`);
|
|
210
|
+
if (typeof c.in_menu !== 'boolean') errors.push(`${where}.in_menu 必须是布尔值`);
|
|
211
|
+
if (!MENU_TYPES.includes(c.menu_type)) errors.push(`${where}.menu_type 必须是 ${MENU_TYPES.join('/')}`);
|
|
212
|
+
if (!LINK_TYPES.includes(c.link_type)) errors.push(`${where}.link_type 必须是 ${LINK_TYPES.join('/')}`);
|
|
213
|
+
if (!Number.isFinite(c.sort)) errors.push(`${where}.sort 必须是数字`);
|
|
214
|
+
if (!isObj(c.seo)) errors.push(`${where}.seo 必须是对象`);
|
|
215
|
+
if (!Array.isArray(c.fields) || !c.fields.length) errors.push(`${where}.fields 必须是非空数组`);
|
|
216
|
+
if (!Array.isArray(c.items)) errors.push(`${where}.items 必须是数组`);
|
|
217
|
+
const fieldKeys = new Set();
|
|
218
|
+
(Array.isArray(c.fields) ? c.fields : []).forEach((f, j) => {
|
|
219
|
+
const w = `${where}.fields[${j}]`;
|
|
220
|
+
if (!isObj(f)) {
|
|
221
|
+
errors.push(`${w} 不是对象`);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
for (const k of FIELD_KEYS) if (!(k in f)) errors.push(`${w} 缺键 ${k}`);
|
|
225
|
+
if (!/^[a-z][a-z0-9_]*$/.test(String(f.key || ''))) errors.push(`${w}.key 必须 snake_case,实际 ${JSON.stringify(f.key)}`);
|
|
226
|
+
if (fieldKeys.has(f.key)) errors.push(`${w}.key 重复:${f.key}`);
|
|
227
|
+
fieldKeys.add(f.key);
|
|
228
|
+
if (!FIELD_TYPES.includes(f.type)) errors.push(`${w}.type 必须是 12 种之一,实际 ${JSON.stringify(f.type)}`);
|
|
229
|
+
if (typeof f.required !== 'boolean') errors.push(`${w}.required 必须是布尔值`);
|
|
230
|
+
if (!Array.isArray(f.options)) errors.push(`${w}.options 必须是数组`);
|
|
231
|
+
if (!Number.isFinite(f.sort)) errors.push(`${w}.sort 必须是数字`);
|
|
232
|
+
});
|
|
233
|
+
(Array.isArray(c.items) ? c.items : []).forEach((it, j) => {
|
|
234
|
+
if (!isObj(it)) {
|
|
235
|
+
errors.push(`${where}.items[${j}] 不是对象`);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
for (const k of Object.keys(it)) if (!fieldKeys.has(k)) errors.push(`${where}.items[${j}] 有 fields 里没声明的键:${k}`);
|
|
239
|
+
});
|
|
240
|
+
if (tpl.demo === true && (!Array.isArray(c.items) || !c.items.length)) errors.push(`${where} demo=true 时 items 必须有内容`);
|
|
241
|
+
});
|
|
242
|
+
add('columns', 'columns 栏目与字段', errors);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ---- 6. pages ----
|
|
246
|
+
const pagesDir = path.join(root, 'pages');
|
|
247
|
+
{
|
|
248
|
+
const errors = [];
|
|
249
|
+
const warnings = [];
|
|
250
|
+
if (!fs.existsSync(path.join(pagesDir, 'index.html'))) errors.push('缺少 pages/index.html(入口页)');
|
|
251
|
+
const cols = Array.isArray(tpl.columns) ? tpl.columns : [];
|
|
252
|
+
for (const c of cols) {
|
|
253
|
+
if (!c || !nonEmptyStr(c.page)) continue;
|
|
254
|
+
const f = path.join(pagesDir, `${c.page}.html`);
|
|
255
|
+
if (!fs.existsSync(f)) errors.push(`栏目 ${c.key} 的 page=${c.page} 没有对应文件 pages/${c.page}.html`);
|
|
256
|
+
}
|
|
257
|
+
const pageFiles = fs.existsSync(pagesDir) ? fs.readdirSync(pagesDir).filter((f) => /\.html$/i.test(f)) : [];
|
|
258
|
+
for (const f of pageFiles) {
|
|
259
|
+
const html = fs.readFileSync(path.join(pagesDir, f), 'utf8');
|
|
260
|
+
if (!/<title[^>]*>[\s\S]*?<\/title>/i.test(html)) warnings.push(`pages/${f} 没有 <title>`);
|
|
261
|
+
if (html.length < 200) warnings.push(`pages/${f} 只有 ${html.length}B,像空页`);
|
|
262
|
+
}
|
|
263
|
+
add('pages', 'pages 页面文件', errors, warnings);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// ---- 7. base ----
|
|
267
|
+
{
|
|
268
|
+
const errors = [];
|
|
269
|
+
const warnings = [];
|
|
270
|
+
const dest = path.join(root, 'assets', 'base');
|
|
271
|
+
const baseDir = resolveBaseDir(base, { cwd });
|
|
272
|
+
if (!fs.existsSync(dest)) {
|
|
273
|
+
errors.push('缺少 assets/base/(应从 templates/_base/assets 逐字节复制)');
|
|
274
|
+
} else if (!fs.existsSync(path.join(dest, 'base.css'))) {
|
|
275
|
+
errors.push('assets/base/ 里没有 base.css');
|
|
276
|
+
} else if (baseDir) {
|
|
277
|
+
const cmp = await compareTrees(path.join(baseDir, 'assets'), dest);
|
|
278
|
+
if (!cmp.ok) {
|
|
279
|
+
const bits = [];
|
|
280
|
+
if (cmp.missing.length) bits.push(`缺 ${cmp.missing.length} 个(${cmp.missing.slice(0, 3).join(', ')})`);
|
|
281
|
+
if (cmp.extra.length) bits.push(`多 ${cmp.extra.length} 个(${cmp.extra.slice(0, 3).join(', ')})`);
|
|
282
|
+
if (cmp.different.length) bits.push(`内容不同 ${cmp.different.length} 个(${cmp.different.slice(0, 3).join(', ')})`);
|
|
283
|
+
errors.push(`assets/base/ 与 ${baseDir}/assets 不是逐字节副本:${bits.join(';')}`);
|
|
284
|
+
}
|
|
285
|
+
} else {
|
|
286
|
+
warnings.push('没找到 templates/_base,只校验了 assets/base 的存在性');
|
|
287
|
+
}
|
|
288
|
+
add('base', 'assets/base 基础库副本', errors, warnings);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ---- 8. links ----
|
|
292
|
+
{
|
|
293
|
+
const errors = [];
|
|
294
|
+
const warnings = [];
|
|
295
|
+
const pageFiles = fs.existsSync(pagesDir) ? fs.readdirSync(pagesDir).filter((f) => /\.html$/i.test(f)) : [];
|
|
296
|
+
let refCount = 0;
|
|
297
|
+
let missingImg = 0;
|
|
298
|
+
for (const f of pageFiles) {
|
|
299
|
+
const file = path.join(pagesDir, f);
|
|
300
|
+
const html = fs.readFileSync(file, 'utf8');
|
|
301
|
+
for (const ref of resourceRefs(html)) {
|
|
302
|
+
refCount++;
|
|
303
|
+
if (/^https?:\/\//i.test(ref.value) || /^\/\//.test(ref.value)) {
|
|
304
|
+
errors.push(`pages/${f} 的资源走外网(禁止 CDN):${ref.value}`);
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const target = path.resolve(pagesDir, ref.value.split(/[?#]/)[0]);
|
|
308
|
+
if (!fs.existsSync(target)) {
|
|
309
|
+
if (ref.kind === 'img') missingImg++;
|
|
310
|
+
else errors.push(`pages/${f} 引用的 ${ref.kind} 不存在:${ref.value}`);
|
|
311
|
+
} else if (/\.css$/i.test(target)) {
|
|
312
|
+
const css = fs.readFileSync(target, 'utf8');
|
|
313
|
+
for (const u of cssRefs(css)) {
|
|
314
|
+
const t2 = path.resolve(path.dirname(target), u.split(/[?#]/)[0]);
|
|
315
|
+
if (!fs.existsSync(t2)) warnings.push(`${path.relative(root, target)} 里引用的 ${u} 不存在`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (missingImg) warnings.push(`${missingImg} 处图片引用指向不存在的文件(原站就是坏图,保留原样以便自比)`);
|
|
321
|
+
if (!refCount) warnings.push('页面里没有资源引用(样式全内联)');
|
|
322
|
+
add('links', '页面资源引用', errors, warnings);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// ---- 9. design ----
|
|
326
|
+
{
|
|
327
|
+
const errors = [];
|
|
328
|
+
const warnings = [];
|
|
329
|
+
const designDir = path.join(root, 'design');
|
|
330
|
+
if (!requireDesign) {
|
|
331
|
+
warnings.push('已按 --no-design 跳过');
|
|
332
|
+
} else {
|
|
333
|
+
for (const vp of VIEWPORTS) {
|
|
334
|
+
const file = path.join(designDir, `${vp.name}.png`);
|
|
335
|
+
if (!fs.existsSync(file)) {
|
|
336
|
+
errors.push(`缺少 design/${vp.name}.png`);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const size = readPngSize(fs.readFileSync(file));
|
|
340
|
+
if (!size) errors.push(`design/${vp.name}.png 不是有效 PNG`);
|
|
341
|
+
else if (size.width !== vp.width || size.height !== vp.height) errors.push(`design/${vp.name}.png 尺寸 ${size.width}×${size.height},应为 ${vp.width}×${vp.height}`);
|
|
342
|
+
for (const suffix of ['-t0', '-t1500']) {
|
|
343
|
+
if (!fs.existsSync(path.join(designDir, `${vp.name}${suffix}.png`))) warnings.push(`缺动画帧 design/${vp.name}${suffix}.png(verify 的动画清单会缺这一组)`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
add('design', 'design 设计稿截图', errors, warnings);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const failed = checks.filter((c) => !c.ok).map((c) => c.id);
|
|
351
|
+
return { ok: failed.length === 0, dir: root, checks, summary: { failed } };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** 校验不过就抛(`validate` 与 `pack` 共用):exit 1 */
|
|
355
|
+
export function assertValid(result, { action = 'validate' } = {}) {
|
|
356
|
+
if (result.ok) return;
|
|
357
|
+
const bad = result.checks.filter((c) => !c.ok);
|
|
358
|
+
const lines = bad.map((c) => ` ✗ [${c.id}] ${c.title}\n${c.errors.map((e) => ` - ${e}`).join('\n')}`).join('\n');
|
|
359
|
+
throw new CliError(`模板校验不通过(${bad.length}/9 关失败):${result.dir}\n${lines}`, {
|
|
360
|
+
kind: 'validate',
|
|
361
|
+
code: 'EVALIDATE',
|
|
362
|
+
exitCode: EXIT.ERROR,
|
|
363
|
+
raw: { failed: result.summary.failed },
|
|
364
|
+
hint: action === 'pack' ? '修完再 pack(pack 不会打包不合格的模板)' : '按上面每条修,validate 必须 exit 0',
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export { VIEWPORTS };
|