Skip to content
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
6 changes: 4 additions & 2 deletions lib/command-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ var commandExistsUnix = function(commandName, cleanedCommandName, callback) {
}

var commandExistsWindows = function(commandName, cleanedCommandName, callback) {
if (/[\x00-\x1f<>:"\|\?\*]/.test(commandName)) {
// Regex from Julio from: https://stackoverflow.com/questions/51494579/regex-windows-path-validator
if (!(/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-zA-Z]:)?(?:(?:[^<>:"\|\?\*\n])+(?:\/\/|\/|\\\\|\\)?)+$/m.test(commandName))) {
callback(null, false);
return;
}
Expand Down Expand Up @@ -93,7 +94,8 @@ var commandExistsUnixSync = function(commandName, cleanedCommandName) {
}

var commandExistsWindowsSync = function(commandName, cleanedCommandName, callback) {
if (/[\x00-\x1f<>:"\|\?\*]/.test(commandName)) {
// Regex from Julio from: https://stackoverflow.com/questions/51494579/regex-windows-path-validator
if (!(/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-zA-Z]:)?(?:(?:[^<>:"\|\?\*\n])+(?:\/\/|\/|\\\\|\\)?)+$/m.test(commandName))) {
return false;
}
try {
Expand Down
33 changes: 25 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
var expect = require('expect.js');
var commandExists = require('..');
var commandExistsSync = commandExists.sync;
var resolve = require('path').resolve;
var isUsingWindows = process.platform == 'win32'

describe('commandExists', function(){
describe('async - callback', function() {
it('it should find a command named ls or dir', function(done){
it('it should find a command named ls or xcopy', function(done){
var commandToUse = 'ls'
if (isUsingWindows) {
commandToUse = 'dir'
commandToUse = 'xcopy'
}

commandExists(commandToUse, function(err, exists) {
Expand All @@ -30,10 +31,10 @@ describe('commandExists', function(){
});

describe('async - promise', function() {
it('it should find a command named ls or dir', function(done){
it('it should find a command named ls or xcopy', function(done){
var commandToUse = 'ls'
if (isUsingWindows) {
commandToUse = 'dir'
commandToUse = 'xcopy'
}

commandExists(commandToUse)
Expand All @@ -56,10 +57,10 @@ describe('commandExists', function(){
});

describe('sync', function() {
it('it should find a command named ls or dir', function(){
it('it should find a command named ls or xcopy', function(){
var commandToUse = 'ls'
if (isUsingWindows) {
commandToUse = 'dir'
commandToUse = 'xcopy'
}
expect(commandExistsSync(commandToUse)).to.be(true);
});
Expand All @@ -68,10 +69,10 @@ describe('commandExists', function(){
expect(commandExistsSync('fdsafdsafdsafdsafdsa')).to.be(false);
});

it('it should not find a command named ls or dir prefixed with some nonsense', function(){
it('it should not find a command named ls or xcopy prefixed with some nonsense', function(){
var commandToUse = 'fdsafdsa ls'
if (isUsingWindows) {
commandToUse = 'fdsafdsaf dir'
commandToUse = 'fdsafdsaf xcopy'
}
expect(commandExistsSync(commandToUse)).to.be(false);
});
Expand Down Expand Up @@ -127,4 +128,20 @@ describe('commandExists', function(){
});
}
});

describe('absolute path', function() {
it('it should report true if there is a command with that name in absolute path', function(done) {
var commandToUse = resolve('test/executable-script.js');
commandExists(commandToUse)
.then(function(command){
expect(command).to.be(commandToUse);
done();
});
});

it('it should report false if there is not a command with that name in absolute path', function() {
var commandToUse = resolve('executable-script.js');
expect(commandExists.sync(commandToUse)).to.be(false);
});
});
});