lib/package-json-discover.js
function discover(from) {
    from = from || caller();

    // check 'from' validity
    if ("string" != typeof from) {
        var given = typeof from;
        throw new Error("'from' must be a string, [" + given + "] given");
    }

    // if 'from' is not absolute, make it absolute
    if (!pathIsAbsolute(from)) {
        from = path.join(
            path.dirname(caller()),
            from
        );
    }

    // if 'from' is a file, keep its dirname
    if (fs.existsSync(from)) {
        var stat = fs.lstatSync(from);

        if (stat.isFile()) {
            from = path.dirname(from);
        }
    }

    // check path cache
    if (discoverCache[from]) {
        return discoverCache[from];
    }

    // process directories from 'from' to system root directory until we find a package.json file
    var dir                 = from;
    var old                 = null;
    var processedDirList    = [];

    while (true) {
        if (discoverCache[dir]) {
            // path from cache
            return discoverCache[dir];
        } else if (fs.existsSync(dir + "/package.json")) {
            // package.json found
            processedDirList.push(dir);
            var discoveredDir = dir + "/package.json";

            for (var i in processedDirList) {
                discoverCache[processedDirList[i]] = discoveredDir;
            }

            return discoveredDir;
        } else {
            // process parent directory
            old = dir;
            dir = path.dirname(dir);

            // check if we reached the system root directory
            if (old === dir) {
                break;
            }

            // add processed dir to the list
            processedDirList.push(old);
        }
    }

    throw new Error("Unabe to find a package.json from '" + from + "'");
}
function discover(from) {
    from = from || caller();

    // check 'from' validity
    if ("string" != typeof from) {
        var given = typeof from;
        throw new Error("'from' must be a string, [" + given + "] given");
    }

    // if 'from' is not absolute, make it absolute
    if (!pathIsAbsolute(from)) {
        from = path.join(
            path.dirname(caller()),
            from
        );
    }

    // if 'from' is a file, keep its dirname
    if (fs.existsSync(from)) {
        var stat = fs.lstatSync(from);

        if (stat.isFile()) {
            from = path.dirname(from);
        }
    }

    // check path cache
    if (discoverCache[from]) {
        return discoverCache[from];
    }

    // process directories from 'from' to system root directory until we find a package.json file
    var dir                 = from;
    var old                 = null;
    var processedDirList    = [];

    while (true) {
        if (discoverCache[dir]) {
            // path from cache
            return discoverCache[dir];
        } else if (fs.existsSync(dir + "/package.json")) {
            // package.json found
            processedDirList.push(dir);
            var discoveredDir = dir + "/package.json";

            for (var i in processedDirList) {
                discoverCache[processedDirList[i]] = discoveredDir;
            }

            return discoveredDir;
        } else {
            // process parent directory
            old = dir;
            dir = path.dirname(dir);

            // check if we reached the system root directory
            if (old === dir) {
                break;
            }

            // add processed dir to the list
            processedDirList.push(old);
        }
    }

    throw new Error("Unabe to find a package.json from '" + from + "'");
}
function discover ( from ) { from = from || caller ( ) ; // check 'from' validity if ( "string" != typeof from ) { var given = typeof from ; throw new Error ( "'from' must be a string, [" + given + "] given" ) ; } // if 'from' is not absolute, make it absolute if ( ! pathIsAbsolute ( from ) ) { from = path . join ( path . dirname ( caller ( ) ) , from ) ; } // if 'from' is a file, keep its dirname if ( fs . existsSync ( from ) ) { var stat = fs . lstatSync ( from ) ; if ( stat . isFile ( ) ) { from = path . dirname ( from ) ; } } // check path cache if ( discoverCache [ from ] ) { return discoverCache [ from ] ; } // process directories from 'from' to system root directory until we find a package.json file var dir = from ; var old = null ; var processedDirList = [ ] ; while ( true ) { if ( discoverCache [ dir ] ) { // path from cache return discoverCache [ dir ] ; } else if ( fs . existsSync ( dir + "/package.json" ) ) { // package.json found processedDirList . push ( dir ) ; var discoveredDir = dir + "/package.json" ; for ( var i in processedDirList ) { discoverCache [ processedDirList [ i ] ] = discoveredDir ; } return discoveredDir ; } else { // process parent directory old = dir ; dir = path . dirname ( dir ) ; // check if we reached the system root directory if ( old === dir ) { break ; } // add processed dir to the list processedDirList . push ( old ) ; } } throw new Error ( "Unabe to find a package.json from '" + from + "'" ) ; }
Discovers the closest package.json file and returns its absolute path
@param {string} [from] - The path to the directory where to start discovering. If omitted, it is the path to the file that calls this function.
@returns {string}
Discovers the closest package . json file and returns its absolute path