-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-json.ts
executable file
·84 lines (72 loc) · 2.31 KB
/
generate-json.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
#!/usr/bin/env bun
import { defineCommand, runMain } from "citty";
import { readdir } from "node:fs/promises";
import { $, file } from "bun";
import dayjs from "dayjs";
import { difference, intersection } from "remeda";
const DOCSET_EXTENSION = ".tgz";
const main = defineCommand({
meta: {
name: "json",
version: "1.0.0",
description: "Docsets",
},
async run({ args, subCommand }) {
const file = Bun.file("./docsets.json");
const fileContent = await file.json();
const docsets = {
docsets: {} as Record<
string,
{
name: string;
alias: string[];
archive: string;
version: string;
hash: string;
}
>,
};
const docsetFiles = (await readdir("./docsets"))
?.filter((file) => file.endsWith(DOCSET_EXTENSION))
?.map((file) => file.slice(0, -DOCSET_EXTENSION.length));
for (const docset of docsetFiles) {
const date = dayjs();
const docsetPath = `./docsets/${docset}${DOCSET_EXTENSION}`;
const resp = await $`shasum ${docsetPath}`;
const text = resp.text();
const hash = text.split(" ")[0];
const version = `1.${date.format("YYMMDD")}.${dayjs().format("HHmm")}`;
docsets.docsets[docset] = {
name: docset,
alias: [docset],
archive: `${docset}${DOCSET_EXTENSION}`,
version,
hash,
};
}
const fileContentKeys = Object.keys(fileContent.docsets);
const docsetFilesKeys = Object.keys(docsets.docsets);
const toUpdate = intersection(fileContentKeys, docsetFilesKeys);
const newDocsets = difference(docsetFilesKeys, fileContentKeys);
const removedDocsets = difference(fileContentKeys, docsetFilesKeys);
for (const docset of toUpdate) {
const fileContentDocset = fileContent.docsets[docset];
const docsetFilesDocset = docsets.docsets[docset];
if (fileContentDocset.hash !== docsetFilesDocset.hash) {
console.log("Updating hash for", docset);
fileContent.docsets[docset].version = docsetFilesDocset.version;
fileContent.docsets[docset].hash = docsetFilesDocset.hash;
}
}
for (const docset of newDocsets) {
console.log("Adding new docset", docset);
fileContent.docsets[docset] = docsets.docsets[docset];
}
for (const docset of removedDocsets) {
console.log("Removing docset", docset);
delete fileContent.docsets[docset];
}
Bun.write(file, JSON.stringify(fileContent, null, 2));
},
});
runMain(main);