-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
executable file
·403 lines (334 loc) · 13.1 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env bun
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as process from 'node:process';
import chalk from 'chalk';
import yargs from 'yargs/yargs';
import config from './config';
const stateDir = path.join(import.meta.dir, 'state');
const oldStateFilePath = path.join(stateDir, 'previous-state.json');
const stateFilePath = path.join(stateDir, 'state.json');
yargs(process.argv.slice(2))
.scriptName(import.meta.file)
.usage('$0 <cmd> [args]')
.help()
.version(false)
.strict()
.recommendCommands()
.demandCommand()
.command({
command: 'discover',
describe: `Browse Paradox Mods to search for translation platforms links and writes results to output/state.json for later markdown generation. Uses configuration from config.ts.`,
builder: args =>
args.option('write', {
type: 'boolean',
describe: `Whether to update state files even if no new/updated/deleted mods were detected.`,
default: false
}),
async handler(args) {
process.stdout.write(chalk.bold(`=> Listing mods\n\n`));
const listingMods = await listMods(
config.gameId,
config.tags.map(String)
);
process.stdout.write(chalk.bold(`\n=> Fetching mod details\n\n`));
const mods = await getModsDetails(listingMods);
process.stdout.write(chalk.bold(`\n=> Writing state file...\n\n`));
const translatableMods = mods
.filter(mod => mod.translationLink)
.sort(
(a, b) =>
Number.parseInt(a.modId, 10) -
Number.parseInt(b.modId, 10)
);
process.stdout.write(
`Found ${chalk.bold.greenBright(translatableMods.length)} mods with translations.\n\n`
);
if (!args.write && (await fs.exists(oldStateFilePath))) {
const oldMods: Mod[] = JSON.parse(
await fs.readFile(stateFilePath, 'utf8')
);
const newIds = new Set(translatableMods.map(mod => mod.modId));
const oldIds = new Set(oldMods.map(mod => mod.modId));
const hasChanges = newIds.symmetricDifference(oldIds).size > 0;
if (!hasChanges) {
process.stdout.write(
`${chalk.bold(`No new/updated/deleted mods detected.`)} Re-run the command using --write to force state files update.\n`
);
return;
}
}
if (await fs.exists(stateFilePath)) {
process.stdout.write(
`Moving ${stateFilePath} to ${oldStateFilePath}...\n`
);
await fs.rename(stateFilePath, oldStateFilePath);
}
process.stdout.write(`Writing ${stateFilePath}...\n`);
await fs.writeFile(
stateFilePath,
`${JSON.stringify(translatableMods, null, 2)}\n`
);
process.stdout.write(
`\nRun ${chalk.bold('list')} or ${chalk.bold('changelog')} subcommands for generating summaries.\n`
);
}
})
.command({
command: 'list',
describe: `Generate a markdown list of mods with translation links from output/state.json.`,
builder: args =>
args.option('write', {
type: 'boolean',
describe: `Whether to write the content generated to the file specified in the config.ts file.`,
default: false
}),
async handler(args) {
if (!(await fs.exists(stateFilePath))) {
throw new Error(
`File not found: ${stateFilePath}. Run "discover" command first.`
);
}
const mods: Mod[] = JSON.parse(
await fs.readFile(stateFilePath, 'utf8')
);
const listString = mods
.sort((a, b) => b.installedCount - a.installedCount)
.map((mod, i) => formatModLine(mod, i, false))
.join('\n');
let template = '{{list}}\n';
if (config.listMarkdownTemplateFilePath) {
const listTemplateFilePath = path.resolve(
import.meta.dir,
config.listMarkdownTemplateFilePath
);
if (await fs.exists(listTemplateFilePath)) {
template = await fs.readFile(listTemplateFilePath, 'utf8');
} else {
process.stderr.write(
chalk.bold.red(
`Template file not found: ${listTemplateFilePath}\n`
)
);
}
}
const contents = template
.replace('{{date}}', new Date().toLocaleString())
.replace('{{list}}', listString);
process.stdout.write(contents);
if (args.write) {
const filePath = path.resolve(
import.meta.dir,
config.listMarkdownFilePath
);
await fs.writeFile(filePath, contents);
process.stdout.write(
chalk.bold(`\nWritten to "${filePath}".\n`)
);
}
}
})
.command({
command: 'changelog',
describe: `Generate a markdown changelog of mods with translation links from output/state.json.`,
async handler() {
let oldMods: Mod[] = [];
if (await fs.exists(oldStateFilePath)) {
oldMods = JSON.parse(
await fs.readFile(oldStateFilePath, 'utf8')
);
}
if (!(await fs.exists(stateFilePath))) {
throw new Error(
`File not found: ${stateFilePath}. Run "discover" command first.`
);
}
const mods: Mod[] = JSON.parse(
await fs.readFile(stateFilePath, 'utf8')
);
const newMods = mods
.filter(
mod => !oldMods.some(oldMod => oldMod.modId == mod.modId)
)
.sort((a, b) => b.installedCount - a.installedCount);
const deletedMods = oldMods
.filter(oldMod => !mods.some(mod => mod.modId == oldMod.modId))
.sort((a, b) => b.installedCount - a.installedCount);
const updatedMods = mods
.filter(mod =>
oldMods.some(
oldMod =>
oldMod.modId == mod.modId &&
oldMod.translationLink != mod.translationLink
)
)
.sort((a, b) => b.installedCount - a.installedCount);
// biome-ignore lint/complexity/useSimplifiedLogicExpression: nonsensical
if (!newMods.length && !updatedMods.length && !deletedMods.length) {
process.stdout.write(
`No changes detected between ${stateFilePath} and ${oldStateFilePath} (if it exists).\n`
);
return;
}
const listString = [
'**List update!**',
...(newMods.length
? [
`\nNew translation projects discovered:`,
...newMods.map((mod, i) =>
formatModLine(mod, i, true)
)
]
: []),
...(updatedMods.length
? [
`\nMods that updated their translation project link:`,
...updatedMods.map((mod, i) =>
formatModLine(mod, i, true)
)
]
: []),
...(deletedMods.length
? [
`\nDeleted mods or translation projects:`,
...deletedMods.map((mod, i) =>
formatModLine(mod, i, true)
)
]
: []),
''
].join('\n');
process.stdout.write(`${listString}\n`);
}
}).argv;
interface ListingMod {
readonly modId: string;
readonly displayName: string;
readonly author: string;
readonly os: string;
readonly installedCount: number;
}
interface Mod extends ListingMod {
readonly translationLink: string | undefined;
}
async function listMods(
gameName: string,
tags: readonly string[]
): Promise<readonly ListingMod[]> {
const allMods = [];
let page = 0;
do {
const { totalCount, mods } = await fetchPage(page++);
allMods.push(...mods);
process.stdout.write(
`Listed ${allMods.length} of ${totalCount} mods...\n`
);
if (allMods.length >= totalCount) {
break;
}
// biome-ignore lint/correctness/noConstantCondition: break condition
} while (true);
return allMods;
async function fetchPage(page: number) {
const response = await fetch(
'https://api.paradox-interactive.com/mods',
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
gameName,
tags,
page,
pageSize: 100 // max allowed by PDX Mods
})
}
);
interface Body {
readonly totalCount: number;
readonly mods: ReadonlyArray<ListingMod>;
}
const { mods, totalCount } = await parseApiResponse<Body>(response);
return {
totalCount,
mods: mods.map<ListingMod>(mod => ({
modId: mod.modId,
displayName: mod.displayName,
author: mod.author,
os: mod.os,
installedCount: mod.installedCount
}))
};
}
}
async function getModsDetails(
listingMods: readonly ListingMod[]
): Promise<readonly Mod[]> {
const mods: Mod[] = [];
for (let i = 0; i < listingMods.length; i++) {
// biome-ignore lint/style/noNonNullAssertion: index is always valid
const listingMod = listingMods[i]!;
process.stdout.write(
`${i + 1}/${listingMods.length}: ${listingMod.displayName}... `
);
const mod = await fetchMod(listingMod);
mods.push(mod);
process.stdout.write(
mod.translationLink
? chalk.greenBright(
`✅ Found ${chalk.bold(mod.translationLink)}\n`
)
: chalk.dim('❌ No translations\n')
);
}
return mods;
async function fetchMod(listingMod: ListingMod): Promise<Mod> {
const response = await fetch(
`https://api.paradox-interactive.com/mods?modId=${listingMod.modId}&os=${listingMod.os}`,
{ method: 'GET' }
);
interface Body {
readonly modDetail: {
readonly longDescription: string;
readonly externalLinks: ReadonlyArray<{ url: string }>;
};
}
const { modDetail } = await parseApiResponse<Body>(response);
const textToSearch = `
${modDetail.externalLinks.map(link => `${link.url}\n`)}
${modDetail.longDescription}`;
const linkLike = textToSearch.match(
/(crowdin\.com\/project\/[-a-z0-9]+|paratranz\.cn\/projects\/[0-9]+)/im
)?.[0];
return {
...listingMod,
translationLink: linkLike && `https://${linkLike}`
};
}
}
async function parseApiResponse<TBody>(response: Response): Promise<TBody> {
interface Body {
readonly result: 'OK' | 'Failure';
readonly errorMessage?: string;
}
let body: Body = { result: 'Failure', errorMessage: 'Unknown error' };
try {
body = await response.json();
} catch {
// Ignore body consumption error.
}
if (!response.ok || body.result != 'OK') {
throw new Error(
`Failed to fetch "${response.url}" (${response.status} ${response.statusText}): ${body.errorMessage}`
);
}
return body as TBody;
}
function formatModLine(mod: Mod, index: number, changelog: boolean): string {
// biome-ignore lint/style/noNonNullAssertion: can't be null here
const host = new URL(mod.translationLink!).host;
const platform = host == 'crowdin.com' ? '' : ` (on ${host})`;
const installedCount = Intl.NumberFormat().format(mod.installedCount);
return `${index + 1}. [${mod.displayName}](${mod.translationLink})${platform}${changelog ? '' : ` ([mod page](https://mods.paradoxplaza.com/mods/${mod.modId}/${mod.os}))`} by *${mod.author}* — ⬇️ ${installedCount} installs`;
}