Skip to content

Commit 91f06d4

Browse files
committed
fix(scripts): 改进 pnpm 执行方式以支持不同环境
检测 npm_execpath 环境变量以判断是否通过 node 运行 pnpm,并统一封装 pnpm 调用逻辑。避免在 Windows 和类 Unix 系统上直接调用 pnpm 命令时可能出现的兼容性问题。
1 parent dab8867 commit 91f06d4

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

scripts/qa.mjs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class CommandFailedError extends Error {
1414
}
1515
}
1616

17+
const pnpmExecPath = process.env.npm_execpath
18+
const runPnpmWithNode =
19+
typeof pnpmExecPath === 'string' && pnpmExecPath.toLowerCase().includes('pnpm')
20+
1721
const args = process.argv.slice(2)
1822
let mode = 'changed'
1923
let verbose = false
@@ -68,6 +72,16 @@ function runCommand(step, command, args, options = {}) {
6872
}
6973
}
7074

75+
function runPnpm(step, pnpmArgs) {
76+
if (runPnpmWithNode) {
77+
runCommand(step, process.execPath, [pnpmExecPath, ...pnpmArgs])
78+
return
79+
}
80+
81+
const command = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm'
82+
runCommand(step, command, pnpmArgs)
83+
}
84+
7185
function runCapture(command, args) {
7286
if (verbose) {
7387
console.log(`[qa:verbose] inspect: ${command} ${args.join(' ')}`)
@@ -164,18 +178,18 @@ function runWorkspaceQa(modeValue, sinceRef) {
164178

165179
if (modeValue === 'all') {
166180
console.log('[qa] run workspace qa (all)')
167-
runCommand('workspace-qa-all', 'pnpm', recursiveArgs)
181+
runPnpm('workspace-qa-all', recursiveArgs)
168182
return
169183
}
170184

171185
if (!sinceRef) {
172186
console.log('[qa] no base ref found, fallback to workspace qa (all)')
173-
runCommand('workspace-qa-fallback-all', 'pnpm', recursiveArgs)
187+
runPnpm('workspace-qa-fallback-all', recursiveArgs)
174188
return
175189
}
176190

177191
console.log(`[qa] run workspace qa (changed since ${sinceRef})`)
178-
runCommand('workspace-qa-changed', 'pnpm', [
192+
runPnpm('workspace-qa-changed', [
179193
'--filter',
180194
`[${sinceRef}]`,
181195
...recursiveArgs,
@@ -185,7 +199,7 @@ function runWorkspaceQa(modeValue, sinceRef) {
185199
function runRootPwshQa(modeValue, sinceRef) {
186200
if (modeValue === 'all') {
187201
console.log('[qa] run root qa:pwsh (all)')
188-
runCommand('root-qa-pwsh-all', 'pnpm', ['run', 'qa:pwsh'])
202+
runPnpm('root-qa-pwsh-all', ['run', 'qa:pwsh'])
189203
return
190204
}
191205

@@ -204,7 +218,7 @@ function runRootPwshQa(modeValue, sinceRef) {
204218
}
205219

206220
console.log('[qa] run root qa:pwsh (changed)')
207-
runCommand('root-qa-pwsh-changed', 'pnpm', ['run', 'qa:pwsh'])
221+
runPnpm('root-qa-pwsh-changed', ['run', 'qa:pwsh'])
208222
}
209223

210224
const sinceRef = mode === 'changed' ? resolveSinceRef() : null
@@ -238,4 +252,4 @@ try {
238252
}
239253

240254
throw error
241-
}
255+
}

0 commit comments

Comments
 (0)