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.
@@ -20,6 +20,7 @@ const REGISTRY_FILE = 'versions.json'
20
20
  * @property {string} patchDir
21
21
  * @property {string} [zipPath]
22
22
  * @property {boolean} [partial] 是否为子集提取包
23
+ * @property {boolean} [worktree] 是否为工作副本包
23
24
  * @property {string} [sourceVersion] 子集提取时的源版本
24
25
  * @property {string} createdAt ISO 时间
25
26
  */
@@ -37,6 +38,34 @@ function writeJsonAtomic (filePath, data) {
37
38
  fs.renameSync(tmpPath, filePath)
38
39
  }
39
40
 
41
+ /**
42
+ * 将绝对路径登记为相对 deployDir 的路径,保证项目迁移后仍可用
43
+ * @param {string} deployDir
44
+ * @param {string} absPath
45
+ * @returns {string}
46
+ */
47
+ function toDeployRelative (deployDir, absPath) {
48
+ if (!absPath) return absPath
49
+ if (!path.isAbsolute(absPath)) return absPath.replace(/\\/g, '/')
50
+
51
+ const rel = path.relative(deployDir, absPath)
52
+ if (rel === '' || rel.startsWith('..') || path.isAbsolute(rel)) {
53
+ return absPath
54
+ }
55
+ return rel.split(path.sep).join('/')
56
+ }
57
+
58
+ /**
59
+ * 兼容旧记录:相对路径按 deployDir 解析
60
+ * @param {string} deployDir
61
+ * @param {string} value
62
+ * @returns {string}
63
+ */
64
+ function fromDeployRelative (deployDir, value) {
65
+ if (!value || path.isAbsolute(value)) return value
66
+ return path.resolve(deployDir, value)
67
+ }
68
+
40
69
  /**
41
70
  * @param {string} deployDir
42
71
  * @returns {{ versions: VersionRecord[] }}
@@ -50,6 +79,11 @@ function readVersionsRegistry (deployDir) {
50
79
  try {
51
80
  const data = JSON.parse(fs.readFileSync(registryPath, 'utf8'))
52
81
  if (Array.isArray(data.versions)) {
82
+ for (const record of data.versions) {
83
+ if (!record || typeof record !== 'object') continue
84
+ if (record.patchDir) record.patchDir = fromDeployRelative(deployDir, record.patchDir)
85
+ if (record.zipPath) record.zipPath = fromDeployRelative(deployDir, record.zipPath)
86
+ }
53
87
  return /** @type {{ versions: VersionRecord[] }} */ (data)
54
88
  }
55
89
  console.warn(`[vcs-patch-deploy] ${REGISTRY_FILE} 格式异常(缺少 versions 数组),已忽略`)
@@ -60,6 +94,21 @@ function readVersionsRegistry (deployDir) {
60
94
  return { versions: [] }
61
95
  }
62
96
 
