-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.ts
More file actions
46 lines (39 loc) · 1.11 KB
/
env.ts
File metadata and controls
46 lines (39 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import EnvPaths from "env-paths";
import fs from "fs";
import path from "path";
// 全局配置结构,可以按需扩展
export interface GlobalConfig {
authToken?: string;
userAgent?: string;
}
const paths = EnvPaths("arenapro-cli");
const configDir = paths.config;
const configFile = path.join(configDir, "auth.json");
export function loadGlobalConfig(): GlobalConfig {
try {
if (!fs.existsSync(configFile)) {
return {};
}
const raw = fs.readFileSync(configFile, "utf8");
const parsed = JSON.parse(raw) as GlobalConfig;
return parsed ?? {};
} catch {
return {};
}
}
export function saveGlobalConfig(partial: GlobalConfig): void {
try {
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
const current = loadGlobalConfig();
const next: GlobalConfig = {
...current,
...partial,
};
fs.writeFileSync(configFile, JSON.stringify(next, null, 2), "utf8");
} catch (err) {
// 全局配置写入失败不应阻断主流程,这里静默失败或简单输出即可
console.error("写入配置失败:", err);
}
}