UNPKG

663 BJavaScriptView Raw
1'use strict';
2
3var resolvePkg = require('./resolve-pkg.js');
4
5/* @private
6 *
7 * given the name of a descendent module this module locates and returns its
8 * package.json. In addition, it provides the baseDir.
9 *
10 * @method pkg
11 * @param {String} name
12 * @param {String} dir (optional) root directory to begin resolution
13 */
14module.exports = function pkg(name, dir) {
15 if (name !== './') { name += '/'; }
16
17 var packagePath = resolvePkg(name, dir);
18 if (packagePath === null) { return null; }
19
20 var thePackage = require(packagePath);
21
22 thePackage.baseDir = packagePath.slice(0, packagePath.length - 12 /* index of `/package.json` */);
23
24 return thePackage;
25};