Skip to content

Commit

Permalink
use own binary-install
Browse files Browse the repository at this point in the history
  • Loading branch information
wighawag committed Apr 27, 2023
1 parent d44df06 commit 80a60a9
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ cache
/run.js
/install.js
/binary.js
/binary-install.js
node_modules
pnpm-lock.yaml
8 changes: 8 additions & 0 deletions build-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ const binary_js = fs.readFileSync("npm/binary.js", "utf-8");
fs.writeFileSync("binary.js", binary_js.replace("__VERSION__", version));
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// binary.js: just copy
// ------------------------------------------------------------------------------------------------
const binary_install = fs.readFileSync("npm/binary-install.js", "utf-8");
fs.writeFileSync("binary-install.js", binary_install);
// ------------------------------------------------------------------------------------------------


if (args[0] === 'publish:npm') {
execFileSync("npm", ["publish"], {stdio});
} else if (args[0] === 'publish') {
Expand Down
120 changes: 120 additions & 0 deletions npm/binary-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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, config) {
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 = config?.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) {
const promise = !this.exists() ? this.install(fetchOptions, true) : Promise.resolve();

promise.then(() => {
const [, , ...args] = process.argv;

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

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

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

process.exit(result.status);

}).catch(() => {
process.exit(1);
})
}
}

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

const error = msg => {
Expand Down
7 changes: 5 additions & 2 deletions npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"homepage": "https://github.com/wighawag/forge-deploy",
"files": [
"binary-install.js",
"install.js",
"run.js",
"binary.js",
Expand All @@ -21,8 +22,10 @@
"Cargo.lock",
"Cargo.toml"
],
"dependencies": {
"binary-install": "^1.0.6"
"devDependencies": {
"axios": "^0.26.1",
"rimraf": "^3.0.2",
"tar": "^6.1.11"
},
"bin": {
"forge-deploy": "run.js"
Expand Down

0 comments on commit 80a60a9

Please sign in to comment.