@aronnaxlin/bgm-cli 1.0.0 → 1.1.0
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/README.en.md +39 -4
- package/README.md +39 -4
- package/README.zh-TW.md +39 -4
- package/package.json +1 -1
- package/src/cli.js +186 -17
- package/src/commands/book.js +156 -0
- package/src/commands/collection.js +10 -0
- package/src/commands/episode.js +22 -0
- package/src/commands/search.js +114 -0
- package/src/commands/subject.js +9 -0
- package/src/commands/url.js +108 -0
- package/src/core/community-api.js +372 -0
- package/src/core/config.js +105 -8
- package/src/core/output.js +456 -7
- package/src/utils/args.js +41 -2
- package/src/utils/bangumi-url.js +650 -0
- package/src/utils/profiles.js +203 -0
- package/src/utils/validators.js +20 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bangumi SearchEncore!! Community API Client
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ SearchEncore API
|
|
5
|
+
* 本模块封装的是 Bangumi SearchEncore 服务(bgmdb.ry.mk)。
|
|
6
|
+
* 所有数据均来自 SearchEncore 爬取/聚合,非 bangumi.tv 官方提供。
|
|
7
|
+
*
|
|
8
|
+
* 设计原则:
|
|
9
|
+
* - 与 src/core/client.js 的 BangumiClient 保持 API 风格一致。
|
|
10
|
+
* - 响应格式统一适配为官方 API 的扁平结构({ data, total, limit, offset }),
|
|
11
|
+
* 抹平第三方 envelope 差异(pagination / meta)。
|
|
12
|
+
* - 不携带任何认证信息(该服务为公开 API)。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { requestJson, BangumiApiError } from "./http.js";
|
|
16
|
+
|
|
17
|
+
const COMMUNITY_API_BASE_URL = "https://bgmdb.ry.mk/v1";
|
|
18
|
+
|
|
19
|
+
export class BangumiCommunityApiError extends Error {
|
|
20
|
+
constructor(message, { status, details, source = "community-api" } = {}) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "BangumiCommunityApiError";
|
|
23
|
+
this.status = status;
|
|
24
|
+
this.details = details;
|
|
25
|
+
this.source = source;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 标识常量:用于 UI 侧判断数据来源。
|
|
31
|
+
* 所有通过本模块返回的结果都会带有此标记。
|
|
32
|
+
*/
|
|
33
|
+
export const COMMUNITY_API_SOURCE_TAG = "bgmdb.ry.mk";
|
|
34
|
+
|
|
35
|
+
/* ------------------------------------------------------------------ */
|
|
36
|
+
/* 通用请求与适配 */
|
|
37
|
+
/* ------------------------------------------------------------------ */
|
|
38
|
+
|
|
39
|
+
async function communityRequest(path, options = {}) {
|
|
40
|
+
const url = `${COMMUNITY_API_BASE_URL}${path}`;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const raw = await requestJson(url, {
|
|
44
|
+
method: options.method ?? "GET",
|
|
45
|
+
query: options.query,
|
|
46
|
+
body: options.body,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return adaptEnvelope(raw);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof BangumiApiError) {
|
|
52
|
+
throw new BangumiCommunityApiError(error.message, {
|
|
53
|
+
status: error.status,
|
|
54
|
+
details: error.details,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
throw new BangumiCommunityApiError(
|
|
58
|
+
`Community API request failed: ${error?.message ?? "unknown error"}`,
|
|
59
|
+
{
|
|
60
|
+
details: { url, path, originalError: error?.message },
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 将第三方的 envelope 格式适配为官方风格:
|
|
68
|
+
* 原始: { data, pagination: { total, limit, offset }, meta: { executionMs } }
|
|
69
|
+
* 输出: { data, total, limit, offset, _meta: { executionMs, source } }
|
|
70
|
+
*
|
|
71
|
+
* 若原始已经是扁平结构,则直接透传并追加 source 标记。
|
|
72
|
+
*/
|
|
73
|
+
function adaptEnvelope(raw) {
|
|
74
|
+
if (!raw || typeof raw !== "object") {
|
|
75
|
+
return raw;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const pagination = raw.pagination;
|
|
79
|
+
const isPaginated =
|
|
80
|
+
pagination &&
|
|
81
|
+
typeof pagination === "object" &&
|
|
82
|
+
("total" in pagination || "limit" in pagination || "offset" in pagination);
|
|
83
|
+
|
|
84
|
+
const base = isPaginated
|
|
85
|
+
? {
|
|
86
|
+
data: Array.isArray(raw.data) ? raw.data : [],
|
|
87
|
+
total: pagination.total ?? 0,
|
|
88
|
+
limit: pagination.limit ?? 20,
|
|
89
|
+
offset: pagination.offset ?? 0,
|
|
90
|
+
}
|
|
91
|
+
: { ...raw };
|
|
92
|
+
|
|
93
|
+
// 注入来源标记,供 UI / CLI 输出层识别
|
|
94
|
+
base._meta = {
|
|
95
|
+
...(raw.meta ?? {}),
|
|
96
|
+
source: COMMUNITY_API_SOURCE_TAG,
|
|
97
|
+
isSearchEncore: true,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
return base;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function encode(key) {
|
|
104
|
+
return encodeURIComponent(String(key));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function buildSearchQuery(params = {}) {
|
|
108
|
+
const q = {};
|
|
109
|
+
if (params.q !== undefined && params.q !== "") q.q = params.q;
|
|
110
|
+
if (params.limit !== undefined) q.limit = params.limit;
|
|
111
|
+
if (params.offset !== undefined) q.offset = params.offset;
|
|
112
|
+
if (params.sort !== undefined) q.sort = params.sort;
|
|
113
|
+
return q;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* ------------------------------------------------------------------ */
|
|
117
|
+
/* 搜索接口 */
|
|
118
|
+
/* ------------------------------------------------------------------ */
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 搜索条目(番剧、游戏、书籍等)。
|
|
122
|
+
* 对应官方搜索能力的增强版,支持更灵活的排序和过滤。
|
|
123
|
+
*/
|
|
124
|
+
export async function searchSubjects(params = {}) {
|
|
125
|
+
return communityRequest("/search/subjects", {
|
|
126
|
+
query: buildSearchQuery(params),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 搜索用户。
|
|
132
|
+
*/
|
|
133
|
+
export async function searchUsers(params = {}) {
|
|
134
|
+
return communityRequest("/search/users", {
|
|
135
|
+
query: buildSearchQuery(params),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 搜索小组。
|
|
141
|
+
*/
|
|
142
|
+
export async function searchGroups(params = {}) {
|
|
143
|
+
return communityRequest("/search/groups", {
|
|
144
|
+
query: buildSearchQuery(params),
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 搜索回复内容。
|
|
150
|
+
*/
|
|
151
|
+
export async function searchReplies(params = {}) {
|
|
152
|
+
return communityRequest("/search/replies", {
|
|
153
|
+
query: buildSearchQuery(params),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 搜索小组话题。
|
|
159
|
+
*/
|
|
160
|
+
export async function searchGroupTopics(params = {}) {
|
|
161
|
+
return communityRequest("/search/group-topics", {
|
|
162
|
+
query: buildSearchQuery(params),
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 搜索条目话题。
|
|
168
|
+
*/
|
|
169
|
+
export async function searchSubjectTopics(params = {}) {
|
|
170
|
+
return communityRequest("/search/subject-topics", {
|
|
171
|
+
query: buildSearchQuery(params),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 搜索目录。
|
|
177
|
+
*/
|
|
178
|
+
export async function searchIndexes(params = {}) {
|
|
179
|
+
return communityRequest("/search/indexes", {
|
|
180
|
+
query: buildSearchQuery(params),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* 搜索日志。
|
|
186
|
+
*/
|
|
187
|
+
export async function searchBlogs(params = {}) {
|
|
188
|
+
return communityRequest("/search/blogs", {
|
|
189
|
+
query: buildSearchQuery(params),
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/* ------------------------------------------------------------------ */
|
|
194
|
+
/* 列表接口 */
|
|
195
|
+
/* ------------------------------------------------------------------ */
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 获取最新的小组话题列表。
|
|
199
|
+
*/
|
|
200
|
+
export async function listRecentGroupTopics(query = {}) {
|
|
201
|
+
return communityRequest("/groups/-/topics", { query });
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 获取指定小组的话题列表。
|
|
206
|
+
*/
|
|
207
|
+
export async function listGroupTopics(groupKey, query = {}) {
|
|
208
|
+
return communityRequest(`/groups/${encode(groupKey)}/topics`, { query });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* 获取指定用户的所有小组话题。
|
|
213
|
+
*/
|
|
214
|
+
export async function listUserGroupTopics(userKey, query = {}) {
|
|
215
|
+
return communityRequest(`/users/${encode(userKey)}/group-topics`, { query });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 获取最新的条目话题列表。
|
|
220
|
+
*/
|
|
221
|
+
export async function listRecentSubjectTopics(query = {}) {
|
|
222
|
+
return communityRequest("/subjects/-/topics", { query });
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 获取指定条目的话题列表。
|
|
227
|
+
*/
|
|
228
|
+
export async function listSubjectTopics(subjectId, query = {}) {
|
|
229
|
+
return communityRequest(`/subjects/${encode(subjectId)}/topics`, { query });
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 获取指定用户的所有条目话题。
|
|
234
|
+
*/
|
|
235
|
+
export async function listUserSubjectTopics(userKey, query = {}) {
|
|
236
|
+
return communityRequest(`/users/${encode(userKey)}/subject-topics`, { query });
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* 获取指定用户的目录列表。
|
|
241
|
+
*/
|
|
242
|
+
export async function listUserIndexes(userKey, query = {}) {
|
|
243
|
+
return communityRequest(`/users/${encode(userKey)}/indexes`, { query });
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 获取指定用户的日志列表。
|
|
248
|
+
*/
|
|
249
|
+
export async function listUserBlogs(userKey, query = {}) {
|
|
250
|
+
return communityRequest(`/users/${encode(userKey)}/blogs`, { query });
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* ------------------------------------------------------------------ */
|
|
254
|
+
/* 详情接口 */
|
|
255
|
+
/* ------------------------------------------------------------------ */
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* 获取单个用户信息。
|
|
259
|
+
*/
|
|
260
|
+
export async function getUser(userKey) {
|
|
261
|
+
if (!userKey) {
|
|
262
|
+
throw new BangumiCommunityApiError("Missing userKey.");
|
|
263
|
+
}
|
|
264
|
+
return communityRequest(`/users/${encode(userKey)}`);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 获取用户头像历史记录。
|
|
269
|
+
*/
|
|
270
|
+
export async function getUserAvatarHistory(userKey) {
|
|
271
|
+
if (!userKey) {
|
|
272
|
+
throw new BangumiCommunityApiError("Missing userKey.");
|
|
273
|
+
}
|
|
274
|
+
return communityRequest(`/users/${encode(userKey)}/avatars`);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 获取单个小组信息。
|
|
279
|
+
*/
|
|
280
|
+
export async function getGroup(groupKey) {
|
|
281
|
+
if (!groupKey) {
|
|
282
|
+
throw new BangumiCommunityApiError("Missing groupKey.");
|
|
283
|
+
}
|
|
284
|
+
return communityRequest(`/groups/${encode(groupKey)}`);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 获取单个小组话题详情。
|
|
289
|
+
*/
|
|
290
|
+
export async function getGroupTopic(topicId) {
|
|
291
|
+
if (!topicId) {
|
|
292
|
+
throw new BangumiCommunityApiError("Missing topicId.");
|
|
293
|
+
}
|
|
294
|
+
return communityRequest(`/groups/-/topics/${encode(topicId)}`);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* 获取小组话题的回复列表。
|
|
299
|
+
*/
|
|
300
|
+
export async function listGroupTopicReplies(topicId, query = {}) {
|
|
301
|
+
if (!topicId) {
|
|
302
|
+
throw new BangumiCommunityApiError("Missing topicId.");
|
|
303
|
+
}
|
|
304
|
+
return communityRequest(`/groups/-/topics/${encode(topicId)}/replies`, { query });
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 获取单个条目话题详情。
|
|
309
|
+
*/
|
|
310
|
+
export async function getSubjectTopic(topicId) {
|
|
311
|
+
if (!topicId) {
|
|
312
|
+
throw new BangumiCommunityApiError("Missing topicId.");
|
|
313
|
+
}
|
|
314
|
+
return communityRequest(`/subjects/-/topics/${encode(topicId)}`);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* 获取条目话题的回复列表。
|
|
319
|
+
*/
|
|
320
|
+
export async function listSubjectTopicReplies(topicId, query = {}) {
|
|
321
|
+
if (!topicId) {
|
|
322
|
+
throw new BangumiCommunityApiError("Missing topicId.");
|
|
323
|
+
}
|
|
324
|
+
return communityRequest(`/subjects/-/topics/${encode(topicId)}/replies`, { query });
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 获取单个目录详情。
|
|
329
|
+
*/
|
|
330
|
+
export async function getIndex(indexId) {
|
|
331
|
+
if (!indexId) {
|
|
332
|
+
throw new BangumiCommunityApiError("Missing indexId.");
|
|
333
|
+
}
|
|
334
|
+
return communityRequest(`/indexes/${encode(indexId)}`);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 获取单篇日志详情。
|
|
339
|
+
*/
|
|
340
|
+
export async function getBlog(blogId) {
|
|
341
|
+
if (!blogId) {
|
|
342
|
+
throw new BangumiCommunityApiError("Missing blogId.");
|
|
343
|
+
}
|
|
344
|
+
return communityRequest(`/blogs/${encode(blogId)}`);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/* ------------------------------------------------------------------ */
|
|
348
|
+
/* 健康检查 */
|
|
349
|
+
/* ------------------------------------------------------------------ */
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* 快速探测社区 API 可用性。
|
|
353
|
+
* 返回 { ok: boolean, latencyMs?: number, error?: string }
|
|
354
|
+
*/
|
|
355
|
+
export async function probeCommunityApi() {
|
|
356
|
+
const start = performance.now();
|
|
357
|
+
try {
|
|
358
|
+
const result = await requestJson("https://bgmdb.ry.mk/health", {
|
|
359
|
+
method: "GET",
|
|
360
|
+
});
|
|
361
|
+
return {
|
|
362
|
+
ok: result?.status === "ok",
|
|
363
|
+
latencyMs: Math.round(performance.now() - start),
|
|
364
|
+
};
|
|
365
|
+
} catch (error) {
|
|
366
|
+
return {
|
|
367
|
+
ok: false,
|
|
368
|
+
latencyMs: Math.round(performance.now() - start),
|
|
369
|
+
error: error?.message ?? "unreachable",
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
}
|
package/src/core/config.js
CHANGED
|
@@ -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.
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
}
|