|
| 1 | +import * as process from "node:process"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import * as url from "node:url"; |
| 5 | +import * as stream from "node:stream"; |
| 6 | +import * as tar from "./tar.cjs"; |
| 7 | + |
| 8 | +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); |
| 9 | +const __bin = path.join(__dirname, "http-server.exe"); |
| 10 | +const __bin_unix = path.join(__dirname, "http-server"); |
| 11 | + |
| 12 | +const HTTP_SERVER_RS_BIN_PATH = process.env.HTTP_SERVER_RS_BIN_PATH; |
| 13 | +const HTTP_SERVER_RS_SKIP_DOWNLOAD = process.env.HTTP_SERVER_RS_SKIP_DOWNLOAD; |
| 14 | +const HTTP_SERVER_RS_FORCE_TAG = process.env.HTTP_SERVER_RS_FORCE_TAG; |
| 15 | + |
| 16 | +const os = { |
| 17 | + darwin: "macos", |
| 18 | + linux: "linux", |
| 19 | + win32: "windows", |
| 20 | +}[process.platform]; |
| 21 | + |
| 22 | +const arch = { |
| 23 | + arm64: "arm64", |
| 24 | + x64: "amd64", |
| 25 | +}[process.arch]; |
| 26 | + |
| 27 | +void (async function main() { |
| 28 | + if (HTTP_SERVER_RS_SKIP_DOWNLOAD === "true") { |
| 29 | + // Do nothing |
| 30 | + } else if ( |
| 31 | + HTTP_SERVER_RS_BIN_PATH && |
| 32 | + fs.existsSync(HTTP_SERVER_RS_BIN_PATH) |
| 33 | + ) { |
| 34 | + await fs.promises.rm(__bin, { recursive: true, force: true }); |
| 35 | + await fs.promises.symlink(__bin, HTTP_SERVER_RS_BIN_PATH); |
| 36 | + } else { |
| 37 | + await downloadBin(); |
| 38 | + } |
| 39 | +})(); |
| 40 | + |
| 41 | +async function downloadBin() { |
| 42 | + if (!arch || !os) { |
| 43 | + throw new Error( |
| 44 | + "Unable to determine what Atlaspack Version Manager binary to download" |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + let tag = HTTP_SERVER_RS_FORCE_TAG || "latest"; |
| 49 | + if (fs.existsSync(path.join(__dirname, "tag"))) { |
| 50 | + tag = ( |
| 51 | + await fs.promises.readFile(path.join(__dirname, "tag"), "utf8") |
| 52 | + ).trim(); |
| 53 | + } |
| 54 | + |
| 55 | + let url = `https://github.com/alshdavid/http-server-rs/releases/latest/download/http-server-${os}-${arch}.tar.gz`; |
| 56 | + if (tag !== "latest") { |
| 57 | + url = `https://github.com/alshdavid/http-server-rs/releases/download/${tag}/http-server-${os}-${arch}.tar.gz`; |
| 58 | + } |
| 59 | + |
| 60 | + const response = await globalThis.fetch(url); |
| 61 | + if (!response.ok) { |
| 62 | + throw new Error("Unable to fetch http-server-rs binary"); |
| 63 | + } |
| 64 | + |
| 65 | + const body = await response.bytes(); |
| 66 | + const file = new stream.Duplex(); |
| 67 | + |
| 68 | + file.push(body); |
| 69 | + file.push(null); |
| 70 | + |
| 71 | + if (fs.existsSync(__bin)) { |
| 72 | + fs.rmSync(__bin, { recursive: true, force: true }); |
| 73 | + } |
| 74 | + |
| 75 | + let writable = tar.x({ |
| 76 | + C: __dirname, |
| 77 | + chmod: true, |
| 78 | + }); |
| 79 | + |
| 80 | + file.pipe(writable); |
| 81 | + await new Promise((res) => writable.on("close", res)); |
| 82 | + |
| 83 | + await fs.promises.rm(__bin, { recursive: true, force: true }); |
| 84 | + if (fs.existsSync(__bin_unix)) { |
| 85 | + await fs.promises.rename(__bin_unix, __bin); |
| 86 | + } |
| 87 | + await fs.promises.chmod(__bin, "755"); |
| 88 | +} |
0 commit comments