Skip to content
Closed
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: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
var fs = require('fs')
var path = require('path')
var modules = require('global-modules')
var cwd = process.cwd()
var getPkgDir = require('pkg-dir').sync
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


module.exports = function detectInstalled (name, local, callback) {
if (!isValidString(name)) {
throw new TypeError('detect-installed: expect `name` be string')
}
var fp = path.join(modules, name)
var nm = path.join(cwd, 'node_modules', name)
var nm = path.join(getPkgDir(), 'node_modules', name)

if (typeof local === 'function') {
callback = local
Expand Down Expand Up @@ -48,7 +48,7 @@ function statAsync (fp, callback) {
function tryStatSync (fp) {
try {
return fs.statSync(fp).isDirectory()
} catch(err) {
} catch (err) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is simply the commit from #6 merged in.

return false
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"test": "standard && node test.js"
},
"dependencies": {
"global-modules": "^0.2.0"
"global-modules": "^0.2.0",
"pkg-dir": "^1.0.0"
},
"devDependencies": {
"assertit": "^0.1.0"
"assertit": "^0.1.0",
"mkdirp": "^0.5.1"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
"keywords": [
"check",
Expand Down
43 changes: 43 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

'use strict'

var mkdirp = require('mkdirp')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var test = require('assertit')
var detectInstalled = require('./index')

mkdirp.sync('subdir')

test('detect-installed:', function () {
test('should throw TypeError if `name` is not a string', function (done) {
function fixture () {
Expand Down Expand Up @@ -116,5 +119,45 @@ test('detect-installed:', function () {
test.equal(actual2, expected)
done()
})
test('asynchronous, when exists, in sub-dir', function (done) {
process.chdir('subdir')
detectInstalled('global-modules', true, function (err, actual) {
test.equal(err, null)
test.equal(actual, true)
done()
process.chdir('..')
})
})
test('asynchronous, when not exists, in sub-dir', function (done) {
process.chdir('subdir')
detectInstalled('when not exists', true, function (err, actual) {
test.equal(err, null)
test.equal(actual, false)
done()
process.chdir('..')
})
})
test('synchronous, when exists, in sub-dir', function (done) {
process.chdir('subdir')
var actual1 = detectInstalled('global-modules', true)
var actual2 = detectInstalled('global-modules', true, {not: 'a function'})
var expected = true

test.equal(actual1, expected)
test.equal(actual2, expected)
done()
process.chdir('..')
})
test('synchronous, when not exists, in sub-dir', function (done) {
process.chdir('subdir')
var actual1 = detectInstalled('npm', true)
var actual2 = detectInstalled('npm', true, {not: 'a function'})
var expected = false

test.equal(actual1, expected)
test.equal(actual2, expected)
done()
process.chdir('..')
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these tests are simply duplicates of the "local" tests, with changing of the directory (and returning it afterwards).

})
})