From 1f26829324abc3ede588c90886d14f623e5e8395 Mon Sep 17 00:00:00 2001 From: Matt Wade Date: Tue, 16 Jun 2020 11:40:15 -0500 Subject: [PATCH 1/2] Adds "has binary" checker --- src/has-binary.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/has-binary.js diff --git a/src/has-binary.js b/src/has-binary.js new file mode 100644 index 0000000..02d58bd --- /dev/null +++ b/src/has-binary.js @@ -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; From 40e847ad86b37d0377977d65cb83a2f0e6ecdc1e Mon Sep 17 00:00:00 2001 From: Matt Wade Date: Tue, 16 Jun 2020 11:43:35 -0500 Subject: [PATCH 2/2] Checks for 'zip' and 'diff' binaries Exits with non-zero code if they both aren't found. --- index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.js b/index.js index 1494a52..f610829 100644 --- a/index.js +++ b/index.js @@ -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',