forked from hronro/protoc-gen-grpc-web-npm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-install.js
108 lines (99 loc) · 2.71 KB
/
post-install.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const AdmZip = require("adm-zip");
const fs = require("fs");
const https = require("https");
const path = require("path");
const PLUGIN = require("./");
let { version } = JSON.parse(
fs.readFileSync(path.join(__dirname, "package.json"), "utf-8")
); // read the version from the package.json
if (version.includes('-')) {
version = version.substring(0, version.indexOf('-'))
}
const DL_PREFIX =
"https://github.com/protocolbuffers/protobuf-javascript/releases/download/v";
const BIN_DIR = path.resolve(__dirname, "bin");
const EXT = process.platform === "win32" ? ".exe" : "";
const PLATFORM_NAME =
process.platform === "win32"
? "win"
: process.platform === "darwin"
? "osx-"
: "linux-";
const ARCH =
process.platform === "win32"
? process.arch === "ia32"
? "32"
: "64"
: process.arch === "ppc64"
? "ppcle_64"
: process.arch === "arm64"
? "aarch_64"
: process.arch === "s390x"
? "s390_64"
: process.arch === "ia32"
? "x86_32"
: "x86_64";
const resHandler = (resolve, reject, res) => {
if (res.statusCode === 302) {
https
.get(res.headers.location, (res2) => resHandler(resolve, reject, res2))
.on("error", reject);
} else {
const data = [];
res
.on("data", (chunk) => {
data.push(chunk);
})
.on("end", () => {
resolve(Buffer.concat(data));
});
}
};
async function run() {
if (
process.arch === "ppc" ||
process.arch === "arm" ||
process.arch === "mips" ||
process.arch === "mipsel" ||
process.arch === "s390"
) {
throw new Error(`Unsupported arch: ${process.arch}`);
}
if (!fs.existsSync(BIN_DIR)) fs.mkdirSync(BIN_DIR);
const zipFilename = `protobuf-javascript-${version}-${PLATFORM_NAME}${ARCH}.zip`;
const downloadUrl = DL_PREFIX + version + "/" + zipFilename;
console.log("Downloading", downloadUrl);
const buffer = await new Promise((resolve, reject) => {
https
.get(downloadUrl, (res) => resHandler(resolve, reject, res))
.on("error", reject);
});
let exeFilename = `bin/protoc-gen-js${EXT}`;
const zipFile = new AdmZip(buffer);
try {
zipFile.extractEntryTo(
exeFilename,
path.dirname(PLUGIN),
false,
true,
false,
path.basename(PLUGIN)
);
} catch (error) {
// 3.21.4 moved the file to be located in a nested folder named for the version in windows
exeFilename = `protobuf-javascript-${version}-${PLATFORM_NAME}${ARCH}/bin/protoc-gen-js${EXT}`
zipFile.extractEntryTo(
exeFilename,
path.dirname(PLUGIN),
false,
true,
false,
path.basename(PLUGIN)
);
}
fs.chmodSync(PLUGIN, "0755");
}
run().catch((e) => {
console.error(e);
process.exit(1);
});