Skip to content

Commit

Permalink
Adding NPM configuration for easier CI/CD install
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Shanley <dave@quobix.com>

Disabled test mode.

Signed-off-by: Dave Shanley <dave@quobix.com>
  • Loading branch information
daveshanley committed Jul 19, 2022
1 parent a84868e commit 23dad99
Show file tree
Hide file tree
Showing 5 changed files with 413 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bin/vacuum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node
import { execFileSync } from "child_process";
import path from "path";
import { exit } from "process";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

try {
execFileSync(path.resolve(`${__dirname}/vacuum`), process.argv.slice(2), {
stdio: "inherit",
});
} catch (e) {
exit(1)
}
15 changes: 15 additions & 0 deletions npm-install/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const CONFIG = {
name: "vacuum",
path: "./bin",
url: "https://github.com/daveshanley/vacuum/releases/download/v{{version}}/{{bin_name}}_{{version}}_{{platform}}_{{arch}}.tar.gz",
};
export const ARCH_MAPPING = {
ia32: "386",
x64: "amd64",
arm64: "arm64",
};
export const PLATFORM_MAPPING = {
darwin: "darwin",
linux: "linux",
win32: "windows",
};
55 changes: 55 additions & 0 deletions npm-install/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createWriteStream } from "fs";
import * as fs from "fs/promises";
import fetch from "node-fetch";
import { pipeline } from "stream/promises";
import tar from "tar";
import { execSync } from "child_process";

import { ARCH_MAPPING, CONFIG, PLATFORM_MAPPING } from "./config.js";

async function install() {
if (process.platform === "android") {
console.log("Installing, may take a moment...");
const cmd =
"pkg upgrade && pkg install golang git -y && git clone https://github.com/daveshanley/vacuum.git && cd cli/ && go build -o $PREFIX/bin/vacuum";
execSync(cmd, { encoding: "utf-8" });
console.log("Installation successful!");
return;
}
const packageJson = await fs.readFile("package.json").then(JSON.parse);
let version = packageJson.version;

if (typeof version !== "string") {
throw new Error("Missing version in package.json");
}

if (version[0] === "v") version = version.slice(1);

let { name: binName, path: binPath, url } = CONFIG;

url = url.replace(/{{arch}}/g, ARCH_MAPPING[process.arch]);
url = url.replace(/{{platform}}/g, PLATFORM_MAPPING[process.platform]);
url = url.replace(/{{version}}/g, version);
url = url.replace(/{{bin_name}}/g, binName);

const response = await fetch(url);
if (!response.ok) {
throw new Error("Failed fetching the binary: " + response.statusText);
}

const tarFile = "downloaded.tar.gz";

await fs.mkdir(binPath, { recursive: true });
await pipeline(response.body, createWriteStream(tarFile));
await tar.x({ file: tarFile, cwd: binPath });
await fs.rm(tarFile);
}

install()
.then(async () => {
process.exit(0);
})
.catch(async (err) => {
console.error(err);
process.exit(1);
});
293 changes: 293 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 23dad99

Please sign in to comment.