detect-installed
Advanced tools
Comparing version 0.0.1 to 0.0.2
49
index.js
@@ -10,2 +10,3 @@ /** | ||
var fs = require('fs'); | ||
@@ -17,2 +18,4 @@ var path = require('path'); | ||
var handle = require('handle-arguments'); | ||
var debug = require('debug')('detect-installed'); | ||
var cwd = process.cwd(); | ||
@@ -26,2 +29,9 @@ | ||
if (!is.string(name)) { | ||
throw new TypeError('[detect-installed] expect `name` to be string'); | ||
} | ||
if (!name.length) { | ||
throw new Error('[detect-installed] expect `name` not to be empty string') | ||
} | ||
var fp = path.join(modules, name); | ||
@@ -31,7 +41,10 @@ var nm = path.join(cwd, 'node_modules', name); | ||
if (is.boolean(local)) { | ||
debug('checking locally') | ||
fp = local ? path.join(cwd, 'node_modules', name) : fp; | ||
} | ||
if (fnName(callback) !== 'defaultHandleArgumentsCallback') { | ||
debug('(async) start') | ||
return statAsync(fp, callback); | ||
} | ||
debug('(sync) start') | ||
return tryStatSync(fp); | ||
@@ -43,11 +56,17 @@ }; | ||
if (err) { | ||
if (err.code === 'ENOENT') { | ||
callback(null, false); | ||
return; | ||
} | ||
err.origin = __dirname; | ||
err.msg = '[detect-installed] async error: ' + fp; | ||
callback(new Error(err)); | ||
debug('(async) not exists, so cb(null, false)') | ||
callback(null, false); | ||
return; | ||
// if (err.code === 'ENOENT') { | ||
// debug('async not exists, so cb(null, false)') | ||
// callback(null, false); | ||
// return; | ||
// } | ||
// debug('async fail, so cb(err, false') | ||
// err.origin = __dirname; | ||
// err.msg = '[detect-installed] async error: ' + fp; | ||
// callback(new Error(err), false); | ||
// return; | ||
} | ||
debug('(async) pass') | ||
callback(null, res.isDirectory()); | ||
@@ -61,9 +80,13 @@ }); | ||
} catch(err) { | ||
if (err.code === 'ENOENT') { | ||
return false; | ||
} | ||
err.origin = __dirname; | ||
err.msg = '[detect-installed] sync error: ' + fp; | ||
throw new Error(err); | ||
debug('(sync) not exists, so return false') | ||
return false; | ||
// if (err.code === 'ENOENT') { | ||
// debug('sync not exists, so cb(null, false)') | ||
// return false; | ||
// } | ||
// debug('sync fail, so throw err') | ||
// err.origin = __dirname; | ||
// err.msg = '[detect-installed] sync error: ' + fp; | ||
// throw new Error(err); | ||
} | ||
} |
{ | ||
"name": "detect-installed", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Checks that given package name is installed locally (in current working directory) or globally.", | ||
@@ -42,2 +42,3 @@ "scripts": { | ||
"dependencies": { | ||
"debug": "^2.1.3", | ||
"fn-name": "~2.0.0", | ||
@@ -44,0 +45,0 @@ "global-modules": "^0.1.0", |
110
test.js
@@ -13,58 +13,60 @@ /** | ||
// test('detect-installed:', function() { | ||
// test('should throw TypeError if `callback` is given, but not is a function', function(done) { | ||
// function fixture() { | ||
// detectInstalled('abcd', [1, 2, 3]); | ||
// } | ||
test('detect-installed:', function() { | ||
test('should checks globally asynchronous. (when exists)', function(done) { | ||
detectInstalled('npm', function(err, actual) { | ||
test.equal(actual, true); | ||
done(); | ||
}); | ||
// detectInstalled('npm', false, function(err, actual) { | ||
// test.equal(actual, true); | ||
// done(); | ||
// }); | ||
// detectInstalled('npm', [1, 2, 3], function(err, actual) { | ||
// test.equal(actual, true); | ||
// done(); | ||
// }); | ||
}); | ||
test('should checks globally asynchronous. (when not exists)', function(done) { | ||
detectInstalled('s', function(err, actual) { | ||
test.equal(actual, false); | ||
done(); | ||
}); | ||
}); | ||
// test('should checks globally synchronous.', function(done) { | ||
// var actual1 = detectInstalled('npm'); | ||
// var actual2 = detectInstalled('npm', false); | ||
// var actual3 = detectInstalled('npm', [1,2,3]); | ||
// var actual4 = detectInstalled('npm', [1,2,3], {not: 'a function'}); | ||
// var actual5 = detectInstalled('not-exist-asdasjkdh', false, {not: 'a function'}); | ||
// var actual6 = detectInstalled('not-exist-asdasjkdh'); | ||
// var expected = true; | ||
// test.throws(fixture, TypeError); | ||
// // test.throws(fixture, /expect `callback` to be function if given/); | ||
// done(); | ||
// }); | ||
// }); | ||
// test.equal(actual1, expected); | ||
// test.equal(actual2, expected); | ||
// test.equal(actual3, expected); | ||
// test.equal(actual4, expected); | ||
// test.equal(actual5, false); | ||
// test.equal(actual6, false); | ||
// done(); | ||
// }); | ||
// test('should checks locally asynchronous.', function(done) { | ||
// detectInstalled('npm', true, function(err, actual) { | ||
// test.equal(actual, false); | ||
// done(); | ||
// }); | ||
// }); | ||
// test('should checks locally synchronous.', function(done) { | ||
// var actual1 = detectInstalled('npm', true); | ||
// var actual2 = detectInstalled('is-kindof', true); | ||
// var actual3 = detectInstalled('npm', true, {not: 'a function'}); | ||
// var actual4 = detectInstalled('not-exist-asdasjkdh', true, {not: 'a function'}); | ||
// var expected = false; | ||
console.log(false, detectInstalled('fn-name')) | ||
// false (checks globally) | ||
console.log(true, detectInstalled('fn-name', true)) | ||
// true (checks locally) | ||
console.log(false, detectInstalled('fn-name', false)) | ||
// false (checks globally) | ||
console.log(false, detectInstalled('fn-name', [1,2,3])) | ||
// false (checks globally) | ||
console.log(false, detectInstalled('fn-name', [1,2,3], {a: 'b'})) | ||
// false (checks globally) | ||
detectInstalled('fn-name', function _cb(err, res) { | ||
console.log(false, res); | ||
// false (checks globally) | ||
}) | ||
detectInstalled('fn-name', true, function _cb(err, res) { | ||
console.log(true, res); | ||
// true (checks locally) | ||
}) | ||
detectInstalled('fn-name', false, function _cb(err, res) { | ||
console.log(false, res); | ||
// false (checks globally) | ||
}) | ||
detectInstalled('fn-name', [1,2,3], function _cb(err, res) { | ||
console.log(false, res); | ||
// false (checks globally) | ||
}) | ||
// detectInstalled('abcd', true); | ||
// detectInstalled('abcd', true, function _cb() {}); | ||
// detectInstalled('abcd', function _cb() {}); | ||
// detectInstalled('abcd', [1, 2, 3]); | ||
// detectInstalled('abcd', [1, 2, 3], function _cb() {}); | ||
// test.equal(actual1, expected); | ||
// test.equal(actual2, true); | ||
// test.equal(actual3, expected); | ||
// test.equal(actual4, expected); | ||
// done(); | ||
// }); | ||
}); |
15182
141
5
+ Addeddebug@^2.1.3
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)