vcs-patch-deploy 1.0.1 → 1.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 +20 -0
- package/README.md +62 -4
- package/assets/php/apply-update.php +255 -4
- package/bin/patch-build.js +21 -1
- package/bin/patch-extract.js +127 -0
- package/env.example +15 -0
- package/lib/build-patch.js +135 -51
- package/lib/cli-args.js +63 -0
- package/lib/config.js +8 -1
- package/lib/extract-patch.js +191 -0
- package/lib/file-filter.js +147 -0
- package/lib/git.js +40 -16
- package/lib/index.js +11 -0
- package/lib/manifest.js +47 -0
- package/lib/version-registry.js +137 -0
- package/lib/zip.js +4 -1
- package/package.json +4 -3
package/env.example
CHANGED
|
@@ -12,9 +12,24 @@ PATCH_BACKEND=php
|
|
|
12
12
|
# deploy 配置目录(含 .last-deploy-marker、out/)
|
|
13
13
|
# PATCH_DEPLOY_DIR=/path/to/your-php-api/deploy
|
|
14
14
|
|
|
15
|
+
# 服务器 apply-update.php:备份根目录(默认 <Backend>/.deploy-backups/)
|
|
16
|
+
# PATCH_BACKUP_DIR=/data/backups/my-api
|
|
17
|
+
|
|
15
18
|
# zip 文件名前缀
|
|
16
19
|
# PATCH_ZIP_PREFIX=backend-update
|
|
17
20
|
|
|
21
|
+
# zip 压缩级别 0-9(默认 6,原 9 较慢)
|
|
22
|
+
# PATCH_ZIP_LEVEL=6
|
|
23
|
+
|
|
24
|
+
# 安装版本号(patch-build --version= 的默认值)
|
|
25
|
+
# PATCH_VERSION=1.2.3
|
|
26
|
+
|
|
27
|
+
# 仅打包/部署的文件列表(每行一个相对路径)
|
|
28
|
+
# PATCH_FILE_LIST=hotfix-files.txt
|
|
29
|
+
|
|
30
|
+
# 服务器 apply-update.php:仅部署指定文件(逗号分隔)
|
|
31
|
+
# PATCH_DEPLOY_FILES=app/service/Foo.php,app/controller/Bar.php
|
|
32
|
+
|
|
18
33
|
# 跳过 PHP 项目结构校验(非标准目录时使用)
|
|
19
34
|
# PATCH_SKIP_VALIDATION=false
|
|
20
35
|
|
package/lib/build-patch.js
CHANGED
|
@@ -9,6 +9,9 @@ const fse = require('fs-extra')
|
|
|
9
9
|
const { shouldExcludePath } = require('./exclude')
|
|
10
10
|
const { getBackend } = require('./backends')
|
|
11
11
|
const { readDeployMarker, writeDeployMarker } = require('./marker')
|
|
12
|
+
const { registerVersion } = require('./version-registry')
|
|
13
|
+
const { sanitizeVersionLabel } = require('./file-filter')
|
|
14
|
+
const { resolveSelectedFiles, filterFilesBySelection } = require('./file-filter')
|
|
12
15
|
const { getSvnInfo, getSvnDiffSummary } = require('./svn')
|
|
13
16
|
const { getGitInfo, getGitDiffSummary, resolveGitRef } = require('./git')
|
|
14
17
|
const { createZipFromDirectory } = require('./zip')
|
|
@@ -44,6 +47,7 @@ function classifyDiffEntries (entries, sourceDir, backendId = 'php') {
|
|
|
44
47
|
for (const entry of entries) {
|
|
45
48
|
if (entry.parseError) {
|
|
46
49
|
warnings.push(entry.parseError)
|
|
50
|
+
continue
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
const rel = entry.relativePath.replace(/\\/g, '/')
|
|
@@ -81,8 +85,18 @@ function classifyDiffEntries (entries, sourceDir, backendId = 'php') {
|
|
|
81
85
|
* 生成 patch 目录名与 zip 名后缀
|
|
82
86
|
* @param {'svn'|'git'} vcs
|
|
83
87
|
* @param {string} toLabel
|
|
88
|
+
* @param {string} [version]
|
|
84
89
|
*/
|
|
85
|
-
function buildArtifactNames (vcs, toLabel, zipPrefix) {
|
|
90
|
+
function buildArtifactNames (vcs, toLabel, zipPrefix, version = null) {
|
|
91
|
+
const versionLabel = version ? sanitizeVersionLabel(version) : null
|
|
92
|
+
|
|
93
|
+
if (versionLabel) {
|
|
94
|
+
return {
|
|
95
|
+
patchName: `patch-v${versionLabel}`,
|
|
96
|
+
zipName: `${zipPrefix}-v${versionLabel}.zip`
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
86
100
|
if (vcs === 'svn') {
|
|
87
101
|
return {
|
|
88
102
|
patchName: `patch-r${toLabel}`,
|
|
@@ -112,7 +126,10 @@ async function buildPatch (config) {
|
|
|
112
126
|
noZip,
|
|
113
127
|
markDeployed,
|
|
114
128
|
skipValidation,
|
|
115
|
-
zipPrefix
|
|
129
|
+
zipPrefix,
|
|
130
|
+
version,
|
|
131
|
+
onlyFiles,
|
|
132
|
+
fileList
|
|
116
133
|
} = config
|
|
117
134
|
|
|
118
135
|
const backendModule = getBackend(backend)
|
|
@@ -152,6 +169,10 @@ async function buildPatch (config) {
|
|
|
152
169
|
}
|
|
153
170
|
}
|
|
154
171
|
|
|
172
|
+
if (!Number.isFinite(fromRev) || !Number.isFinite(toRev)) {
|
|
173
|
+
throw new Error(`无效的 SVN revision:from=${fromRev} to=${toRev},请检查 marker 或 --from/--to`)
|
|
174
|
+
}
|
|
175
|
+
|
|
155
176
|
if (fromRev >= toRev) {
|
|
156
177
|
if (markDeployed && fromRev === toRev) {
|
|
157
178
|
writeDeployMarker(deployDir, 'svn', String(toRev))
|
|
@@ -226,7 +247,25 @@ async function buildPatch (config) {
|
|
|
226
247
|
|
|
227
248
|
const { addedOrModified, deleted, warnings } = classifyDiffEntries(diffEntries, sourceDir, backend)
|
|
228
249
|
|
|
229
|
-
|
|
250
|
+
// 按 --only / --file-list 筛选本次打包文件(用于单独提取部署)
|
|
251
|
+
const selectedFiles = resolveSelectedFiles(onlyFiles || [], fileList || null)
|
|
252
|
+
let deployFiles = addedOrModified
|
|
253
|
+
let deployDeleted = deleted
|
|
254
|
+
if (selectedFiles.length > 0) {
|
|
255
|
+
const filePick = filterFilesBySelection(addedOrModified, selectedFiles)
|
|
256
|
+
if (filePick.missing.length > 0) {
|
|
257
|
+
warnings.push(`--only 中以下文件不在本次增量内: ${filePick.missing.join(', ')}`)
|
|
258
|
+
}
|
|
259
|
+
deployFiles = filePick.matched
|
|
260
|
+
|
|
261
|
+
const deletedPick = filterFilesBySelection(deleted, selectedFiles)
|
|
262
|
+
deployDeleted = deletedPick.matched
|
|
263
|
+
if (deletedPick.missing.length > 0) {
|
|
264
|
+
warnings.push(`--only 中以下删除项不在本次增量内: ${deletedPick.missing.join(', ')}`)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (deployFiles.length === 0 && deployDeleted.length === 0) {
|
|
230
269
|
return {
|
|
231
270
|
success: true,
|
|
232
271
|
skipped: true,
|
|
@@ -238,78 +277,123 @@ async function buildPatch (config) {
|
|
|
238
277
|
}
|
|
239
278
|
}
|
|
240
279
|
|
|
241
|
-
const { patchName, zipName } = buildArtifactNames(vcs, toLabel, zipPrefix)
|
|
280
|
+
const { patchName, zipName } = buildArtifactNames(vcs, toLabel, zipPrefix, version)
|
|
242
281
|
const patchDir = path.join(outDir, patchName)
|
|
243
282
|
const zipPath = path.join(outDir, zipName)
|
|
283
|
+
const stagingDir = `${patchDir}.staging-${Date.now()}`
|
|
284
|
+
const versionLabel = version ? sanitizeVersionLabel(version) : null
|
|
244
285
|
|
|
245
|
-
await fse.emptyDir(
|
|
286
|
+
await fse.emptyDir(stagingDir)
|
|
246
287
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
288
|
+
let zipSize = 0
|
|
289
|
+
try {
|
|
290
|
+
for (const rel of deployFiles) {
|
|
291
|
+
const src = path.join(sourceDir, rel)
|
|
292
|
+
const dst = path.join(stagingDir, rel)
|
|
293
|
+
await fse.ensureDir(path.dirname(dst))
|
|
294
|
+
await fse.copy(src, dst)
|
|
295
|
+
}
|
|
253
296
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
297
|
+
await fse.copy(applyScriptPath, path.join(stagingDir, 'apply-update.php'))
|
|
298
|
+
|
|
299
|
+
const manifestLines = [
|
|
300
|
+
'# PHP 后端增量部署清单',
|
|
301
|
+
`backend=${backend}`,
|
|
302
|
+
`vcs=${vcs}`,
|
|
303
|
+
...(versionLabel ? [`version=${versionLabel}`] : []),
|
|
304
|
+
vcs === 'svn'
|
|
305
|
+
? `from_revision=${fromLabel}`
|
|
306
|
+
: `from_commit=${fromLabel}`,
|
|
307
|
+
vcs === 'svn'
|
|
308
|
+
? `to_revision=${toLabel}`
|
|
309
|
+
: `to_commit=${toLabel}`,
|
|
310
|
+
`generated_at=${new Date().toISOString().replace('T', ' ').slice(0, 19)}`,
|
|
311
|
+
`generated_by=${os.userInfo().username}`,
|
|
312
|
+
'',
|
|
313
|
+
'# files',
|
|
314
|
+
...deployFiles,
|
|
315
|
+
'',
|
|
316
|
+
'# deleted (需人工确认是否删除线上文件)',
|
|
317
|
+
...(deployDeleted.length > 0 ? deployDeleted : ['# (none)'])
|
|
318
|
+
]
|
|
319
|
+
|
|
320
|
+
if (warnings.length > 0) {
|
|
321
|
+
manifestLines.push('', '# warnings', ...warnings)
|
|
322
|
+
}
|
|
279
323
|
|
|
280
|
-
|
|
324
|
+
fs.writeFileSync(path.join(stagingDir, 'MANIFEST.txt'), manifestLines.join('\n'), 'utf8')
|
|
325
|
+
|
|
326
|
+
if (versionLabel) {
|
|
327
|
+
fs.writeFileSync(
|
|
328
|
+
path.join(stagingDir, 'VERSION.txt'),
|
|
329
|
+
[
|
|
330
|
+
`version=${versionLabel}`,
|
|
331
|
+
`vcs=${vcs}`,
|
|
332
|
+
vcs === 'svn' ? `to_revision=${toLabel}` : `to_commit=${toLabel}`,
|
|
333
|
+
`file_count=${deployFiles.length}`
|
|
334
|
+
].join('\n'),
|
|
335
|
+
'utf8'
|
|
336
|
+
)
|
|
337
|
+
}
|
|
281
338
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
339
|
+
if (deployDeleted.length > 0) {
|
|
340
|
+
fs.writeFileSync(
|
|
341
|
+
path.join(stagingDir, 'MANIFEST-deleted.txt'),
|
|
342
|
+
deployDeleted.join('\n'),
|
|
343
|
+
'utf8'
|
|
344
|
+
)
|
|
345
|
+
}
|
|
289
346
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
347
|
+
// 构建成功后再替换目标 patch 目录,避免中途失败清空旧包
|
|
348
|
+
if (fs.existsSync(patchDir)) {
|
|
349
|
+
await fse.remove(patchDir)
|
|
350
|
+
}
|
|
351
|
+
await fse.move(stagingDir, patchDir)
|
|
352
|
+
|
|
353
|
+
if (!noZip) {
|
|
354
|
+
const zipResult = await createZipFromDirectory(patchDir, zipPath)
|
|
355
|
+
zipSize = zipResult.size
|
|
356
|
+
}
|
|
357
|
+
} catch (error) {
|
|
358
|
+
await fse.remove(stagingDir).catch(() => {})
|
|
359
|
+
throw error
|
|
294
360
|
}
|
|
295
361
|
|
|
296
362
|
if (markDeployed) {
|
|
297
363
|
writeDeployMarker(deployDir, vcs, toLabel)
|
|
298
364
|
}
|
|
299
365
|
|
|
366
|
+
// 注册版本号到 deploy/versions.json,便于后续 patch-extract 按版本提取
|
|
367
|
+
if (versionLabel) {
|
|
368
|
+
registerVersion(deployDir, {
|
|
369
|
+
version: String(version),
|
|
370
|
+
versionLabel,
|
|
371
|
+
vcs,
|
|
372
|
+
fromLabel,
|
|
373
|
+
toLabel,
|
|
374
|
+
files: deployFiles,
|
|
375
|
+
deleted: deployDeleted,
|
|
376
|
+
patchDir,
|
|
377
|
+
zipPath: noZip ? undefined : zipPath,
|
|
378
|
+
partial: selectedFiles.length > 0,
|
|
379
|
+
createdAt: new Date().toISOString()
|
|
380
|
+
})
|
|
381
|
+
}
|
|
382
|
+
|
|
300
383
|
return {
|
|
301
384
|
success: true,
|
|
302
385
|
skipped: false,
|
|
303
|
-
message: `打包完成:${vcs === 'svn' ? `r${fromLabel} -> r${toLabel}` : `${fromLabel.slice(0, 8)}..${toLabel.slice(0, 8)}`}`,
|
|
386
|
+
message: `打包完成:${versionLabel ? `v${versionLabel}` : (vcs === 'svn' ? `r${fromLabel} -> r${toLabel}` : `${fromLabel.slice(0, 8)}..${toLabel.slice(0, 8)}`)}`,
|
|
304
387
|
patchDir,
|
|
305
388
|
zipPath: noZip ? undefined : zipPath,
|
|
306
389
|
zipSize,
|
|
307
|
-
fileCount:
|
|
308
|
-
deletedCount:
|
|
390
|
+
fileCount: deployFiles.length,
|
|
391
|
+
deletedCount: deployDeleted.length,
|
|
309
392
|
warnings,
|
|
310
393
|
vcs,
|
|
311
394
|
fromLabel,
|
|
312
395
|
toLabel,
|
|
396
|
+
version: versionLabel || undefined,
|
|
313
397
|
markDeployedHint: markDeployed
|
|
314
398
|
? undefined
|
|
315
399
|
: `上线成功后执行:patch-build --vcs=${vcs} --mark-deployed --from=${toLabel} --to=${toLabel}`
|
package/lib/cli-args.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI 公共参数解析(patch-build / patch-extract 复用)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {object} CommonCliOptions
|
|
7
|
+
* @property {boolean} showHelp
|
|
8
|
+
* @property {boolean} [noZip]
|
|
9
|
+
* @property {string} [deployDir]
|
|
10
|
+
* @property {string[]} onlyFiles
|
|
11
|
+
* @property {string} [fileList]
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string[]} args process.argv.slice(2)
|
|
16
|
+
* @param {Record<string, string>} flagMap 布尔开关,如 { noZip: '--no-zip' }
|
|
17
|
+
* @param {Record<string, string>} valueMap 键值参数前缀,如 { deployDir: '--deploy=' }
|
|
18
|
+
* @returns {CommonCliOptions & Record<string, unknown>}
|
|
19
|
+
*/
|
|
20
|
+
function parseCommonArgs (args, flagMap = {}, valueMap = {}) {
|
|
21
|
+
/** @type {CommonCliOptions & Record<string, unknown>} */
|
|
22
|
+
const opts = { showHelp: false, onlyFiles: [] }
|
|
23
|
+
|
|
24
|
+
for (const arg of args) {
|
|
25
|
+
if (arg === '--help' || arg === '-h') {
|
|
26
|
+
opts.showHelp = true
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let handled = false
|
|
31
|
+
|
|
32
|
+
for (const [key, flag] of Object.entries(flagMap)) {
|
|
33
|
+
if (arg === flag) {
|
|
34
|
+
opts[key] = true
|
|
35
|
+
handled = true
|
|
36
|
+
break
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (handled) continue
|
|
40
|
+
|
|
41
|
+
for (const [key, prefix] of Object.entries(valueMap)) {
|
|
42
|
+
if (arg.startsWith(prefix)) {
|
|
43
|
+
opts[key] = arg.slice(prefix.length)
|
|
44
|
+
handled = true
|
|
45
|
+
break
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (handled) continue
|
|
49
|
+
|
|
50
|
+
if (arg.startsWith('--only=')) {
|
|
51
|
+
opts.onlyFiles.push(...arg.slice(7).split(','))
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
opts.unknownArg = arg
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return opts
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = {
|
|
62
|
+
parseCommonArgs
|
|
63
|
+
}
|
package/lib/config.js
CHANGED
|
@@ -65,6 +65,9 @@ function detectVcs (sourceDir) {
|
|
|
65
65
|
* @property {boolean} markDeployed
|
|
66
66
|
* @property {boolean} skipValidation
|
|
67
67
|
* @property {string} zipPrefix
|
|
68
|
+
* @property {string|null} version 安装版本号(如 1.2.3)
|
|
69
|
+
* @property {string[]} onlyFiles 仅打包/部署指定文件
|
|
70
|
+
* @property {string|null} fileList 文件列表路径(每行一个相对路径)
|
|
68
71
|
*/
|
|
69
72
|
|
|
70
73
|
/**
|
|
@@ -106,7 +109,10 @@ function resolveConfig (input = {}) {
|
|
|
106
109
|
noZip: Boolean(input.noZip),
|
|
107
110
|
markDeployed: Boolean(input.markDeployed),
|
|
108
111
|
skipValidation: Boolean(input.skipValidation || process.env.PATCH_SKIP_VALIDATION === 'true'),
|
|
109
|
-
zipPrefix: input.zipPrefix || process.env.PATCH_ZIP_PREFIX || backend.defaultZipPrefix
|
|
112
|
+
zipPrefix: input.zipPrefix || process.env.PATCH_ZIP_PREFIX || backend.defaultZipPrefix,
|
|
113
|
+
version: input.version || process.env.PATCH_VERSION || null,
|
|
114
|
+
onlyFiles: input.onlyFiles || [],
|
|
115
|
+
fileList: input.fileList || process.env.PATCH_FILE_LIST || null
|
|
110
116
|
}
|
|
111
117
|
}
|
|
112
118
|
|
|
@@ -131,6 +137,7 @@ function initDeployDir (deployDir, vcs = 'svn') {
|
|
|
131
137
|
## 基准文件
|
|
132
138
|
|
|
133
139
|
- \`.last-deploy-marker\`:上次成功上线标记,格式 \`svn:467\` 或 \`git:abc123...\`
|
|
140
|
+
- \`versions.json\`:版本号与 patch 目录映射(\`patch-build --version=1.2.3\`)
|
|
134
141
|
- 兼容旧版 SVN:\`.last-deploy-rev\`(仅数字)
|
|
135
142
|
|
|
136
143
|
## 本机打包
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 从完整版本包中提取指定文件,生成可单独部署的子集 patch
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
const path = require('path')
|
|
7
|
+
const fse = require('fs-extra')
|
|
8
|
+
const { getBackend } = require('./backends')
|
|
9
|
+
const { createZipFromDirectory } = require('./zip')
|
|
10
|
+
const { resolveSelectedFiles, filterFilesBySelection, sanitizeVersionLabel } = require('./file-filter')
|
|
11
|
+
const { findVersionRecord, registerVersion } = require('./version-registry')
|
|
12
|
+
|
|
13
|
+
const { readManifestFiles } = require('./manifest')
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 解析源 patch 目录(--from-patch 或 --version 查注册表)
|
|
17
|
+
* @param {object} options
|
|
18
|
+
* @param {string} options.deployDir
|
|
19
|
+
* @param {string} [options.fromPatch]
|
|
20
|
+
* @param {string} [options.version]
|
|
21
|
+
*/
|
|
22
|
+
function resolveSourcePatchDir ({ deployDir, fromPatch, version }) {
|
|
23
|
+
if (fromPatch) {
|
|
24
|
+
const abs = path.resolve(fromPatch)
|
|
25
|
+
if (!fs.existsSync(abs)) {
|
|
26
|
+
throw new Error(`源 patch 目录不存在: ${abs}`)
|
|
27
|
+
}
|
|
28
|
+
return abs
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!version) {
|
|
32
|
+
throw new Error('请指定 --version=<版本号> 或 --from-patch=<patch目录>')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const record = findVersionRecord(deployDir, version)
|
|
36
|
+
if (!record) {
|
|
37
|
+
throw new Error(`未找到版本 ${version},请先 patch-build --version=${version} 或检查 deploy/versions.json`)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!fs.existsSync(record.patchDir)) {
|
|
41
|
+
throw new Error(`版本 ${version} 的 patch 目录已不存在: ${record.patchDir}`)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return record.patchDir
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} ExtractResult
|
|
49
|
+
* @property {boolean} success
|
|
50
|
+
* @property {string} message
|
|
51
|
+
* @property {string} sourcePatchDir
|
|
52
|
+
* @property {string} patchDir
|
|
53
|
+
* @property {string} [zipPath]
|
|
54
|
+
* @property {number} [zipSize]
|
|
55
|
+
* @property {number} fileCount
|
|
56
|
+
* @property {string[]} files
|
|
57
|
+
* @property {string[]} missing
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {object} config
|
|
62
|
+
* @param {string} config.deployDir
|
|
63
|
+
* @param {string} config.outDir
|
|
64
|
+
* @param {string} config.applyScriptPath
|
|
65
|
+
* @param {string} [config.fromPatch]
|
|
66
|
+
* @param {string} [config.version]
|
|
67
|
+
* @param {string[]} [config.onlyFiles]
|
|
68
|
+
* @param {string|null} [config.fileList]
|
|
69
|
+
* @param {string} [config.subsetLabel] 子集包标识(默认 partial-<时间戳>)
|
|
70
|
+
* @param {boolean} [config.noZip]
|
|
71
|
+
* @param {string} [config.zipPrefix]
|
|
72
|
+
* @param {boolean} [config.registerSubset] 是否写入 versions.json
|
|
73
|
+
* @returns {Promise<ExtractResult>}
|
|
74
|
+
*/
|
|
75
|
+
async function extractPatchSubset (config) {
|
|
76
|
+
const {
|
|
77
|
+
deployDir,
|
|
78
|
+
outDir,
|
|
79
|
+
applyScriptPath,
|
|
80
|
+
fromPatch,
|
|
81
|
+
version,
|
|
82
|
+
onlyFiles = [],
|
|
83
|
+
fileList = null,
|
|
84
|
+
subsetLabel,
|
|
85
|
+
noZip = false,
|
|
86
|
+
zipPrefix = 'backend-update',
|
|
87
|
+
registerSubset = false
|
|
88
|
+
} = config
|
|
89
|
+
|
|
90
|
+
const sourcePatchDir = resolveSourcePatchDir({ deployDir, fromPatch, version })
|
|
91
|
+
const allFiles = readManifestFiles(sourcePatchDir)
|
|
92
|
+
const selected = resolveSelectedFiles(onlyFiles, fileList)
|
|
93
|
+
|
|
94
|
+
if (selected.length === 0) {
|
|
95
|
+
throw new Error('请指定 --only=<文件> 或 --file-list=<列表文件> 以提取子集')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const { matched, missing } = filterFilesBySelection(allFiles, selected)
|
|
99
|
+
if (missing.length > 0) {
|
|
100
|
+
throw new Error(`以下文件不在源 patch 中:\n - ${missing.join('\n - ')}`)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (matched.length === 0) {
|
|
104
|
+
throw new Error('筛选后无文件可提取')
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const label = subsetLabel || `partial-${new Date().toISOString().replace(/[-:T.Z]/g, '').slice(0, 14)}`
|
|
108
|
+
const safeLabel = sanitizeVersionLabel(label)
|
|
109
|
+
const patchName = version
|
|
110
|
+
? `patch-v${sanitizeVersionLabel(version)}-${safeLabel}`
|
|
111
|
+
: `patch-${safeLabel}`
|
|
112
|
+
const patchDir = path.join(outDir, patchName)
|
|
113
|
+
const zipPath = path.join(outDir, `${zipPrefix}-${safeLabel}.zip`)
|
|
114
|
+
|
|
115
|
+
await fse.emptyDir(patchDir)
|
|
116
|
+
|
|
117
|
+
for (const rel of matched) {
|
|
118
|
+
const src = path.join(sourcePatchDir, rel)
|
|
119
|
+
const dst = path.join(patchDir, rel)
|
|
120
|
+
if (!fs.existsSync(src)) {
|
|
121
|
+
throw new Error(`源 patch 缺少文件: ${rel}`)
|
|
122
|
+
}
|
|
123
|
+
await fse.ensureDir(path.dirname(dst))
|
|
124
|
+
await fse.copy(src, dst)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
await fse.copy(applyScriptPath, path.join(patchDir, 'apply-update.php'))
|
|
128
|
+
|
|
129
|
+
const versionTxt = version ? sanitizeVersionLabel(version) : 'unknown'
|
|
130
|
+
const manifestLines = [
|
|
131
|
+
'# PHP 后端增量部署清单(子集提取)',
|
|
132
|
+
`extract_mode=partial`,
|
|
133
|
+
version ? `source_version=${versionTxt}` : `source_patch=${sourcePatchDir}`,
|
|
134
|
+
`extract_label=${label}`,
|
|
135
|
+
`generated_at=${new Date().toISOString().replace('T', ' ').slice(0, 19)}`,
|
|
136
|
+
'',
|
|
137
|
+
'# files',
|
|
138
|
+
...matched
|
|
139
|
+
]
|
|
140
|
+
|
|
141
|
+
fs.writeFileSync(path.join(patchDir, 'MANIFEST.txt'), manifestLines.join('\n'), 'utf8')
|
|
142
|
+
fs.writeFileSync(
|
|
143
|
+
path.join(patchDir, 'VERSION.txt'),
|
|
144
|
+
[
|
|
145
|
+
version ? `version=${versionTxt}` : '',
|
|
146
|
+
`subset=${label}`,
|
|
147
|
+
`file_count=${matched.length}`
|
|
148
|
+
].filter(Boolean).join('\n'),
|
|
149
|
+
'utf8'
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
let zipSize = 0
|
|
153
|
+
if (!noZip) {
|
|
154
|
+
const zipResult = await createZipFromDirectory(patchDir, zipPath)
|
|
155
|
+
zipSize = zipResult.size
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (registerSubset && version) {
|
|
159
|
+
registerVersion(deployDir, {
|
|
160
|
+
version: `${version}-${label}`,
|
|
161
|
+
versionLabel: `${sanitizeVersionLabel(version)}-${safeLabel}`,
|
|
162
|
+
vcs: findVersionRecord(deployDir, version)?.vcs || 'svn',
|
|
163
|
+
fromLabel: findVersionRecord(deployDir, version)?.fromLabel || '',
|
|
164
|
+
toLabel: findVersionRecord(deployDir, version)?.toLabel || '',
|
|
165
|
+
files: matched,
|
|
166
|
+
deleted: [],
|
|
167
|
+
patchDir,
|
|
168
|
+
zipPath: noZip ? undefined : zipPath,
|
|
169
|
+
partial: true,
|
|
170
|
+
sourceVersion: version,
|
|
171
|
+
createdAt: new Date().toISOString()
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
success: true,
|
|
177
|
+
message: `已从 ${path.basename(sourcePatchDir)} 提取 ${matched.length} 个文件`,
|
|
178
|
+
sourcePatchDir,
|
|
179
|
+
patchDir,
|
|
180
|
+
zipPath: noZip ? undefined : zipPath,
|
|
181
|
+
zipSize,
|
|
182
|
+
fileCount: matched.length,
|
|
183
|
+
files: matched,
|
|
184
|
+
missing
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
module.exports = {
|
|
189
|
+
resolveSourcePatchDir,
|
|
190
|
+
extractPatchSubset
|
|
191
|
+
}
|