-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.ts
63 lines (55 loc) · 2.49 KB
/
prepare.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
import {
Project,
PropertyAssignment,
SourceFile,
StringLiteral,
SyntaxKind,
} from "https://deno.land/x/[email protected]/mod.ts";
function getPropertiesOfObject(source: SourceFile, key: string) {
return source.getVariableDeclarationOrThrow(key)
.getInitializerIfKindOrThrow(SyntaxKind.AsExpression)
.getExpressionIfKindOrThrow(SyntaxKind.ObjectLiteralExpression)
.getProperties() as PropertyAssignment[];
}
function getObject(source: SourceFile, key: string) {
return getPropertiesOfObject(source, key).reduce((properties, property) => {
const propertyNameKind = property.getNameNode().getKind();
const propertyName = propertyNameKind === SyntaxKind.Identifier
? property.getName()
: property.getNameNode()
.asKindOrThrow(SyntaxKind.StringLiteral)
.getLiteralText();
const value = property.getInitializerIfKindOrThrow(
SyntaxKind.ArrayLiteralExpression,
).getElements().map((element) => {
return (element as StringLiteral).getLiteralText();
});
return { ...properties, [propertyName]: value };
}, {});
}
type Shortcuts = Record<string, string[]>;
// redirects to latest version of grammY = up to date filter queries.
const response = await fetch("https://deno.land/x/grammy/filter.ts");
if (!response.ok) {
console.log(response);
throw new Error("Request failed");
}
const filterFileContent = await response.text();
const project = new Project();
// NOTE: ts_morph doesn't resolve the imports in the filter.ts file because, currently
// FilterQuery and other local variables are independent of those relative imports.
// This logic needs to be changed if this isn't the situation in the future.
const source = project.createSourceFile(".filter.ts", filterFileContent);
const UPDATE_KEYS = getPropertiesOfObject(source, "UPDATE_KEYS")
.map((property) => property.getName()) as string[];
const L1_SHORTCUTS = getObject(source, "L1_SHORTCUTS") as Shortcuts;
const L2_SHORTCUTS = getObject(source, "L2_SHORTCUTS") as Shortcuts;
const FILTER_QUERIES = source.getTypeAliasOrThrow("FilterQuery")
.getType().getUnionTypes().map((u) => u.getLiteralValue()) as string[];
const modFile = `export default ${JSON.stringify(FILTER_QUERIES)};`;
const filterFile = `
export const UPDATE_KEYS = ${JSON.stringify(UPDATE_KEYS)};\n
export const L1_SHORTCUTS = ${JSON.stringify(L1_SHORTCUTS)};\n
export const L2_SHORTCUTS = ${JSON.stringify(L2_SHORTCUTS)};\n`;
await Deno.writeTextFile("./mod.ts", modFile);
await Deno.writeTextFile("./filter.ts", filterFile);