-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-qualities.ts
64 lines (57 loc) · 2.24 KB
/
generate-qualities.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
import { execSync } from "child_process";
import { constantCase } from "constant-case";
import fetch from "node-fetch";
import path from "path";
import { Project, SyntaxKind, VariableDeclarationKind } from "ts-morph";
const SOURCE_URL = "https://raw.githubusercontent.com/filebot/data/master/media-sources.txt";
const OUTPUT_PATH = path.join(__dirname, "../src/data/qualities.data.ts");
const FILE_DELIMITER = "\t";
const PATTERN_PREFIX = "(?<!^)(?:\\.|\\[| |^)";
const PATTERN_SUFFIX = "(?=\\.|\\]| |$)";
const GENERATED_DATE = new Date().toISOString();
const project = new Project();
async function main() {
const response = await fetch(SOURCE_URL);
if (!response.ok) throw new Error(`Bad response ${response.status}`);
const body = await response.text();
const data: Array<{ key: string; name: string; pattern: RegExp }> = [];
const lines = body.trim().split("\n");
for (const line of lines) {
const fields = line.split(FILE_DELIMITER);
if (fields.length !== 2) throw new Error(`Malformed source file.`);
const [name, pattern] = fields;
const formattedPattern = new RegExp(`${PATTERN_PREFIX}(${pattern})${PATTERN_SUFFIX}`, "gi");
data.push({ key: constantCase(name), name, pattern: formattedPattern });
}
const file = project.createSourceFile(OUTPUT_PATH, undefined, { overwrite: true });
file.addEnum({
name: "Quality",
isExported: true,
leadingTrivia: `// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.\n// Last updated: ${GENERATED_DATE}\n\n`,
members: data.map(({ key, name }) => ({
name: key,
value: name,
})),
});
const qualities = file.addVariableStatement({
isExported: true,
declarationKind: VariableDeclarationKind.Const,
declarations: [
{
name: "QUALITIES",
initializer: "[]",
},
],
});
const declaration = qualities.getDeclarations()[0];
const arrayLiteral = declaration.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression);
for (const quality of data) {
arrayLiteral.addElement(`{ \nname: Quality.${quality.key}, \npattern: ${quality.pattern}\n }`);
}
project.saveSync();
console.log(`Created ${OUTPUT_PATH}, formatting...`);
execSync(`prettier --write ${OUTPUT_PATH}`, {
stdio: "inherit",
});
}
main();