-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathinstall-re2-bin.ts
75 lines (68 loc) · 2.55 KB
/
install-re2-bin.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
import yargs from "yargs";
import https from "https";
import zlib from "zlib";
import fs from "fs-extra";
import {promisify} from "util";
import path from "path";
import assert from "assert";
const artifactPath = "./node_modules/re2/build/Release/re2.node";
const downloadFile = (url: string): Promise<Buffer> => {
return new Promise((resolve, reject) => {
let buffer: Buffer;
https
.get(url, (response) => {
const statusCode = response.statusCode;
assert(statusCode);
if (statusCode >= 300 && statusCode < 400 && response.headers && response.headers.location) {
downloadFile(response.headers.location).then(resolve, reject);
return;
}
if (response.statusCode != 200) {
reject(Error(`Status ${response.statusCode} for ${url}`));
return;
}
response.on("data", data => {
if (buffer) {
buffer = Buffer.concat([buffer, data]);
} else {
buffer = data;
}
});
response.on("end", () => resolve(buffer));
})
.on("error", e => reject(e))
.end();
});
};
const write = async (name: string, data: Buffer) => {
await fs.mkdir(path.dirname(name), {recursive: true});
await fs.writeFile(name, data);
};
const main = async () => {
const yparser = yargs(process.argv.slice(2));
const argv = await yparser
.option("platform", {
type: "string",
choices: ["darwin", "linux", "win32"],
demandOption: true,
})
.option("platformArch", {
type: "string",
choices: ["x64", "arm64"],
demandOption: true,
})
.option("platformABI", {
type: "string",
choices: ["127", "115"],
default: "127",
demandOption: true,
})
.argv;
const {platform, platformArch, platformABI} = argv;
const re2Version = JSON.parse(fs.readFileSync("./node_modules/re2/package.json", "utf8")).version;
const url = `https://github.com/uhop/node-re2/releases/download/${re2Version}/${platform}-${platformArch}-${platformABI}.gz`;
const artifact = await downloadFile(url);
console.log(`Patching ${artifactPath} with ${platform}-${platformArch}-${platformABI} ...`);
await write(artifactPath, await promisify(zlib.gunzip)(artifact));
};
await main();