1. 环境变量读取秘钥
PART1: 如何在powershell中读取环境变量
- 读取范围: 无论①用户环境 ②系统环境 变量都可以读取到
- 更新延迟: 注意idea要重启, 才能(在idea代码中)读取到
![]() |
|---|
[Environment]::GetEnvironmentVariable("FEISHU_APPID","User")
$env:FEISHU_APPSECRET
PART2: 如何在TS中读取环境变量
这个也是TS中运行powershell的方法
private readWindowsEnvironmentVariable(variableName: string, target: 'User' | 'Machine') {
const readCommand = `[Environment]::GetEnvironmentVariable('${variableName}','${target}')`
try {
const variableValue = execFileSync('powershell.exe', ['-NoProfile', '-Command', readCommand], {
encoding: 'utf8',
windowsHide: true
}).trim() // 每次读取都实时从 Windows 当前配置取值,保证用户刚修改环境变量后无需依赖 process.env 刷新
return variableValue
} catch (error) {
// 把读取失败的目标层级一并打印出来,便于区分是 User 还是 Machine 读取失败
console.error(`[feishu] Failed to read Windows ${target} env ${variableName}:`, error)
return ''
}
}
private readFeiShuCredential(variableName: 'FEISHU_APPID' | 'FEISHU_APPSECRET') {
const userVariableValue = this.readWindowsEnvironmentVariable(variableName, 'User')
if (userVariableValue) return userVariableValue
const machineVariableValue = this.readWindowsEnvironmentVariable(variableName, 'Machine')
return machineVariableValue
}
- Title:
- Author: 明廷盛
- Created at : 2026-05-22 04:55:19
- Updated at : 2026-05-22 04:55:19
- Link: https://blog.20040424.xyz/2026/05/22/😼Java全栈工程师/7. 前端部分/5.Electron开发零碎技术/
- License: All Rights Reserved © 明廷盛
