@aronnaxlin/bgm-cli 1.0.0 → 1.1.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.
@@ -23,13 +23,34 @@ const DEV_ENV_FILE = path.join(REPO_ROOT, "bgm-dev.env");
23
23
 
24
24
  const DEFAULT_CONFIG = {
25
25
  appName: "bgm-cli",
26
- appVersion: "1.0.0",
26
+ appVersion: "1.0.2",
27
27
  homepageLink: "https://github.com/aronnaxlin/bgm-cli",
28
28
  developerId: "aronnaxlin",
29
29
  oauthServerBaseUrl: "https://oauth-backend-jet.vercel.app",
30
30
  timezone: "Asia/Shanghai",
31
31
  };
32
32
 
33
+ export const AUTH_CONFIG_KEYS = [
34
+ "accessToken",
35
+ "refreshToken",
36
+ "tokenType",
37
+ "privateSessionId",
38
+ "privateSessionUpdatedAt",
39
+ "clientId",
40
+ "clientSecret",
41
+ "redirectUri",
42
+ ];
43
+
44
+ let profileOverrideName = null;
45
+
46
+ export function setProfileOverride(name) {
47
+ profileOverrideName = typeof name === "string" && name.trim() !== "" ? name.trim() : null;
48
+ }
49
+
50
+ export function getProfileOverride() {
51
+ return profileOverrideName;
52
+ }
53
+
33
54
  const ENV_TO_KEY = {
34
55
  BGM_ACCESS_TOKEN: "accessToken",
35
56
  BGM_REFRESH_TOKEN: "refreshToken",
@@ -55,7 +76,7 @@ export function getConfigSourceFilePath() {
55
76
  }
56
77
 
57
78
  export function getConfig() {
58
- const runtimeConfig = getActiveRuntimeConfigMeta().config;
79
+ const { profiles, ...runtimeConfig } = getActiveRuntimeConfigMeta().config;
59
80
  const devConfig = readEnvConfigFileSafe(DEV_ENV_FILE);
60
81
  const envConfig = {};
61
82
 
@@ -66,12 +87,29 @@ export function getConfig() {
66
87
  }
67
88
  }
68
89
 
69
- const merged = {
70
- ...DEFAULT_CONFIG,
71
- ...devConfig,
72
- ...runtimeConfig,
73
- ...envConfig,
74
- };
90
+ let overlay = null;
91
+ if (profileOverrideName) {
92
+ const snapshot = isPlainObject(profiles) ? profiles[profileOverrideName] : undefined;
93
+ if (!isPlainObject(snapshot)) {
94
+ throw new ConfigError(`Profile not found: ${profileOverrideName}`);
95
+ }
96
+ overlay = pickAuthConfigKeys(snapshot);
97
+ }
98
+
99
+ const merged = overlay
100
+ ? {
101
+ ...DEFAULT_CONFIG,
102
+ ...omitAuthConfigKeys(devConfig),
103
+ ...omitAuthConfigKeys(runtimeConfig),
104
+ ...overlay,
105
+ ...envConfig,
106
+ }
107
+ : {
108
+ ...DEFAULT_CONFIG,
109
+ ...devConfig,
110
+ ...runtimeConfig,
111
+ ...envConfig,
112
+ };
75
113
 
76
114
  return {
77
115
  ...merged,
@@ -181,6 +219,22 @@ export async function clearConfigValues(keys) {
181
219
  await writeFile(configFile, `${JSON.stringify(current, null, 2)}\n`, "utf8");
182
220
  }
183
221
 
222
+ export async function replaceConfigValues({ set = {}, remove = [] } = {}) {
223
+ const runtimeMeta = getActiveRuntimeConfigMeta();
224
+ const configFile = runtimeMeta.writeFile;
225
+ const configDir = path.dirname(configFile);
226
+ const current = { ...runtimeMeta.config };
227
+
228
+ for (const key of remove) {
229
+ delete current[key];
230
+ }
231
+
232
+ const next = { ...current, ...set };
233
+
234
+ await mkdir(configDir, { recursive: true });
235
+ await writeFile(configFile, `${JSON.stringify(next, null, 2)}\n`, "utf8");
236
+ }
237
+
184
238
  export async function readConfig() {
185
239
  return getActiveRuntimeConfigMeta().config;
186
240
  }
@@ -224,12 +278,50 @@ export async function enableGlobalConfigMode() {
224
278
  }
225
279
 
226
280
  function getActiveRuntimeConfigMeta() {
281
+ if (hasConfigDirOverride()) {
282
+ const userState = readJsonFileState(USER_CONFIG_FILE);
283
+ if (userState.exists && !userState.ok) {
284
+ throw new ConfigError(`Failed to parse global config file: ${USER_CONFIG_FILE}`);
285
+ }
286
+ return {
287
+ config: userState.value,
288
+ sourceFile: USER_CONFIG_FILE,
289
+ writeFile: USER_CONFIG_FILE,
290
+ };
291
+ }
227
292
  if (isGlobalInstallEnabled() || isPackageInstall()) {
228
293
  return getGlobalRuntimeConfigMeta();
229
294
  }
230
295
  return getProjectRuntimeConfigMeta();
231
296
  }
232
297
 
298
+ function hasConfigDirOverride() {
299
+ const value = process.env.BGM_CONFIG_DIR;
300
+ return typeof value === "string" && value.trim() !== "";
301
+ }
302
+
303
+ function pickAuthConfigKeys(source) {
304
+ const result = {};
305
+ for (const key of AUTH_CONFIG_KEYS) {
306
+ if (source[key] !== undefined) {
307
+ result[key] = source[key];
308
+ }
309
+ }
310
+ return result;
311
+ }
312
+
313
+ function omitAuthConfigKeys(source) {
314
+ const result = { ...source };
315
+ for (const key of AUTH_CONFIG_KEYS) {
316
+ delete result[key];
317
+ }
318
+ return result;
319
+ }
320
+
321
+ function isPlainObject(value) {
322
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
323
+ }
324
+
233
325
  function getGlobalRuntimeConfigMeta() {
234
326
  const userState = readJsonFileState(USER_CONFIG_FILE);
235
327
  if (userState.exists) {
@@ -291,6 +383,11 @@ function isPackageInstall() {
291
383
  }
292
384
 
293
385
  function resolveUserConfigDir() {
386
+ const overrideDir = process.env.BGM_CONFIG_DIR;
387
+ if (typeof overrideDir === "string" && overrideDir.trim() !== "") {
388
+ return path.resolve(overrideDir.trim());
389
+ }
390
+
294
391
  if (process.platform === "win32" && process.env.APPDATA) {
295
392
  return path.join(process.env.APPDATA, "bgm-cli");
296
393
  }