|
| 1 | +const fs = require('fs'); |
| 2 | +const os = require('os'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const isGlobal = process.env.npm_config_global === 'true'; |
| 6 | + |
| 7 | +let AdmZip; |
| 8 | +try { |
| 9 | + AdmZip = require('adm-zip'); |
| 10 | +} catch (e) { |
| 11 | + if (isGlobal) { |
| 12 | + console.warn( |
| 13 | + 'Skipping protoc-gen-js download due to --omit=optional in global install.' |
| 14 | + ); |
| 15 | + console.warn( |
| 16 | + 'If you have a system-installed protoc-gen-js, it will be preserved.' |
| 17 | + ); |
| 18 | + // On global installs, we refuse to overwrite a potentially real binary |
| 19 | + // with our placeholder. In this case, we must delete our placeholder. |
| 20 | + const placeholderPath = path.join(__dirname, 'bin', 'protoc-gen-js'); |
| 21 | + if (fs.existsSync(placeholderPath)) { |
| 22 | + fs.unlinkSync(placeholderPath); |
| 23 | + } |
| 24 | + } else { |
| 25 | + // For local installs, the placeholder script is fine. It's sandboxed |
| 26 | + // to the project's node_modules/.bin and provides a helpful error. |
| 27 | + console.warn( |
| 28 | + 'adm-zip not found, skipping binary download. The protoc-gen-js command will show a help message.' |
| 29 | + ); |
| 30 | + } |
| 31 | + process.exit(0); |
| 32 | +} |
| 33 | + |
| 34 | +const VERSION = '4.0.0'; |
| 35 | +const BIN_DIR = path.join(__dirname, 'bin'); |
| 36 | + |
| 37 | +function getPlatform() { |
| 38 | + const platform = os.platform(); |
| 39 | + if (platform === 'darwin') { |
| 40 | + return 'osx'; |
| 41 | + } |
| 42 | + if (platform === 'win32') { |
| 43 | + return 'windows'; |
| 44 | + } |
| 45 | + return platform; |
| 46 | +} |
| 47 | + |
| 48 | +function getArch() { |
| 49 | + const arch = os.arch(); |
| 50 | + if (arch === 'x64') { |
| 51 | + return 'x86_64'; |
| 52 | + } |
| 53 | + if (arch === 'arm64') { |
| 54 | + return 'aarch64'; |
| 55 | + } |
| 56 | + return arch; |
| 57 | +} |
| 58 | + |
| 59 | +function getDownloadUrl() { |
| 60 | + const platform = getPlatform(); |
| 61 | + const arch = getArch(); |
| 62 | + const supportedPlatforms = { |
| 63 | + linux: ['x86_64', 'aarch64'], |
| 64 | + osx: ['x86_64', 'aarch64'], |
| 65 | + windows: ['x86_64'], |
| 66 | + }; |
| 67 | + |
| 68 | + if ( |
| 69 | + !supportedPlatforms[platform] || |
| 70 | + !supportedPlatforms[platform].includes(arch) |
| 71 | + ) { |
| 72 | + console.error(`Unsupported platform: ${platform}-${arch}`); |
| 73 | + process.exit(1); |
| 74 | + } |
| 75 | + |
| 76 | + return `https://github.com/protocolbuffers/protobuf-javascript/releases/download/v${VERSION}/protobuf-javascript-${VERSION}-${platform}-${arch}.zip`; |
| 77 | +} |
| 78 | + |
| 79 | +function unzip(zipFile, destDir, binaryName) { |
| 80 | + return new Promise((resolve, reject) => { |
| 81 | + const binaryPathInZip = `bin/${binaryName}`; |
| 82 | + const zip = new AdmZip(zipFile); |
| 83 | + const zipEntries = zip.getEntries(); |
| 84 | + let found = false; |
| 85 | + |
| 86 | + for (const entry of zipEntries) { |
| 87 | + const entryFileName = entry.entryName.replace(/\\/g, '/'); |
| 88 | + if (entryFileName.endsWith(binaryPathInZip)) { |
| 89 | + found = true; |
| 90 | + fs.writeFile(path.join(destDir, binaryName), entry.getData(), (err) => { |
| 91 | + if (err) { |
| 92 | + reject(err); |
| 93 | + } else { |
| 94 | + resolve(); |
| 95 | + } |
| 96 | + }); |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (!found) { |
| 102 | + reject(new Error(`Binary ${binaryPathInZip} not found in zip file.`)); |
| 103 | + } |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +async function main() { |
| 108 | + try { |
| 109 | + const downloadUrl = getDownloadUrl(); |
| 110 | + const zipFile = path.join(__dirname, 'google-protobuf.zip'); |
| 111 | + const binaryName = |
| 112 | + os.platform() === 'win32' ? 'protoc-gen-js.exe' : 'protoc-gen-js'; |
| 113 | + |
| 114 | + await fs.promises.mkdir(BIN_DIR, {recursive: true}); |
| 115 | + |
| 116 | + console.log(`Downloading ${downloadUrl}`); |
| 117 | + const response = await fetch(downloadUrl); |
| 118 | + if (!response.ok) { |
| 119 | + throw new Error( |
| 120 | + `Failed to download: ${response.status} ${response.statusText}` |
| 121 | + ); |
| 122 | + } |
| 123 | + const buffer = await response.arrayBuffer(); |
| 124 | + await fs.promises.writeFile(zipFile, Buffer.from(buffer)); |
| 125 | + |
| 126 | + console.log('Unzipping...'); |
| 127 | + await unzip(zipFile, BIN_DIR, binaryName); |
| 128 | + |
| 129 | + await fs.promises.unlink(zipFile); |
| 130 | + |
| 131 | + const binaryPath = path.join(BIN_DIR, binaryName); |
| 132 | + |
| 133 | + console.log(`Making ${binaryPath} executable...`); |
| 134 | + await fs.promises.chmod(binaryPath, 0o755); |
| 135 | + |
| 136 | + console.log('Done!'); |
| 137 | + } catch (err) { |
| 138 | + console.error(`Failed to download protoc-gen-js: ${err.message}`); |
| 139 | + process.exit(1); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +main(); |
0 commit comments