Skip to content

Commit

Permalink
Wiring in NPM releases.
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Shanley <dave@quobix.com>
  • Loading branch information
daveshanley committed Jun 4, 2023
1 parent 9fa9989 commit 30850e5
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 0 deletions.
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: "wiretap",
path: "./bin",
url: "https://github.com/pb33f/wiretap/releases/download/v{{version}}/{{bin_name}}_{{version}}_{{platform}}_{{arch}}.tar.gz",
};
export const ARCH_MAPPING = {
ia32: "i386",
x64: "x86_64",
arm64: "arm64",
};
export const PLATFORM_MAPPING = {
darwin: "darwin",
linux: "linux",
win32: "windows",
};
57 changes: 57 additions & 0 deletions npm-install/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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/pb33f/wiertap.git && make build";
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);


console.log('fetching from URL', url)
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);
});
201 changes: 201 additions & 0 deletions package-lock.json

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

33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@pb33f/wiretap",
"version": "0.0.0",
"description": "The world's coolest OpenAPI compliance API Proxy in the world",
"type": "module",
"author": "Princess Beef Heavy Industries, LLC / Quobix",
"license": "GPL-3.0",
"homepage": "https://pb33f.io/wiretap",
"repository": {
"type": "git",
"url": "git+https://github.com/pb33f/wiretap.git"
},
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"postinstall": "node ./npm-install/postinstall.js"
},
"bin": {
"wiretap": "bin/wiretap.js"
},
"files": [
"npm-install"
],
"dependencies": {
"node-fetch": "^3.2.10",
"tar": "^6.1.11"
},
"bugs": {
"url": "https://github.com/pb33f/wiretap/issues"
},
"main": "index.js"
}

0 comments on commit 30850e5

Please sign in to comment.