Skip to content

Commit

Permalink
Merge pull request #23 from Designory/fix/check-for-zip-and-diff-binary
Browse files Browse the repository at this point in the history
Fix - Check for `zip` and `diff` binaries
  • Loading branch information
romellem authored Jun 17, 2020
2 parents f272a6f + 40e847a commit fbc694d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ const fs = require('fs-extra');
const colors = require('colors');
const parseArgs = require('minimist');

const hasBinary = require('./src/has-binary');
const diffDirectories = require('./src/gen-diff-object');

(async () => {
const has_zip = hasBinary('zip');
const has_diff = hasBinary('diff');

if (!(has_zip && has_diff)) {
console.error(`Could not find both "${'zip'.yellow}" and "${'diff'.yellow}" binaries, both of which are required. Exiting.`);
process.exit(1);
}

const arg_options = {
boolean: [
'quiet',
Expand Down
32 changes: 32 additions & 0 deletions src/has-binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs');
const path = require('path');

const getPaths = (bin_path) => {
const envPath = process.env.PATH || '';
const envExt = process.env.PATHEXT || '';
return envPath
.replace(/["]+/g, '')
.split(path.delimiter)
.map((chunk) => {
return envExt.split(path.delimiter).map((ext) => path.join(chunk, bin_path + ext));
})
.reduce((a, b) => a.concat(b));
};

/**
* Helper utility to check if a binary exists in our $PATH variable. Used to
* see if the user has `zip` installed (and maybe others).
* @see https://github.com/springernature/hasbin/blob/5af037b/lib/hasbin.js
* @returns {Boolean}
*/
const hasBinary = (bin) => {
return getPaths(bin).some((filePath) => {
try {
return fs.statSync(filePath).isFile();
} catch (error) {
return false;
}
});
};

module.exports = hasBinary;

0 comments on commit fbc694d

Please sign in to comment.