-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.ts
More file actions
182 lines (159 loc) · 4.92 KB
/
Copy pathconfig.ts
File metadata and controls
182 lines (159 loc) · 4.92 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import type { GdbConfig, GdbConfigFile } from "./types.js";
export function getConfigDir(): string {
return process.env.GEONIC_CONFIG_DIR ?? join(homedir(), ".config", "geonic");
}
function getConfigFile(): string {
return join(getConfigDir(), "config.json");
}
export function ensureConfigDir(): void {
const dir = getConfigDir();
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
}
function migrateV1ToV2(data: Record<string, unknown>): GdbConfigFile {
const profile: GdbConfig = {};
const knownKeys = [
"url",
"service",
"tenantId",
"token",
"refreshToken",
"format",
"apiKey",
];
for (const key of knownKeys) {
if (key in data) {
(profile as Record<string, unknown>)[key] = data[key];
}
}
return {
version: 2,
currentProfile: "default",
profiles: { default: profile },
};
}
function isGdbConfigFile(value: unknown): value is GdbConfigFile {
/* v8 ignore next -- loadConfigFile always passes an object with version key */
if (typeof value !== "object" || value === null) return false;
const v = value as Record<string, unknown>;
return (
v.version === 2 &&
typeof v.currentProfile === "string" &&
typeof v.profiles === "object" &&
v.profiles !== null
);
}
function defaultConfig(): GdbConfigFile {
return { version: 2, currentProfile: "default", profiles: { default: {} } };
}
export function loadConfigFile(): GdbConfigFile {
try {
const raw = readFileSync(getConfigFile(), "utf-8");
const data = JSON.parse(raw) as Record<string, unknown>;
if (!("version" in data)) {
const migrated = migrateV1ToV2(data);
saveConfigFile(migrated);
return migrated;
}
if (!isGdbConfigFile(data)) {
return defaultConfig();
}
return data;
} catch {
return defaultConfig();
}
}
export function saveConfigFile(configFile: GdbConfigFile): void {
ensureConfigDir();
writeFileSync(getConfigFile(), JSON.stringify(configFile, null, 2) + "\n", "utf-8");
}
export function loadConfig(profileName?: string): GdbConfig {
const configFile = loadConfigFile();
const name = profileName ?? configFile.currentProfile;
return configFile.profiles[name] ?? {};
}
export function saveConfig(config: GdbConfig, profileName?: string): void {
const configFile = loadConfigFile();
const name = profileName ?? configFile.currentProfile;
configFile.profiles[name] = config;
saveConfigFile(configFile);
}
export function getConfigValue(key: string, profileName?: string): unknown {
const config = loadConfig(profileName);
return config[key as keyof GdbConfig];
}
export function validateUrl(url: string): string {
url = url.trim();
if (!url) {
throw new Error("URL must not be empty.");
}
if (!/^https?:\/\//i.test(url)) {
throw new Error(`Invalid URL: "${url}". URL must start with http:// or https://.`);
}
try {
new URL(url);
} catch {
throw new Error(`Invalid URL: "${url}".`);
}
return url.replace(/\/+$/, "") + "/";
}
export function setConfigValue(key: string, value: string, profileName?: string): void {
const config = loadConfig(profileName);
if (key === "url") {
value = validateUrl(value);
}
(config as Record<string, unknown>)[key] = value;
saveConfig(config, profileName);
}
export function deleteConfigValue(key: string, profileName?: string): void {
const config = loadConfig(profileName);
delete (config as Record<string, unknown>)[key];
saveConfig(config, profileName);
}
export function getConfigPath(): string {
return getConfigFile();
}
export function listProfiles(): { name: string; active: boolean }[] {
const configFile = loadConfigFile();
return Object.keys(configFile.profiles).map((name) => ({
name,
active: name === configFile.currentProfile,
}));
}
export function getCurrentProfile(): string {
return loadConfigFile().currentProfile;
}
export function setCurrentProfile(name: string): void {
const configFile = loadConfigFile();
if (!(name in configFile.profiles)) {
throw new Error(`Profile "${name}" does not exist.`);
}
configFile.currentProfile = name;
saveConfigFile(configFile);
}
export function createProfile(name: string): void {
const configFile = loadConfigFile();
if (name in configFile.profiles) {
throw new Error(`Profile "${name}" already exists.`);
}
configFile.profiles[name] = {};
saveConfigFile(configFile);
}
export function deleteProfile(name: string): void {
if (name === "default") {
throw new Error('Cannot delete the "default" profile.');
}
const configFile = loadConfigFile();
if (!(name in configFile.profiles)) {
throw new Error(`Profile "${name}" does not exist.`);
}
delete configFile.profiles[name];
if (configFile.currentProfile === name) {
configFile.currentProfile = "default";
}
saveConfigFile(configFile);
}