Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

chore: include @cloudflare/binary-install directly as source #1877

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions npm/binary-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const { existsSync, mkdirSync } = require("fs");
const { homedir } = require("os");
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(url, data) {
if (typeof url !== "string") {
errors.push("url must be a string");
} else {
try {
new URL(url);
} catch (e) {
errors.push(e);
}
}
let errors = [];
if (data.name && typeof data.name !== "string") {
errors.push("name must be a string");
}
if (data.installDirectory && typeof data.installDirectory !== "string") {
errors.push("installDirectory must be a string");
}
if (!data.installDirectory && !data.name) {
errors.push("You must specify either name or installDirectory");
}
if (errors.length > 0) {
let errorMsg = "Your Binary constructor is invalid:";
errors.forEach(error => {
errorMsg += error;
});
error(errorMsg);
}
this.url = url;
this.name = data.name || -1;
this.installDirectory = data.installDirectory || join(__dirname, "bin");
this.binaryDirectory = -1;
this.binaryPath = -1;
}

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

_getBinaryDirectory() {
const installDirectory = this._getInstallDirectory();
const binaryDirectory = join(this.installDirectory, "bin");
if (existsSync(binaryDirectory)) {
this.binaryDirectory = binaryDirectory;
} else {
error(`You have not installed ${this.name ? this.name : "this package"}`);
}
return this.binaryDirectory;
}

_getBinaryPath() {
if (this.binaryPath === -1) {
const binaryDirectory = this._getBinaryDirectory();
this.binaryPath = join(binaryDirectory, this.name);
}

return this.binaryPath;
}

install() {
const dir = this._getInstallDirectory();
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}

this.binaryDirectory = join(dir, "bin");

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

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

console.log(`Downloading release from ${this.url}`);

return axios({ url: this.url, responseType: "stream" })
.then(res => {
res.data.pipe(tar.x({ strip: 1, C: this.binaryDirectory }));
})
.then(() => {
console.log(
`${this.name ? this.name : "Your package"} has been installed!`
);
})
.catch(e => {
error(`Error fetching release: ${e.message}`);
});
}

uninstall() {
if (existsSync(this._getInstallDirectory())) {
rimraf.sync(this.installDirectory);
console.log(
`${this.name ? this.name : "Your package"} has been uninstalled`
);
}
}

run() {
const binaryPath = this._getBinaryPath();
const [, , ...args] = process.argv;

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

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

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

process.exit(result.status);
}
}

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

Expand Down Expand Up @@ -59,4 +59,4 @@ module.exports = {
install,
run,
uninstall,
};
};
226 changes: 1 addition & 225 deletions npm/npm-shrinkwrap.json

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

4 changes: 3 additions & 1 deletion npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"cli"
],
"dependencies": {
"@cloudflare/binary-install": "0.2.0"
"axios": "^0.21.1",
"rimraf": "^3.0.2",
"tar": "^6.0.2"
}
}