forked from
mary.my.id/atcute
a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
1import * as path from 'node:path';
2import * as fs from 'node:fs/promises';
3
4import { untar } from '@mary/tar';
5import prettier from 'prettier';
6
7const config = {
8 repo: `bluesky-social/atproto`,
9 path: `lexicons/`,
10 out: `lexdocs/bluesky/`,
11};
12
13async function main() {
14 const prettierConfig = await prettier.resolveConfig(process.cwd() + '/foo', { editorconfig: true });
15
16 let sha;
17 {
18 console.log(`retrieving latest commit`);
19 const response = await fetch(`https://api.github.com/repos/${config.repo}/commits?path=${config.path}/`);
20
21 if (!response.ok) {
22 console.log(` response error ${response.status}`);
23 return;
24 }
25
26 const json = await response.json();
27 const latest = json[0];
28
29 if (!latest) {
30 console.log(` latest commit missing?`);
31 return;
32 }
33
34 sha = latest.sha;
35 console.log(` got ${sha}`);
36 }
37
38 const tmpdir = `lexicons-tmp/`;
39
40 {
41 console.log(`retrieving zip file`);
42 const response = await fetch(`https://github.com/${config.repo}/archive/${sha}.tar.gz`);
43
44 if (!response.ok) {
45 console.log(` response error ${response.status}`);
46 return;
47 }
48
49 const reponame = config.repo.replace(/^.*?\//, '');
50 const basename = `${reponame}-${sha}/${config.path}`;
51
52 const ds = new DecompressionStream('gzip');
53 const stream = response.body.pipeThrough(ds);
54
55 const promises = [];
56
57 console.log(` reading`);
58 for await (const entry of untar(stream)) {
59 if (entry.type === 'file' && entry.name.startsWith(basename) && entry.name.endsWith('.json')) {
60 const name = entry.name.slice(basename.length);
61 const basedir = tmpdir + path.dirname(name);
62
63 const code = await entry.text();
64
65 const promise = (async () => {
66 const formatted = await prettier.format(code, { ...prettierConfig, parser: 'json' });
67
68 await fs.mkdir(basedir, { recursive: true });
69 await fs.writeFile(tmpdir + name, formatted);
70 })();
71
72 promises.push(promise);
73 }
74 }
75
76 console.log(` flushing writes`);
77 await Promise.all(promises);
78 }
79
80 {
81 const source = `https://github.com/${config.repo}/tree/${sha}/${config.path}\n`;
82
83 console.log(`writing readme file`);
84
85 await fs.writeFile(tmpdir + `README.md`, source);
86 }
87
88 {
89 console.log(`moving folder`);
90
91 await fs.rm(config.out, { recursive: true, force: true });
92 await fs.rename(tmpdir, config.out);
93 }
94}
95
96await main();