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