@aronnaxlin/bgm-cli 1.1.5 → 1.1.6
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/package.json +1 -1
- package/src/commands/search.js +66 -9
- package/src/utils/validators.js +17 -0
package/package.json
CHANGED
package/src/commands/search.js
CHANGED
|
@@ -20,14 +20,19 @@ import {
|
|
|
20
20
|
searchBlogs,
|
|
21
21
|
} from "../core/community-api.js";
|
|
22
22
|
import { CommandError, printResult } from "../core/output.js";
|
|
23
|
-
import { firstPositional, parseFlags } from "../utils/args.js";
|
|
24
|
-
import {
|
|
23
|
+
import { firstPositional, parseFlags, splitFilterValues } from "../utils/args.js";
|
|
24
|
+
import {
|
|
25
|
+
normalizeNonNegativeInteger,
|
|
26
|
+
normalizePageSize,
|
|
27
|
+
parseOptionalBoolean,
|
|
28
|
+
} from "../utils/helpers.js";
|
|
29
|
+
import { normalizeSubjectTypeName } from "../utils/validators.js";
|
|
25
30
|
|
|
26
31
|
export async function runSearchCommand(command, args, context) {
|
|
27
32
|
switch (command) {
|
|
28
33
|
case "subject":
|
|
29
34
|
case "subjects": {
|
|
30
|
-
const result = await executeEntitySearch("subjects", args, searchSubjects);
|
|
35
|
+
const result = await executeEntitySearch("subjects", args, searchSubjects, { allowSubjectType: true });
|
|
31
36
|
printResult(result, context);
|
|
32
37
|
return;
|
|
33
38
|
}
|
|
@@ -77,7 +82,7 @@ export async function runSearchCommand(command, args, context) {
|
|
|
77
82
|
}
|
|
78
83
|
default:
|
|
79
84
|
throw new CommandError(
|
|
80
|
-
"Usage: bgm search <subject|user|group|topic|subject-topic|reply|index|blog> <keyword> [--limit n] [--offset n] [--sort <sort>]",
|
|
85
|
+
"Usage: bgm search <subject|user|group|topic|subject-topic|reply|index|blog> <keyword> [--limit n] [--offset n] [--sort <sort>] [--user <name>] [--group <slug>] [--exact] [--include <nsfw|blocked>] [--exclude nsfw] [--type <type>] [--source <source>]",
|
|
81
86
|
);
|
|
82
87
|
}
|
|
83
88
|
}
|
|
@@ -86,15 +91,61 @@ const REPLY_SOURCES = new Set([
|
|
|
86
91
|
"all", "group", "subject", "episode", "ep", "character", "crt", "person", "prsn", "blog",
|
|
87
92
|
]);
|
|
88
93
|
|
|
89
|
-
|
|
94
|
+
// SearchEncore 内联指令的合法取值(写入 q,而非独立 query 参数)。
|
|
95
|
+
const DIRECTIVE_INCLUDE_VALUES = new Set(["nsfw", "blocked"]);
|
|
96
|
+
const DIRECTIVE_EXCLUDE_VALUES = new Set(["nsfw"]);
|
|
97
|
+
|
|
98
|
+
function directiveValues(flag, flagName, { allowed, lowercase = true } = {}) {
|
|
99
|
+
if (flag === undefined) {
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
if (flag === true) {
|
|
103
|
+
throw new CommandError(`--${flagName} requires a value.`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return splitFilterValues(flag).map((entry) => {
|
|
107
|
+
const raw = String(entry);
|
|
108
|
+
const value = lowercase ? raw.toLowerCase() : raw;
|
|
109
|
+
if (allowed && !allowed.has(value)) {
|
|
110
|
+
throw new CommandError(
|
|
111
|
+
`Invalid --${flagName}: ${entry}. Use one of: ${[...allowed].join(", ")}.`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return value;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function executeEntitySearch(resource, args, searchFn, { allowSource = false, allowSubjectType = false } = {}) {
|
|
90
119
|
const options = parseFlags(args);
|
|
91
|
-
const
|
|
92
|
-
if (!
|
|
120
|
+
const rawKeyword = firstPositional(options);
|
|
121
|
+
if (!rawKeyword) {
|
|
93
122
|
throw new CommandError(
|
|
94
|
-
`Usage: bgm search ${resource} <keyword> [--limit n] [--offset n] [--sort <sort>]${allowSource ? " [--source <source>]" : ""}`,
|
|
123
|
+
`Usage: bgm search ${resource} <keyword> [--limit n] [--offset n] [--sort <sort>] [--user <name>] [--group <slug>] [--exact] [--include <nsfw|blocked>] [--exclude nsfw]${allowSubjectType ? " [--type <book|anime|music|game|real>]" : ""}${allowSource ? " [--source <source>]" : ""}`,
|
|
95
124
|
);
|
|
96
125
|
}
|
|
97
126
|
|
|
127
|
+
// 通用指令:用户名/组 slug 保留原始大小写,不做小写归一化。
|
|
128
|
+
const users = directiveValues(options.user, "user", { lowercase: false });
|
|
129
|
+
const groups = directiveValues(options.group, "group", { lowercase: false });
|
|
130
|
+
const includes = directiveValues(options.include, "include", { allowed: DIRECTIVE_INCLUDE_VALUES });
|
|
131
|
+
const excludes = directiveValues(options.exclude, "exclude", { allowed: DIRECTIVE_EXCLUDE_VALUES });
|
|
132
|
+
const exact = parseOptionalBoolean(options.exact);
|
|
133
|
+
|
|
134
|
+
// --type 只对 subject 资源生效;其他资源传了会被忽略(与服务端忽略不适用指令的语义一致)。
|
|
135
|
+
const subjectTypes = allowSubjectType
|
|
136
|
+
? directiveValues(options.type, "type").map((entry) => normalizeSubjectTypeName(entry))
|
|
137
|
+
: [];
|
|
138
|
+
|
|
139
|
+
const directives = [
|
|
140
|
+
...users.map((name) => `user:${name}`),
|
|
141
|
+
...groups.map((slug) => `group:${slug}`),
|
|
142
|
+
...subjectTypes.map((name) => `type:${name}`),
|
|
143
|
+
...includes.map((value) => `include:${value}`),
|
|
144
|
+
...excludes.map((value) => `exclude:${value}`),
|
|
145
|
+
...(exact === true ? ["exact:true"] : []),
|
|
146
|
+
];
|
|
147
|
+
const keyword = directives.length > 0 ? `${rawKeyword} ${directives.join(" ")}` : rawKeyword;
|
|
148
|
+
|
|
98
149
|
const query = {
|
|
99
150
|
q: keyword,
|
|
100
151
|
limit: normalizePageSize(options.limit),
|
|
@@ -121,10 +172,16 @@ async function executeEntitySearch(resource, args, searchFn, { allowSource = fal
|
|
|
121
172
|
resource: `community-${resource}`,
|
|
122
173
|
title: `SearchEncore: ${resource}`,
|
|
123
174
|
filters: {
|
|
124
|
-
keyword,
|
|
175
|
+
keyword: rawKeyword,
|
|
125
176
|
limit: query.limit,
|
|
126
177
|
offset: query.offset,
|
|
127
178
|
sort: query.sort,
|
|
179
|
+
...(users.length > 0 ? { user: users } : {}),
|
|
180
|
+
...(groups.length > 0 ? { group: groups } : {}),
|
|
181
|
+
...(subjectTypes.length > 0 ? { type: subjectTypes } : {}),
|
|
182
|
+
...(includes.length > 0 ? { include: includes } : {}),
|
|
183
|
+
...(excludes.length > 0 ? { exclude: excludes } : {}),
|
|
184
|
+
...(exact === true ? { exact: true } : {}),
|
|
128
185
|
...(query.source ? { source: query.source } : {}),
|
|
129
186
|
},
|
|
130
187
|
};
|
package/src/utils/validators.js
CHANGED
|
@@ -38,6 +38,10 @@ export const SUBJECT_TYPE_MAP = {
|
|
|
38
38
|
real: 6,
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
const SUBJECT_TYPE_NAME_MAP = Object.fromEntries(
|
|
42
|
+
Object.entries(SUBJECT_TYPE_MAP).map(([name, id]) => [id, name]),
|
|
43
|
+
);
|
|
44
|
+
|
|
41
45
|
export const COLLECTION_STATUS_MAP = {
|
|
42
46
|
wish: 1,
|
|
43
47
|
collect: 2,
|
|
@@ -140,6 +144,19 @@ export function normalizeSubjectType(value) {
|
|
|
140
144
|
return normalized;
|
|
141
145
|
}
|
|
142
146
|
|
|
147
|
+
/**
|
|
148
|
+
* 归一化为规范的小写类型名(book|anime|music|game|real),
|
|
149
|
+
* 供需要字符串类型名而非数字 ID 的场景使用(如 SearchEncore 的 `type:` 指令)。
|
|
150
|
+
*/
|
|
151
|
+
export function normalizeSubjectTypeName(value) {
|
|
152
|
+
const numeric = normalizeSubjectType(value);
|
|
153
|
+
if (numeric === undefined) {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return SUBJECT_TYPE_NAME_MAP[numeric];
|
|
158
|
+
}
|
|
159
|
+
|
|
143
160
|
export function normalizeSubjectTypeFilter(value) {
|
|
144
161
|
if (value === undefined || value === null || value === "") {
|
|
145
162
|
return [];
|