Skip to content

Commit

Permalink
Remove binary install and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindell committed Sep 28, 2022
1 parent d4aa287 commit 8d75344
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## Bug fixes

- Fixed npm install location (node_modules/.bin)
- Remove binary-install dependency and control download of binaries
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "envful"
version = "1.0.3"
version = "1.0.4"
edition = "2021"
authors = ["Alex Vilchis <alex@quentli.com>"]
license = "MIT"
Expand Down
122 changes: 122 additions & 0 deletions npm/binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const { existsSync, mkdirSync } = require("fs");
const { join } = require("path");
const { spawnSync } = require("child_process");

const axios = require("axios");
const tar = require("tar");
const rimraf = require("rimraf");

const error = (msg) => {
console.error(msg);
process.exit(1);
};

class Binary {
constructor(name, url) {
let errors = [];
if (typeof url !== "string") {
errors.push("url must be a string");
} else {
try {
new URL(url);
} catch (e) {
errors.push(e);
}
}
if (name && typeof name !== "string") {
errors.push("name must be a string");
}

if (!name) {
errors.push("You must specify the name of your binary");
}
if (errors.length > 0) {
let errorMsg =
"One or more of the parameters you passed to the Binary constructor are invalid:\n";
errors.forEach((error) => {
errorMsg += error;
});
errorMsg +=
'\n\nCorrect usage: new Binary("my-binary", "https://example.com/binary/download.tar.gz")';
error(errorMsg);
}
this.url = url;
this.name = name;
this.installDirectory = join(__dirname, "node_modules", ".bin");

if (!existsSync(this.installDirectory)) {
mkdirSync(this.installDirectory, { recursive: true });
}

this.binaryPath = join(this.installDirectory, this.name);
}

exists() {
return existsSync(this.binaryPath);
}

install(fetchOptions, suppressLogs = false) {
if (this.exists()) {
if (!suppressLogs) {
console.error(
`${this.name} is already installed, skipping installation.`
);
}
return Promise.resolve();
}

if (existsSync(this.installDirectory)) {
rimraf.sync(this.installDirectory);
}

mkdirSync(this.installDirectory, { recursive: true });

if (suppressLogs) {
console.error(`Downloading release from ${this.url}`);
}

return axios({ ...fetchOptions, url: this.url, responseType: "stream" })
.then((res) => {
return new Promise((resolve, reject) => {
const sink = res.data.pipe(
tar.x({ strip: 1, C: this.installDirectory })
);
sink.on("finish", () => resolve());
sink.on("error", (err) => reject(err));
});
})
.then(() => {
if (suppressLogs) {
console.error(`${this.name} has been installed!`);
}
})
.catch((e) => {
error(`Error fetching release: ${e.message}`);
});
}

run(fetchOptions) {
if (!this.exists()) {
this.install(fetchOptions, true).then(() => {
this.execute();
});
return;
}

this.execute();
}

execute(args, options) {
const [, , ...args] = process.argv;

const options = { cwd: process.cwd(), stdio: "inherit" };

const result = spawnSync(this.binaryPath, args, options);

if (result.error) {
error(result.error);
}
}
}

module.exports.Binary = Binary;
2 changes: 1 addition & 1 deletion npm/getBinary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Binary } = require("binary-install");
const { Binary } = require("./binary");
const os = require("os");

function getPlatform() {
Expand Down
142 changes: 131 additions & 11 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 8d75344

Please sign in to comment.