1 | ;
|
2 |
|
3 | var findParentDir = require('find-parent-dir')
|
4 | , path = require('path')
|
5 |
|
6 | var go = module.exports =
|
7 |
|
8 | /**
|
9 | * Searches upwards from start for package.json files, asking for each if it is the mothership.
|
10 | * If a mothership is found it calls back with that.
|
11 | * If it reaches the top of the univers it calls back with nothing.
|
12 | *
|
13 | * ##### mothership result
|
14 | * - `path`: full path to the `package.json` that is the mother ship
|
15 | * - `pack`: the `package.json` object, same that was passed to ismothership
|
16 | *
|
17 | * @name mothership
|
18 | * @function
|
19 | * @param {string} start full path at which to start looking for the mothership
|
20 | * @param {function} ismothership invoked with the package object, needs to return true if it is the mothership
|
21 | * @param {function} cb called back with either an error or full path to package.json that is the mothership
|
22 | */
|
23 | function mothership(start, ismothership, cb) {
|
24 | (function findShip (root) {
|
25 | findParentDir(root, 'package.json', function (err, packageDir) {
|
26 | if (err) return cb(err);
|
27 | if (!packageDir) return cb();
|
28 |
|
29 | var pack;
|
30 | try {
|
31 | pack = require(path.join(packageDir, 'package.json'));
|
32 | if (ismothership(pack)) return cb(null, { path: path.join(packageDir, 'package.json'), pack: pack });
|
33 | findShip(path.resolve(root, '..'));
|
34 | } catch (e) {
|
35 | cb(e);
|
36 | }
|
37 | });
|
38 |
|
39 | })(start);
|
40 | }
|