forked from lexanth/python-ast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ts
48 lines (42 loc) · 1.46 KB
/
update.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
import { promises as fs } from 'fs';
import fetch from 'node-fetch';
import path from 'path';
import { getHead, grammerFiles, main as buildMain, mergeAll, withLog } from './build';
const main = async () => {
const isStale = await withLog('Checking if head is stale... ', getIsStale(), (isStaleValue) =>
isStaleValue ? 'Stale' : 'Up-to date',
);
if (!isStale && !process.argv.includes('--force')) {
console.log('Exiting, use --force to build anyway');
return;
}
await withLog('Updating head.json... ', updateHead());
await buildMain();
};
const getIsStale = async () => {
const [head, upstreamHead] = await Promise.all([getHead(), getUpstreamHead()]);
return grammerFiles.some((file) => head[file] !== upstreamHead[file]);
};
let upstreamHeadCache: { [file: string]: string } | undefined;
const getUpstreamHead = async () => {
if (upstreamHeadCache) return upstreamHeadCache;
const upstreamHead = mergeAll(
await Promise.all(
grammerFiles.map(async (file) => {
const res = await fetch(
`https://api.github.com/repos/antlr/grammars-v4/commits?path=python/python3-ts/${file}`,
);
const commits = await res.json();
return { [file]: commits[0].sha as string };
}),
),
);
upstreamHeadCache = upstreamHead;
return upstreamHead;
};
const updateHead = async () =>
fs.writeFile(
path.join(__dirname, 'src/head.json'),
JSON.stringify(await getUpstreamHead(), null, ' '),
);
main();