|
1 | 1 | import { register, run } from "@coder/runner"; |
2 | 2 | import * as fs from "fs"; |
3 | 3 | import * as fse from "fs-extra"; |
| 4 | +import * as os from "os"; |
4 | 5 | import * as path from "path"; |
5 | 6 | import * as zlib from "zlib"; |
6 | 7 |
|
@@ -30,12 +31,48 @@ const buildServerBinaryPackage = register("build:server:binary:package", async ( |
30 | 31 | throw new Error("Cannot build binary without web bundle built"); |
31 | 32 | } |
32 | 33 | await buildServerBinaryCopy(); |
| 34 | + await dependencyNexeBinary(); |
33 | 35 | const resp = await runner.execute("npm", ["run", "build:nexe"]); |
34 | 36 | if (resp.exitCode !== 0) { |
35 | 37 | throw new Error(`Failed to package binary: ${resp.stderr}`); |
36 | 38 | } |
37 | 39 | }); |
38 | 40 |
|
| 41 | +const dependencyNexeBinary = register("dependency:nexe", async (runner) => { |
| 42 | + if (os.platform() === "linux") { |
| 43 | + const nexeDir = path.join(os.homedir(), ".nexe"); |
| 44 | + const targetBinaryName = `${os.platform()}-${os.arch()}-${process.version.substr(1)}`; |
| 45 | + const targetBinaryPath = path.join(nexeDir, targetBinaryName); |
| 46 | + if (!fs.existsSync(targetBinaryPath)) { |
| 47 | + /** |
| 48 | + * We create a binary with nexe |
| 49 | + * so we can compress it |
| 50 | + */ |
| 51 | + fse.mkdirpSync(nexeDir); |
| 52 | + runner.cwd = nexeDir; |
| 53 | + await runner.execute("wget", [`https://github.com/nexe/nexe/releases/download/v3.0.0-beta.15/${targetBinaryName}`]); |
| 54 | + await runner.execute("chmod", ["+x", targetBinaryPath]); |
| 55 | + } |
| 56 | + if (fs.statSync(targetBinaryPath).size >= 20000000) { |
| 57 | + // Compress w/ upx |
| 58 | + const upxFolder = path.join(os.tmpdir(), "upx"); |
| 59 | + const upxBinary = path.join(upxFolder, "upx"); |
| 60 | + if (!fs.existsSync(upxBinary)) { |
| 61 | + fse.mkdirpSync(upxFolder); |
| 62 | + runner.cwd = upxFolder; |
| 63 | + const upxExtract = await runner.execute("bash", ["-c", "curl -L https://github.com/upx/upx/releases/download/v3.95/upx-3.95-amd64_linux.tar.xz | tar xJ --strip-components=1"]); |
| 64 | + if (upxExtract.exitCode !== 0) { |
| 65 | + throw new Error(`Failed to extract upx: ${upxExtract.stderr}`); |
| 66 | + } |
| 67 | + } |
| 68 | + if (!fs.existsSync(upxBinary)) { |
| 69 | + throw new Error("Not sure how, but the UPX binary does not exist"); |
| 70 | + } |
| 71 | + await runner.execute(upxBinary, [targetBinaryPath]); |
| 72 | + } |
| 73 | + } |
| 74 | +}); |
| 75 | + |
39 | 76 | const buildServerBinaryCopy = register("build:server:binary:copy", async (runner) => { |
40 | 77 | const cliPath = path.join(pkgsPath, "server"); |
41 | 78 | const cliBuildPath = path.join(cliPath, "build"); |
|
0 commit comments