-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.ts
executable file
·171 lines (156 loc) · 4.63 KB
/
build.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
#!/usr/bin/env node
import { isDeepStrictEqual } from "util";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { execFileSync } from "child_process";
interface SparkQLBinding {
type: string;
value: string;
}
interface SparkQL {
head: Array<string>; // We don't actually use this, but it gives us the keys for the bindings
results: {
bindings: Array<{
OSM_key: SparkQLBinding;
formatter_URL: SparkQLBinding;
rank: SparkQLBinding;
source: SparkQLBinding;
}>;
};
}
interface PackageJson {
author: string;
name: string;
description: string;
homepage: string;
contact_name: string;
contact_email: string;
}
interface IndexData {
key: string;
url: string;
source: string;
rank: string;
}
async function parse_osm_wikidata(): Promise<SparkQL> {
const text = readFileSync("wikidata.json", "utf8");
const osm_wikidata = JSON.parse(text);
const bindings = osm_wikidata.map((x) =>
Object.fromEntries(Object.entries(x).map(([k, v]) => [k, { value: v }])),
);
return { head: [], results: { bindings: bindings } };
}
async function writeWikidataSophoxRules(): Promise<
[Array<IndexData>, boolean]
> {
const osm_wikidata = await parse_osm_wikidata();
const fromWikidata = await sparql(
"https://query.wikidata.org/sparql",
"tag2link.wikidata.sparql",
);
// we used to have sparql("https://sophox.org/sparql", "tag2link.sophox.sparql"), but that was extremely outdated
const data = [
...fromWikidata.results.bindings,
...osm_wikidata.results.bindings,
].map(
(i) =>
({
key: i.OSM_key.value,
url: i.formatter_URL.value,
source: i.source.value,
rank: {
"http://wikiba.se/ontology#PreferredRank": "preferred",
"http://wikiba.se/ontology#NormalRank": "normal",
"http://wikiba.se/ontology#DeprecatedRank": "deprecated",
}[i.rank.value],
}) as IndexData,
);
data.sort((link1, link2) => {
const comparators = [
(x) => x.key,
(x) =>
({
preferred: "1",
normal: "2",
deprecated: "3",
})[x.rank] || "9",
(x) => x.url,
(x) => x.source,
];
return (
comparators
.map((cmp) => cmp(link1).localeCompare(cmp(link2)))
.find((i) => i !== 0) || 0
);
});
const original = existsSync("index.json")
? JSON.parse(readFileSync("index.json").toString())
: {};
const changed = !isDeepStrictEqual(original, data);
if (changed) {
console.log(`Writing ${data.length} rules to index.json`);
writeFileSync("index.json", JSON.stringify(data, undefined, 2));
} else {
console.log(`index.json did not need to be updated`);
}
return [data, changed];
}
function updatePackageVersion(now: Date): PackageJson {
const packageJson = JSON.parse(readFileSync("package.json").toString());
packageJson.version = now.getUTCFullYear() + '.' + (now.getUTCMonth() + 1) + '.' + now.getUTCDate();
console.log(`Updating package version to ${packageJson.version}`);
writeFileSync("package.json", JSON.stringify(packageJson, undefined, 2));
return packageJson;
}
function updateTag2Link(
tag2linkPackage: PackageJson,
data: Array<IndexData>,
now: Date,
): void {
const packageAuthor = tag2linkPackage.author.match(/(.*) <(.*)>/);
if (packageAuthor == null) {
throw TypeError("Author could not be parsed: " + tag2linkPackage.author);
}
const taginfo = {
data_format: 1,
data_updated: now.toISOString().replace(/-|:|\.\d{3}/g, ""),
project: {
name: tag2linkPackage.name,
description: tag2linkPackage.description,
project_url: tag2linkPackage.homepage,
contact_name: packageAuthor[1],
contact_email: packageAuthor[2],
},
tags: data.map(({ key, url }) => ({
key: key.replace(/^Key:/, ""),
description: url,
})),
};
console.log(`Updating taginfo.json`);
writeFileSync("taginfo.json", JSON.stringify(taginfo, undefined, 2));
}
async function main(): Promise<void> {
const [data, indexChanged] = await writeWikidataSophoxRules();
if (indexChanged) {
const now = new Date();
now.setMinutes(0, 0, 0);
const tag2linkPackage = updatePackageVersion(now);
updateTag2Link(tag2linkPackage, data, now);
}
}
async function sparql(url: string, filename: string): Promise<SparkQL> {
return JSON.parse(
curl(
"--request",
"POST",
"--header",
"Accept:application/json",
"--data-urlencode",
"query@" + filename,
url,
),
);
}
function curl(...args: string[]): string {
return execFileSync("curl", ["--silent", ...args]).toString();
}
main().catch((error) => console.log(error));