vcs-patch-deploy 1.0.1 → 1.1.1
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 +27 -0
- package/README.md +63 -5
- package/assets/php/apply-update.php +354 -13
- package/bin/patch-build.js +21 -1
- package/bin/patch-extract.js +127 -0
- package/env.example +15 -0
- package/lib/backends/php.js +16 -5
- 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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 部署文件列表解析与过滤(版本子集提取、按文件单独部署)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
const path = require('path')
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 规范化相对路径(统一 /,去掉 ./ 前缀)
|
|
10
|
+
* @param {string} relativePath
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
function normalizeRelativePath (relativePath) {
|
|
14
|
+
return String(relativePath).replace(/\\/g, '/').replace(/^\.\//, '').trim()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 解析逗号分隔或数组形式的文件列表
|
|
19
|
+
* @param {string|string[]|null|undefined} input
|
|
20
|
+
* @returns {string[]}
|
|
21
|
+
*/
|
|
22
|
+
function parseFileListInput (input) {
|
|
23
|
+
if (input == null || input === '') {
|
|
24
|
+
return []
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (Array.isArray(input)) {
|
|
28
|
+
return [...new Set(input.map(normalizeRelativePath).filter(Boolean))]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return [...new Set(
|
|
32
|
+
String(input)
|
|
33
|
+
.split(',')
|
|
34
|
+
.map(normalizeRelativePath)
|
|
35
|
+
.filter(Boolean)
|
|
36
|
+
)]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 从 @file 或 file 路径读取每行一个相对路径
|
|
41
|
+
* @param {string} listPath
|
|
42
|
+
* @returns {string[]}
|
|
43
|
+
*/
|
|
44
|
+
function readFileListFromPath (listPath) {
|
|
45
|
+
const abs = path.resolve(listPath)
|
|
46
|
+
if (!fs.existsSync(abs)) {
|
|
47
|
+
throw new Error(`文件列表不存在: ${abs}`)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const lines = fs.readFileSync(abs, 'utf8').split(/\r?\n/)
|
|
51
|
+
const files = []
|
|
52
|
+
|
|
53
|
+
for (const line of lines) {
|
|
54
|
+
const trimmed = line.trim()
|
|
55
|
+
if (!trimmed || trimmed.startsWith('#')) {
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
files.push(normalizeRelativePath(trimmed))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return [...new Set(files)]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 合并 CLI 传入的文件与列表文件(支持 @files.txt 前缀)
|
|
66
|
+
* @param {string[]} onlyFiles
|
|
67
|
+
* @param {string|null} fileListPath
|
|
68
|
+
* @returns {string[]}
|
|
69
|
+
*/
|
|
70
|
+
function resolveSelectedFiles (onlyFiles = [], fileListPath = null) {
|
|
71
|
+
/** @type {string[]} */
|
|
72
|
+
const selected = []
|
|
73
|
+
|
|
74
|
+
for (const item of onlyFiles) {
|
|
75
|
+
if (item.startsWith('@')) {
|
|
76
|
+
selected.push(...readFileListFromPath(item.slice(1)))
|
|
77
|
+
} else {
|
|
78
|
+
selected.push(normalizeRelativePath(item))
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (fileListPath) {
|
|
83
|
+
selected.push(...readFileListFromPath(fileListPath))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return [...new Set(selected.filter(Boolean))]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 从全量文件列表中筛选指定文件;支持目录前缀(以 / 结尾)
|
|
91
|
+
* @param {string[]} allFiles
|
|
92
|
+
* @param {string[]} selected
|
|
93
|
+
* @returns {{ matched: string[], missing: string[] }}
|
|
94
|
+
*/
|
|
95
|
+
function filterFilesBySelection (allFiles, selected) {
|
|
96
|
+
if (selected.length === 0) {
|
|
97
|
+
return { matched: [...allFiles], missing: [] }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @type {Set<string>} */
|
|
101
|
+
const matchedSet = new Set()
|
|
102
|
+
/** @type {string[]} */
|
|
103
|
+
const missing = []
|
|
104
|
+
|
|
105
|
+
for (const sel of selected) {
|
|
106
|
+
const prefix = sel.endsWith('/') ? sel : null
|
|
107
|
+
|
|
108
|
+
if (prefix) {
|
|
109
|
+
const hits = allFiles.filter(f => f.startsWith(prefix))
|
|
110
|
+
if (hits.length === 0) {
|
|
111
|
+
missing.push(sel)
|
|
112
|
+
} else {
|
|
113
|
+
hits.forEach(f => matchedSet.add(f))
|
|
114
|
+
}
|
|
115
|
+
continue
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (allFiles.includes(sel)) {
|
|
119
|
+
matchedSet.add(sel)
|
|
120
|
+
} else {
|
|
121
|
+
missing.push(sel)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
matched: [...matchedSet].sort(),
|
|
127
|
+
missing
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 清理版本号用于目录/zip 命名
|
|
133
|
+
* @param {string} version
|
|
134
|
+
* @returns {string}
|
|
135
|
+
*/
|
|
136
|
+
function sanitizeVersionLabel (version) {
|
|
137
|
+
return String(version).trim().replace(/^v/i, '').replace(/[^a-zA-Z0-9._-]/g, '_')
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = {
|
|
141
|
+
normalizeRelativePath,
|
|
142
|
+
parseFileListInput,
|
|
143
|
+
readFileListFromPath,
|
|
144
|
+
resolveSelectedFiles,
|
|
145
|
+
filterFilesBySelection,
|
|
146
|
+
sanitizeVersionLabel
|
|
147
|
+
}
|
package/lib/git.js
CHANGED
|
@@ -6,19 +6,30 @@ const { execFileSync, execSync } = require('child_process')
|
|
|
6
6
|
const fs = require('fs')
|
|
7
7
|
const path = require('path')
|
|
8
8
|
|
|
9
|
+
/** @type {string|null} */
|
|
10
|
+
let cachedGitExe = null
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* @returns {string}
|
|
11
14
|
*/
|
|
12
15
|
function findGitExe () {
|
|
16
|
+
if (cachedGitExe) {
|
|
17
|
+
return cachedGitExe
|
|
18
|
+
}
|
|
19
|
+
|
|
13
20
|
if (process.env.GIT_EXE && fs.existsSync(process.env.GIT_EXE)) {
|
|
14
|
-
|
|
21
|
+
cachedGitExe = process.env.GIT_EXE
|
|
22
|
+
return cachedGitExe
|
|
15
23
|
}
|
|
16
24
|
|
|
17
25
|
try {
|
|
18
26
|
const which = process.platform === 'win32' ? 'where' : 'command -v'
|
|
19
27
|
const result = execSync(`${which} git`, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] })
|
|
20
28
|
const first = result.split(/\r?\n/).find(Boolean)
|
|
21
|
-
if (first)
|
|
29
|
+
if (first) {
|
|
30
|
+
cachedGitExe = first.trim()
|
|
31
|
+
return cachedGitExe
|
|
32
|
+
}
|
|
22
33
|
} catch {
|
|
23
34
|
// ignore
|
|
24
35
|
}
|
|
@@ -51,10 +62,11 @@ function runGit (gitExe, args, cwd) {
|
|
|
51
62
|
* @param {string} [gitExe]
|
|
52
63
|
* @returns {{ commit: string, shortCommit: string, root: string }}
|
|
53
64
|
*/
|
|
54
|
-
function getGitInfo (sourceDir, gitExe
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const
|
|
65
|
+
function getGitInfo (sourceDir, gitExe) {
|
|
66
|
+
const exe = gitExe || findGitExe()
|
|
67
|
+
const root = runGit(exe, ['rev-parse', '--show-toplevel'], sourceDir).trim()
|
|
68
|
+
const commit = runGit(exe, ['rev-parse', 'HEAD'], sourceDir).trim()
|
|
69
|
+
const shortCommit = runGit(exe, ['rev-parse', '--short', 'HEAD'], sourceDir).trim()
|
|
58
70
|
|
|
59
71
|
return { commit, shortCommit, root }
|
|
60
72
|
}
|
|
@@ -65,8 +77,9 @@ function getGitInfo (sourceDir, gitExe = findGitExe()) {
|
|
|
65
77
|
* @param {string} [gitExe]
|
|
66
78
|
* @returns {string} 完整 commit hash
|
|
67
79
|
*/
|
|
68
|
-
function resolveGitRef (sourceDir, ref, gitExe
|
|
69
|
-
|
|
80
|
+
function resolveGitRef (sourceDir, ref, gitExe) {
|
|
81
|
+
const exe = gitExe || findGitExe()
|
|
82
|
+
return runGit(exe, ['rev-parse', ref], sourceDir).trim()
|
|
70
83
|
}
|
|
71
84
|
|
|
72
85
|
/**
|
|
@@ -82,13 +95,14 @@ function resolveGitRef (sourceDir, ref, gitExe = findGitExe()) {
|
|
|
82
95
|
* @param {string} [options.gitExe]
|
|
83
96
|
* @returns {DiffEntry[]}
|
|
84
97
|
*/
|
|
85
|
-
function getGitDiffSummary ({ sourceDir, fromRef, toRef, gitExe
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
const
|
|
98
|
+
function getGitDiffSummary ({ sourceDir, fromRef, toRef, gitExe }) {
|
|
99
|
+
const exe = gitExe || findGitExe()
|
|
100
|
+
const root = runGit(exe, ['rev-parse', '--show-toplevel'], sourceDir).trim()
|
|
101
|
+
const fromCommit = resolveGitRef(sourceDir, fromRef, exe)
|
|
102
|
+
const toCommit = resolveGitRef(sourceDir, toRef, exe)
|
|
89
103
|
|
|
90
104
|
const output = runGit(
|
|
91
|
-
|
|
105
|
+
exe,
|
|
92
106
|
['diff', '--name-status', `${fromCommit}..${toCommit}`],
|
|
93
107
|
root
|
|
94
108
|
)
|
|
@@ -123,6 +137,15 @@ function getGitDiffSummary ({ sourceDir, fromRef, toRef, gitExe = findGitExe() }
|
|
|
123
137
|
continue
|
|
124
138
|
}
|
|
125
139
|
|
|
140
|
+
// 类型变更(如文件 ↔ 符号链接)
|
|
141
|
+
if (/^T\d*$/.test(status) && parts.length >= 2) {
|
|
142
|
+
entries.push({
|
|
143
|
+
action: 'M',
|
|
144
|
+
relativePath: parts[1].replace(/\\/g, '/')
|
|
145
|
+
})
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
|
|
126
149
|
entries.push({
|
|
127
150
|
action: 'M',
|
|
128
151
|
relativePath: parts[parts.length - 1].replace(/\\/g, '/'),
|
|
@@ -134,13 +157,14 @@ function getGitDiffSummary ({ sourceDir, fromRef, toRef, gitExe = findGitExe() }
|
|
|
134
157
|
}
|
|
135
158
|
|
|
136
159
|
/**
|
|
137
|
-
* 判断目录是否在 Git
|
|
160
|
+
* 判断目录是否在 Git 工作区内(Git 未安装时返回 false,不抛错)
|
|
138
161
|
* @param {string} dir
|
|
139
162
|
* @param {string} [gitExe]
|
|
140
163
|
*/
|
|
141
|
-
function isGitRepository (dir, gitExe
|
|
164
|
+
function isGitRepository (dir, gitExe) {
|
|
142
165
|
try {
|
|
143
|
-
|
|
166
|
+
const exe = gitExe || findGitExe()
|
|
167
|
+
runGit(exe, ['rev-parse', '--is-inside-work-tree'], dir)
|
|
144
168
|
return true
|
|
145
169
|
} catch {
|
|
146
170
|
return false
|
package/lib/index.js
CHANGED
|
@@ -3,21 +3,32 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
const { buildPatch } = require('./build-patch')
|
|
6
|
+
const { extractPatchSubset } = require('./extract-patch')
|
|
7
|
+
const { readManifestFiles } = require('./manifest')
|
|
6
8
|
const { resolveConfig, detectVcs, initDeployDir } = require('./config')
|
|
7
9
|
const { readDeployMarker, writeDeployMarker } = require('./marker')
|
|
8
10
|
const { shouldExcludePath } = require('./exclude')
|
|
9
11
|
const { getBackend, BACKENDS } = require('./backends')
|
|
12
|
+
const { registerVersion, findVersionRecord, listVersionRecords } = require('./version-registry')
|
|
13
|
+
const { resolveSelectedFiles, filterFilesBySelection } = require('./file-filter')
|
|
10
14
|
const svn = require('./svn')
|
|
11
15
|
const git = require('./git')
|
|
12
16
|
const { createZipFromDirectory } = require('./zip')
|
|
13
17
|
|
|
14
18
|
module.exports = {
|
|
15
19
|
buildPatch,
|
|
20
|
+
extractPatchSubset,
|
|
21
|
+
readManifestFiles,
|
|
16
22
|
resolveConfig,
|
|
17
23
|
initDeployDir,
|
|
18
24
|
detectVcs,
|
|
19
25
|
readDeployMarker,
|
|
20
26
|
writeDeployMarker,
|
|
27
|
+
registerVersion,
|
|
28
|
+
findVersionRecord,
|
|
29
|
+
listVersionRecords,
|
|
30
|
+
resolveSelectedFiles,
|
|
31
|
+
filterFilesBySelection,
|
|
21
32
|
shouldExcludePath,
|
|
22
33
|
getBackend,
|
|
23
34
|
BACKENDS,
|
package/lib/manifest.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MANIFEST.txt 解析(build / extract / 测试共用)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
const path = require('path')
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 从 patch 目录 MANIFEST.txt 读取 # files 段
|
|
10
|
+
* @param {string} patchDir
|
|
11
|
+
* @returns {string[]}
|
|
12
|
+
*/
|
|
13
|
+
function readManifestFiles (patchDir) {
|
|
14
|
+
const manifestPath = path.join(patchDir, 'MANIFEST.txt')
|
|
15
|
+
if (!fs.existsSync(manifestPath)) {
|
|
16
|
+
throw new Error(`缺少 MANIFEST.txt: ${manifestPath}`)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const lines = fs.readFileSync(manifestPath, 'utf8').split(/\r?\n/)
|
|
20
|
+
/** @type {string[]} */
|
|
21
|
+
const files = []
|
|
22
|
+
let inFiles = false
|
|
23
|
+
|
|
24
|
+
for (const line of lines) {
|
|
25
|
+
const trimmed = line.trim()
|
|
26
|
+
if (trimmed === '# files') {
|
|
27
|
+
inFiles = true
|
|
28
|
+
continue
|
|
29
|
+
}
|
|
30
|
+
if (inFiles && trimmed.startsWith('#')) {
|
|
31
|
+
break
|
|
32
|
+
}
|
|
33
|
+
if (inFiles && trimmed) {
|
|
34
|
+
files.push(trimmed.replace(/\\/g, '/'))
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (files.length === 0) {
|
|
39
|
+
throw new Error(`MANIFEST.txt 中未找到文件列表: ${manifestPath}`)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return files
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = {
|
|
46
|
+
readManifestFiles
|
|
47
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 部署版本注册表 — 记录每次打包的版本号、VCS 基准与文件清单
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
const path = require('path')
|
|
7
|
+
const { sanitizeVersionLabel } = require('./file-filter')
|
|
8
|
+
|
|
9
|
+
const REGISTRY_FILE = 'versions.json'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} VersionRecord
|
|
13
|
+
* @property {string} version 原始版本号(如 1.2.3)
|
|
14
|
+
* @property {string} versionLabel 文件名安全版本(如 1.2.3)
|
|
15
|
+
* @property {'svn'|'git'} vcs
|
|
16
|
+
* @property {string} fromLabel
|
|
17
|
+
* @property {string} toLabel
|
|
18
|
+
* @property {string[]} files 本次包内文件(相对路径)
|
|
19
|
+
* @property {string[]} deleted
|
|
20
|
+
* @property {string} patchDir
|
|
21
|
+
* @property {string} [zipPath]
|
|
22
|
+
* @property {boolean} [partial] 是否为子集提取包
|
|
23
|
+
* @property {string} [sourceVersion] 子集提取时的源版本
|
|
24
|
+
* @property {string} createdAt ISO 时间
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 原子写入 JSON,避免中途失败导致 versions.json 损坏
|
|
29
|
+
* @param {string} filePath
|
|
30
|
+
* @param {unknown} data
|
|
31
|
+
*/
|
|
32
|
+
function writeJsonAtomic (filePath, data) {
|
|
33
|
+
const dir = path.dirname(filePath)
|
|
34
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
35
|
+
const tmpPath = `${filePath}.${process.pid}.${Date.now()}.tmp`
|
|
36
|
+
fs.writeFileSync(tmpPath, JSON.stringify(data, null, 2), 'utf8')
|
|
37
|
+
fs.renameSync(tmpPath, filePath)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} deployDir
|
|
42
|
+
* @returns {{ versions: VersionRecord[] }}
|
|
43
|
+
*/
|
|
44
|
+
function readVersionsRegistry (deployDir) {
|
|
45
|
+
const registryPath = path.join(deployDir, REGISTRY_FILE)
|
|
46
|
+
if (!fs.existsSync(registryPath)) {
|
|
47
|
+
return { versions: [] }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const data = JSON.parse(fs.readFileSync(registryPath, 'utf8'))
|
|
52
|
+
if (Array.isArray(data.versions)) {
|
|
53
|
+
return /** @type {{ versions: VersionRecord[] }} */ (data)
|
|
54
|
+
}
|
|
55
|
+
console.warn(`[vcs-patch-deploy] ${REGISTRY_FILE} 格式异常(缺少 versions 数组),已忽略`)
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.warn(`[vcs-patch-deploy] ${REGISTRY_FILE} 解析失败: ${error.message}`)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return { versions: [] }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {string} deployDir
|
|
65
|
+
* @param {VersionRecord} record
|
|
66
|
+
*/
|
|
67
|
+
function registerVersion (deployDir, record) {
|
|
68
|
+
const registry = readVersionsRegistry(deployDir)
|
|
69
|
+
const registryPath = path.join(deployDir, REGISTRY_FILE)
|
|
70
|
+
|
|
71
|
+
if (record.partial) {
|
|
72
|
+
// 子集包:同 version+sourceVersion 去重后追加
|
|
73
|
+
const dupIdx = registry.versions.findIndex(v =>
|
|
74
|
+
v.partial &&
|
|
75
|
+
v.version === record.version &&
|
|
76
|
+
v.sourceVersion === record.sourceVersion
|
|
77
|
+
)
|
|
78
|
+
if (dupIdx >= 0) {
|
|
79
|
+
registry.versions[dupIdx] = record
|
|
80
|
+
} else {
|
|
81
|
+
registry.versions.push(record)
|
|
82
|
+
}
|
|
83
|
+
} else {
|
|
84
|
+
const idx = registry.versions.findIndex(v => v.version === record.version && !v.partial)
|
|
85
|
+
if (idx >= 0) {
|
|
86
|
+
registry.versions[idx] = record
|
|
87
|
+
} else {
|
|
88
|
+
registry.versions.push(record)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
registry.versions.sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1))
|
|
93
|
+
writeJsonAtomic(registryPath, registry)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {string} deployDir
|
|
98
|
+
* @param {string} version
|
|
99
|
+
* @returns {VersionRecord|null}
|
|
100
|
+
*/
|
|
101
|
+
function findVersionRecord (deployDir, version) {
|
|
102
|
+
const label = sanitizeVersionLabel(version)
|
|
103
|
+
const registry = readVersionsRegistry(deployDir)
|
|
104
|
+
|
|
105
|
+
// 优先返回非 partial 的完整版本包
|
|
106
|
+
const full = registry.versions.find(v =>
|
|
107
|
+
!v.partial && (
|
|
108
|
+
v.version === version ||
|
|
109
|
+
v.versionLabel === label ||
|
|
110
|
+
sanitizeVersionLabel(v.version) === label
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
if (full) return full
|
|
114
|
+
|
|
115
|
+
return registry.versions.find(v =>
|
|
116
|
+
v.version === version ||
|
|
117
|
+
v.versionLabel === label ||
|
|
118
|
+
sanitizeVersionLabel(v.version) === label
|
|
119
|
+
) || null
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @param {string} deployDir
|
|
124
|
+
* @returns {VersionRecord[]}
|
|
125
|
+
*/
|
|
126
|
+
function listVersionRecords (deployDir) {
|
|
127
|
+
return readVersionsRegistry(deployDir).versions
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = {
|
|
131
|
+
REGISTRY_FILE,
|
|
132
|
+
readVersionsRegistry,
|
|
133
|
+
registerVersion,
|
|
134
|
+
findVersionRecord,
|
|
135
|
+
listVersionRecords,
|
|
136
|
+
writeJsonAtomic
|
|
137
|
+
}
|
package/lib/zip.js
CHANGED
|
@@ -12,11 +12,14 @@ const archiver = require('archiver')
|
|
|
12
12
|
* @returns {Promise<{ zipPath: string, size: number }>}
|
|
13
13
|
*/
|
|
14
14
|
function createZipFromDirectory (sourceDir, zipPath) {
|
|
15
|
+
const level = Number(process.env.PATCH_ZIP_LEVEL ?? 6)
|
|
16
|
+
const zipLevel = Number.isFinite(level) ? Math.min(9, Math.max(0, level)) : 6
|
|
17
|
+
|
|
15
18
|
return new Promise((resolve, reject) => {
|
|
16
19
|
fs.mkdirSync(path.dirname(zipPath), { recursive: true })
|
|
17
20
|
|
|
18
21
|
const output = fs.createWriteStream(zipPath)
|
|
19
|
-
const archive = archiver('zip', { zlib: { level:
|
|
22
|
+
const archive = archiver('zip', { zlib: { level: zipLevel } })
|
|
20
23
|
|
|
21
24
|
output.on('close', () => {
|
|
22
25
|
resolve({ zipPath, size: archive.pointer() })
|