-
-
Notifications
You must be signed in to change notification settings - Fork 2
Sub-directory support (fixes #5) #7
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
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 | ||
|
@@ -48,7 +48,7 @@ function statAsync (fp, callback) { | |
function tryStatSync (fp) { | ||
try { | ||
return fs.statSync(fp).isDirectory() | ||
} catch(err) { | ||
} catch (err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is simply the commit from #6 merged in. |
||
return false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}, | ||
"keywords": [ | ||
"check", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,12 @@ | |
|
||
'use strict' | ||
|
||
var mkdirp = require('mkdirp') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 () { | ||
|
@@ -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('..') | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.npmjs.com/package/pkg-dir