@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,156 @@
|
|
|
1
|
+
import { BangumiClient } from "../core/client.js";
|
|
2
|
+
import { getConfig } from "../core/config.js";
|
|
3
|
+
import { BangumiApiError } from "../core/http.js";
|
|
4
|
+
import { CommandError, printResult } from "../core/output.js";
|
|
5
|
+
import { firstPositional, getPositional, parseFlags } from "../utils/args.js";
|
|
6
|
+
import { normalizeNonNegativeInteger, normalizePositiveNumber } from "../utils/helpers.js";
|
|
7
|
+
import { SUBJECT_TYPE_MAP } from "../utils/validators.js";
|
|
8
|
+
import {
|
|
9
|
+
buildCollectionActionResult,
|
|
10
|
+
fetchMySubjectCollection,
|
|
11
|
+
fetchMySubjectCollectionVerified,
|
|
12
|
+
} from "../utils/collection-ops.js";
|
|
13
|
+
|
|
14
|
+
export async function runBookCommand(command, args, context) {
|
|
15
|
+
switch (command) {
|
|
16
|
+
case "get": {
|
|
17
|
+
const result = await executeBookGetCommand(args);
|
|
18
|
+
printResult(result, context);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
case "ep": {
|
|
22
|
+
const result = await executeBookEpCommand(args);
|
|
23
|
+
printResult(result, context);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
case "vol": {
|
|
27
|
+
const result = await executeBookVolCommand(args);
|
|
28
|
+
printResult(result, context);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
default:
|
|
32
|
+
throw new CommandError(
|
|
33
|
+
"Usage: bgm book <get|ep|vol> ...",
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function executeBookGetCommand(args) {
|
|
39
|
+
const options = parseFlags(args);
|
|
40
|
+
const client = new BangumiClient(getConfig());
|
|
41
|
+
const subjectId = firstPositional(options);
|
|
42
|
+
if (!subjectId) {
|
|
43
|
+
throw new CommandError("Usage: bgm book get <subject_id>");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const subject = await client.getSubject(subjectId);
|
|
47
|
+
if (Number(subject?.type) !== SUBJECT_TYPE_MAP.book) {
|
|
48
|
+
throw new CommandError(
|
|
49
|
+
`Subject ${subjectId} is not a book-type entry (${subject?.name || "unknown"}). Use \`bgm episode list\` or \`bgm episode watch\` for anime/game/real entries.`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let collection;
|
|
54
|
+
try {
|
|
55
|
+
collection = await fetchMySubjectCollection(client, subjectId);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (error instanceof BangumiApiError && error.status === 404) {
|
|
58
|
+
throw new CommandError(
|
|
59
|
+
`Subject ${subjectId} (${subject?.name || "unknown"}) is not in your collection. Add it first with \`bgm collection collect ${subjectId} doing\`.`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
resource: "book-progress",
|
|
67
|
+
action: "get",
|
|
68
|
+
actionLabel: "Book reading progress",
|
|
69
|
+
subjectId: Number(subjectId),
|
|
70
|
+
subject,
|
|
71
|
+
collection,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function executeBookEpCommand(args) {
|
|
76
|
+
const options = parseFlags(args);
|
|
77
|
+
const client = new BangumiClient(getConfig());
|
|
78
|
+
const subjectId = firstPositional(options);
|
|
79
|
+
const chapterNumber = normalizePositiveNumber(getPositional(options, 1), "chapter number");
|
|
80
|
+
if (!subjectId || chapterNumber === undefined) {
|
|
81
|
+
throw new CommandError("Usage: bgm book ep <subject_id> <chapter_number>");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const subject = await client.getSubject(subjectId);
|
|
85
|
+
if (Number(subject?.type) !== SUBJECT_TYPE_MAP.book) {
|
|
86
|
+
throw new CommandError(
|
|
87
|
+
`Subject ${subjectId} is not a book-type entry (${subject?.name || "unknown"}). Use \`bgm episode watch\` for anime/game/real entries.`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const collection = await ensureBookCollection(client, subjectId, subject);
|
|
92
|
+
await client.patchMyCollection(subjectId, { epStatus: chapterNumber });
|
|
93
|
+
|
|
94
|
+
const updatedCollection = await fetchMySubjectCollectionVerified(client, subjectId, {
|
|
95
|
+
expected: { ep_status: chapterNumber },
|
|
96
|
+
actionLabel: "Book chapter progress update",
|
|
97
|
+
mismatchMessage: (latest) =>
|
|
98
|
+
`Bangumi did not persist the requested chapter progress. Requested ep_status ${chapterNumber}, but read back ${latest?.ep_status ?? "-"}.`,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return buildCollectionActionResult({
|
|
102
|
+
action: "ep",
|
|
103
|
+
actionLabel: "Chapter progress updated",
|
|
104
|
+
subjectId,
|
|
105
|
+
subject,
|
|
106
|
+
collection: updatedCollection,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function executeBookVolCommand(args) {
|
|
111
|
+
const options = parseFlags(args);
|
|
112
|
+
const client = new BangumiClient(getConfig());
|
|
113
|
+
const subjectId = firstPositional(options);
|
|
114
|
+
const volumeNumber = normalizePositiveNumber(getPositional(options, 1), "volume number");
|
|
115
|
+
if (!subjectId || volumeNumber === undefined) {
|
|
116
|
+
throw new CommandError("Usage: bgm book vol <subject_id> <volume_number>");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const subject = await client.getSubject(subjectId);
|
|
120
|
+
if (Number(subject?.type) !== SUBJECT_TYPE_MAP.book) {
|
|
121
|
+
throw new CommandError(
|
|
122
|
+
`Subject ${subjectId} is not a book-type entry (${subject?.name || "unknown"}). Use \`bgm episode watch\` for anime/game/real entries.`,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const collection = await ensureBookCollection(client, subjectId, subject);
|
|
127
|
+
await client.patchMyCollection(subjectId, { volStatus: volumeNumber });
|
|
128
|
+
|
|
129
|
+
const updatedCollection = await fetchMySubjectCollectionVerified(client, subjectId, {
|
|
130
|
+
expected: { vol_status: volumeNumber },
|
|
131
|
+
actionLabel: "Book volume progress update",
|
|
132
|
+
mismatchMessage: (latest) =>
|
|
133
|
+
`Bangumi did not persist the requested volume progress. Requested vol_status ${volumeNumber}, but read back ${latest?.vol_status ?? "-"}.`,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return buildCollectionActionResult({
|
|
137
|
+
action: "vol",
|
|
138
|
+
actionLabel: "Volume progress updated",
|
|
139
|
+
subjectId,
|
|
140
|
+
subject,
|
|
141
|
+
collection: updatedCollection,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function ensureBookCollection(client, subjectId, subject) {
|
|
146
|
+
try {
|
|
147
|
+
return await fetchMySubjectCollection(client, subjectId);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error instanceof BangumiApiError && error.status === 404) {
|
|
150
|
+
throw new CommandError(
|
|
151
|
+
`Subject ${subjectId} (${subject?.name || "unknown"}) is not in your collection. Add it first with \`bgm collection collect ${subjectId} doing\`, then retry.`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
normalizeCollectionStatusValue,
|
|
18
18
|
normalizeSortOrder,
|
|
19
19
|
normalizeSubjectTypeFilter,
|
|
20
|
+
normalizeTagFilter,
|
|
20
21
|
} from "../utils/validators.js";
|
|
21
22
|
import { fetchAllCollections, sortCollections } from "../utils/collection.js";
|
|
22
23
|
import {
|
|
@@ -103,6 +104,7 @@ export async function executeCollectionListCommand(args) {
|
|
|
103
104
|
const order = normalizeSortOrder(options.order);
|
|
104
105
|
const limit = parseOptionalInteger(options.limit);
|
|
105
106
|
const offset = parseOptionalInteger(options.offset);
|
|
107
|
+
const tagFilter = normalizeTagFilter(options.tag);
|
|
106
108
|
|
|
107
109
|
const apiQuery = {};
|
|
108
110
|
if (subjectTypes.length === 1) {
|
|
@@ -125,6 +127,13 @@ export async function executeCollectionListCommand(args) {
|
|
|
125
127
|
data = data.filter((item) => allowed.has(item.type));
|
|
126
128
|
}
|
|
127
129
|
|
|
130
|
+
if (tagFilter.length > 0) {
|
|
131
|
+
data = data.filter((item) => {
|
|
132
|
+
const itemTags = item.interest?.tags ?? item.tags ?? [];
|
|
133
|
+
return tagFilter.every((tag) => itemTags.some((t) => String(t).toLowerCase() === tag.toLowerCase()));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
128
137
|
data = sortCollections(data, sort, order);
|
|
129
138
|
|
|
130
139
|
const start = offset ?? 0;
|
|
@@ -141,6 +150,7 @@ export async function executeCollectionListCommand(args) {
|
|
|
141
150
|
user: username,
|
|
142
151
|
status: collectionTypes,
|
|
143
152
|
subjectType: subjectTypes,
|
|
153
|
+
tag: tagFilter,
|
|
144
154
|
sort,
|
|
145
155
|
order,
|
|
146
156
|
offset: offset ?? 0,
|
package/src/commands/episode.js
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
import {
|
|
13
13
|
EPISODE_COLLECTION_STATUS_MAP,
|
|
14
14
|
EPISODE_TYPE_MAP,
|
|
15
|
+
SUBJECT_TYPE_MAP,
|
|
15
16
|
normalizeEpisodeCollectionStatusValue,
|
|
16
17
|
normalizeEpisodeTypeFilter,
|
|
17
18
|
} from "../utils/validators.js";
|
|
@@ -135,6 +136,15 @@ export async function executeEpisodeListCommand(args) {
|
|
|
135
136
|
handleEpisodeListError(error, subjectId);
|
|
136
137
|
}
|
|
137
138
|
|
|
139
|
+
if (filtered.length === 0) {
|
|
140
|
+
const subject = await client.getSubject(subjectId);
|
|
141
|
+
if (Number(subject?.type) === SUBJECT_TYPE_MAP.book) {
|
|
142
|
+
throw new CommandError(
|
|
143
|
+
`Subject ${subjectId} is a book-type entry. Bangumi does not provide episode lists for books. Use \`bgm book ep ${subjectId} <chapter_number>\` to track reading progress.`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
138
148
|
return {
|
|
139
149
|
...result,
|
|
140
150
|
resource: "episode-list",
|
|
@@ -200,6 +210,12 @@ export async function executeEpisodeWatchCommand(args) {
|
|
|
200
210
|
const episodes = await fetchAllEpisodes(client, subjectId, { type: EPISODE_TYPE_MAP.main });
|
|
201
211
|
const episode = episodes.find((item) => Number(item?.type) === EPISODE_TYPE_MAP.main && Number(item?.ep) === episodeNumber);
|
|
202
212
|
if (!episode) {
|
|
213
|
+
const subject = await client.getSubject(subjectId);
|
|
214
|
+
if (Number(subject?.type) === SUBJECT_TYPE_MAP.book) {
|
|
215
|
+
throw new CommandError(
|
|
216
|
+
`Subject ${subjectId} is a book-type entry. Bangumi does not support episode-level tracking for books. Use \`bgm book ep ${subjectId} ${episodeNumber}\` to update reading progress.`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
203
219
|
throw new CommandError(`Could not find main episode ${episodeNumber} under subject ${subjectId}.`);
|
|
204
220
|
}
|
|
205
221
|
|
|
@@ -372,6 +388,12 @@ async function resolveEpisodeByNumber(client, subjectId, episodeNumber, typeFilt
|
|
|
372
388
|
|
|
373
389
|
if (!episode) {
|
|
374
390
|
const typeLabel = typeFilter.label ?? "main";
|
|
391
|
+
const subject = await client.getSubject(subjectId);
|
|
392
|
+
if (Number(subject?.type) === SUBJECT_TYPE_MAP.book) {
|
|
393
|
+
throw new CommandError(
|
|
394
|
+
`Subject ${subjectId} is a book-type entry. Bangumi does not support episode-level tracking for books. Use \`bgm book ep ${subjectId} ${episodeNumber}\` to update reading progress.`,
|
|
395
|
+
);
|
|
396
|
+
}
|
|
375
397
|
throw new CommandError(`Could not find ${typeLabel} episode ${episodeNumber} under subject ${subjectId}.`);
|
|
376
398
|
}
|
|
377
399
|
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Search command group — powered by the SearchEncore API.
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ All commands in this group use the SearchEncore service at bgmdb.ry.mk.
|
|
5
|
+
* Results are crawled/aggregated data, NOT from bangumi.tv official sources.
|
|
6
|
+
* Every response carries _meta.isSearchEncore = true.
|
|
7
|
+
*
|
|
8
|
+
* This module is intentionally isolated from official API command groups
|
|
9
|
+
* (subject, user, group, etc.) to avoid path collisions.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
searchSubjects,
|
|
14
|
+
searchUsers,
|
|
15
|
+
searchGroups,
|
|
16
|
+
searchReplies,
|
|
17
|
+
searchGroupTopics,
|
|
18
|
+
searchSubjectTopics,
|
|
19
|
+
searchIndexes,
|
|
20
|
+
searchBlogs,
|
|
21
|
+
} from "../core/community-api.js";
|
|
22
|
+
import { CommandError, printResult } from "../core/output.js";
|
|
23
|
+
import { firstPositional, parseFlags } from "../utils/args.js";
|
|
24
|
+
import { normalizeNonNegativeInteger, normalizePageSize } from "../utils/helpers.js";
|
|
25
|
+
|
|
26
|
+
export async function runSearchCommand(command, args, context) {
|
|
27
|
+
switch (command) {
|
|
28
|
+
case "subject":
|
|
29
|
+
case "subjects": {
|
|
30
|
+
const result = await executeEntitySearch("subjects", args, searchSubjects);
|
|
31
|
+
printResult(result, context);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
case "user":
|
|
35
|
+
case "users": {
|
|
36
|
+
const result = await executeEntitySearch("users", args, searchUsers);
|
|
37
|
+
printResult(result, context);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
case "group":
|
|
41
|
+
case "groups": {
|
|
42
|
+
const result = await executeEntitySearch("groups", args, searchGroups);
|
|
43
|
+
printResult(result, context);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
case "topic":
|
|
47
|
+
case "topics":
|
|
48
|
+
case "group-topic":
|
|
49
|
+
case "group-topics": {
|
|
50
|
+
const result = await executeEntitySearch("group-topics", args, searchGroupTopics);
|
|
51
|
+
printResult(result, context);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
case "subject-topic":
|
|
55
|
+
case "subject-topics": {
|
|
56
|
+
const result = await executeEntitySearch("subject-topics", args, searchSubjectTopics);
|
|
57
|
+
printResult(result, context);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
case "reply":
|
|
61
|
+
case "replies": {
|
|
62
|
+
const result = await executeEntitySearch("replies", args, searchReplies);
|
|
63
|
+
printResult(result, context);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
case "index":
|
|
67
|
+
case "indexes": {
|
|
68
|
+
const result = await executeEntitySearch("indexes", args, searchIndexes);
|
|
69
|
+
printResult(result, context);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
case "blog":
|
|
73
|
+
case "blogs": {
|
|
74
|
+
const result = await executeEntitySearch("blogs", args, searchBlogs);
|
|
75
|
+
printResult(result, context);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
default:
|
|
79
|
+
throw new CommandError(
|
|
80
|
+
"Usage: bgm search <subject|user|group|topic|subject-topic|reply|index|blog> <keyword> [--limit n] [--offset n] [--sort <sort>]",
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function executeEntitySearch(resource, args, searchFn) {
|
|
86
|
+
const options = parseFlags(args);
|
|
87
|
+
const keyword = firstPositional(options);
|
|
88
|
+
if (!keyword) {
|
|
89
|
+
throw new CommandError(`Usage: bgm search ${resource} <keyword> [--limit n] [--offset n] [--sort <sort>]`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const query = {
|
|
93
|
+
q: keyword,
|
|
94
|
+
limit: normalizePageSize(options.limit),
|
|
95
|
+
offset: normalizeNonNegativeInteger(options.offset, "offset"),
|
|
96
|
+
};
|
|
97
|
+
if (options.sort) {
|
|
98
|
+
query.sort = options.sort;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const result = await searchFn(query);
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
...result,
|
|
105
|
+
resource: `community-${resource}`,
|
|
106
|
+
title: `SearchEncore: ${resource}`,
|
|
107
|
+
filters: {
|
|
108
|
+
keyword,
|
|
109
|
+
limit: query.limit,
|
|
110
|
+
offset: query.offset,
|
|
111
|
+
sort: query.sort,
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
package/src/commands/subject.js
CHANGED
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
import {
|
|
13
13
|
normalizeCollectionStatusValue,
|
|
14
14
|
normalizeSubjectType,
|
|
15
|
+
normalizeTagFilter,
|
|
16
|
+
normalizeTagsCat,
|
|
15
17
|
} from "../utils/validators.js";
|
|
16
18
|
import { fetchAllSubjects, sortSubjectsByRank } from "../utils/collection.js";
|
|
17
19
|
import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
|
|
@@ -131,6 +133,9 @@ export async function executeSubjectListCommand(args) {
|
|
|
131
133
|
|
|
132
134
|
const limit = parseOptionalInteger(options.limit);
|
|
133
135
|
const offset = parseOptionalInteger(options.offset);
|
|
136
|
+
const tagFilter = normalizeTagFilter(options.tag);
|
|
137
|
+
const tagsCat = normalizeTagsCat(options.tagsCat);
|
|
138
|
+
|
|
134
139
|
const query = {
|
|
135
140
|
type,
|
|
136
141
|
cat: options.cat,
|
|
@@ -139,6 +144,8 @@ export async function executeSubjectListCommand(args) {
|
|
|
139
144
|
sort: options.sort,
|
|
140
145
|
year: parseOptionalInteger(options.year),
|
|
141
146
|
month: parseOptionalInteger(options.month),
|
|
147
|
+
tags: tagFilter.length > 0 ? tagFilter : undefined,
|
|
148
|
+
tagsCat,
|
|
142
149
|
};
|
|
143
150
|
|
|
144
151
|
let result;
|
|
@@ -166,6 +173,8 @@ export async function executeSubjectListCommand(args) {
|
|
|
166
173
|
cat: options.cat,
|
|
167
174
|
series: parseOptionalBoolean(options.series),
|
|
168
175
|
platform: options.platform,
|
|
176
|
+
tag: tagFilter,
|
|
177
|
+
tagsCat,
|
|
169
178
|
},
|
|
170
179
|
};
|
|
171
180
|
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL command: turn a pasted Bangumi link into the equivalent bgm CLI command.
|
|
3
|
+
*
|
|
4
|
+
* Resolution is read-only. A link never triggers a write operation.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { CommandError, printResult } from "../core/output.js";
|
|
8
|
+
import {
|
|
9
|
+
formatResolvedCommand,
|
|
10
|
+
looksLikeBangumiUrl,
|
|
11
|
+
resolveBangumiUrl,
|
|
12
|
+
} from "../utils/bangumi-url.js";
|
|
13
|
+
import { runBlogCommand } from "./blog.js";
|
|
14
|
+
import { runCalendarCommand } from "./calendar.js";
|
|
15
|
+
import { runCharacterCommand } from "./character.js";
|
|
16
|
+
import { runCollectionCommand } from "./collection.js";
|
|
17
|
+
import { runEpisodeCommand } from "./episode.js";
|
|
18
|
+
import { runGroupCommand } from "./group.js";
|
|
19
|
+
import { runIndexCommand } from "./index.js";
|
|
20
|
+
import { runNotifyCommand } from "./notify.js";
|
|
21
|
+
import { runPersonCommand } from "./person.js";
|
|
22
|
+
import { runSubjectCommand } from "./subject.js";
|
|
23
|
+
import { runTimelineCommand } from "./timeline.js";
|
|
24
|
+
import { runUserCommand } from "./user.js";
|
|
25
|
+
|
|
26
|
+
const COMMAND_RUNNERS = {
|
|
27
|
+
blog: runBlogCommand,
|
|
28
|
+
calendar: runCalendarCommand,
|
|
29
|
+
character: runCharacterCommand,
|
|
30
|
+
collection: runCollectionCommand,
|
|
31
|
+
episode: runEpisodeCommand,
|
|
32
|
+
group: runGroupCommand,
|
|
33
|
+
index: runIndexCommand,
|
|
34
|
+
notify: runNotifyCommand,
|
|
35
|
+
person: runPersonCommand,
|
|
36
|
+
subject: runSubjectCommand,
|
|
37
|
+
timeline: runTimelineCommand,
|
|
38
|
+
user: runUserCommand,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const USAGE = "Usage: bgm [--json] url <bangumi_url> [--dry-run] [extra flags passed to the resolved command]";
|
|
42
|
+
|
|
43
|
+
export async function runUrlCommand(args, context = {}) {
|
|
44
|
+
const { url, dryRun, passthrough } = splitUrlArgs(args);
|
|
45
|
+
if (!url) {
|
|
46
|
+
throw new CommandError(USAGE);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const resolved = resolveBangumiUrl(url);
|
|
50
|
+
const commandArgs = [...resolved.args, ...passthrough];
|
|
51
|
+
|
|
52
|
+
if (dryRun) {
|
|
53
|
+
printResult(buildDryRunPayload(resolved, commandArgs), context);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const runner = COMMAND_RUNNERS[resolved.group];
|
|
58
|
+
if (!runner) {
|
|
59
|
+
throw new CommandError(`No CLI runner for command group: ${resolved.group}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
await runner(resolved.command, commandArgs, {
|
|
63
|
+
...context,
|
|
64
|
+
resolvedFrom: {
|
|
65
|
+
url: resolved.url,
|
|
66
|
+
site: resolved.site,
|
|
67
|
+
command: `${resolved.group} ${resolved.command}`,
|
|
68
|
+
args: commandArgs,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function buildDryRunPayload(resolved, commandArgs = resolved.args) {
|
|
74
|
+
return {
|
|
75
|
+
resource: "url-resolve",
|
|
76
|
+
url: resolved.url,
|
|
77
|
+
site: resolved.site,
|
|
78
|
+
path: resolved.path,
|
|
79
|
+
command: `${resolved.group} ${resolved.command}`,
|
|
80
|
+
args: commandArgs,
|
|
81
|
+
commandLine: formatResolvedCommand({ ...resolved, args: commandArgs }),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Split raw args into the URL, the --dry-run switch, and flags forwarded to the
|
|
87
|
+
* resolved command. Done by hand so `--dry-run <url>` cannot swallow the URL.
|
|
88
|
+
*/
|
|
89
|
+
export function splitUrlArgs(args) {
|
|
90
|
+
const tokens = args.filter((token) => token !== undefined && token !== null).map(String);
|
|
91
|
+
const rest = tokens.filter((token) => token !== "--dry-run");
|
|
92
|
+
const dryRun = tokens.length !== rest.length;
|
|
93
|
+
|
|
94
|
+
let urlIndex = rest.findIndex((token) => looksLikeBangumiUrl(token));
|
|
95
|
+
if (urlIndex === -1) {
|
|
96
|
+
urlIndex = rest.findIndex((token) => !token.startsWith("-"));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (urlIndex === -1) {
|
|
100
|
+
return { url: undefined, dryRun, passthrough: rest };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
url: rest[urlIndex],
|
|
105
|
+
dryRun,
|
|
106
|
+
passthrough: [...rest.slice(0, urlIndex), ...rest.slice(urlIndex + 1)],
|
|
107
|
+
};
|
|
108
|
+
}
|