Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
Use Zip when uninstalling using nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed May 31, 2020
1 parent 44d0628 commit 86c38fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
30 changes: 14 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const request = require("request"),
path = require("path"),
zlib = require("zlib"),
unzipper = require("unzipper"),
mkdirp = require("mkdirp"),
fs = require("fs"),
exec = require("child_process").exec;
Expand All @@ -28,7 +28,8 @@ function getInstallationPath(callback) {
// `$npm_execpath bin` will output the path where binary files should be installed
// using whichever package manager is current
const execPath = process.env.npm_execpath;
const packageManager = execPath.includes("yarn") ? "yarn global" : "npm";
const packageManager =
execPath && execPath.includes("yarn") ? "yarn global" : "npm";
exec(`${packageManager} bin`, function (err, stdout, stderr) {
let dir = null;
if (
Expand Down Expand Up @@ -172,30 +173,27 @@ function install(callback) {
return callback("Invalid inputs");
}
mkdirp.sync(opts.binPath);
let ungz = zlib.createGunzip();

ungz.on("error", callback);

// First we will Un-GZip, then we will untar. So once untar is completed,
// binary is downloaded into `binPath`. Verify the binary and call it good
ungz.on(
"end",
verifyAndPlaceBinary.bind(null, opts.binName, opts.binPath, callback)
);

console.log("Downloading from URL: " + opts.url);
let req = request({ uri: opts.url });
req.on(
"error",
callback.bind(null, "Error downloading from URL: " + opts.url)
);
req.on("response", function (res) {
if (res.statusCode !== 200)
if (res.statusCode !== 200) {
return callback(
"Error downloading binary. HTTP Status Code: " + res.statusCode
);

req.pipe(ungz);
}
req
.pipe(unzipper.Extract({ path: opts.binPath }))
.on("error", callback)
// First we will Un-GZip, then we will untar. So once untar is completed,
// binary is downloaded into `binPath`. Verify the binary and call it good
.on(
"close",
verifyAndPlaceBinary.bind(null, opts.binName, opts.binPath, callback)
);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"mkdirp": "^0.5.1",
"request": "^2.81.0",
"tar": "^2.2.1"
"unzipper": "^0.10.11"
},
"devDependencies": {
"babel-cli": "^6.24.1",
Expand Down

0 comments on commit 86c38fe

Please sign in to comment.