-
-
Notifications
You must be signed in to change notification settings - Fork 735
/
Copy pathdownload_plugins.js
90 lines (79 loc) · 2.69 KB
/
download_plugins.js
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
//@ts-check
// Helper script to download recently updated plugins
// Used to review code changes for breaking changes.
// 180 is somewhat arbitrary.
const CUTOFF_DAYS = 180;
const CUTOFF_MS = Date.now() - 1000 * 60 * 60 * 24 * CUTOFF_DAYS;
const cp = require("child_process");
const path = require("path");
const https = require("https");
const fs = require("fs");
/**
* @param {string} command
* @returns {Promise<string>}
*/
function exec(command) {
return new Promise((resolve, reject) => {
cp.exec(command, (err, stdout, stderr) => {
if (err) reject(err);
if (stderr.length)
reject(new Error(`Command: ${command}\n${stderr}`));
resolve(stdout.trim());
});
});
}
async function getPlugins() {
const plugins = JSON.parse(await exec("npm search --json typedocplugin"));
return plugins.filter((plugin) => Date.parse(plugin.date) > CUTOFF_MS);
}
function getTarballUrl(package) {
return exec(`npm view ${package.name} dist.tarball`);
}
function downloadTarball(url, outDir) {
const outFile = path.join(outDir, path.basename(url));
return new Promise((resolve, reject) => {
const out = fs.createWriteStream(outFile);
out.on("finish", () => {
out.close();
resolve(outFile);
});
https
.get(url, (response) => {
response.pipe(out);
})
.on("error", (err) => {
reject(err);
});
});
}
// Could do this with node... but this works in my environment which is the only place I
// expect it to be used. If you want to use this, somewhere else, feel free to update.
async function inflate(file) {
await exec(`gzip -d "${file}"`);
await fs.promises.mkdir(file.replace(".tgz", ""));
await exec(
`tar -C "${file.replace(".tgz", "")}" -xf "${file.replace(
".tgz",
".tar",
)}"`,
);
}
/** @param {string[]} args */
async function main(args) {
const outDir = path.resolve(args[0] || "../typedoc_plugins");
const plugins = await getPlugins();
console.log(
`Found ${plugins.length} plugins updated in the past ${CUTOFF_DAYS} days.`,
);
const tarballs = await Promise.all(plugins.map(getTarballUrl));
console.log(`Downloading tarballs...`);
await fs.promises.rm(outDir, { recursive: true, force: true });
await fs.promises.mkdir(outDir, { recursive: true });
const tarballFiles = await Promise.all(
tarballs.map((tar) => downloadTarball(tar, outDir)),
);
console.log(`Inflating...`);
await Promise.all(tarballFiles.map(inflate));
console.log(`Done.`);
}
main(process.argv.slice(2)).catch(console.error);