|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require("fs"); |
| 4 | +const tar = require("tar"); |
| 5 | +const zlib = require("zlib"); |
| 6 | +const http = require("http"); |
| 7 | +const https = require("https"); |
| 8 | +const packageJSON = require("./package.json"); |
| 9 | + |
| 10 | +// Determine the URL of the file. |
| 11 | +const platformName = { |
| 12 | + darwin: "darwin", |
| 13 | + linux: "linux", |
| 14 | + win32: "windows", |
| 15 | +}[process.platform]; |
| 16 | + |
| 17 | +let archName = { |
| 18 | + x64: "amd64", |
| 19 | + x86: "amd64", |
| 20 | + ia32: "amd64", |
| 21 | + amd64: "amd64", |
| 22 | + arm64: "arm64", |
| 23 | +}[process.arch]; |
| 24 | + |
| 25 | +if (!platformName || !archName) { |
| 26 | + console.error( |
| 27 | + `Cannot install src for platform ${process.platform}, architecture ${process.arch}` |
| 28 | + ); |
| 29 | + process.exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +const version = packageJSON.version; |
| 33 | +const assetURL = `https://github.com/sourcegraph/src-cli/releases/download/${version}/src-cli_${version}_${platformName}_${archName}.tar.gz`; |
| 34 | + |
| 35 | +// Remove previously-downloaded files. |
| 36 | +const executableName = process.platform === "win32" ? "src.exe" : "src"; |
| 37 | +if (fs.existsSync(executableName)) { |
| 38 | + fs.unlinkSync(executableName); |
| 39 | +} |
| 40 | + |
| 41 | +// Download the compressed file. |
| 42 | +console.log(`Downloading ${assetURL}`); |
| 43 | +get(assetURL, (response) => { |
| 44 | + if (response.statusCode > 299) { |
| 45 | + console.error( |
| 46 | + [ |
| 47 | + "Download failed", |
| 48 | + "", |
| 49 | + `url: ${assetURL}`, |
| 50 | + `status: ${response.statusCode}`, |
| 51 | + `headers: ${JSON.stringify(response.headers, null, 2)}`, |
| 52 | + "", |
| 53 | + ].join("\n") |
| 54 | + ); |
| 55 | + process.exit(1); |
| 56 | + } |
| 57 | + response |
| 58 | + .pipe(zlib.createGunzip()) |
| 59 | + .pipe(tar.x({ filter: (x) => x === "src" })) |
| 60 | + .addListener("close", () => fs.chmodSync(executableName, "755")); |
| 61 | +}); |
| 62 | + |
| 63 | +// Follow redirects. |
| 64 | +function get(url, callback) { |
| 65 | + const requestUrl = new URL(url); |
| 66 | + let request = https; |
| 67 | + let requestConfig = requestUrl; |
| 68 | + const proxyEnv = process.env["HTTPS_PROXY"] || process.env["https_proxy"]; |
| 69 | + |
| 70 | + if (proxyEnv) { |
| 71 | + const proxyUrl = new URL(proxyEnv); |
| 72 | + request = proxyUrl.protocol === "https:" ? https : http; |
| 73 | + requestConfig = { |
| 74 | + hostname: proxyUrl.hostname, |
| 75 | + port: proxyUrl.port, |
| 76 | + path: requestUrl.toString(), |
| 77 | + headers: { |
| 78 | + Host: requestUrl.hostname, |
| 79 | + }, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + request.get(requestConfig, (response) => { |
| 84 | + if (response.statusCode === 301 || response.statusCode === 302) { |
| 85 | + get(response.headers.location, callback); |
| 86 | + } else { |
| 87 | + callback(response); |
| 88 | + } |
| 89 | + }); |
| 90 | +} |
0 commit comments