findup-package-json
Find the closest package.json file meeting specific criteria by searching upwards from a given directory until hitting root.
Usage
Can be used as a regular module and/or a browserify transform to inline specific fields of a projects 'package.json'
findup([fields], [dir], [filter], found(err, packageJson))
Given a starting directory dir
, look up through every directory to see if it contains a package.json
file matching the filter
function, for example:
var findup =/******************************************************************************PARAMETER:findup({fields: optional array,* array of fields you want from package.json* defaults to ALL FIELDS if not providedpath: optional string,* the path where the search starts, going one level up towards the root directory and returns the package.json content if it can find it* defaults to 'process.cwd()'filter: optional function,* a function (json, filename) in which you can test the content and return true if it's the right packageJson, otherwise return false to continue the search.* defaults to function (packageJson) { return true } to return the first foundfound: required callback function* a callback (err, packageJson) that will be called with the content of the found package.json which has all the fields you asked for})******************************************************************************/// Below a list of all different ways to call "findup"// or
Note that filter
is optional and takes the following arguments:
json
: the parsedpackage.json
file.filename
: thepackage.json
's absolute filename.
file = findup.sync([fields], [dir], [filter])
Same as the findup
function, however executed synchronously:
var findup =/******************************************************************************PARAMETER:var found = findup.sync({fields: optional array,* array of fields you want from package.json* defaults to ALL FIELDS if not providedpath: optional string,* the path where the search starts, going one level up towards the root directory and returns the package.json content if it can find it* defaults to 'process.cwd()'filter: optional function* a function (json, filename) in which you can test the content and return true if it's the right packageJson, otherwise return false to continue the search.* defaults to function (packageJson) { return true } to return the first found}) // returns the content of the found package.json which has all the fields you asked for******************************************************************************/// Below a list of all different ways to call "findup"var result = findup// orvar result = findup
browserify
Turns the calls to require('findup-package-json')
as shown above and the selected package.json
fields into an inlined object that goes into the bundle.js
, but requires the arguments to findup
or findup.sync
to be statically analysable
It only works if the call is directly made on the require statement
{ // ... use err and/or json console console}// orvar json = consoleconsole
after browserify index.js -t findup-package-json > bundle.js
will be turned into
{ // ... use err and/or json console console} pkgfile : '...' pkg : /* package.json with requested fields */ // orvar json = pkgfile : '...' pkg : /* package.json with requested fields */ consoleconsole
License
MIT. See LICENSE.md for details.