-
-
Notifications
You must be signed in to change notification settings - Fork 185
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
[New] add readPackage
and readPackageSync
#236
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 |
---|---|---|
|
@@ -45,6 +45,14 @@ var maybeRealpathSync = function maybeRealpathSync(realpathSync, x, opts) { | |
return x; | ||
}; | ||
|
||
var defaultReadPackageSync = function defaultReadPackageSync(readFileSync, pkgfile) { | ||
var body = readFileSync(pkgfile); | ||
try { | ||
var pkg = JSON.parse(body); | ||
return pkg; | ||
} catch (jsonErr) {} | ||
}; | ||
|
||
var getPackageCandidates = function getPackageCandidates(x, start, opts) { | ||
var dirs = nodeModulesPaths(start, opts, x); | ||
for (var i = 0; i < dirs.length; i++) { | ||
|
@@ -63,6 +71,10 @@ module.exports = function resolveSync(x, options) { | |
var isDirectory = opts.isDirectory || defaultIsDir; | ||
var readFileSync = opts.readFileSync || fs.readFileSync; | ||
var realpathSync = opts.realpathSync || defaultRealpathSync; | ||
var readPackageSync = opts.readPackageSync || defaultReadPackageSync; | ||
if (opts.readFileSync && opts.readPackageSync) { | ||
throw new TypeError('`readFileSync` and `readPackageSync` are mutually exclusive.'); | ||
} | ||
var packageIterator = opts.packageIterator; | ||
|
||
var extensions = opts.extensions || ['.js']; | ||
|
@@ -133,11 +145,7 @@ module.exports = function resolveSync(x, options) { | |
return loadpkg(path.dirname(dir)); | ||
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. @SimenB Separately from this PR, it would also be nice if there was a way to short-circuit this recursive check for package.json file in every directory. 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. agreed, like the 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. Yeah! 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. I'm open to a PR that enables that, for sure. |
||
} | ||
|
||
var body = readFileSync(pkgfile); | ||
|
||
try { | ||
var pkg = JSON.parse(body); | ||
} catch (jsonErr) {} | ||
var pkg = readPackageSync(readFileSync, pkgfile); | ||
|
||
if (pkg && opts.packageFilter) { | ||
pkg = opts.packageFilter(pkg, pkgfile, dir); | ||
|
@@ -150,8 +158,7 @@ module.exports = function resolveSync(x, options) { | |
var pkgfile = path.join(isDirectory(x) ? maybeRealpathSync(realpathSync, x, opts) : x, '/package.json'); | ||
if (isFile(pkgfile)) { | ||
try { | ||
var body = readFileSync(pkgfile, 'UTF8'); | ||
var pkg = JSON.parse(body); | ||
var pkg = readPackageSync(readFileSync, pkgfile); | ||
} catch (e) {} | ||
|
||
if (pkg && opts.packageFilter) { | ||
|
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.
if it wasn't obvious - this is done to satisfy an eslint rule about parameter reassignment (which
packageFilter
does below)