|
1 |
| -// TODO: |
2 |
| -// 1. Determine the platform and architecture of the system |
3 |
| -// 2. Determine the version of the npm package |
4 |
| -// 3. Download the Golang binary for the platform and architecture from GitHub releases |
5 |
| -console.log("Coming soon..."); |
| 1 | +// Credits: |
| 2 | +// - Bun.js (https://github.com/oven-sh/bun/blob/main/packages/bun-release/src) |
| 3 | +const child_process = require("child_process"); |
| 4 | +const { unzipSync } = require("zlib"); |
| 5 | +const packageJson = require("./package.json"); |
| 6 | +const fs = require("fs"); |
| 7 | + |
| 8 | +function getPlatform() {} |
| 9 | + |
| 10 | +async function downloadCli(version, platform) { |
| 11 | + const ext = platform.os === "win32" ? ".zip" : ".tar.gz"; |
| 12 | + const response = await fetch( |
| 13 | + `https://github.com/flexstack/new-dockerfile/releases/download/v${version}/${platform.bin}${ext}` |
| 14 | + ); |
| 15 | + const tgz = await response.arrayBuffer(); |
| 16 | + let buffer; |
| 17 | + |
| 18 | + try { |
| 19 | + buffer = unzipSync(tgz); |
| 20 | + } catch (cause) { |
| 21 | + throw new Error("Invalid gzip data", { cause }); |
| 22 | + } |
| 23 | + |
| 24 | + function str(i, n) { |
| 25 | + return String.fromCharCode(...buffer.subarray(i, i + n)).replace( |
| 26 | + /\0.*$/, |
| 27 | + "" |
| 28 | + ); |
| 29 | + } |
| 30 | + let offset = 0; |
| 31 | + const dst = platform.exe; |
| 32 | + while (offset < buffer.length) { |
| 33 | + const name = str(offset, 100).replace("package/", ""); |
| 34 | + const size = parseInt(str(offset + 124, 12), 8); |
| 35 | + offset += 512; |
| 36 | + if (!isNaN(size)) { |
| 37 | + write(dst, buffer.subarray(offset, offset + size)); |
| 38 | + if (name === platform.exe) { |
| 39 | + try { |
| 40 | + fs.chmodSync(dst, 0o755); |
| 41 | + } catch (error) {} |
| 42 | + } |
| 43 | + offset += (size + 511) & ~511; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const fetch = "fetch" in globalThis ? webFetch : nodeFetch; |
| 49 | + |
| 50 | +async function webFetch(url, options) { |
| 51 | + const response = await globalThis.fetch(url, options); |
| 52 | + if (options?.assert !== false && !isOk(response.status)) { |
| 53 | + try { |
| 54 | + await response.text(); |
| 55 | + } catch {} |
| 56 | + throw new Error(`${response.status}: ${url}`); |
| 57 | + } |
| 58 | + return response; |
| 59 | +} |
| 60 | + |
| 61 | +async function nodeFetch(url, options) { |
| 62 | + const { get } = await import("node:http"); |
| 63 | + return new Promise((resolve, reject) => { |
| 64 | + get(url, (response) => { |
| 65 | + const status = response.statusCode ?? 501; |
| 66 | + if (response.headers.location && isRedirect(status)) { |
| 67 | + return nodeFetch(url).then(resolve, reject); |
| 68 | + } |
| 69 | + if (options?.assert !== false && !isOk(status)) { |
| 70 | + return reject(new Error(`${status}: ${url}`)); |
| 71 | + } |
| 72 | + const body = []; |
| 73 | + response.on("data", (chunk) => { |
| 74 | + body.push(chunk); |
| 75 | + }); |
| 76 | + response.on("end", () => { |
| 77 | + resolve({ |
| 78 | + ok: isOk(status), |
| 79 | + status, |
| 80 | + async arrayBuffer() { |
| 81 | + return Buffer.concat(body).buffer; |
| 82 | + }, |
| 83 | + async text() { |
| 84 | + return Buffer.concat(body).toString("utf-8"); |
| 85 | + }, |
| 86 | + async json() { |
| 87 | + const text = Buffer.concat(body).toString("utf-8"); |
| 88 | + return JSON.parse(text); |
| 89 | + }, |
| 90 | + }); |
| 91 | + }); |
| 92 | + }).on("error", reject); |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +function isOk(status) { |
| 97 | + return status >= 200 && status <= 204; |
| 98 | +} |
| 99 | + |
| 100 | +function isRedirect(status) { |
| 101 | + switch (status) { |
| 102 | + case 301: // Moved Permanently |
| 103 | + case 308: // Permanent Redirect |
| 104 | + case 302: // Found |
| 105 | + case 307: // Temporary Redirect |
| 106 | + case 303: // See Other |
| 107 | + return true; |
| 108 | + } |
| 109 | + return false; |
| 110 | +} |
| 111 | + |
| 112 | +const os = process.platform; |
| 113 | + |
| 114 | +const arch = |
| 115 | + os === "darwin" && process.arch === "x64" && isRosetta2() |
| 116 | + ? "arm64" |
| 117 | + : process.arch; |
| 118 | + |
| 119 | +const platforms = [ |
| 120 | + { |
| 121 | + os: "darwin", |
| 122 | + arch: "x64", |
| 123 | + bin: "new-dockerfile-darwin-x86_64", |
| 124 | + exe: "bin/new-dockerfile", |
| 125 | + }, |
| 126 | + { |
| 127 | + os: "darwin", |
| 128 | + arch: "arm64", |
| 129 | + bin: "new-dockerfile-darwin-arm64", |
| 130 | + exe: "bin/new-dockerfile", |
| 131 | + }, |
| 132 | + { |
| 133 | + os: "linux", |
| 134 | + arch: "x64", |
| 135 | + bin: "new-dockerfile-linux-x86_64", |
| 136 | + exe: "bin/new-dockerfile", |
| 137 | + }, |
| 138 | + { |
| 139 | + os: "linux", |
| 140 | + arch: "arm64", |
| 141 | + bin: "new-dockerfile-linux-arm64", |
| 142 | + exe: "bin/new-dockerfile", |
| 143 | + }, |
| 144 | + { |
| 145 | + os: "win32", |
| 146 | + arch: "x64", |
| 147 | + bin: "new-dockerfile-windows-x86_64", |
| 148 | + exe: "bin/new-dockerfile.exe", |
| 149 | + }, |
| 150 | + { |
| 151 | + os: "win32", |
| 152 | + arch: "arm64", |
| 153 | + bin: "new-dockerfile-windows-arm64", |
| 154 | + exe: "bin/new-dockerfile.exe", |
| 155 | + }, |
| 156 | +]; |
| 157 | + |
| 158 | +const supportedPlatforms = platforms.filter( |
| 159 | + (platform) => platform.os === os && platform.arch === arch |
| 160 | +); |
| 161 | + |
| 162 | +function isRosetta2() { |
| 163 | + try { |
| 164 | + const { exitCode, stdout } = spawn("sysctl", [ |
| 165 | + "-n", |
| 166 | + "sysctl.proc_translated", |
| 167 | + ]); |
| 168 | + return exitCode === 0 && stdout.includes("1"); |
| 169 | + } catch (error) { |
| 170 | + return false; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +function spawn(cmd, args, options = {}) { |
| 175 | + const { status, stdout, stderr } = child_process.spawnSync(cmd, args, { |
| 176 | + stdio: "pipe", |
| 177 | + encoding: "utf-8", |
| 178 | + ...options, |
| 179 | + }); |
| 180 | + return { |
| 181 | + exitCode: status ?? 1, |
| 182 | + stdout, |
| 183 | + stderr, |
| 184 | + }; |
| 185 | +} |
| 186 | + |
| 187 | +function write(dst, content) { |
| 188 | + try { |
| 189 | + fs.writeFileSync(dst, content); |
| 190 | + return; |
| 191 | + } catch (error) { |
| 192 | + // If there is an error, ensure the parent directory |
| 193 | + // exists and try again. |
| 194 | + try { |
| 195 | + fs.mkdirSync(path.dirname(dst), { recursive: true }); |
| 196 | + } catch (error) { |
| 197 | + // The directory could have been created already. |
| 198 | + } |
| 199 | + fs.writeFileSync(dst, content); |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +if (supportedPlatforms.length === 0) { |
| 204 | + throw new Error("Unsupported platform: " + os + " " + arch); |
| 205 | +} |
| 206 | + |
| 207 | +// Read version from package.json |
| 208 | +downloadCli(packageJson.config.bin_version, supportedPlatforms[0]); |
0 commit comments