Skip to content

Commit 800c3c9

Browse files
committed
Add postinstall hook for fetching official protoc-gen-js binary
1 parent cbeded2 commit 800c3c9

File tree

7 files changed

+152
-5
lines changed

7 files changed

+152
-5
lines changed

MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

download-protoc-gen-js.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const fs = require('fs');
2+
const os = require('os');
3+
const path = require('path');
4+
const AdmZip = require('adm-zip');
5+
6+
const VERSION = '4.0.0';
7+
const BIN_DIR = path.join(__dirname, 'bin');
8+
9+
function getPlatform() {
10+
const platform = os.platform();
11+
if (platform === 'darwin') {
12+
return 'osx';
13+
}
14+
if (platform === 'win32') {
15+
return 'windows';
16+
}
17+
return platform;
18+
}
19+
20+
function getArch() {
21+
const arch = os.arch();
22+
if (arch === 'x64') {
23+
return 'x86_64';
24+
}
25+
if (arch === 'arm64') {
26+
return 'aarch64';
27+
}
28+
return arch;
29+
}
30+
31+
function getDownloadUrl() {
32+
const platform = getPlatform();
33+
const arch = getArch();
34+
const supportedPlatforms = {
35+
'linux': ['x86_64', 'aarch64'],
36+
'osx': ['x86_64', 'aarch64'],
37+
'windows': ['x86_64']
38+
};
39+
40+
if (!supportedPlatforms[platform] || !supportedPlatforms[platform].includes(arch)) {
41+
console.error(`Unsupported platform: ${platform}-${arch}`);
42+
process.exit(1);
43+
}
44+
45+
return `https://github.com/protocolbuffers/protobuf-javascript/releases/download/v${VERSION}/protobuf-javascript-${VERSION}-${platform}-${arch}.zip`;
46+
}
47+
48+
function unzip(zipFile, destDir, binaryName) {
49+
return new Promise((resolve, reject) => {
50+
const binaryPathInZip = `bin/${binaryName}`;
51+
const zip = new AdmZip(zipFile);
52+
const zipEntries = zip.getEntries();
53+
let found = false;
54+
55+
for (const entry of zipEntries) {
56+
const entryFileName = entry.entryName.replace(/\\/g, '/');
57+
if (entryFileName.endsWith(binaryPathInZip)) {
58+
found = true;
59+
fs.writeFile(path.join(destDir, binaryName), entry.getData(), (err) => {
60+
if (err) {
61+
reject(err);
62+
} else {
63+
resolve();
64+
}
65+
});
66+
break;
67+
}
68+
}
69+
70+
if (!found) {
71+
reject(new Error(`Binary ${binaryPathInZip} not found in zip file.`));
72+
}
73+
});
74+
}
75+
76+
async function main() {
77+
try {
78+
const downloadUrl = getDownloadUrl();
79+
const zipFile = path.join(__dirname, 'google-protobuf.zip');
80+
const binaryName = os.platform() === 'win32' ? 'protoc-gen-js.exe' : 'protoc-gen-js';
81+
82+
await fs.promises.mkdir(BIN_DIR, { recursive: true });
83+
84+
console.log(`Downloading ${downloadUrl}`);
85+
const response = await fetch(downloadUrl);
86+
if (!response.ok) {
87+
throw new Error(`Failed to download: ${response.status} ${response.statusText}`);
88+
}
89+
const buffer = await response.arrayBuffer();
90+
await fs.promises.writeFile(zipFile, Buffer.from(buffer));
91+
92+
console.log('Unzipping...');
93+
await unzip(zipFile, BIN_DIR, binaryName);
94+
95+
await fs.promises.unlink(zipFile);
96+
97+
const binaryPath = path.join(BIN_DIR, binaryName);
98+
99+
console.log(`Making ${binaryPath} executable...`);
100+
await fs.promises.chmod(binaryPath, 0o755);
101+
102+
console.log('Done!');
103+
} catch (err) {
104+
console.error(`Failed to download protoc-gen-js: ${err.message}`);
105+
process.exit(1);
106+
}
107+
}
108+
109+
main();

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function test_commonjs(cb) {
244244
}
245245

246246
function remove_gen_files(cb) {
247-
exec('rm -rf commonjs_out google-protobuf.js deps.js google-protobuf-*.tgz conformance/protos/*.js',
247+
exec('rm -rf bin/ commonjs_out google-protobuf.js deps.js google-protobuf-*.tgz conformance/protos/*.js',
248248
make_exec_logging_callback(cb));
249249
}
250250

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
"google-closure-deps": "20230802.0.0",
1919
"google-closure-library": "20230802.0.0",
2020
"gulp": "~5.0.0",
21-
"jasmine": "~3.5.0"
21+
"jasmine": "~3.5.0",
22+
"adm-zip": "^0.5.16"
2223
},
2324
"scripts": {
2425
"build": "node ./node_modules/gulp/bin/gulp.js dist",
2526
"test": "node ./node_modules/gulp/bin/gulp.js test",
26-
"clean": "node ./node_modules/gulp/bin/gulp.js clean"
27+
"clean": "node ./node_modules/gulp/bin/gulp.js clean",
28+
"postinstall": "node download-protoc-gen-js.js"
2729
},
2830
"repository": {
2931
"type": "git",

postinstall.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
const { spawn } = require('child_process');
3+
4+
const child = spawn('node', ['download-protoc-gen-js.js'], {
5+
stdio: 'inherit'
6+
});
7+
8+
child.on('close', (code) => {
9+
if (code !== 0) {
10+
console.error('Failed to download protoc-gen-js');
11+
process.exit(1);
12+
}
13+
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
dependencies:
1515
is-negated-glob "^1.0.0"
1616

17+
adm-zip@^0.5.16:
18+
version "0.5.16"
19+
resolved "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.16.tgz"
20+
integrity sha512-TGw5yVi4saajsSEgz25grObGHEUaDrniwvA2qwSC060KfqGPdglhvPMA2lPIoxs3PQIItj2iag35fONcQqgUaQ==
21+
1722
ansi-regex@^5.0.1:
1823
version "5.0.1"
1924
resolved "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz"

0 commit comments

Comments
 (0)