|
| 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(); |
0 commit comments