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+ const SUPPORTED_PLATFORMS = [
10+ { platform : 'linux' , arch : 'x86_64' } ,
11+ { platform : 'linux' , arch : 'aarch64' } ,
12+ { platform : 'osx' , arch : 'x86_64' } ,
13+ { platform : 'osx' , arch : 'aarch64' } ,
14+ { platform : 'windows' , arch : 'x86_64' } ,
15+ ] ;
16+
17+ function getDownloadUrl ( platform , arch ) {
18+ return `https://github.com/protocolbuffers/protobuf-javascript/releases/download/v${ VERSION } /protobuf-javascript-${ VERSION } -${ platform } -${ arch } .zip` ;
19+ }
20+
21+ function unzip ( zipFile , destDir , binaryName ) {
22+ return new Promise ( ( resolve , reject ) => {
23+ const binaryPathInZip = `bin/${ binaryName } ` ;
24+ const zip = new AdmZip ( zipFile ) ;
25+ const entry = zip . getEntry ( binaryPathInZip ) ;
26+ if ( entry ) {
27+ zip . extractEntryTo ( entry , destDir , false , true ) ;
28+ resolve ( ) ;
29+ } else {
30+ reject ( new Error ( `Binary ${ binaryPathInZip } not found in zip file.` ) ) ;
31+ }
32+ } ) ;
33+ }
34+
35+ function getCurrentPlatform ( ) {
36+ const platform = os . platform ( ) ;
37+ if ( platform === 'darwin' ) {
38+ return 'osx' ;
39+ }
40+ if ( platform === 'win32' ) {
41+ return 'windows' ;
42+ }
43+ return 'linux' ;
44+ }
45+
46+ function getCurrentArch ( ) {
47+ const arch = os . arch ( ) ;
48+ if ( arch === 'x64' ) {
49+ return 'x86_64' ;
50+ }
51+ if ( arch === 'arm64' ) {
52+ return 'aarch64' ;
53+ }
54+ return arch ;
55+ }
56+
57+ async function main ( ) {
58+ try {
59+ await fs . promises . mkdir ( BIN_DIR , { recursive : true } ) ;
60+
61+ const platform = getCurrentPlatform ( ) ;
62+ const arch = getCurrentArch ( ) ;
63+
64+ const supportedPlatform = SUPPORTED_PLATFORMS . find ( p => p . platform === platform && p . arch === arch ) ;
65+
66+ if ( ! supportedPlatform ) {
67+ console . error ( `Unsupported platform: ${ platform } ${ arch } ` ) ;
68+ process . exit ( 1 ) ;
69+ }
70+
71+ const downloadUrl = getDownloadUrl ( supportedPlatform . platform , supportedPlatform . arch ) ;
72+ const zipFile = path . join ( __dirname , `google-protobuf-${ supportedPlatform . platform } -${ supportedPlatform . arch } .zip` ) ;
73+ const isWindows = supportedPlatform . platform === 'windows' ;
74+ const binaryName = isWindows ? 'protoc-gen-js.exe' : 'protoc-gen-js' ;
75+ const binaryPath = path . join ( BIN_DIR , binaryName ) ;
76+
77+ console . log ( `Downloading ${ downloadUrl } ` ) ;
78+ const response = await fetch ( downloadUrl ) ;
79+ if ( ! response . ok ) {
80+ throw new Error (
81+ `Failed to download: ${ response . status } ${ response . statusText } `
82+ ) ;
83+ }
84+ const buffer = await response . arrayBuffer ( ) ;
85+ await fs . promises . writeFile ( zipFile , Buffer . from ( buffer ) ) ;
86+
87+ console . log ( 'Unzipping...' ) ;
88+ await unzip ( zipFile , BIN_DIR , binaryName ) ;
89+
90+ await fs . promises . unlink ( zipFile ) ;
91+
92+ console . log ( `Making ${ binaryPath } executable...` ) ;
93+ if ( ! isWindows ) {
94+ await fs . promises . chmod ( binaryPath , 0o755 ) ;
95+ }
96+
97+ console . log ( 'Done!' ) ;
98+ } catch ( err ) {
99+ console . error ( `Failed to install protoc-gen-js: ${ err . message } ` ) ;
100+ process . exit ( 1 ) ;
101+ }
102+ }
103+
104+ main ( ) ;
0 commit comments