97
+ /**
98
+ * 写入前统一转成 deployDir 相对路径(读取时会解析回绝对路径)
99
+ * @param {string} deployDir
100
+ * @param {VersionRecord[]} versions
101
+ * @returns {VersionRecord[]}
102
+ */
103
+ function normalizeRegistryForWrite (deployDir, versions) {
104
+ for (const record of versions) {
105
+ if (!record || typeof record !== 'object') continue
106
+ if (record.patchDir) record.patchDir = toDeployRelative(deployDir, record.patchDir)
107
+ if (record.zipPath) record.zipPath = toDeployRelative(deployDir, record.zipPath)
108
+ }
109
+ return versions
110
+ }
111
+
63
112
  /**
64
113
  * @param {string} deployDir
65
114
  * @param {VersionRecord} record
@@ -67,32 +116,61 @@ function readVersionsRegistry (deployDir) {
67
116
  function registerVersion (deployDir, record) {
68
117
  const registry = readVersionsRegistry(deployDir)
69
118
  const registryPath = path.join(deployDir, REGISTRY_FILE)
119
+ const normalized = { ...record }
120
+ if (normalized.patchDir) {
121
+ normalized.patchDir = toDeployRelative(deployDir, normalized.patchDir)
122
+ }
123
+ if (normalized.zipPath) {
124
+ normalized.zipPath = toDeployRelative(deployDir, normalized.zipPath)
125
+ }
70
126
 
71
- if (record.partial) {
127
+ if (normalized.partial) {
72
128
  // 子集包:同 version+sourceVersion 去重后追加
73
129
  const dupIdx = registry.versions.findIndex(v =>
74
130
  v.partial &&
75
- v.version === record.version &&
76
- v.sourceVersion === record.sourceVersion
131
+ v.version === normalized.version &&
132
+ v.sourceVersion === normalized.sourceVersion
77
133
  )
78
134
  if (dupIdx >= 0) {
79
- registry.versions[dupIdx] = record
135
+ registry.versions[dupIdx] = normalized
80
136
  } else {
81
- registry.versions.push(record)
137
+ registry.versions.push(normalized)
82
138
  }
83
139
  } else {
84
- const idx = registry.versions.findIndex(v => v.version === record.version && !v.partial)
140
+ const idx = registry.versions.findIndex(v => v.version === normalized.version && !v.partial)
85
141
  if (idx >= 0) {
86
- registry.versions[idx] = record
142
+ registry.versions[idx] = normalized
87
143
  } else {
88
- registry.versions.push(record)
144
+ registry.versions.push(normalized)
89
145
  }
90
146
  }
91
147
 
92
148
  registry.versions.sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1))
149
+ normalizeRegistryForWrite(deployDir, registry.versions)
93
150
  writeJsonAtomic(registryPath, registry)
94
151
  }
95
152
 
153
+ /**
154
+ * 清理旧的工作副本包登记(可按需重建,避免 versions.json 无限增长)
155
+ * @param {string} deployDir
156
+ * @param {number} [keep=5]
157
+ * @returns {number} 删除的记录数
158
+ */
159
+ function pruneWorktreeRecords (deployDir, keep = 5) {
160
+ const registry = readVersionsRegistry(deployDir)
161
+ const worktrees = registry.versions.filter(v => v.worktree)
162
+ if (worktrees.length <= keep) {
163
+ return 0
164
+ }
165
+
166
+ const retained = new Set(worktrees.slice(0, keep))
167
+ const removed = worktrees.slice(keep)
168
+ registry.versions = registry.versions.filter(v => !v.worktree || retained.has(v))
169
+ normalizeRegistryForWrite(deployDir, registry.versions)
170
+ writeJsonAtomic(path.join(deployDir, REGISTRY_FILE), registry)
171
+ return removed.length
172
+ }
173
+
96
174
  /**
97
175
  * @param {string} deployDir
98
176
  * @param {string} version
@@ -131,6 +209,7 @@ module.exports = {
131
209
  REGISTRY_FILE,
132
210
  readVersionsRegistry,
133
211
  registerVersion,
212
+ pruneWorktreeRecords,
134
213
  findVersionRecord,
135
214
  listVersionRecords,
136
215
  writeJsonAtomic
package/lib/zip.js CHANGED
@@ -1,39 +1,157 @@
1
1
  /**
2
2
  * 将 patch 目录压缩为 zip
3
+ *
4
+ * 逐条写入(而不是 archive.directory),以便:
5
+ * - 保留 Unix 执行位(Windows 上 fs.stat 不体现 exec 位,需由 VCS 元数据提供)
6
+ * - 固定文件/目录权限,避免不同平台产物差异
7
+ * - 跳过符号链接,目录条目显式写入
3
8
  */
4
9
 
5
10
  const fs = require('fs')
6
11
  const path = require('path')
7
12
  const archiver = require('archiver')
8
13
 
14
+ const DEFAULT_FILE_MODE = 0o644
15
+ const DEFAULT_DIR_MODE = 0o755
16
+
9
17
  /**
10
- * @param {string} sourceDir patch 目录
11
- * @param {string} zipPath 输出 zip 路径
18
+ * @param {object} [options]
19
+ * @param {Record<string, number>} [options.modes] 相对路径 -> 权限(如 { 'app/run.sh': 0o755 })
20
+ * @param {number} [options.fileMode] 未在 modes 中指定时的默认文件权限
21
+ * @param {number} [options.dirMode] 目录权限
12
22
  * @returns {Promise<{ zipPath: string, size: number }>}
13
23
  */
