@simonyea/holysheep-cli 1.7.99 → 1.7.101

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simonyea/holysheep-cli",
3
- "version": "1.7.99",
3
+ "version": "1.7.101",
4
4
  "description": "Claude Code/Cursor/Cline API relay for China — ¥1=$1, WeChat/Alipay payment, no credit card, no VPN. One command setup for all AI coding tools.",
5
5
  "keywords": [
6
6
  "openai-china",
@@ -18,7 +18,10 @@
18
18
  'use strict'
19
19
 
20
20
  const { writeEnvToShell, removeEnvFromShell, getShellRcFiles } = require('../utils/shell')
21
+ const { execSync } = require('child_process')
21
22
  const fs = require('fs')
23
+ const path = require('path')
24
+ const os = require('os')
22
25
 
23
26
  const MANAGED_KEYS = [
24
27
  'ANTHROPIC_API_KEY',
@@ -29,14 +32,55 @@ const MANAGED_KEYS = [
29
32
  ]
30
33
 
31
34
  const MARKER = '# >>> holysheep-cli managed >>>'
35
+ const STATE_FILE = path.join(os.homedir(), '.holysheep', 'env-config.json')
36
+
37
+ /**
38
+ * Windows: 用 PowerShell 写入用户级环境变量(比 setx 更可靠)
39
+ */
40
+ function winSetEnvVar(key, value) {
41
+ try {
42
+ execSync(
43
+ `powershell.exe -NoProfile -Command "[Environment]::SetEnvironmentVariable('${key}', '${value}', 'User')"`,
44
+ { stdio: 'ignore', timeout: 10000, windowsHide: true }
45
+ )
46
+ return true
47
+ } catch {
48
+ return false
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Windows: 用 PowerShell 删除用户级环境变量
54
+ */
55
+ function winRemoveEnvVar(key) {
56
+ try {
57
+ execSync(
58
+ `powershell.exe -NoProfile -Command "[Environment]::SetEnvironmentVariable('${key}', $null, 'User')"`,
59
+ { stdio: 'ignore', timeout: 10000, windowsHide: true }
60
+ )
61
+ } catch {}
62
+ }
63
+
64
+ /**
65
+ * Windows: 用 PowerShell 读取用户级环境变量
66
+ */
67
+ function winGetEnvVar(key) {
68
+ try {
69
+ const out = execSync(
70
+ `powershell.exe -NoProfile -Command "[Environment]::GetEnvironmentVariable('${key}', 'User')"`,
71
+ { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 5000, windowsHide: true }
72
+ )
73
+ const val = (out || '').trim()
74
+ return val || null
75
+ } catch {
76
+ return null
77
+ }
78
+ }
32
79
 
33
80
  function isConfiguredInShell() {
34
81
  if (process.platform === 'win32') {
35
- // Windows: 检查环境变量是否存在且包含 holysheep
36
- return !!(
37
- process.env.ANTHROPIC_BASE_URL?.includes('holysheep') ||
38
- process.env.OPENAI_BASE_URL?.includes('holysheep')
39
- )
82
+ const val = winGetEnvVar('ANTHROPIC_BASE_URL')
83
+ return !!(val && val.includes('holysheep'))
40
84
  }
41
85
 
42
86
  // Mac/Linux: 检查 shell rc 文件里是否有 managed 块
@@ -54,6 +98,18 @@ function isConfiguredInShell() {
54
98
  return false
55
99
  }
56
100
 
101
+ function saveState(envVars) {
102
+ try {
103
+ const dir = path.dirname(STATE_FILE)
104
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })
105
+ fs.writeFileSync(STATE_FILE, JSON.stringify({ configuredAt: new Date().toISOString(), keys: Object.keys(envVars) }, null, 2), 'utf8')
106
+ } catch {}
107
+ }
108
+
109
+ function clearState() {
110
+ try { fs.unlinkSync(STATE_FILE) } catch {}
111
+ }
112
+
57
113
  module.exports = {
58
114
  name: '全局环境变量',
59
115
  id: 'env-config',
@@ -75,19 +131,54 @@ module.exports = {
75
131
  OPENAI_BASE_URL: baseUrlOpenAI,
76
132
  }
77
133
 
78
- const written = writeEnvToShell(envVars)
134
+ let written
135
+ if (process.platform === 'win32') {
136
+ // Windows: 用 PowerShell 直接写用户级环境变量
137
+ written = []
138
+ for (const [k, v] of Object.entries(envVars)) {
139
+ if (winSetEnvVar(k, v)) {
140
+ written.push(k)
141
+ }
142
+ }
143
+ } else {
144
+ // Mac/Linux: 写入 shell rc 文件
145
+ written = writeEnvToShell(envVars)
146
+ }
147
+
148
+ saveState(envVars)
79
149
 
80
150
  return {
81
- file: written[0] || 'shell env',
151
+ file: written.join(', ') || 'shell env',
82
152
  hot: false,
83
153
  }
84
154
  },
85
155
 
86
156
  reset() {
87
- removeEnvFromShell(MANAGED_KEYS)
157
+ if (process.platform === 'win32') {
158
+ for (const k of MANAGED_KEYS) winRemoveEnvVar(k)
159
+ } else {
160
+ removeEnvFromShell(MANAGED_KEYS)
161
+ }
162
+ clearState()
88
163
  },
89
164
 
90
165
  getConfigPath() { return null },
166
+
167
+ /**
168
+ * 获取已配置的环境变量值(Windows 从注册表读,其他从 process.env 读)
169
+ */
170
+ getConfiguredValues() {
171
+ const result = {}
172
+ for (const key of MANAGED_KEYS) {
173
+ if (process.platform === 'win32') {
174
+ result[key] = winGetEnvVar(key) || null
175
+ } else {
176
+ result[key] = process.env[key] || null
177
+ }
178
+ }
179
+ return result
180
+ },
181
+
91
182
  hint: '配置后 VS Code Claude 扩展、Cursor、Continue、Cline、Windsurf 等工具均可直接使用',
92
183
  launchCmd: null,
93
184
  docsUrl: 'https://holysheep.ai',
@@ -702,8 +702,13 @@ const MARKER_START = '# >>> holysheep-cli managed >>>'
702
702
 
703
703
  function handleEnv(_req, res) {
704
704
  const vars = {}
705
+
706
+ // Windows: process.env 看不到 setx 写入的值,从注册表读
707
+ const envConfigTool = TOOLS.find(t => t.id === 'env-config')
708
+ const registryValues = (process.platform === 'win32' && envConfigTool?.getConfiguredValues?.()) || {}
709
+
705
710
  for (const k of HS_ENV_KEYS) {
706
- const v = process.env[k]
711
+ const v = process.env[k] || registryValues[k] || null
707
712
  vars[k] = v ? (k.includes('KEY') || k.includes('TOKEN') ? maskKey(v) : v) : null
708
713
  }
709
714