Skip to content

Commit a72b545

Browse files
committed
增强了环境变量设置功能
1 parent 73512c4 commit a72b545

1 file changed

Lines changed: 63 additions & 10 deletions

File tree

packages/core/src/shell/scripts/set-system-env.js

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,73 @@ const execute = Shell.execute
77

88
const executor = {
99
async windows (exec, { list }) {
10-
const cmds = []
10+
const psCmdsMachine = []
11+
const psCmdsUser = []
1112
for (const item of list) {
12-
// [Environment]::SetEnvironmentVariable('FOO', 'bar', 'Machine')
13-
cmds.push(`[Environment]::SetEnvironmentVariable('${item.key}', '${item.value}', 'Machine')`)
13+
const v = item.value == null ? '' : String(item.value)
14+
// escape single quotes for PowerShell single-quoted string
15+
const escaped = v.replace(/'/g, "''")
16+
psCmdsMachine.push(`[Environment]::SetEnvironmentVariable('${item.key}', '${escaped}', 'Machine')`)
17+
psCmdsUser.push(`[Environment]::SetEnvironmentVariable('${item.key}', '${escaped}', 'User')`)
1418
}
15-
const ret = await exec(cmds, { type: 'ps' })
1619

17-
const cmds2 = []
18-
for (const item of list) {
19-
// [Environment]::SetEnvironmentVariable('FOO', 'bar', 'Machine')
20-
cmds2.push(`set ${item.key}=""`)
20+
// Wrap whole flow and return structured result so caller can show detailed errors
21+
try {
22+
let ret
23+
let appliedScope = null
24+
try {
25+
ret = await exec(psCmdsMachine, { type: 'ps' })
26+
appliedScope = 'Machine:PowerShell'
27+
} catch (eMachine) {
28+
// try User scope via PowerShell
29+
try {
30+
ret = await exec(psCmdsUser, { type: 'ps' })
31+
appliedScope = 'User:PowerShell'
32+
} catch (eUser) {
33+
// PowerShell attempts failed; fallback to setx via cmd
34+
const cmdsSetxMachine = []
35+
const cmdsSetxUser = []
36+
for (const item of list) {
37+
const v = item.value == null ? '' : String(item.value)
38+
// basic escape for double quotes in cmd
39+
const escaped = v.replace(/"/g, '\\"')
40+
cmdsSetxMachine.push(`setx ${item.key} "${escaped}" /M`)
41+
cmdsSetxUser.push(`setx ${item.key} "${escaped}"`)
42+
}
43+
try {
44+
ret = await exec(cmdsSetxMachine, { type: 'cmd' })
45+
appliedScope = 'Machine:setx'
46+
} catch (eSetxMachine) {
47+
try {
48+
ret = await exec(cmdsSetxUser, { type: 'cmd' })
49+
appliedScope = 'User:setx'
50+
} catch (eSetxUser) {
51+
// all attempts failed — include all error messages
52+
const combined = [eMachine, eUser, eSetxMachine, eSetxUser].map(e => (e && e.message) || String(e)).join(' | ')
53+
return { success: false, error: 'Failed to set environment variables', details: combined }
54+
}
55+
}
56+
}
57+
}
58+
59+
// inject into current process so subsequent exec/child processes can inherit immediately
60+
let envUpdateError = null
61+
try {
62+
for (const item of list) {
63+
if (item.value == null) {
64+
delete process.env[item.key]
65+
} else {
66+
process.env[item.key] = String(item.value)
67+
}
68+
}
69+
} catch (e) {
70+
envUpdateError = e.message || String(e)
71+
}
72+
73+
return { success: true, scope: appliedScope, output: ret, envUpdateError }
74+
} catch (finalErr) {
75+
return { success: false, error: finalErr.message || String(finalErr), details: finalErr.stack }
2176
}
22-
await exec(cmds2, { type: 'cmd' })
23-
return ret
2477
},
2578
async linux (exec, { port }) {
2679
throw new Error('暂未实现此功能')

0 commit comments

Comments
 (0)