vcs-patch-deploy 1.1.1 → 1.3.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 +57 -2
- package/README.md +181 -8
- package/assets/php/apply-update.php +1207 -67
- package/bin/patch-build.js +74 -13
- package/bin/patch-extract.js +24 -8
- package/env.example +30 -0
- package/lib/backends/php.js +5 -0
- package/lib/build-patch.js +382 -58
- package/lib/config.js +101 -14
- package/lib/extract-patch.js +100 -17
- package/lib/file-filter.js +20 -0
- package/lib/git.js +161 -22
- package/lib/index.js +33 -4
- package/lib/manifest.js +65 -2
- package/lib/marker.js +79 -14
- package/lib/modes.js +72 -0
- package/lib/out-prune.js +121 -0
- package/lib/protect.js +124 -0
- package/lib/svn.js +409 -12
- package/lib/version-archive.js +238 -0
- package/lib/version-registry.js +87 -8
- package/lib/zip.js +126 -8
- package/package.json +5 -2
package/lib/index.js
CHANGED
|
@@ -2,37 +2,66 @@
|
|
|
2
2
|
* vcs-patch-deploy 主入口
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
const { buildPatch } = require('./build-patch')
|
|
5
|
+
const { buildPatch, buildVersionLabels } = require('./build-patch')
|
|
6
6
|
const { extractPatchSubset } = require('./extract-patch')
|
|
7
|
-
const { readManifestFiles } = require('./manifest')
|
|
7
|
+
const { readManifestFiles, readManifestDeleted } = require('./manifest')
|
|
8
8
|
const { resolveConfig, detectVcs, initDeployDir } = require('./config')
|
|
9
9
|
const { readDeployMarker, writeDeployMarker } = require('./marker')
|
|
10
10
|
const { shouldExcludePath } = require('./exclude')
|
|
11
|
+
const { resolveProtectFiles, readProtectListFromFile, PROTECT_FILE } = require('./protect')
|
|
12
|
+
const { MODES_FILE, readModesFile, writeModesFile } = require('./modes')
|
|
11
13
|
const { getBackend, BACKENDS } = require('./backends')
|
|
12
|
-
const {
|
|
13
|
-
|
|
14
|
+
const {
|
|
15
|
+
registerVersion,
|
|
16
|
+
pruneWorktreeRecords,
|
|
17
|
+
findVersionRecord,
|
|
18
|
+
listVersionRecords
|
|
19
|
+
} = require('./version-registry')
|
|
20
|
+
const { resolveSelectedFiles, filterFilesBySelection, isSafeRelativePath } = require('./file-filter')
|
|
14
21
|
const svn = require('./svn')
|
|
15
22
|
const git = require('./git')
|
|
16
23
|
const { createZipFromDirectory } = require('./zip')
|
|
24
|
+
const { pruneOutDirectory } = require('./out-prune')
|
|
25
|
+
const {
|
|
26
|
+
buildDeployVersionLabel,
|
|
27
|
+
archivePatchPackage,
|
|
28
|
+
readArchiveIndex,
|
|
29
|
+
getArchiveRoot
|
|
30
|
+
} = require('./version-archive')
|
|
17
31
|
|
|
18
32
|
module.exports = {
|
|
19
33
|
buildPatch,
|
|
34
|
+
buildVersionLabels,
|
|
20
35
|
extractPatchSubset,
|
|
21
36
|
readManifestFiles,
|
|
37
|
+
readManifestDeleted,
|
|
22
38
|
resolveConfig,
|
|
23
39
|
initDeployDir,
|
|
24
40
|
detectVcs,
|
|
25
41
|
readDeployMarker,
|
|
26
42
|
writeDeployMarker,
|
|
27
43
|
registerVersion,
|
|
44
|
+
pruneWorktreeRecords,
|
|
28
45
|
findVersionRecord,
|
|
29
46
|
listVersionRecords,
|
|
30
47
|
resolveSelectedFiles,
|
|
31
48
|
filterFilesBySelection,
|
|
49
|
+
isSafeRelativePath,
|
|
32
50
|
shouldExcludePath,
|
|
51
|
+
resolveProtectFiles,
|
|
52
|
+
readProtectListFromFile,
|
|
53
|
+
PROTECT_FILE,
|
|
54
|
+
MODES_FILE,
|
|
55
|
+
readModesFile,
|
|
56
|
+
writeModesFile,
|
|
33
57
|
getBackend,
|
|
34
58
|
BACKENDS,
|
|
35
59
|
createZipFromDirectory,
|
|
60
|
+
pruneOutDirectory,
|
|
61
|
+
buildDeployVersionLabel,
|
|
62
|
+
archivePatchPackage,
|
|
63
|
+
readArchiveIndex,
|
|
64
|
+
getArchiveRoot,
|
|
36
65
|
svn,
|
|
37
66
|
git
|
|
38
67
|
}
|
package/lib/manifest.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
const fs = require('fs')
|
|
6
6
|
const path = require('path')
|
|
7
|
+
const { isSafeRelativePath } = require('./file-filter')
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* 从 patch 目录 MANIFEST.txt 读取 # files 段
|
|
@@ -31,7 +32,11 @@ function readManifestFiles (patchDir) {
|
|
|
31
32
|
break
|
|
32
33
|
}
|
|
33
34
|
if (inFiles && trimmed) {
|
|
34
|
-
|
|
35
|
+
const rel = trimmed.replace(/\\/g, '/')
|
|
36
|
+
if (!isSafeRelativePath(rel)) {
|
|
37
|
+
throw new Error(`MANIFEST.txt 含不安全路径(拒绝处理): ${rel}`)
|
|
38
|
+
}
|
|
39
|
+
files.push(rel)
|
|
35
40
|
}
|
|
36
41
|
}
|
|
37
42
|
|
|
@@ -42,6 +47,64 @@ function readManifestFiles (patchDir) {
|
|
|
42
47
|
return files
|
|
43
48
|
}
|
|
44
49
|
|
|
50
|
+
/**
|
|
51
|
+
* 读取 MANIFEST.txt 的删除段;不存在时返回空数组
|
|
52
|
+
* @param {string} patchDir
|
|
53
|
+
* @returns {string[]}
|
|
54
|
+
*/
|
|
55
|
+
function readManifestDeleted (patchDir) {
|
|
56
|
+
const manifestPath = path.join(patchDir, 'MANIFEST.txt')
|
|
57
|
+
if (!fs.existsSync(manifestPath)) {
|
|
58
|
+
return []
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const lines = fs.readFileSync(manifestPath, 'utf8').split(/\r?\n/)
|
|
62
|
+
/** @type {string[]} */
|
|
63
|
+
const files = []
|
|
64
|
+
let inDeleted = false
|
|
65
|
+
let hasSection = false
|
|
66
|
+
|
|
67
|
+
for (const line of lines) {
|
|
68
|
+
const trimmed = line.trim()
|
|
69
|
+
if (trimmed.startsWith('# deleted')) {
|
|
70
|
+
inDeleted = true
|
|
71
|
+
hasSection = true
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
if (inDeleted && trimmed.startsWith('#')) {
|
|
75
|
+
break
|
|
76
|
+
}
|
|
77
|
+
if (inDeleted && trimmed) {
|
|
78
|
+
const rel = trimmed.replace(/\\/g, '/')
|
|
79
|
+
if (isSafeRelativePath(rel)) {
|
|
80
|
+
files.push(rel)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (hasSection) {
|
|
86
|
+
return files
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 兼容只有 MANIFEST-deleted.txt、没有 # deleted 段的旧包
|
|
90
|
+
const deletedPath = path.join(patchDir, 'MANIFEST-deleted.txt')
|
|
91
|
+
if (!fs.existsSync(deletedPath)) {
|
|
92
|
+
return []
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const line of fs.readFileSync(deletedPath, 'utf8').split(/\r?\n/)) {
|
|
96
|
+
const trimmed = line.trim()
|
|
97
|
+
if (!trimmed || trimmed.startsWith('#')) continue
|
|
98
|
+
const rel = trimmed.replace(/\\/g, '/')
|
|
99
|
+
if (isSafeRelativePath(rel)) {
|
|
100
|
+
files.push(rel)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return files
|
|
105
|
+
}
|
|
106
|
+
|
|
45
107
|
module.exports = {
|
|
46
|
-
readManifestFiles
|
|
108
|
+
readManifestFiles,
|
|
109
|
+
readManifestDeleted
|
|
47
110
|
}
|
package/lib/marker.js
CHANGED
|
@@ -5,7 +5,11 @@
|
|
|
5
5
|
* svn:467
|
|
6
6
|
* git:abc123def456789...
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* 多环境 / 多 profile:`.last-deploy-marker.<profile>`
|
|
9
|
+
* .last-deploy-marker 生产(默认)
|
|
10
|
+
* .last-deploy-marker.php85 PHP 8.5 预发
|
|
11
|
+
*
|
|
12
|
+
* 兼容旧版工具:若无 marker 则读取 `.last-deploy-rev`(仅 SVN,仅默认 profile)
|
|
9
13
|
*/
|
|
10
14
|
|
|
11
15
|
const fs = require('fs')
|
|
@@ -14,18 +18,58 @@ const path = require('path')
|
|
|
14
18
|
const MARKER_FILE = '.last-deploy-marker'
|
|
15
19
|
const LEGACY_SVN_FILE = '.last-deploy-rev'
|
|
16
20
|
|
|
21
|
+
/**
|
|
22
|
+
* profile 只允许安全字符,避免路径穿越(如 profile=../x)
|
|
23
|
+
* @param {string} profile
|
|
24
|
+
* @returns {boolean}
|
|
25
|
+
*/
|
|
26
|
+
function isValidProfile (profile) {
|
|
27
|
+
return typeof profile === 'string' && /^[A-Za-z0-9._-]+$/.test(profile)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 生成 marker 文件名(默认 profile 返回 `.last-deploy-marker`)
|
|
32
|
+
* @param {string|null} [profile]
|
|
33
|
+
* @returns {string}
|
|
34
|
+
*/
|
|
35
|
+
function markerFileName (profile = null) {
|
|
36
|
+
if (!profile) {
|
|
37
|
+
return MARKER_FILE
|
|
38
|
+
}
|
|
39
|
+
if (!isValidProfile(profile)) {
|
|
40
|
+
throw new Error(`非法 profile: ${profile}(仅允许字母、数字、. _ -)`)
|
|
41
|
+
}
|
|
42
|
+
return `${MARKER_FILE}.${profile}`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {string} filePath
|
|
47
|
+
* @returns {{ vcs: 'svn'|'git', value: string } | null}
|
|
48
|
+
*/
|
|
49
|
+
function parseMarkerFile (filePath) {
|
|
50
|
+
if (!fs.existsSync(filePath)) {
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const line = fs.readFileSync(filePath, 'utf8').trim()
|
|
55
|
+
const match = line.match(/^(svn|git):(.+)$/)
|
|
56
|
+
if (!match) {
|
|
57
|
+
return null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return { vcs: /** @type {'svn'|'git'} */ (match[1]), value: match[2].trim() }
|
|
61
|
+
}
|
|
62
|
+
|
|
17
63
|
/**
|
|
18
64
|
* @param {string} deployDir
|
|
65
|
+
* @param {string|null} [profile]
|
|
19
66
|
* @returns {{ vcs: 'svn'|'git', value: string } | null}
|
|
20
67
|
*/
|
|
21
|
-
function readDeployMarker (deployDir) {
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (match) {
|
|
27
|
-
return { vcs: /** @type {'svn'|'git'} */ (match[1]), value: match[2].trim() }
|
|
28
|
-
}
|
|
68
|
+
function readDeployMarker (deployDir, profile = null) {
|
|
69
|
+
const marker = parseMarkerFile(path.join(deployDir, markerFileName(profile)))
|
|
70
|
+
if (marker || profile) {
|
|
71
|
+
// 指定 profile 时不回退到默认 marker / legacy rev,避免跨环境误用基准
|
|
72
|
+
return marker
|
|
29
73
|
}
|
|
30
74
|
|
|
31
75
|
const legacyPath = path.join(deployDir, LEGACY_SVN_FILE)
|
|
@@ -39,17 +83,34 @@ function readDeployMarker (deployDir) {
|
|
|
39
83
|
return null
|
|
40
84
|
}
|
|
41
85
|
|
|
86
|
+
/**
|
|
87
|
+
* 列出 deploy 目录下已有的 marker 文件(含 profile),默认 marker 排在前面
|
|
88
|
+
* @param {string} deployDir
|
|
89
|
+
* @returns {string[]} 文件名
|
|
90
|
+
*/
|
|
91
|
+
function listMarkerFiles (deployDir) {
|
|
92
|
+
if (!fs.existsSync(deployDir)) {
|
|
93
|
+
return []
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return fs.readdirSync(deployDir)
|
|
97
|
+
.filter(name => name === MARKER_FILE || name.startsWith(`${MARKER_FILE}.`))
|
|
98
|
+
.filter(name => parseMarkerFile(path.join(deployDir, name)) !== null)
|
|
99
|
+
.sort((a, b) => (a === MARKER_FILE ? -1 : b === MARKER_FILE ? 1 : a.localeCompare(b)))
|
|
100
|
+
}
|
|
101
|
+
|
|
42
102
|
/**
|
|
43
103
|
* @param {string} deployDir
|
|
44
104
|
* @param {'svn'|'git'} vcs
|
|
45
105
|
* @param {string} value
|
|
106
|
+
* @param {string|null} [profile]
|
|
46
107
|
*/
|
|
47
|
-
function writeDeployMarker (deployDir, vcs, value) {
|
|
108
|
+
function writeDeployMarker (deployDir, vcs, value, profile = null) {
|
|
48
109
|
fs.mkdirSync(deployDir, { recursive: true })
|
|
49
|
-
fs.writeFileSync(path.join(deployDir,
|
|
110
|
+
fs.writeFileSync(path.join(deployDir, markerFileName(profile)), `${vcs}:${value}`, 'utf8')
|
|
50
111
|
|
|
51
|
-
// SVN
|
|
52
|
-
if (vcs === 'svn') {
|
|
112
|
+
// SVN 兼容:同步写旧文件(仅默认 profile,profile 环境不写全局 rev)
|
|
113
|
+
if (vcs === 'svn' && !profile) {
|
|
53
114
|
fs.writeFileSync(path.join(deployDir, LEGACY_SVN_FILE), value, 'utf8')
|
|
54
115
|
}
|
|
55
116
|
}
|
|
@@ -57,6 +118,10 @@ function writeDeployMarker (deployDir, vcs, value) {
|
|
|
57
118
|
module.exports = {
|
|
58
119
|
MARKER_FILE,
|
|
59
120
|
LEGACY_SVN_FILE,
|
|
121
|
+
isValidProfile,
|
|
122
|
+
markerFileName,
|
|
123
|
+
parseMarkerFile,
|
|
60
124
|
readDeployMarker,
|
|
61
|
-
writeDeployMarker
|
|
125
|
+
writeDeployMarker,
|
|
126
|
+
listMarkerFiles
|
|
62
127
|
}
|
package/lib/modes.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MODES.txt — 包内可执行文件清单
|
|
3
|
+
*
|
|
4
|
+
* 格式(每行):
|
|
5
|
+
* 0755 app/console/run.sh
|
|
6
|
+
*
|
|
7
|
+
* Windows 打包时 zip 无法可靠携带 Unix 执行位,服务器端据此 chmod。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const fs = require('fs')
|
|
11
|
+
const path = require('path')
|
|
12
|
+
const { isSafeRelativePath } = require('./file-filter')
|
|
13
|
+
|
|
14
|
+
const MODES_FILE = 'MODES.txt'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} patchDir
|
|
18
|
+
* @param {string[]} executableFiles
|
|
19
|
+
* @returns {boolean} 是否写入
|
|
20
|
+
*/
|
|
21
|
+
function writeModesFile (patchDir, executableFiles) {
|
|
22
|
+
if (!executableFiles || executableFiles.length === 0) {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fs.writeFileSync(
|
|
27
|
+
path.join(patchDir, MODES_FILE),
|
|
28
|
+
['# mode 相对路径(0755 = 可执行)', ...executableFiles.map(rel => `0755 ${rel}`)].join('\n'),
|
|
29
|
+
'utf8'
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
return true
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 读取 MODES.txt;不存在时返回空对象
|
|
37
|
+
* @param {string} patchDir
|
|
38
|
+
* @returns {Record<string, number>}
|
|
39
|
+
*/
|
|
40
|
+
function readModesFile (patchDir) {
|
|
41
|
+
const filePath = path.join(patchDir, MODES_FILE)
|
|
42
|
+
if (!fs.existsSync(filePath)) {
|
|
43
|
+
return {}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @type {Record<string, number>} */
|
|
47
|
+
const modes = {}
|
|
48
|
+
|
|
49
|
+
for (const line of fs.readFileSync(filePath, 'utf8').split(/\r?\n/)) {
|
|
50
|
+
const trimmed = line.trim()
|
|
51
|
+
if (!trimmed || trimmed.startsWith('#')) continue
|
|
52
|
+
|
|
53
|
+
const match = trimmed.match(/^([0-7]{3,4})\s+(.+)$/)
|
|
54
|
+
if (!match) continue
|
|
55
|
+
|
|
56
|
+
const rel = match[2].trim().replace(/\\/g, '/')
|
|
57
|
+
if (!isSafeRelativePath(rel)) continue
|
|
58
|
+
|
|
59
|
+
const mode = parseInt(match[1], 8)
|
|
60
|
+
if (Number.isFinite(mode)) {
|
|
61
|
+
modes[rel] = mode
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return modes
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = {
|
|
69
|
+
MODES_FILE,
|
|
70
|
+
writeModesFile,
|
|
71
|
+
readModesFile
|
|
72
|
+
}
|
package/lib/out-prune.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* deploy/out 清理:按保留数量删除旧的 patch 目录与 zip
|
|
3
|
+
*
|
|
4
|
+
* 只处理本工具命名的产物(patch-* / <zipPrefix>-*.zip),不会碰用户文件;
|
|
5
|
+
* 当前构建的产物与最近 keep 个产物始终保留。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const fs = require('fs')
|
|
9
|
+
const path = require('path')
|
|
10
|
+
const fse = require('fs-extra')
|
|
11
|
+
|
|
12
|
+
const STAGING_MAX_AGE_MS = 60 * 60 * 1000
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} outDir
|
|
16
|
+
* @param {object} [options]
|
|
17
|
+
* @param {number} [options.keep=10] 保留的 patch 目录/zip 数量
|
|
18
|
+
* @param {string} [options.zipPrefix='backend-update']
|
|
19
|
+
* @param {string[]} [options.keepPaths] 必须保留的路径(当前产物)
|
|
20
|
+
* @returns {Promise<string[]>} 被删除的产物名
|
|
21
|
+
*/
|
|
22
|
+
async function pruneOutDirectory (outDir, options = {}) {
|
|
23
|
+
if (!fs.existsSync(outDir)) {
|
|
24
|
+
return []
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const keep = Math.max(1, Number(options.keep) || 10)
|
|
28
|
+
const zipPrefix = options.zipPrefix || 'backend-update'
|
|
29
|
+
/** @type {Set<string>} */
|
|
30
|
+
const forced = new Set(
|
|
31
|
+
(options.keepPaths || [])
|
|
32
|
+
.filter(Boolean)
|
|
33
|
+
.map(p => path.basename(p))
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
let entries
|
|
37
|
+
try {
|
|
38
|
+
entries = fs.readdirSync(outDir, { withFileTypes: true })
|
|
39
|
+
} catch {
|
|
40
|
+
return []
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** @type {string[]} */
|
|
44
|
+
const removed = []
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {import('fs').Dirent[]} items
|
|
48
|
+
* @returns {import('fs').Dirent[]}
|
|
49
|
+
*/
|
|
50
|
+
const sortByMtimeDesc = (items) => {
|
|
51
|
+
return [...items].sort((a, b) => {
|
|
52
|
+
const ma = safeMtime(path.join(outDir, a.name))
|
|
53
|
+
const mb = safeMtime(path.join(outDir, b.name))
|
|
54
|
+
if (ma !== mb) return mb - ma
|
|
55
|
+
return b.name.localeCompare(a.name)
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {import('fs').Dirent[]} items
|
|
61
|
+
*/
|
|
62
|
+
const pruneItems = async (items) => {
|
|
63
|
+
let kept = 0
|
|
64
|
+
for (const item of sortByMtimeDesc(items)) {
|
|
65
|
+
if (forced.has(item.name)) {
|
|
66
|
+
kept++
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const item of sortByMtimeDesc(items)) {
|
|
71
|
+
if (forced.has(item.name)) continue
|
|
72
|
+
if (kept >= keep) {
|
|
73
|
+
const abs = path.join(outDir, item.name)
|
|
74
|
+
await fse.remove(abs).catch(() => {})
|
|
75
|
+
removed.push(item.name)
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
kept++
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const patchDirs = entries.filter(
|
|
83
|
+
e => e.isDirectory() && e.name.startsWith('patch-') && !e.name.includes('.staging-')
|
|
84
|
+
)
|
|
85
|
+
const zipFiles = entries.filter(
|
|
86
|
+
e => e.isFile() && e.name.startsWith(`${zipPrefix}-`) && e.name.endsWith('.zip')
|
|
87
|
+
)
|
|
88
|
+
const stagingDirs = entries.filter(
|
|
89
|
+
e => e.isDirectory() && /\.staging-\d+$/.test(e.name)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
await pruneItems(patchDirs)
|
|
93
|
+
await pruneItems(zipFiles)
|
|
94
|
+
|
|
95
|
+
// 异常中断留下的 staging 目录:超过 1 小时才清理,避免误删并发构建
|
|
96
|
+
const now = Date.now()
|
|
97
|
+
for (const item of stagingDirs) {
|
|
98
|
+
const abs = path.join(outDir, item.name)
|
|
99
|
+
if (now - safeMtime(abs) < STAGING_MAX_AGE_MS) continue
|
|
100
|
+
await fse.remove(abs).catch(() => {})
|
|
101
|
+
removed.push(item.name)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return removed
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {string} filePath
|
|
109
|
+
* @returns {number}
|
|
110
|
+
*/
|
|
111
|
+
function safeMtime (filePath) {
|
|
112
|
+
try {
|
|
113
|
+
return fs.statSync(filePath).mtimeMs
|
|
114
|
+
} catch {
|
|
115
|
+
return 0
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = {
|
|
120
|
+
pruneOutDirectory
|
|
121
|
+
}
|
package/lib/protect.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 受保护文件清单(上线时线上已存在则跳过覆盖)
|
|
3
|
+
*
|
|
4
|
+
* 来源(合并去重):
|
|
5
|
+
* 1. deploy/protect.txt(每行一个相对路径,# 注释)
|
|
6
|
+
* 2. --protect=<路径>(逗号分隔,可重复)
|
|
7
|
+
* 3. --protect-file=<文件>
|
|
8
|
+
* 4. PATCH_PROTECT / PATCH_PROTECT_FILE
|
|
9
|
+
*
|
|
10
|
+
* 打包时写入包内 PROTECT.txt,apply-update.php 读取后对“线上已存在”的
|
|
11
|
+
* 受保护文件跳过覆盖(不存在时仍写入,便于首次部署)。适用于运行时
|
|
12
|
+
* 被程序改写、又被版本控制的配置文件(如 config/date_config.json)。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const fs = require('fs')
|
|
16
|
+
const path = require('path')
|
|
17
|
+
const { normalizeRelativePath, isSafeRelativePath } = require('./file-filter')
|
|
18
|
+
|
|
19
|
+
const PROTECT_FILE = 'PROTECT.txt'
|
|
20
|
+
const DEPLOY_PROTECT_FILE = 'protect.txt'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 从列表文件读取(每行一个相对路径,忽略空行与 # 注释)
|
|
24
|
+
* @param {string} filePath
|
|
25
|
+
* @returns {string[]}
|
|
26
|
+
*/
|
|
27
|
+
function readProtectListFromFile (filePath) {
|
|
28
|
+
const abs = path.resolve(filePath)
|
|
29
|
+
if (!fs.existsSync(abs)) {
|
|
30
|
+
throw new Error(`保护清单不存在: ${abs}`)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return fs.readFileSync(abs, 'utf8')
|
|
34
|
+
.split(/\r?\n/)
|
|
35
|
+
.map(line => line.trim())
|
|
36
|
+
.filter(line => line && !line.startsWith('#'))
|
|
37
|
+
.map(line => normalizeRelativePath(line))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 解析 --protect 等输入并合并 deploy/protect.txt
|
|
42
|
+
* @param {object} options
|
|
43
|
+
* @param {string} options.deployDir
|
|
44
|
+
* @param {string[]} [options.protectFiles]
|
|
45
|
+
* @param {string|null} [options.protectFile]
|
|
46
|
+
* @param {string} [options.envList]
|
|
47
|
+
* @param {string} [options.envFile]
|
|
48
|
+
* @returns {string[]}
|
|
49
|
+
*/
|
|
50
|
+
function resolveProtectFiles ({
|
|
51
|
+
deployDir,
|
|
52
|
+
protectFiles = [],
|
|
53
|
+
protectFile = null,
|
|
54
|
+
envList = process.env.PATCH_PROTECT,
|
|
55
|
+
envFile = process.env.PATCH_PROTECT_FILE
|
|
56
|
+
}) {
|
|
57
|
+
/** @type {string[]} */
|
|
58
|
+
const collected = []
|
|
59
|
+
|
|
60
|
+
const deployList = path.join(deployDir, DEPLOY_PROTECT_FILE)
|
|
61
|
+
if (fs.existsSync(deployList)) {
|
|
62
|
+
collected.push(...readProtectListFromFile(deployList))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for (const item of protectFiles) {
|
|
66
|
+
if (!item) continue
|
|
67
|
+
collected.push(...String(item).split(',').map(normalizeRelativePath).filter(Boolean))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (envList) {
|
|
71
|
+
collected.push(...String(envList).split(',').map(normalizeRelativePath).filter(Boolean))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const listFile = protectFile || envFile
|
|
75
|
+
if (listFile) {
|
|
76
|
+
collected.push(...readProtectListFromFile(listFile))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @type {string[]} */
|
|
80
|
+
const invalid = []
|
|
81
|
+
/** @type {Set<string>} */
|
|
82
|
+
const unique = new Set()
|
|
83
|
+
|
|
84
|
+
for (const rel of collected) {
|
|
85
|
+
if (!isSafeRelativePath(rel)) {
|
|
86
|
+
invalid.push(rel)
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
unique.add(rel.replace(/\\/g, '/'))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (invalid.length > 0) {
|
|
93
|
+
throw new Error(`保护清单含不安全路径(拒绝): ${invalid.join(', ')}`)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return [...unique].sort()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 将保护清单写入 patch 包根目录
|
|
101
|
+
* @param {string} patchDir
|
|
102
|
+
* @param {string[]} files
|
|
103
|
+
* @returns {boolean} 是否写入
|
|
104
|
+
*/
|
|
105
|
+
function writeProtectFile (patchDir, files) {
|
|
106
|
+
if (!files || files.length === 0) {
|
|
107
|
+
return false
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
fs.writeFileSync(
|
|
111
|
+
path.join(patchDir, PROTECT_FILE),
|
|
112
|
+
['# 受保护文件:线上已存在时不覆盖(每行一个相对路径)', ...files].join('\n'),
|
|
113
|
+
'utf8'
|
|
114
|
+
)
|
|
115
|
+
return true
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
module.exports = {
|
|
119
|
+
PROTECT_FILE,
|
|
120
|
+
DEPLOY_PROTECT_FILE,
|
|
121
|
+
readProtectListFromFile,
|
|
122
|
+
resolveProtectFiles,
|
|
123
|
+
writeProtectFile
|
|
124
|
+
}
|