-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathgenerate-db.js
executable file
·71 lines (57 loc) · 2.07 KB
/
generate-db.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
#!/usr/bin/env node
import { readFile, writeFile } from 'node:fs/promises';
import { gunzip } from 'node:zlib';
import { join } from 'node:path';
import { promisify } from 'node:util';
const gunzipAsync = promisify(gunzip);
// TODO(@thunder-coding): Do not hardcode list of known architectures.
const archs = ["aarch64", "arm", "i686", "x86_64"];
const { TERMUX_PKG_CACHEDIR, TERMUX_PREFIX } = process.env;
if (!TERMUX_PKG_CACHEDIR) {
throw new Error('TERMUX_PKG_CACHEDIR environment variable is not defined');
}
if (!TERMUX_PREFIX) {
throw new Error('TERMUX_PREFIX environment variable is not defined');
}
const binPrefix = TERMUX_PREFIX.substring(1) + '/bin/';
const repos = JSON.parse(await readFile(join(TERMUX_PKG_CACHEDIR, 'repo.json')));
async function processRepo(repo) {
for (const arch of archs) {
const url = `${repo.url}/dists/${repo.distribution}/Contents-${arch}.gz`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`${url} returned ${response.status}`);
}
const data = await gunzipAsync(await response.arrayBuffer());
const binMap = {};
data
.toString()
.split('\n')
.filter(line => line.startsWith(binPrefix))
.forEach(line => {
const [pathToBinary, packageNames] = line.split(' ');
const binary = pathToBinary.substring(pathToBinary.lastIndexOf('/') + 1);
const packages = packageNames.split(',');
packages.forEach(packageName => {
binMap[packageName] ??= [];
binMap[packageName].push(binary);
});
});
const headerFile = `commands-${arch}-${repo.name}.h`;
const header = Object.keys(binMap)
.sort()
.map(packageName => {
const binaries = binMap[packageName].sort().map(bin => `" ${bin}",`);
return `"${packageName}",\n${binaries.join('\n')}`;
})
.join('\n');
await writeFile(headerFile, header);
}
}
const promises = [];
for (const path in repos) {
if (path === 'pkg_format') continue;
const repo = repos[path];
promises.push(processRepo(repo));
}
await Promise.all(promises);