14
- function createZipFromDirectory (sourceDir, zipPath) {
24
+ function createZipFromDirectory (sourceDir, zipPath, options = {}) {
15
25
  const level = Number(process.env.PATCH_ZIP_LEVEL ?? 6)
16
26
  const zipLevel = Number.isFinite(level) ? Math.min(9, Math.max(0, level)) : 6
27
+ const modes = options.modes || {}
28
+ const fileMode = options.fileMode ?? DEFAULT_FILE_MODE
29
+ const dirMode = options.dirMode ?? DEFAULT_DIR_MODE
17
30
 
18
31
  return new Promise((resolve, reject) => {
19
32
  fs.mkdirSync(path.dirname(zipPath), { recursive: true })
20
33
 
21
- const output = fs.createWriteStream(zipPath)
22
34
  const archive = archiver('zip', { zlib: { level: zipLevel } })
23
35
 
36
+ // 先收集条目:源目录不可读等错误会在创建临时文件前抛出
37
+ try {
38
+ addDirectory(archive, sourceDir, '', { modes, fileMode, dirMode })
39
+ } catch (error) {
40
+ reject(/** @type {Error} */ (error))
41
+ return
42
+ }
43
+
44
+ // 先写临时文件,成功后再替换:失败时不会留下截断的 zip,也不会提前销毁旧产物
45
+ const tmpPath = `${zipPath}.${process.pid}.${Date.now()}.tmp`
46
+ const output = fs.createWriteStream(tmpPath)
47
+ let settled = false
48
+
49
+ /** @param {Error} error */
50
+ const fail = (error) => {
51
+ if (settled) return
52
+ settled = true
53
+ try {
54
+ archive.abort()
55
+ } catch {
56
+ // ignore
57
+ }
58
+ try {
59
+ output.destroy()
60
+ } catch {
61
+ // ignore
62
+ }
63
+ const cleanup = () => {
64
+ try {
65
+ fs.rmSync(tmpPath, { force: true })
66
+ } catch {
67
+ // ignore
68
+ }
69
+ }
70
+ if (output.closed) {
71
+ cleanup()
72
+ } else {
73
+ output.once('close', cleanup)
74
+ }
75
+ reject(error)
76
+ }
77
+
24
78
  output.on('close', () => {
79
+ if (settled) return
80
+ try {
81
+ try {
82
+ fs.renameSync(tmpPath, zipPath)
83
+ } catch {
84
+ // Windows 上目标已存在时 rename 可能失败:先移除旧文件再重试
85
+ fs.rmSync(zipPath, { force: true })
86
+ fs.renameSync(tmpPath, zipPath)
87
+ }
88
+ } catch (error) {
89
+ fail(error)
90
+ return
91
+ }
92
+ settled = true
25
93
  resolve({ zipPath, size: archive.pointer() })
26
94
  })
27
95
 
28
- archive.on('error', reject)
29
- output.on('error', reject)
96
+ archive.on('error', fail)
97
+ output.on('error', fail)
30
98
 
31
99
  archive.pipe(output)
32
- archive.directory(sourceDir, false)
33
- archive.finalize()
100
+ archive.finalize().catch(fail)
34
101
  })
35
102
  }
36
103
 
104
+ /**
105
+ * 递归写入目录(排序保证产物稳定;跳过符号链接)
106
+ * @param {import('archiver')} archive
107
+ * @param {string} absDir
108
+ * @param {string} relDir
109
+ * @param {{ modes: Record<string, number>, fileMode: number, dirMode: number }} options
110
+ */
111
+ function addDirectory (archive, absDir, relDir, options) {
112
+ const names = fs.readdirSync(absDir).sort()
113
+ if (relDir !== '') {
114
+ archive.append(null, {
115
+ name: `${relDir}/`,
116
+ type: 'directory',
117
+ mode: options.dirMode
118
+ })
119
+ }
120
+
121
+ for (const name of names) {
122
+ const abs = path.join(absDir, name)
123
+ const rel = relDir ? `${relDir}/${name}` : name
124
+
125
+ let stat
126
+ try {
127
+ stat = fs.lstatSync(abs)
128
+ } catch {
129
+ continue
130
+ }
131
+
132
+ // 符号链接一律跳过(服务器端同样拒绝)
133
+ if (stat.isSymbolicLink()) {
134
+ continue
135
+ }
136
+
137
+ if (stat.isDirectory()) {
138
+ addDirectory(archive, abs, rel, options)
139
+ continue
140
+ }
141
+
142
+ if (!stat.isFile()) {
143
+ continue
144
+ }
145
+
146
+ archive.file(abs, {
147
+ name: rel,
148
+ mode: options.modes[rel] ?? options.fileMode
149
+ })
150
+ }
151
+ }
152
+
37
153
  module.exports = {
154
+ DEFAULT_FILE_MODE,
155
+ DEFAULT_DIR_MODE,
38
156
  createZipFromDirectory
39
157
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vcs-patch-deploy",
3
- "version": "1.1.1",
3
+ "version": "1.3.0",
4
4
  "description": "PHP 后端(ThinkPHP)增量部署包生成工具 — 支持 SVN revision 与 Git commit 差异对比,打 zip 并附带 apply-update.php,供堡垒机/Web 终端上线",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
@@ -8,7 +8,10 @@
8
8
  "patch-extract": "bin/patch-extract.js"
9
9
  },
10
10
  "scripts": {
11
+ "check": "node scripts/check-syntax.js",
12
+ "pretest": "node scripts/check-syntax.js",
11
13
  "test": "node --test test/*.test.js && node bin/patch-build.js --help",
14
+ "test:php": "bash test/php-smoke.sh",
12
15
  "prepublishOnly": "node -e \"require('./lib/index.js')\""
13
16
  },
14
17
  "keywords": [
@@ -29,7 +32,7 @@
29
32
  "author": "qingyuai",
30
33
  "license": "MIT",
31
34
  "engines": {
32
- "node": ">=16.0.0"
35
+ "node": ">=18.0.0"
33
36
  },
34
37
  "dependencies": {
35
38
  "archiver": "^7.0.1",