command-exists
Advanced tools
Comparing version 1.0.2 to 1.1.0
'use strict'; | ||
var exec = require('child_process').exec; | ||
var execSync = require('child_process').execSync; | ||
@@ -16,3 +17,3 @@ var isUsingWindows = process.platform == 'win32' | ||
var commandExistsWindows = function function_name(commandName, callback) { | ||
var commandExistsWindows = function(commandName, callback) { | ||
var child = exec('where ' + commandName, | ||
@@ -29,8 +30,37 @@ function (error) { | ||
var commandExistsUnixSync = function(commandName) { | ||
try { | ||
var stdout = execSync('command -v ' + commandName + | ||
' 2>/dev/null' + | ||
' && { echo >&1 \'' + commandName + ' found\'; exit 0; }' | ||
); | ||
return !!stdout; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
var commandExistsWindowsSync = function(commandName, callback) { | ||
try { | ||
var stdout = execSync('where ' + commandName); | ||
return !!stdout; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
module.exports = function(commandName, callback) { | ||
if (isUsingWindows) { | ||
commandExistsWindows(commandName, callback) | ||
commandExistsWindows(commandName, callback); | ||
} else { | ||
commandExistsUnix(commandName, callback) | ||
commandExistsUnix(commandName, callback); | ||
} | ||
}; | ||
module.exports.sync = function(commandName) { | ||
if (isUsingWindows) { | ||
return commandExistsWindowsSync(commandName); | ||
} else { | ||
return commandExistsUnixSync(commandName); | ||
} | ||
}; |
{ | ||
"name": "command-exists", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "check whether a command line command exists in the current environment", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,2 +14,4 @@ command-exists | ||
### async | ||
```js | ||
@@ -19,3 +21,3 @@ var commandExists = require('command-exists'); | ||
commandExists('ls', function(err, commandExists) { | ||
if(commandExists) { | ||
@@ -28,1 +30,12 @@ // proceed confidently knowing this command is available | ||
``` | ||
### sync | ||
```js | ||
var commandExistsSync = require('command-exists').sync; | ||
// returns true/false; doesn't throw | ||
if (commandExistsSync('ls')) { | ||
// proceed | ||
} else { | ||
// ... | ||
} | ||
``` |
@@ -5,27 +5,42 @@ 'use strict'; | ||
var commandExists = require('..'); | ||
var commandExistsSync = commandExists.sync; | ||
var isUsingWindows = process.platform == 'win32' | ||
describe('commandExists()', function(){ | ||
describe('commandExists', function(){ | ||
describe('async', function() { | ||
it('it should find a command named ls or dir', function(done){ | ||
var commandToUse = 'ls' | ||
if (isUsingWindows) { | ||
commandToUse = 'dir' | ||
} | ||
it('it should find a command named ls or dir', function(done){ | ||
var commandToUse = 'ls' | ||
if (isUsingWindows) { | ||
commandToUse = 'dir' | ||
} | ||
commandExists(commandToUse, function(err, exists) { | ||
expect(err).to.be(null); | ||
expect(exists).to.be(true); | ||
done(); | ||
}); | ||
}); | ||
commandExists(commandToUse, function(err, exists) { | ||
expect(err).to.be(null); | ||
expect(exists).to.be(true); | ||
done(); | ||
it('it should not find a command named fdsafdsafdsafdsafdsa', function(done){ | ||
commandExists('fdsafdsafdsafdsafdsa', function(err, exists) { | ||
expect(err).to.be(null); | ||
expect(exists).to.be(false); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('it should not find a command named fdsafdsafdsafdsafdsa', function(done){ | ||
commandExists('fdsafdsafdsafdsafdsa', function(err, exists) { | ||
expect(err).to.be(null); | ||
expect(exists).to.be(false); | ||
done(); | ||
describe('sync', function() { | ||
it('it should find a command named ls or dir', function(done){ | ||
var commandToUse = 'ls' | ||
if (isUsingWindows) { | ||
commandToUse = 'dir' | ||
} | ||
expect(commandExistsSync(commandToUse)).to.be(true); | ||
}); | ||
it('it should not find a command named fdsafdsafdsafdsafdsa', function(done){ | ||
expect(commandExistsSync('fdsafdsafdsafdsafdsa')).to.be(false); | ||
}); | ||
}); | ||
}); | ||
}); |
6241
96
39
2