-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Designory/fix/check-for-zip-and-diff-binary
Fix - Check for `zip` and `diff` binaries
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |