@wkgeo/cli 2.0.3 → 2.2.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 +19 -2
- package/README.md +7 -7
- package/SKILL.md +1038 -1038
- 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 +17 -1
- package/lib/commands/init.mjs +111 -111
- package/lib/commands/orders.mjs +691 -691
- package/lib/commands/site.mjs +82 -0
- package/lib/commands/siteops.mjs +382 -0
- package/lib/commands/sites.mjs +968 -801
- package/lib/commands/spec.mjs +72 -72
- 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 -1650
- package/lib/manifest-schema.json +480 -480
- package/lib/manifest.mjs +1106 -1106
- package/lib/runtime.mjs +798 -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-publish.mjs +410 -0
- package/lib/site-spec.mjs +302 -302
- package/lib/site-template.mjs +405 -0
- package/lib/template-gen.mjs +1354 -1354
- 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 -56
- package/spec/SITE-SPEC.md +198 -198
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `site publish` 的**打包 + 上传**实现(task-68,2026-11-22)。
|
|
3
|
+
*
|
|
4
|
+
* 老板口径:「待发布」是一个**动作** —— 把本地 `<站点目录>\html\` 里 build 出来的整站
|
|
5
|
+
* 上传到平台 OSS(站点有自有存储时服务端再复制过去)。
|
|
6
|
+
*
|
|
7
|
+
* 链路:本地 `html/**` → 零依赖 tar.gz → `POST /site/build-upload?siteId=…&mode=plan|apply&prune=0|1`
|
|
8
|
+
* · 不带 `--confirm` ⇒ `mode=plan`(服务端**零写**,只回 missing/changed/deleted);
|
|
9
|
+
* · 带 `--confirm` ⇒ `mode=apply`(服务端只 PUT 缺失/变更,`--prune` 才删前缀内孤儿)。
|
|
10
|
+
*
|
|
11
|
+
* 约定:
|
|
12
|
+
* · tar 路径 = **站内相对路径**(`index.html` / `syhz/index.html` / `upload/a.png`),
|
|
13
|
+
* 由服务端拼成 `<siteId>/<rel>`,所以这里**不带**站点目录名、不带 `./`;
|
|
14
|
+
* · 文件系统垃圾(`.DS_Store` / `__MACOSX/**` / `._*`)静默跳过(服务端同样会跳过,双保险);
|
|
15
|
+
* · 绝不打印 / 打印到日志:`access_token`(站点票/账号 Key)。只报 keyPrefix。
|
|
16
|
+
*/
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import zlib from 'node:zlib';
|
|
20
|
+
|
|
21
|
+
import { CliError, EXIT, networkError } from './errors.mjs';
|
|
22
|
+
import { listSiteCredentials, sitePathKey, isSiteCredentialExpired } from './site-credentials.mjs';
|
|
23
|
+
import { resolveSiteTarget } from './build.mjs';
|
|
24
|
+
|
|
25
|
+
/** 与上传接口契约一致的上限(服务端同样校验;这里先拦一次,给出"该删什么"的提示) */
|
|
26
|
+
export const PUBLISH_LIMITS = Object.freeze({
|
|
27
|
+
fileBytes: 20 * 1024 * 1024,
|
|
28
|
+
totalBytes: 100 * 1024 * 1024,
|
|
29
|
+
entries: 5000,
|
|
30
|
+
/** 网关默认请求体闸门(GW_MAX_BODY_BYTES 默认 8 MiB)——只用于提醒,不做拒绝 */
|
|
31
|
+
gatewayBytes: 8 * 1024 * 1024,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const SKIP_FILES = new Set(['.DS_Store', 'Thumbs.db', 'desktop.ini']);
|
|
35
|
+
|
|
36
|
+
function isSkippedRel(rel) {
|
|
37
|
+
const base = rel.slice(rel.lastIndexOf('/') + 1);
|
|
38
|
+
if (SKIP_FILES.has(base)) return true;
|
|
39
|
+
if (base.startsWith('._')) return true;
|
|
40
|
+
if (rel === '__MACOSX' || rel.startsWith('__MACOSX/')) return true;
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 单段路径白名单(与服务端 `src/build-upload-archive.js` 的 `SAFE_SEGMENT_RE` 同口径) */
|
|
45
|
+
const SAFE_SEGMENT_RE = /^[\p{L}\p{N}\p{M}._~+@()()\-]+$/u;
|
|
46
|
+
|
|
47
|
+
function assertSafeRel(rel) {
|
|
48
|
+
for (const part of rel.split('/')) {
|
|
49
|
+
if (!part || part === '.' || part === '..') {
|
|
50
|
+
throw new CliError(`本地文件名不合法:${rel}`, {
|
|
51
|
+
kind: 'build_bad_path',
|
|
52
|
+
code: 'BAD_PATH',
|
|
53
|
+
hint: '页面/资产文件名里不要有 ".." 或空目录名,重命名后再发布',
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
if (!SAFE_SEGMENT_RE.test(part)) {
|
|
57
|
+
throw new CliError(`本地文件名含不允许的字符:${rel}`, {
|
|
58
|
+
kind: 'build_bad_path',
|
|
59
|
+
code: 'BAD_PATH',
|
|
60
|
+
hint: '文件名只用字母 / 数字 / 中文 / . _ - ~ + @ ( )(),不要用空格、%、#、?、& 等字符',
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 递归列 `html/` 下的文件(返回 `{rel, file, size}`,rel 一律 `/` 分隔) */
|
|
67
|
+
export function listHtmlFiles(htmlDir) {
|
|
68
|
+
const root = path.resolve(htmlDir);
|
|
69
|
+
const out = [];
|
|
70
|
+
const walk = (dirAbs, relPrefix) => {
|
|
71
|
+
const names = fs.readdirSync(dirAbs, { withFileTypes: true });
|
|
72
|
+
names.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
73
|
+
for (const one of names) {
|
|
74
|
+
const abs = path.join(dirAbs, one.name);
|
|
75
|
+
const rel = relPrefix ? `${relPrefix}/${one.name}` : one.name;
|
|
76
|
+
if (one.isDirectory()) {
|
|
77
|
+
if (one.name === '__MACOSX') continue;
|
|
78
|
+
walk(abs, rel);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (!one.isFile()) continue; // 符号链接 / 设备文件一律不要
|
|
82
|
+
if (isSkippedRel(rel)) continue;
|
|
83
|
+
const stat = fs.statSync(abs);
|
|
84
|
+
out.push({ rel, file: abs, size: stat.size });
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
walk(root, '');
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 零依赖 tar writer(ustar):只用得到普通文件 + 目录层级;不写 uid/gid/注释。
|
|
93
|
+
* 头 512 字节 + 数据按 512 对齐 + 结尾两个全 0 块。
|
|
94
|
+
*/
|
|
95
|
+
function writeOctal(buf, offset, length, value) {
|
|
96
|
+
const text = Math.max(0, Math.floor(Number(value) || 0)).toString(8);
|
|
97
|
+
const padded = text.padStart(length - 1, '0').slice(-(length - 1));
|
|
98
|
+
buf.write(padded, offset, length - 1, 'ascii');
|
|
99
|
+
buf[offset + length - 1] = 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function tarHeaderOf(rel, size, mtimeSec) {
|
|
103
|
+
const header = Buffer.alloc(512);
|
|
104
|
+
/** name 字段 100 字节;长路径拆 prefix(155) + name(100)(在 `/` 处切) */
|
|
105
|
+
let namePart = rel;
|
|
106
|
+
let prefixPart = '';
|
|
107
|
+
if (Buffer.byteLength(rel) > 100) {
|
|
108
|
+
let cut = -1;
|
|
109
|
+
const slashPositions = [];
|
|
110
|
+
for (let i = 0; i < rel.length; i += 1) if (rel[i] === '/') slashPositions.push(i);
|
|
111
|
+
for (const pos of slashPositions) {
|
|
112
|
+
const head = rel.slice(0, pos);
|
|
113
|
+
const tail = rel.slice(pos + 1);
|
|
114
|
+
if (Buffer.byteLength(tail) <= 100 && Buffer.byteLength(head) <= 155) {
|
|
115
|
+
cut = pos;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (cut === -1) {
|
|
120
|
+
throw new CliError(`文件路径太长,无法打包:${rel}`, {
|
|
121
|
+
kind: 'build_bad_path',
|
|
122
|
+
code: 'PATH_TOO_LONG',
|
|
123
|
+
hint: '把目录层级缩短,或给文件换个短一点的名字',
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
prefixPart = rel.slice(0, cut);
|
|
127
|
+
namePart = rel.slice(cut + 1);
|
|
128
|
+
}
|
|
129
|
+
header.write(namePart, 0, 100, 'utf8');
|
|
130
|
+
writeOctal(header, 100, 8, 0o644); // mode
|
|
131
|
+
writeOctal(header, 108, 8, 0); // uid
|
|
132
|
+
writeOctal(header, 116, 8, 0); // gid
|
|
133
|
+
writeOctal(header, 124, 12, size); // size
|
|
134
|
+
writeOctal(header, 136, 12, mtimeSec); // mtime
|
|
135
|
+
header.fill(0x20, 148, 156); // chksum 先填空格
|
|
136
|
+
header.write('0', 156, 1, 'ascii'); // typeflag = 普通文件
|
|
137
|
+
header.write('ustar\0', 257, 6, 'ascii');
|
|
138
|
+
header.write('00', 263, 2, 'ascii');
|
|
139
|
+
if (prefixPart) header.write(prefixPart, 345, 155, 'utf8');
|
|
140
|
+
let sum = 0;
|
|
141
|
+
for (let i = 0; i < 512; i += 1) sum += header[i];
|
|
142
|
+
header.write(sum.toString(8).padStart(6, '0'), 148, 6, 'ascii');
|
|
143
|
+
header[154] = 0;
|
|
144
|
+
header[155] = 0x20;
|
|
145
|
+
return header;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 把 `html/` 打成 tar.gz。
|
|
150
|
+
* @returns {{ok:true, buffer:Buffer, files:number, rawBytes:number, gzipBytes:number, entries:Array<{rel,size}>}}
|
|
151
|
+
*/
|
|
152
|
+
export function packHtmlDirectory(htmlDir, { limits = PUBLISH_LIMITS } = {}) {
|
|
153
|
+
const root = path.resolve(htmlDir);
|
|
154
|
+
if (!fs.existsSync(root) || !fs.statSync(root).isDirectory()) {
|
|
155
|
+
throw new CliError(`没有找到 html 目录:${root}`, {
|
|
156
|
+
kind: 'build_missing',
|
|
157
|
+
code: 'BUILD_MISSING',
|
|
158
|
+
exitCode: EXIT.USAGE,
|
|
159
|
+
hint: '先在这个站点目录里执行 `wkgeo site build`,生成 html/ 后再发布',
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
const files = listHtmlFiles(root);
|
|
163
|
+
if (!files.length) {
|
|
164
|
+
throw new CliError(`html 目录里没有可发布的文件:${root}`, {
|
|
165
|
+
kind: 'build_missing',
|
|
166
|
+
code: 'BUILD_MISSING',
|
|
167
|
+
exitCode: EXIT.USAGE,
|
|
168
|
+
hint: '先执行 `wkgeo site build`;如果刚 build 过,检查产物是不是在别的目录(--out)',
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
const mtimeSec = Math.floor(Date.now() / 1000);
|
|
172
|
+
const chunks = [];
|
|
173
|
+
let rawBytes = 0;
|
|
174
|
+
for (const one of files) {
|
|
175
|
+
assertSafeRel(one.rel);
|
|
176
|
+
if (one.size > limits.fileBytes) {
|
|
177
|
+
throw new CliError(`单个文件超过上限(${limits.fileBytes} 字节):${one.rel}`, {
|
|
178
|
+
kind: 'build_too_large',
|
|
179
|
+
code: 'FILE_TOO_LARGE',
|
|
180
|
+
hint: '压缩/换掉这个大文件(视频请走素材库或外部链接),再发布',
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
rawBytes += one.size;
|
|
184
|
+
if (rawBytes > limits.totalBytes) {
|
|
185
|
+
throw new CliError(`产物总大小超过上限(${limits.totalBytes} 字节)`, {
|
|
186
|
+
kind: 'build_too_large',
|
|
187
|
+
code: 'ARCHIVE_TOO_LARGE',
|
|
188
|
+
hint: '删掉 html/ 里不需要上传的大文件(临时视频、备份包等)后再发布',
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (files.length > limits.entries) {
|
|
192
|
+
throw new CliError(`产物文件数超过上限(${limits.entries} 个)`, {
|
|
193
|
+
kind: 'build_too_large',
|
|
194
|
+
code: 'TOO_MANY_ENTRIES',
|
|
195
|
+
hint: '检查 html/ 里有没有误生成的临时目录',
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
const data = fs.readFileSync(one.file);
|
|
199
|
+
const header = tarHeaderOf(one.rel, data.length, mtimeSec);
|
|
200
|
+
chunks.push(header, data);
|
|
201
|
+
const pad = (512 - (data.length % 512)) % 512;
|
|
202
|
+
if (pad) chunks.push(Buffer.alloc(pad));
|
|
203
|
+
}
|
|
204
|
+
chunks.push(Buffer.alloc(1024)); // 结尾两个全 0 块
|
|
205
|
+
const tarBuf = Buffer.concat(chunks);
|
|
206
|
+
let gzipBuf;
|
|
207
|
+
try {
|
|
208
|
+
gzipBuf = zlib.gzipSync(tarBuf, { level: 9 });
|
|
209
|
+
} catch (error) {
|
|
210
|
+
throw new CliError(`打包失败:${String((error && error.message) || error)}`, { kind: 'build', code: 'PACK_FAILED' });
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
ok: true,
|
|
214
|
+
buffer: gzipBuf,
|
|
215
|
+
files: files.length,
|
|
216
|
+
rawBytes,
|
|
217
|
+
gzipBytes: gzipBuf.length,
|
|
218
|
+
entries: files.map((one) => ({ rel: one.rel, size: one.size })),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Windows 下路径大小写不敏感:比较前统一小写 + 去掉末尾斜杠 */
|
|
223
|
+
function dirKeyOf(dir) {
|
|
224
|
+
return sitePathKey(path.resolve(String(dir || ''))).replace(/[\\/]+$/, '').toLowerCase();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 选这次上传用哪张凭据:
|
|
229
|
+
* ① `--dir` 指向的站点目录命中了本地站点票(`%LOCALAPPDATA%\wkgeo-ops\creds\*.json` 里的 site.dir)
|
|
230
|
+
* ⇒ 用站点票(最常用:老板在站点目录里 publish,不需要 OAuth 登录);
|
|
231
|
+
* ② `--site` 是 uuid 且命中某张票的 siteId/cmsId ⇒ 用那张票;
|
|
232
|
+
* ③ 都没命中 ⇒ 交给 `ctx.cmsAuth()` 的账号 Key(服务端按"本人名下站"校验)。
|
|
233
|
+
*/
|
|
234
|
+
export function pickSiteCredential({ dir = '', siteRef = '', env = process.env, now = Date.now() } = {}) {
|
|
235
|
+
let creds = [];
|
|
236
|
+
try {
|
|
237
|
+
creds = listSiteCredentials({ env, now }) || [];
|
|
238
|
+
} catch {
|
|
239
|
+
creds = [];
|
|
240
|
+
}
|
|
241
|
+
creds = creds.filter((one) => one && !isSiteCredentialExpired(one, { now }));
|
|
242
|
+
const wantDir = dir ? dirKeyOf(dir) : '';
|
|
243
|
+
if (wantDir) {
|
|
244
|
+
const byDir = creds.filter((one) => one.siteDir && dirKeyOf(one.siteDir) === wantDir);
|
|
245
|
+
if (byDir.length) {
|
|
246
|
+
/** 同目录多张票(续期残留):取最晚过期的那张 */
|
|
247
|
+
byDir.sort((a, b) => Number(b.expiresMs || 0) - Number(a.expiresMs || 0));
|
|
248
|
+
return { credential: byDir[0], source: 'dir' };
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
const ref = String(siteRef || '').trim();
|
|
252
|
+
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(ref)) {
|
|
253
|
+
const byId = creds.filter((one) => one.siteId === ref || one.cmsId === ref);
|
|
254
|
+
if (byId.length) {
|
|
255
|
+
byId.sort((a, b) => Number(b.expiresMs || 0) - Number(a.expiresMs || 0));
|
|
256
|
+
return { credential: byId[0], source: 'site-id' };
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return { credential: null, source: '' };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* 上传一次(plan / apply 同一条链路,只有 query 不同)。
|
|
264
|
+
*
|
|
265
|
+
* @param {object} p
|
|
266
|
+
* @param {Buffer} p.buffer 已打包的 tar.gz
|
|
267
|
+
* @param {string} p.siteId 平台站 id(query 用;服务端也认 cmsId)
|
|
268
|
+
* @param {string} p.base 平台 API 基址(`config.base`,默认 https://api.wkgeo.cn)
|
|
269
|
+
* @param {string} p.token Bearer 凭据(站点票 / 账号 Key);**不打印**
|
|
270
|
+
* @param {'plan'|'apply'} p.mode
|
|
271
|
+
* @param {boolean} p.prune
|
|
272
|
+
* @param {Function} p.fetchImpl
|
|
273
|
+
* @param {string} [p.traceId]
|
|
274
|
+
*/
|
|
275
|
+
export async function postBuildUpload({ buffer, siteId, base, token, mode = 'plan', prune = false, fetchImpl }) {
|
|
276
|
+
const qs = new URLSearchParams({ siteId: String(siteId), mode, prune: prune ? '1' : '0' });
|
|
277
|
+
const url = `${String(base || 'https://api.wkgeo.cn').replace(/\/+$/, '')}/site/build-upload?${qs.toString()}`;
|
|
278
|
+
const headers = {
|
|
279
|
+
authorization: `Bearer ${token}`,
|
|
280
|
+
'content-type': 'application/gzip',
|
|
281
|
+
accept: 'application/json',
|
|
282
|
+
};
|
|
283
|
+
let res;
|
|
284
|
+
try {
|
|
285
|
+
const signal =
|
|
286
|
+
typeof AbortSignal !== 'undefined' && typeof AbortSignal.timeout === 'function'
|
|
287
|
+
? AbortSignal.timeout(180000)
|
|
288
|
+
: undefined;
|
|
289
|
+
res = await fetchImpl(url, { method: 'POST', headers, body: buffer, ...(signal ? { signal } : {}) });
|
|
290
|
+
} catch (error) {
|
|
291
|
+
throw networkError(String((error && error.message) || error), { cause: error });
|
|
292
|
+
}
|
|
293
|
+
const status = Number(res && res.status) || 0;
|
|
294
|
+
let text = '';
|
|
295
|
+
try {
|
|
296
|
+
text = await res.text();
|
|
297
|
+
} catch {
|
|
298
|
+
text = '';
|
|
299
|
+
}
|
|
300
|
+
let body = null;
|
|
301
|
+
try {
|
|
302
|
+
body = text ? JSON.parse(text) : null;
|
|
303
|
+
} catch {
|
|
304
|
+
body = null;
|
|
305
|
+
}
|
|
306
|
+
if (!body || typeof body !== 'object') {
|
|
307
|
+
throw new CliError(`上传接口返回了非 JSON(HTTP ${status})`, {
|
|
308
|
+
kind: 'server',
|
|
309
|
+
code: status,
|
|
310
|
+
raw: String(text || '').slice(0, 500),
|
|
311
|
+
hint: '确认网关已下发 /site/build-upload 路由,或稍后重试',
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
if (status < 200 || status >= 300 || body.ok !== true) {
|
|
315
|
+
const message = String(body.message || body.error || `上传接口返回 HTTP ${status}`);
|
|
316
|
+
const code = body.code == null ? status : body.code;
|
|
317
|
+
/** 401 ⇒ 凭据问题(票过期 / Key 错);其余按一般失败,把服务端 code 留着好排查 */
|
|
318
|
+
const authLike = status === 401 || /^KEY_(INVALID|REVOKED|EXPIRED)$/.test(String(code));
|
|
319
|
+
throw new CliError(message, {
|
|
320
|
+
kind: authLike ? 'auth' : 'server',
|
|
321
|
+
code,
|
|
322
|
+
exitCode: authLike ? EXIT.AUTH : EXIT.ERROR,
|
|
323
|
+
raw: body,
|
|
324
|
+
details: body.details || null,
|
|
325
|
+
hint: authLike
|
|
326
|
+
? '站点票可能过期:在站点目录里重新执行 `wkgeo site credential`(或控制台重新生成站点票)后再发布'
|
|
327
|
+
: status === 413
|
|
328
|
+
? '包太大:删掉 html/ 里不需要上传的大文件;网关还有 8 MiB 请求体闸门,超过要调 GW_MAX_BODY_BYTES'
|
|
329
|
+
: status === 403
|
|
330
|
+
? '这个凭据没有该站点的写权限:确认 --site 是票绑定的那个站,或换账号 Key'
|
|
331
|
+
: '把 --json 的输出贴给平台排查',
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
return { status, body };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 命令主流程:打包 → 解析站点 → plan/apply。
|
|
339
|
+
* 返回服务端 body(已经在内部通过 `ctx.done` 之外的打印由命令负责)。
|
|
340
|
+
*/
|
|
341
|
+
export async function publishLocalBuild({ ctx, dir, site, prune = false, confirm = false }) {
|
|
342
|
+
const htmlDir = path.join(path.resolve(dir), 'html');
|
|
343
|
+
const pack = packHtmlDirectory(htmlDir);
|
|
344
|
+
const credPick = pickSiteCredential({ dir, siteRef: site, env: ctx.env || process.env });
|
|
345
|
+
const credential = credPick.credential;
|
|
346
|
+
const token = credential ? credential.accessToken : null;
|
|
347
|
+
|
|
348
|
+
/** 站点 id:有票就用票里的(服务端会再校验票 ↔ 站),否则按 `--site` 解析 */
|
|
349
|
+
let siteId = '';
|
|
350
|
+
let siteLabel = '';
|
|
351
|
+
if (credential) {
|
|
352
|
+
siteId = String(credential.siteId || credential.cmsId || '');
|
|
353
|
+
siteLabel = String(credential.cmsId || credential.siteId || '');
|
|
354
|
+
} else {
|
|
355
|
+
const credentials = ctx.cmsAuth().creds;
|
|
356
|
+
const resolved = await resolveSiteTarget({
|
|
357
|
+
site: site || '',
|
|
358
|
+
cmsSite: ctx.flags && ctx.flags.cmsSite ? String(ctx.flags.cmsSite) : '',
|
|
359
|
+
config: ctx.config,
|
|
360
|
+
credentials,
|
|
361
|
+
fetchImpl: ctx.fetchImpl,
|
|
362
|
+
});
|
|
363
|
+
siteId = String(resolved.id || '');
|
|
364
|
+
siteLabel = String(resolved.label || '');
|
|
365
|
+
}
|
|
366
|
+
if (!siteId) {
|
|
367
|
+
throw new CliError('没能确定要发布到哪个站点', {
|
|
368
|
+
kind: 'usage',
|
|
369
|
+
code: 'SITE_REQUIRED',
|
|
370
|
+
exitCode: EXIT.USAGE,
|
|
371
|
+
hint: '用 `--site <域名|站点uuid>` 指定站点,或在站点目录里用站点票发布',
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
let bearer = token;
|
|
376
|
+
if (!bearer) {
|
|
377
|
+
const auth = ctx.cmsAuth();
|
|
378
|
+
bearer = auth && auth.creds && auth.creds.access_token ? auth.creds.access_token : '';
|
|
379
|
+
}
|
|
380
|
+
if (!bearer) {
|
|
381
|
+
throw new CliError('没有可用的凭据', {
|
|
382
|
+
kind: 'auth',
|
|
383
|
+
code: 'NO_CREDENTIAL',
|
|
384
|
+
exitCode: EXIT.AUTH,
|
|
385
|
+
hint: '在站点目录里生成站点票,或先 `wkgeo login` 用账号 Key 发布',
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const mode = confirm ? 'apply' : 'plan';
|
|
390
|
+
const { status, body } = await postBuildUpload({
|
|
391
|
+
buffer: pack.buffer,
|
|
392
|
+
siteId,
|
|
393
|
+
base: ctx.config.base,
|
|
394
|
+
token: bearer,
|
|
395
|
+
mode,
|
|
396
|
+
prune,
|
|
397
|
+
fetchImpl: ctx.fetchImpl,
|
|
398
|
+
});
|
|
399
|
+
return {
|
|
400
|
+
status,
|
|
401
|
+
body,
|
|
402
|
+
pack,
|
|
403
|
+
siteId,
|
|
404
|
+
siteLabel,
|
|
405
|
+
credentialSource: credPick.source,
|
|
406
|
+
keyPrefix: credential ? credential.keyPrefix || '' : '',
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export default { packHtmlDirectory, postBuildUpload, publishLocalBuild, pickSiteCredential, listHtmlFiles, PUBLISH_LIMITS };
|