Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix to work with pathnames for windows #21

Merged
merged 3 commits into from
Oct 13, 2018
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
20 changes: 20 additions & 0 deletions lib/command-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var exec = require('child_process').exec;
var execSync = require('child_process').execSync;
var fs = require('fs');
var path = require('path');
var access = fs.access;
var accessSync = fs.accessSync;
var constants = fs.constants || fs;
Expand Down Expand Up @@ -61,6 +62,10 @@ var commandExistsUnix = function(commandName, cleanedCommandName, callback) {
}

var commandExistsWindows = function(commandName, cleanedCommandName, callback) {
if (/[\x00-\x1f<>:"\|\?\*]/.test(commandName)) {
callback(null, false);
return;
}
var child = exec('where ' + cleanedCommandName,
function (error) {
if (error !== null){
Expand Down Expand Up @@ -88,6 +93,9 @@ var commandExistsUnixSync = function(commandName, cleanedCommandName) {
}

var commandExistsWindowsSync = function(commandName, cleanedCommandName, callback) {
if (/[\x00-\x1f<>:"\|\?\*]/.test(commandName)) {
return false;
}
try {
var stdout = execSync('where ' + cleanedCommandName, {stdio: []});
return !!stdout;
Expand All @@ -105,6 +113,18 @@ var cleanInput = function(s) {
return s;
}

if (isUsingWindows) {
cleanInput = function(s) {
var isPathName = /[\\]/.test(s);
if (isPathName) {
var dirname = '"' + path.dirname(s) + '"';
var basename = '"' + path.basename(s) + '"';
return dirname + ':' + basename;
}
return '"' + s + '"';
}
}

module.exports = function commandExists(commandName, callback) {
var cleanedCommandName = cleanInput(commandName);
if (!callback && typeof Promise !== 'undefined') {
Expand Down
Empty file added test/executable-script.cmd
Empty file.
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,21 @@ describe('commandExists', function(){
});
});
}

if (isUsingWindows) {
it('it should report true if there is an executable file with that name', function(done) {
var commandToUse = 'test\\executable-script.cmd'
commandExists(commandToUse)
.then(function(command){
expect(command).to.be(commandToUse);
done();
});
});

it('it should report false if there is a double quotation mark in the file path', function() {
var commandToUse = 'test\\"executable-script.cmd'
expect(commandExists.sync(commandToUse)).to.be(false);
});
}
});
});