@simonyea/holysheep-cli 1.2.1 → 1.2.2
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/package.json +1 -1
- package/src/utils/shell.js +37 -5
package/package.json
CHANGED
package/src/utils/shell.js
CHANGED
|
@@ -41,6 +41,25 @@ function removeHsBlock(content) {
|
|
|
41
41
|
return content.replace(re, '')
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* 移除 rc 文件里用户手动写的同名 export/set -gx 行
|
|
46
|
+
* 防止旧值在 holysheep-cli managed 块之后覆盖新值
|
|
47
|
+
*/
|
|
48
|
+
function removeStaleExports(content, keys, isFish = false) {
|
|
49
|
+
let result = content
|
|
50
|
+
for (const key of keys) {
|
|
51
|
+
if (isFish) {
|
|
52
|
+
// fish: set -gx KEY "..." 或 set -gx KEY ...
|
|
53
|
+
result = result.replace(new RegExp(`\\n?set\\s+-gx\\s+${escapeRegex(key)}\\s+[^\\n]*\\n?`, 'g'), '\n')
|
|
54
|
+
} else {
|
|
55
|
+
// bash/zsh: export KEY="..." 或 export KEY=...
|
|
56
|
+
result = result.replace(new RegExp(`\\n?export\\s+${escapeRegex(key)}=[^\\n]*\\n?`, 'g'), '\n')
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// 清理多余空行
|
|
60
|
+
return result.replace(/\n{3,}/g, '\n\n')
|
|
61
|
+
}
|
|
62
|
+
|
|
44
63
|
function buildEnvBlock(envVars, isFish = false) {
|
|
45
64
|
const lines = [MARKER_START]
|
|
46
65
|
for (const [k, v] of Object.entries(envVars)) {
|
|
@@ -75,8 +94,12 @@ function writeEnvToShell(envVars) {
|
|
|
75
94
|
for (const file of files) {
|
|
76
95
|
let content = ''
|
|
77
96
|
try { content = fs.readFileSync(file, 'utf8') } catch {}
|
|
78
|
-
content = removeHsBlock(content)
|
|
79
97
|
const isFish = file.endsWith('config.fish')
|
|
98
|
+
// 1. 清理旧的 holysheep managed 块
|
|
99
|
+
content = removeHsBlock(content)
|
|
100
|
+
// 2. 清理用户手动写的同名 export(防止旧值覆盖新值)
|
|
101
|
+
content = removeStaleExports(content, Object.keys(envVars), isFish)
|
|
102
|
+
// 3. 追加新的 managed 块
|
|
80
103
|
content += buildEnvBlock(envVars, isFish)
|
|
81
104
|
fs.writeFileSync(file, content, 'utf8')
|
|
82
105
|
written.push(file)
|
|
@@ -84,15 +107,24 @@ function writeEnvToShell(envVars) {
|
|
|
84
107
|
return written
|
|
85
108
|
}
|
|
86
109
|
|
|
87
|
-
function removeEnvFromShell() {
|
|
110
|
+
function removeEnvFromShell(extraKeys = []) {
|
|
111
|
+
// 默认清理的 key 列表(holysheep 相关的所有环境变量)
|
|
112
|
+
const HS_KEYS = [
|
|
113
|
+
'ANTHROPIC_AUTH_TOKEN', 'ANTHROPIC_API_KEY', 'ANTHROPIC_BASE_URL',
|
|
114
|
+
'OPENAI_API_KEY', 'OPENAI_BASE_URL',
|
|
115
|
+
'HOLYSHEEP_API_KEY',
|
|
116
|
+
...extraKeys,
|
|
117
|
+
]
|
|
88
118
|
const files = getShellRcFiles()
|
|
89
119
|
const cleaned = []
|
|
90
120
|
for (const file of files) {
|
|
91
121
|
if (!fs.existsSync(file)) continue
|
|
122
|
+
const isFish = file.endsWith('config.fish')
|
|
92
123
|
let content = fs.readFileSync(file, 'utf8')
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
124
|
+
let updated = removeHsBlock(content)
|
|
125
|
+
updated = removeStaleExports(updated, HS_KEYS, isFish)
|
|
126
|
+
if (updated !== content) {
|
|
127
|
+
fs.writeFileSync(file, updated, 'utf8')
|
|
96
128
|
cleaned.push(file)
|
|
97
129
|
}
|
|
98
130
|
}
|