@aronnaxlin/bgm-cli 1.0.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/LICENSE +660 -0
- package/README.en.md +122 -0
- package/README.md +127 -0
- package/README.zh-TW.md +122 -0
- package/package.json +41 -0
- package/src/cli.js +993 -0
- package/src/commands/blog.js +213 -0
- package/src/commands/calendar.js +32 -0
- package/src/commands/character.js +291 -0
- package/src/commands/collection.js +418 -0
- package/src/commands/episode.js +379 -0
- package/src/commands/group.js +488 -0
- package/src/commands/index.js +362 -0
- package/src/commands/notify.js +74 -0
- package/src/commands/person.js +297 -0
- package/src/commands/status.js +74 -0
- package/src/commands/subject.js +537 -0
- package/src/commands/timeline.js +288 -0
- package/src/commands/trending.js +58 -0
- package/src/commands/tui.js +1537 -0
- package/src/commands/user.js +83 -0
- package/src/core/client.js +2065 -0
- package/src/core/config.js +450 -0
- package/src/core/http.js +144 -0
- package/src/core/output.js +3888 -0
- package/src/core/proxy.js +47 -0
- package/src/core/reactions.js +62 -0
- package/src/core/turnstile.js +526 -0
- package/src/data/bangumi-emotes.json +311 -0
- package/src/utils/args.js +113 -0
- package/src/utils/auth-flow.js +373 -0
- package/src/utils/auth.js +90 -0
- package/src/utils/calendar.js +23 -0
- package/src/utils/collection-ops.js +333 -0
- package/src/utils/collection.js +240 -0
- package/src/utils/formatters.js +157 -0
- package/src/utils/helpers.js +283 -0
- package/src/utils/hot.js +182 -0
- package/src/utils/install.js +40 -0
- package/src/utils/prompts.js +87 -0
- package/src/utils/relay.js +74 -0
- package/src/utils/status.js +68 -0
- package/src/utils/tui-interactive.js +197 -0
- package/src/utils/tui-render.js +86 -0
- package/src/utils/turnstile-flow.js +154 -0
- package/src/utils/validators.js +412 -0
package/src/cli.js
ADDED
|
@@ -0,0 +1,993 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import http from "node:http";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import process from "node:process";
|
|
7
|
+
import { spawnSync } from "node:child_process";
|
|
8
|
+
import { chmodSync } from "node:fs";
|
|
9
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
10
|
+
import readline from "node:readline/promises";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { BangumiClient, BangumiOAuthClient } from "./core/client.js";
|
|
13
|
+
import { getInstalledProxy, installProxyFromConfig, resolveProxyUrl } from "./core/proxy.js";
|
|
14
|
+
import { DEFAULT_TURNSTILE_TIMEOUT_MS, startTurnstileFlow } from "./core/turnstile.js";
|
|
15
|
+
import {
|
|
16
|
+
ConfigError,
|
|
17
|
+
clearConfigValue,
|
|
18
|
+
clearConfigValues,
|
|
19
|
+
enableGlobalConfigMode,
|
|
20
|
+
getConfig,
|
|
21
|
+
getConfigFilePath,
|
|
22
|
+
getConfigSourceFilePath,
|
|
23
|
+
normalizeConfigValue,
|
|
24
|
+
setConfigValues,
|
|
25
|
+
} from "./core/config.js";
|
|
26
|
+
import { CommandError, formatDisplayResult, printResult, printUsage } from "./core/output.js";
|
|
27
|
+
import {
|
|
28
|
+
firstPositional,
|
|
29
|
+
getPositional,
|
|
30
|
+
hasHelpFlag,
|
|
31
|
+
parseFlags,
|
|
32
|
+
parseGlobalArgs,
|
|
33
|
+
resolveHelpTarget,
|
|
34
|
+
splitFilterValues,
|
|
35
|
+
storeFlagValue,
|
|
36
|
+
} from "./utils/args.js";
|
|
37
|
+
import {
|
|
38
|
+
buildVersionStatusPayload,
|
|
39
|
+
compareStrings,
|
|
40
|
+
delayMs,
|
|
41
|
+
ensureExecutable,
|
|
42
|
+
escapeHtml,
|
|
43
|
+
formatPlatformName,
|
|
44
|
+
hasSavedConfigValue,
|
|
45
|
+
inferConfigScope,
|
|
46
|
+
normalizeConfigKey,
|
|
47
|
+
normalizeNonNegativeInteger,
|
|
48
|
+
normalizePageSize,
|
|
49
|
+
normalizeTurnstileTimeoutMs,
|
|
50
|
+
parseOptionalBoolean,
|
|
51
|
+
parseOptionalInteger,
|
|
52
|
+
pathsEqual,
|
|
53
|
+
previewToken,
|
|
54
|
+
sleep,
|
|
55
|
+
toBoolean,
|
|
56
|
+
tryOpenExternalUrl,
|
|
57
|
+
writeProgress,
|
|
58
|
+
} from "./utils/helpers.js";
|
|
59
|
+
import {
|
|
60
|
+
SUBJECT_TYPE_MAP,
|
|
61
|
+
TIMELINE_MODE_VALUES,
|
|
62
|
+
normalizeSortOrder,
|
|
63
|
+
} from "./utils/validators.js";
|
|
64
|
+
import {
|
|
65
|
+
extractPrivateSessionId,
|
|
66
|
+
fallbackUserAgent,
|
|
67
|
+
getPrivateLoginUrl,
|
|
68
|
+
} from "./utils/auth.js";
|
|
69
|
+
import {
|
|
70
|
+
getManagedInstallDir,
|
|
71
|
+
getShellReloadHint,
|
|
72
|
+
getUpdateShellHint,
|
|
73
|
+
} from "./utils/install.js";
|
|
74
|
+
import {
|
|
75
|
+
askChoice,
|
|
76
|
+
askHiddenRequired,
|
|
77
|
+
askRequired,
|
|
78
|
+
} from "./utils/prompts.js";
|
|
79
|
+
import {
|
|
80
|
+
buildHostedRelayCorsHeaders,
|
|
81
|
+
computeHostedSessionTimeoutMs,
|
|
82
|
+
readHostedRelayJsonBody,
|
|
83
|
+
respondHostedRelayJson,
|
|
84
|
+
respondHostedRelayPreflight,
|
|
85
|
+
respondHtml,
|
|
86
|
+
} from "./utils/relay.js";
|
|
87
|
+
import {
|
|
88
|
+
runPrivateSessionLogin,
|
|
89
|
+
} from "./utils/auth-flow.js";
|
|
90
|
+
import {
|
|
91
|
+
acquireTurnstileToken,
|
|
92
|
+
} from "./utils/turnstile-flow.js";
|
|
93
|
+
import { summarizeCurrentStatus } from "./utils/status.js";
|
|
94
|
+
import { runTui } from "./commands/tui.js";
|
|
95
|
+
import { runCalendarCommand } from "./commands/calendar.js";
|
|
96
|
+
import { runStatusCommand } from "./commands/status.js";
|
|
97
|
+
import { runUserCommand } from "./commands/user.js";
|
|
98
|
+
import { runSubjectCommand } from "./commands/subject.js";
|
|
99
|
+
import { runCollectionCommand } from "./commands/collection.js";
|
|
100
|
+
import { runEpisodeCommand } from "./commands/episode.js";
|
|
101
|
+
import { runIndexCommand } from "./commands/index.js";
|
|
102
|
+
import { runBlogCommand } from "./commands/blog.js";
|
|
103
|
+
import { runTimelineCommand } from "./commands/timeline.js";
|
|
104
|
+
import { runGroupCommand } from "./commands/group.js";
|
|
105
|
+
import { runCharacterCommand } from "./commands/character.js";
|
|
106
|
+
import { runPersonCommand } from "./commands/person.js";
|
|
107
|
+
import { runTrendingCommand } from "./commands/trending.js";
|
|
108
|
+
import { runNotifyCommand } from "./commands/notify.js";
|
|
109
|
+
|
|
110
|
+
const CLI_DIR = path.dirname(fileURLToPath(import.meta.url));
|
|
111
|
+
const REPO_ROOT = path.resolve(CLI_DIR, "..");
|
|
112
|
+
|
|
113
|
+
const REMOTE_INSTALL_SCRIPT_BASE_URL = "https://raw.githubusercontent.com/aronnaxlin/bgm-cli/main/scripts";
|
|
114
|
+
const AUTH_CONFIG_KEYS = [
|
|
115
|
+
"accessToken",
|
|
116
|
+
"refreshToken",
|
|
117
|
+
"tokenType",
|
|
118
|
+
"privateSessionId",
|
|
119
|
+
"privateSessionUpdatedAt",
|
|
120
|
+
"clientId",
|
|
121
|
+
"clientSecret",
|
|
122
|
+
"redirectUri",
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
async function main(argv) {
|
|
126
|
+
const parsed = parseGlobalArgs(argv);
|
|
127
|
+
const context = {
|
|
128
|
+
json: parsed.json,
|
|
129
|
+
rawArgs: parsed.args,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
installProxyFromConfig(getConfig());
|
|
134
|
+
} catch (error) {
|
|
135
|
+
process.stderr.write(`Warning: ${error?.message ?? error}\n`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (parsed.version) {
|
|
139
|
+
printResult(buildVersionStatusPayload(REPO_ROOT), context);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (parsed.init) {
|
|
144
|
+
await runInitWizard(context);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (parsed.args.length === 0) {
|
|
149
|
+
printUsage();
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (hasHelpFlag(parsed.args)) {
|
|
154
|
+
printUsage(resolveHelpTarget(parsed.args));
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const [group, command, ...rest] = parsed.args;
|
|
159
|
+
|
|
160
|
+
switch (group) {
|
|
161
|
+
case "tui":
|
|
162
|
+
await runTui(context, { runConfigCommand, runSetupCommand });
|
|
163
|
+
return;
|
|
164
|
+
case "config":
|
|
165
|
+
await runConfigCommand(command, rest, context);
|
|
166
|
+
return;
|
|
167
|
+
case "proxy":
|
|
168
|
+
await runProxyCommand(command, rest, context);
|
|
169
|
+
return;
|
|
170
|
+
case "auth":
|
|
171
|
+
await runAuthCommand(command, rest, context);
|
|
172
|
+
return;
|
|
173
|
+
case "setup":
|
|
174
|
+
await runSetupCommand(command, rest, context);
|
|
175
|
+
return;
|
|
176
|
+
case "subject":
|
|
177
|
+
await runSubjectCommand(command, rest, context);
|
|
178
|
+
return;
|
|
179
|
+
case "character":
|
|
180
|
+
await runCharacterCommand(command, rest, context);
|
|
181
|
+
return;
|
|
182
|
+
case "person":
|
|
183
|
+
await runPersonCommand(command, rest, context);
|
|
184
|
+
return;
|
|
185
|
+
case "episode":
|
|
186
|
+
case "ep":
|
|
187
|
+
await runEpisodeCommand(command, rest, context);
|
|
188
|
+
return;
|
|
189
|
+
case "group":
|
|
190
|
+
await runGroupCommand(command, rest, context);
|
|
191
|
+
return;
|
|
192
|
+
case "blog":
|
|
193
|
+
await runBlogCommand(command, rest, context);
|
|
194
|
+
return;
|
|
195
|
+
case "timeline":
|
|
196
|
+
await runTimelineCommand(command, rest, context);
|
|
197
|
+
return;
|
|
198
|
+
case "trending":
|
|
199
|
+
await runTrendingCommand(command, rest, context);
|
|
200
|
+
return;
|
|
201
|
+
case "index":
|
|
202
|
+
await runIndexCommand(command, rest, context);
|
|
203
|
+
return;
|
|
204
|
+
case "collection":
|
|
205
|
+
await runCollectionCommand(command, rest, context);
|
|
206
|
+
return;
|
|
207
|
+
case "status":
|
|
208
|
+
await runStatusCommand(command, rest, context);
|
|
209
|
+
return;
|
|
210
|
+
case "user":
|
|
211
|
+
await runUserCommand(command, rest, context);
|
|
212
|
+
return;
|
|
213
|
+
case "notify":
|
|
214
|
+
await runNotifyCommand(command, rest, context);
|
|
215
|
+
return;
|
|
216
|
+
case "calendar":
|
|
217
|
+
await runCalendarCommand(command, rest, context);
|
|
218
|
+
return;
|
|
219
|
+
default:
|
|
220
|
+
throw new CommandError(`Unknown command group: ${group}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function runInitWizard(context) {
|
|
225
|
+
if (context.json) {
|
|
226
|
+
throw new CommandError("--init does not support --json because it requires interactive prompts.");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const currentConfig = getConfig();
|
|
230
|
+
let rl = readline.createInterface({
|
|
231
|
+
input: process.stdin,
|
|
232
|
+
output: process.stdout,
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
console.log("Bangumi CLI initialization");
|
|
237
|
+
console.log(`Config file: ${getConfigFilePath()}`);
|
|
238
|
+
console.log("");
|
|
239
|
+
|
|
240
|
+
const authMode = await askChoice(
|
|
241
|
+
rl,
|
|
242
|
+
"Choose a login method",
|
|
243
|
+
[
|
|
244
|
+
{
|
|
245
|
+
key: "1",
|
|
246
|
+
label: "Official Bangumi login (Recommended)",
|
|
247
|
+
value: "login",
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
key: "2",
|
|
251
|
+
label: "Paste your own Access Token",
|
|
252
|
+
value: "token",
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
"1",
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const userAgent = currentConfig.userAgent ?? fallbackUserAgent(currentConfig);
|
|
259
|
+
|
|
260
|
+
await setConfigValues({
|
|
261
|
+
userAgent,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
if (authMode === "login") {
|
|
265
|
+
const loginArgs = [];
|
|
266
|
+
if (getSavedPrivateSessionId(currentConfig)) {
|
|
267
|
+
const replaceChoice = await askChoice(
|
|
268
|
+
rl,
|
|
269
|
+
"A private session is already saved",
|
|
270
|
+
[
|
|
271
|
+
{
|
|
272
|
+
key: "1",
|
|
273
|
+
label: "Keep the current session (Recommended)",
|
|
274
|
+
value: "keep",
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
key: "2",
|
|
278
|
+
label: "Replace it with a new official login",
|
|
279
|
+
value: "replace",
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
"1",
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
if (replaceChoice === "keep") {
|
|
286
|
+
console.log("Keeping the current private session.");
|
|
287
|
+
await promptInstallPathSetup(context, rl);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
loginArgs.push("--force");
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
console.log("");
|
|
295
|
+
console.log("Starting official Bangumi login.");
|
|
296
|
+
console.log("This is the same flow as `bgm auth login`: email, hidden password prompt, and official Turnstile verification.");
|
|
297
|
+
console.log("");
|
|
298
|
+
rl.close();
|
|
299
|
+
rl = null;
|
|
300
|
+
await runAuthCommand("login", loginArgs, context);
|
|
301
|
+
await promptInstallPathSetup(context);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (authMode === "token") {
|
|
306
|
+
const confirmedUserAgent = userAgent;
|
|
307
|
+
console.log("The CLI will use this User-Agent:");
|
|
308
|
+
console.log(confirmedUserAgent);
|
|
309
|
+
console.log("");
|
|
310
|
+
console.log("Recommended steps to get an access token:");
|
|
311
|
+
console.log("1. Sign in to Bangumi in your browser");
|
|
312
|
+
console.log("2. Open the access token page");
|
|
313
|
+
console.log("3. Copy the token and paste it here");
|
|
314
|
+
console.log("");
|
|
315
|
+
console.log("Access token page:");
|
|
316
|
+
console.log("https://next.bgm.tv/demo/access-token");
|
|
317
|
+
console.log("");
|
|
318
|
+
console.log("If you do not want to continue right now, press Ctrl+C to exit.");
|
|
319
|
+
console.log("");
|
|
320
|
+
|
|
321
|
+
if (currentConfig.accessToken) {
|
|
322
|
+
console.log("A local access token is already saved.");
|
|
323
|
+
console.log("Enter a new token to replace it.");
|
|
324
|
+
console.log("Press Ctrl+C to keep the current token unchanged.");
|
|
325
|
+
console.log("");
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const manualToken = await askRequired(rl, "Enter access token");
|
|
329
|
+
await setConfigValues({
|
|
330
|
+
accessToken: manualToken,
|
|
331
|
+
tokenType: "Bearer",
|
|
332
|
+
userAgent: confirmedUserAgent,
|
|
333
|
+
});
|
|
334
|
+
console.log("Access token saved.");
|
|
335
|
+
|
|
336
|
+
await promptInstallPathSetup(context, rl);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
} finally {
|
|
340
|
+
if (rl) {
|
|
341
|
+
rl.close();
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async function promptInstallPathSetup(context, existingReadline) {
|
|
347
|
+
const rl = existingReadline ?? readline.createInterface({
|
|
348
|
+
input: process.stdin,
|
|
349
|
+
output: process.stdout,
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
try {
|
|
353
|
+
const installPathChoice = await askChoice(
|
|
354
|
+
rl,
|
|
355
|
+
"Optional: add this repository to PATH so you can run bgm from any directory",
|
|
356
|
+
[
|
|
357
|
+
{
|
|
358
|
+
key: "1",
|
|
359
|
+
label: "Run global command setup now (Recommended)",
|
|
360
|
+
value: "install",
|
|
361
|
+
},
|
|
362
|
+
{
|
|
363
|
+
key: "2",
|
|
364
|
+
label: "Skip for now",
|
|
365
|
+
value: "skip",
|
|
366
|
+
},
|
|
367
|
+
],
|
|
368
|
+
"1",
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
if (installPathChoice === "install") {
|
|
372
|
+
console.log("");
|
|
373
|
+
printResult(await runInstallPathSetup(), context);
|
|
374
|
+
}
|
|
375
|
+
} finally {
|
|
376
|
+
if (!existingReadline) {
|
|
377
|
+
rl.close();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async function runConfigCommand(command, args, context) {
|
|
383
|
+
switch (command) {
|
|
384
|
+
case "show": {
|
|
385
|
+
const config = getConfig();
|
|
386
|
+
const proxy = resolveProxyUrl(config);
|
|
387
|
+
printResult(
|
|
388
|
+
{
|
|
389
|
+
configFile: getConfigFilePath(),
|
|
390
|
+
configSourceFile: getConfigSourceFilePath(),
|
|
391
|
+
config,
|
|
392
|
+
effectiveProxy: {
|
|
393
|
+
url: proxy.url || null,
|
|
394
|
+
source: proxy.source,
|
|
395
|
+
active: Boolean(getInstalledProxy()),
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
context,
|
|
399
|
+
);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
case "set": {
|
|
403
|
+
const [key, value] = args;
|
|
404
|
+
if (!key || value === undefined) {
|
|
405
|
+
throw new CommandError("Usage: bgm config set <key> <value>");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const normalizedKey = normalizeConfigKey(key);
|
|
409
|
+
const normalizedValue = normalizeConfigValue(normalizedKey, value);
|
|
410
|
+
await setConfigValues({ [normalizedKey]: normalizedValue });
|
|
411
|
+
printResult(
|
|
412
|
+
{
|
|
413
|
+
updated: normalizedKey,
|
|
414
|
+
configFile: getConfigFilePath(),
|
|
415
|
+
value: normalizedValue,
|
|
416
|
+
},
|
|
417
|
+
context,
|
|
418
|
+
);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
case "unset": {
|
|
422
|
+
const [key] = args;
|
|
423
|
+
if (!key) {
|
|
424
|
+
throw new CommandError("Usage: bgm config unset <key>");
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const normalizedKey = normalizeConfigKey(key);
|
|
428
|
+
await clearConfigValue(normalizedKey);
|
|
429
|
+
printResult(
|
|
430
|
+
{
|
|
431
|
+
removed: normalizedKey,
|
|
432
|
+
configFile: getConfigFilePath(),
|
|
433
|
+
},
|
|
434
|
+
context,
|
|
435
|
+
);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
default:
|
|
439
|
+
throw new CommandError("Usage: bgm config <show|set|unset> ...");
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
async function runProxyCommand(command, args, context) {
|
|
444
|
+
switch (command) {
|
|
445
|
+
case undefined:
|
|
446
|
+
case "show": {
|
|
447
|
+
const proxy = resolveProxyUrl(getConfig());
|
|
448
|
+
printResult(
|
|
449
|
+
{
|
|
450
|
+
proxy: {
|
|
451
|
+
url: proxy.url || null,
|
|
452
|
+
source: proxy.source,
|
|
453
|
+
active: Boolean(getInstalledProxy()),
|
|
454
|
+
},
|
|
455
|
+
},
|
|
456
|
+
context,
|
|
457
|
+
);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
case "set": {
|
|
461
|
+
const [url] = args;
|
|
462
|
+
if (!url) {
|
|
463
|
+
throw new CommandError("Usage: bgm proxy set <url>");
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
const normalizedValue = normalizeConfigValue("proxy", url);
|
|
467
|
+
await setConfigValues({ proxy: normalizedValue });
|
|
468
|
+
printResult(
|
|
469
|
+
{
|
|
470
|
+
updated: "proxy",
|
|
471
|
+
configFile: getConfigFilePath(),
|
|
472
|
+
value: normalizedValue,
|
|
473
|
+
},
|
|
474
|
+
context,
|
|
475
|
+
);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
case "unset": {
|
|
479
|
+
await clearConfigValue("proxy");
|
|
480
|
+
printResult(
|
|
481
|
+
{
|
|
482
|
+
removed: "proxy",
|
|
483
|
+
configFile: getConfigFilePath(),
|
|
484
|
+
},
|
|
485
|
+
context,
|
|
486
|
+
);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
default:
|
|
490
|
+
throw new CommandError("Usage: bgm proxy <show|set|unset> ...");
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
async function runSetupCommand(command, args, context) {
|
|
495
|
+
switch (command) {
|
|
496
|
+
case "install-path": {
|
|
497
|
+
printResult(await runInstallPathSetup(), context);
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
case "update": {
|
|
501
|
+
printResult(await runManagedInstallUpdate(), context);
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
default:
|
|
505
|
+
throw new CommandError("Usage: bgm setup <install-path|update>");
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
async function runAuthCommand(command, args, context) {
|
|
510
|
+
const options = parseFlags(args);
|
|
511
|
+
const config = getConfig();
|
|
512
|
+
const oauth = new BangumiOAuthClient(config);
|
|
513
|
+
|
|
514
|
+
switch (command) {
|
|
515
|
+
case "login": {
|
|
516
|
+
const savedSessionId = getSavedPrivateSessionId(config);
|
|
517
|
+
if (savedSessionId && !toBoolean(options.force, false)) {
|
|
518
|
+
throw new CommandError("Private session is already saved. Run `bgm auth status` to inspect it, `bgm auth logout` to end it, or `bgm auth login --force` to replace it.");
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const { email, password } = await resolveOfficialLoginCredentials(options, context);
|
|
522
|
+
let turnstileToken = typeof options.turnstileToken === "string" ? options.turnstileToken.trim() : "";
|
|
523
|
+
if (!turnstileToken) {
|
|
524
|
+
const result = await acquireTurnstileToken(options, context, {
|
|
525
|
+
actionLabel: "log in to Bangumi private API",
|
|
526
|
+
});
|
|
527
|
+
turnstileToken = result.token;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const login = await new BangumiClient(config).login({
|
|
531
|
+
email,
|
|
532
|
+
password,
|
|
533
|
+
turnstileToken,
|
|
534
|
+
});
|
|
535
|
+
if (!login.privateSessionId) {
|
|
536
|
+
throw new CommandError("Bangumi login succeeded, but the response did not include chiiNextSessionID.");
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
await setConfigValues({
|
|
540
|
+
privateSessionId: login.privateSessionId,
|
|
541
|
+
privateSessionUpdatedAt: new Date().toISOString(),
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
printResult(
|
|
545
|
+
{
|
|
546
|
+
resource: "auth-login",
|
|
547
|
+
saved: true,
|
|
548
|
+
configFile: getConfigFilePath(),
|
|
549
|
+
sessionPreview: previewToken(login.privateSessionId),
|
|
550
|
+
user: login.user,
|
|
551
|
+
},
|
|
552
|
+
context,
|
|
553
|
+
);
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
case "logout": {
|
|
557
|
+
const sessionId = typeof config.privateSessionId === "string" ? config.privateSessionId.trim() : "";
|
|
558
|
+
if (!sessionId) {
|
|
559
|
+
throw new CommandError("No saved private API session. Run `bgm auth login` first, or use `bgm auth clear` to clear local auth state.");
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
await new BangumiClient(config).logout();
|
|
563
|
+
await clearConfigValues(["privateSessionId", "privateSessionUpdatedAt"]);
|
|
564
|
+
printResult(
|
|
565
|
+
{
|
|
566
|
+
resource: "auth-logout",
|
|
567
|
+
clearedSession: true,
|
|
568
|
+
configFile: getConfigFilePath(),
|
|
569
|
+
},
|
|
570
|
+
context,
|
|
571
|
+
);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
case "login-url": {
|
|
575
|
+
const loginUrl = oauth.createAuthorizationUrl({
|
|
576
|
+
clientId: options.clientId ?? config.clientId,
|
|
577
|
+
redirectUri: options.redirectUri ?? config.redirectUri,
|
|
578
|
+
state: options.state,
|
|
579
|
+
});
|
|
580
|
+
printResult({ loginUrl }, context);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
case "token": {
|
|
584
|
+
const code = firstPositional(options);
|
|
585
|
+
if (!code && !options.code) {
|
|
586
|
+
throw new CommandError("Usage: bgm auth token --code <code> [--save]");
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const token = await oauth.exchangeCode({
|
|
590
|
+
code: options.code ?? code,
|
|
591
|
+
clientId: options.clientId ?? config.clientId,
|
|
592
|
+
clientSecret: options.clientSecret ?? config.clientSecret,
|
|
593
|
+
redirectUri: options.redirectUri ?? config.redirectUri,
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
if (toBoolean(options.save, false)) {
|
|
597
|
+
await setConfigValues({
|
|
598
|
+
clientId: options.clientId ?? config.clientId,
|
|
599
|
+
clientSecret: options.clientSecret ?? config.clientSecret,
|
|
600
|
+
redirectUri: options.redirectUri ?? config.redirectUri,
|
|
601
|
+
accessToken: token.access_token,
|
|
602
|
+
refreshToken: token.refresh_token ?? null,
|
|
603
|
+
tokenType: token.token_type ?? "Bearer",
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
printResult(token, context);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
case "refresh": {
|
|
611
|
+
const token = await oauth.refreshToken({
|
|
612
|
+
refreshToken: options.refreshToken ?? config.refreshToken,
|
|
613
|
+
clientId: options.clientId ?? config.clientId,
|
|
614
|
+
clientSecret: options.clientSecret ?? config.clientSecret,
|
|
615
|
+
redirectUri: options.redirectUri ?? config.redirectUri,
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
if (toBoolean(options.save, false)) {
|
|
619
|
+
await setConfigValues({
|
|
620
|
+
clientId: options.clientId ?? config.clientId,
|
|
621
|
+
clientSecret: options.clientSecret ?? config.clientSecret,
|
|
622
|
+
redirectUri: options.redirectUri ?? config.redirectUri,
|
|
623
|
+
accessToken: token.access_token,
|
|
624
|
+
refreshToken: token.refresh_token ?? options.refreshToken ?? config.refreshToken,
|
|
625
|
+
tokenType: token.token_type ?? "Bearer",
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
printResult(token, context);
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
case "status": {
|
|
633
|
+
printResult(buildAuthStatusPayload(config), context);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
case "token-status": {
|
|
637
|
+
const status = await oauth.getTokenStatus({
|
|
638
|
+
accessToken: options.accessToken ?? config.accessToken,
|
|
639
|
+
});
|
|
640
|
+
printResult(status, context);
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
case "turnstile": {
|
|
644
|
+
const result = await acquireTurnstileToken(options, context);
|
|
645
|
+
printResult(
|
|
646
|
+
{
|
|
647
|
+
resource: "turnstile-token",
|
|
648
|
+
token: result.token,
|
|
649
|
+
tokenPreview: previewToken(result.token),
|
|
650
|
+
verificationUrl: result.verificationUrl,
|
|
651
|
+
authorizeUrl: result.authorizeUrl,
|
|
652
|
+
redirectUri: result.redirectUri,
|
|
653
|
+
backendBaseUrl: result.backendBaseUrl,
|
|
654
|
+
sessionId: result.sessionId,
|
|
655
|
+
listenHost: result.listenHost,
|
|
656
|
+
port: result.port,
|
|
657
|
+
openedBrowser: result.openedBrowser,
|
|
658
|
+
timeoutSeconds: Math.floor(result.timeoutMs / 1000),
|
|
659
|
+
},
|
|
660
|
+
context,
|
|
661
|
+
);
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
case "session-login": {
|
|
665
|
+
const result = await runPrivateSessionLogin(options, context);
|
|
666
|
+
printResult(result, context);
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
case "set-token": {
|
|
670
|
+
const accessToken = options.accessToken ?? firstPositional(options);
|
|
671
|
+
if (!accessToken) {
|
|
672
|
+
throw new CommandError("Usage: bgm auth set-token <access_token>");
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
await setConfigValues({
|
|
676
|
+
accessToken,
|
|
677
|
+
tokenType: "Bearer",
|
|
678
|
+
});
|
|
679
|
+
|
|
680
|
+
printResult(
|
|
681
|
+
{
|
|
682
|
+
saved: true,
|
|
683
|
+
configFile: getConfigFilePath(),
|
|
684
|
+
accessTokenPreview: previewToken(accessToken),
|
|
685
|
+
},
|
|
686
|
+
context,
|
|
687
|
+
);
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
case "set-session": {
|
|
691
|
+
const rawSession = options.session ?? firstPositional(options);
|
|
692
|
+
if (!rawSession) {
|
|
693
|
+
throw new CommandError("Usage: bgm auth set-session <chiiNextSessionID|cookie_string>");
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const sessionId = extractPrivateSessionId(rawSession);
|
|
697
|
+
if (!sessionId) {
|
|
698
|
+
throw new CommandError("Could not find chiiNextSessionID in the provided value.");
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
await setConfigValues({
|
|
702
|
+
privateSessionId: sessionId,
|
|
703
|
+
privateSessionUpdatedAt: new Date().toISOString(),
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
printResult(
|
|
707
|
+
{
|
|
708
|
+
resource: "private-session-mutation",
|
|
709
|
+
saved: true,
|
|
710
|
+
configFile: getConfigFilePath(),
|
|
711
|
+
sessionPreview: previewToken(sessionId),
|
|
712
|
+
loginUrl: getPrivateLoginUrl(),
|
|
713
|
+
},
|
|
714
|
+
context,
|
|
715
|
+
);
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
case "session-status": {
|
|
719
|
+
const sessionId = typeof config.privateSessionId === "string" ? config.privateSessionId.trim() : "";
|
|
720
|
+
printResult(
|
|
721
|
+
{
|
|
722
|
+
resource: "private-session-status",
|
|
723
|
+
saved: Boolean(sessionId),
|
|
724
|
+
sessionPreview: sessionId ? previewToken(sessionId) : null,
|
|
725
|
+
updatedAt: config.privateSessionUpdatedAt ?? null,
|
|
726
|
+
loginUrl: getPrivateLoginUrl(),
|
|
727
|
+
},
|
|
728
|
+
context,
|
|
729
|
+
);
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
case "clear": {
|
|
733
|
+
const clearKeys = resolveAuthClearKeys(options);
|
|
734
|
+
await clearConfigValues(clearKeys);
|
|
735
|
+
printResult(
|
|
736
|
+
{
|
|
737
|
+
resource: "auth-clear",
|
|
738
|
+
cleared: clearKeys,
|
|
739
|
+
configFile: getConfigFilePath(),
|
|
740
|
+
},
|
|
741
|
+
context,
|
|
742
|
+
);
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
default:
|
|
746
|
+
throw new CommandError("Usage: bgm auth <login|logout|status|token-status|login-url|token|refresh|turnstile|session-login|set-token|set-session|session-status|clear> ...");
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
async function resolveOfficialLoginCredentials(options, context) {
|
|
751
|
+
let email = options.email ?? process.env.BGM_LOGIN_EMAIL;
|
|
752
|
+
let password = options.password ?? process.env.BGM_LOGIN_PASSWORD;
|
|
753
|
+
|
|
754
|
+
if ((!email || !password) && !context.json && process.stdin.isTTY && process.stdout.isTTY) {
|
|
755
|
+
if (!email) {
|
|
756
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
757
|
+
try {
|
|
758
|
+
email = email || await askRequired(rl, "Bangumi email");
|
|
759
|
+
} finally {
|
|
760
|
+
rl.close();
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
if (!password) {
|
|
764
|
+
password = await askHiddenRequired("Bangumi password");
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
if (!email || !password) {
|
|
769
|
+
throw new CommandError("Usage: bgm auth login [--email <email>] [--password <password>] [--turnstile-token <token>] [--manual]");
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
return {
|
|
773
|
+
email,
|
|
774
|
+
password,
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
function buildAuthStatusPayload(config) {
|
|
779
|
+
const accessToken = typeof config.accessToken === "string" ? config.accessToken.trim() : "";
|
|
780
|
+
const refreshToken = typeof config.refreshToken === "string" ? config.refreshToken.trim() : "";
|
|
781
|
+
const privateSession = getSavedPrivateSessionId(config);
|
|
782
|
+
|
|
783
|
+
return {
|
|
784
|
+
resource: "auth-status",
|
|
785
|
+
configFile: getConfigFilePath(),
|
|
786
|
+
policy: "p1 requests use the private session cookie when saved; Access Token is not sent together with it.",
|
|
787
|
+
channels: {
|
|
788
|
+
accessToken: {
|
|
789
|
+
saved: Boolean(accessToken),
|
|
790
|
+
tokenPreview: accessToken ? previewToken(accessToken) : null,
|
|
791
|
+
refreshTokenSaved: Boolean(refreshToken),
|
|
792
|
+
statusCommand: "bgm auth token-status",
|
|
793
|
+
setCommand: "bgm auth set-token <access_token>",
|
|
794
|
+
},
|
|
795
|
+
privateSession: {
|
|
796
|
+
saved: Boolean(privateSession),
|
|
797
|
+
sessionPreview: privateSession ? previewToken(privateSession) : null,
|
|
798
|
+
updatedAt: config.privateSessionUpdatedAt ?? null,
|
|
799
|
+
loginCommand: "bgm auth login",
|
|
800
|
+
logoutCommand: "bgm auth logout",
|
|
801
|
+
},
|
|
802
|
+
},
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
function getSavedPrivateSessionId(config) {
|
|
807
|
+
return typeof config.privateSessionId === "string" ? config.privateSessionId.trim() : "";
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
function resolveAuthClearKeys(options) {
|
|
811
|
+
const clearToken = toBoolean(options.token, false) || options.channel === "token";
|
|
812
|
+
const clearSession = toBoolean(options.session, false) || options.channel === "session";
|
|
813
|
+
|
|
814
|
+
if (clearToken && clearSession) {
|
|
815
|
+
return AUTH_CONFIG_KEYS;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
if (clearToken) {
|
|
819
|
+
return ["accessToken", "refreshToken", "tokenType", "clientId", "clientSecret", "redirectUri"];
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
if (clearSession) {
|
|
823
|
+
return ["privateSessionId", "privateSessionUpdatedAt"];
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
return AUTH_CONFIG_KEYS;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
async function runInstallPathSetup() {
|
|
830
|
+
const repoDir = REPO_ROOT;
|
|
831
|
+
const isWindows = process.platform === "win32";
|
|
832
|
+
const scriptPath = isWindows
|
|
833
|
+
? path.join(repoDir, "scripts", "install-global-bgm.ps1")
|
|
834
|
+
: path.join(repoDir, "scripts", "install-global-bgm.sh");
|
|
835
|
+
|
|
836
|
+
if (!isWindows) {
|
|
837
|
+
ensureExecutable(path.join(repoDir, "bgm"));
|
|
838
|
+
ensureExecutable(scriptPath);
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
const command = isWindows
|
|
842
|
+
? "powershell"
|
|
843
|
+
: "sh";
|
|
844
|
+
const commandArgs = isWindows
|
|
845
|
+
? ["-ExecutionPolicy", "Bypass", "-File", scriptPath]
|
|
846
|
+
: [scriptPath];
|
|
847
|
+
|
|
848
|
+
const result = spawnSync(command, commandArgs, {
|
|
849
|
+
cwd: repoDir,
|
|
850
|
+
encoding: "utf8",
|
|
851
|
+
});
|
|
852
|
+
|
|
853
|
+
if (result.error) {
|
|
854
|
+
throw new CommandError(`Failed to run global command setup: ${result.error.message}`);
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
const stdout = String(result.stdout ?? "").trim();
|
|
858
|
+
const stderr = String(result.stderr ?? "").trim();
|
|
859
|
+
|
|
860
|
+
if (result.status !== 0) {
|
|
861
|
+
throw new CommandError(
|
|
862
|
+
[
|
|
863
|
+
"Global command setup failed.",
|
|
864
|
+
stdout,
|
|
865
|
+
stderr,
|
|
866
|
+
].filter(Boolean).join("\n"),
|
|
867
|
+
);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
const setupResult = await enableGlobalConfigMode();
|
|
871
|
+
|
|
872
|
+
return {
|
|
873
|
+
action: "install-path",
|
|
874
|
+
platform: formatPlatformName(process.platform),
|
|
875
|
+
repoDir,
|
|
876
|
+
shellHint: getShellReloadHint(),
|
|
877
|
+
output: stdout || "Installer script completed.",
|
|
878
|
+
configFile: setupResult.configFile,
|
|
879
|
+
migratedConfig: setupResult.migrated,
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
async function runManagedInstallUpdate() {
|
|
884
|
+
const repoDir = REPO_ROOT;
|
|
885
|
+
const installDir = getManagedInstallDir();
|
|
886
|
+
|
|
887
|
+
if (!pathsEqual(repoDir, installDir)) {
|
|
888
|
+
throw new CommandError(
|
|
889
|
+
[
|
|
890
|
+
"`bgm setup update` only supports the one-click managed install.",
|
|
891
|
+
`Current repository: ${repoDir}`,
|
|
892
|
+
`Managed install path: ${installDir}`,
|
|
893
|
+
"If you are using a git checkout, update it with your normal git workflow instead.",
|
|
894
|
+
].join("\n"),
|
|
895
|
+
);
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
const isWindows = process.platform === "win32";
|
|
899
|
+
const scriptName = isWindows ? "install-remote.ps1" : "install-remote.sh";
|
|
900
|
+
const scriptUrl = `${REMOTE_INSTALL_SCRIPT_BASE_URL}/${scriptName}`;
|
|
901
|
+
const tempDir = await mkdtemp(path.join(os.tmpdir(), "bgm-cli-update-"));
|
|
902
|
+
const scriptPath = path.join(tempDir, scriptName);
|
|
903
|
+
|
|
904
|
+
try {
|
|
905
|
+
const response = await fetch(scriptUrl, {
|
|
906
|
+
headers: {
|
|
907
|
+
Accept: "text/plain",
|
|
908
|
+
},
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
if (!response.ok) {
|
|
912
|
+
throw new CommandError(`Failed to download update script from ${scriptUrl}: HTTP ${response.status}`);
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
await writeFile(scriptPath, await response.text(), "utf8");
|
|
916
|
+
|
|
917
|
+
if (!isWindows) {
|
|
918
|
+
ensureExecutable(scriptPath);
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
const command = isWindows ? "powershell" : "sh";
|
|
922
|
+
const commandArgs = isWindows
|
|
923
|
+
? ["-ExecutionPolicy", "Bypass", "-File", scriptPath]
|
|
924
|
+
: [scriptPath];
|
|
925
|
+
|
|
926
|
+
const result = spawnSync(command, commandArgs, {
|
|
927
|
+
cwd: repoDir,
|
|
928
|
+
encoding: "utf8",
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
if (result.error) {
|
|
932
|
+
throw new CommandError(`Failed to run self-update: ${result.error.message}`);
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const stdout = String(result.stdout ?? "").trim();
|
|
936
|
+
const stderr = String(result.stderr ?? "").trim();
|
|
937
|
+
|
|
938
|
+
if (result.status !== 0) {
|
|
939
|
+
throw new CommandError(
|
|
940
|
+
[
|
|
941
|
+
"bgm-cli update failed.",
|
|
942
|
+
stdout,
|
|
943
|
+
stderr,
|
|
944
|
+
].filter(Boolean).join("\n"),
|
|
945
|
+
);
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
return {
|
|
949
|
+
action: "update",
|
|
950
|
+
platform: formatPlatformName(process.platform),
|
|
951
|
+
repoDir,
|
|
952
|
+
installDir,
|
|
953
|
+
output: stdout || "Update script completed.",
|
|
954
|
+
configFile: getConfigFilePath(),
|
|
955
|
+
shellHint: getUpdateShellHint(),
|
|
956
|
+
};
|
|
957
|
+
} finally {
|
|
958
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
main(process.argv.slice(2)).catch((error) => {
|
|
963
|
+
if (error instanceof CommandError) {
|
|
964
|
+
console.error(`Error: ${error.message}`);
|
|
965
|
+
process.exitCode = 1;
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
if (error instanceof ConfigError) {
|
|
970
|
+
console.error(`Error: ${error.message}`);
|
|
971
|
+
process.exitCode = 1;
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
if (error?.name === "BangumiApiError") {
|
|
976
|
+
console.error(`Bangumi API error (${error.status}): ${error.message}`);
|
|
977
|
+
if (error.details !== undefined) {
|
|
978
|
+
console.error(JSON.stringify(error.details, null, 2));
|
|
979
|
+
}
|
|
980
|
+
if (error.status === 401 && error.details?.code !== "CAPTCHA_ERROR") {
|
|
981
|
+
console.error("");
|
|
982
|
+
console.error("Tip: Your access token may have expired. Try refreshing it with:");
|
|
983
|
+
console.error(" bgm auth refresh --save");
|
|
984
|
+
console.error("Or set a new token with:");
|
|
985
|
+
console.error(" bgm auth set-token <access_token>");
|
|
986
|
+
}
|
|
987
|
+
process.exitCode = 1;
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
console.error(error);
|
|
992
|
+
process.exitCode = 1;
|
|
993
|
+
});
|