-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
65 lines (57 loc) · 2.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
var findParentDir = require('find-parent-dir')
, path = require('path')
var go = module.exports =
/**
* Searches upwards from start for package.json files, asking for each if it is the mothership.
* If a mothership is found it calls back with that.
* If it reaches the top of the univers it calls back with nothing.
*
* ##### mothership result
* - `path`: full path to the `package.json` that is the mother ship
* - `pack`: the `package.json` object, same that was passed to ismothership
*
* @name mothership
* @function
* @param {string} start full path at which to start looking for the mothership
* @param {function} ismothership invoked with the package object, needs to return true if it is the mothership
* @param {function} cb called back with either an error or full path to package.json that is the mothership
*/
function mothership(start, ismothership, cb) {
(function findShip (root) {
findParentDir(root, 'package.json', function (err, packageDir) {
if (err) return cb(err);
if (!packageDir) return cb();
var pack;
try {
pack = require(path.join(packageDir, 'package.json'));
if (ismothership(pack)) return cb(null, { path: path.join(packageDir, 'package.json'), pack: pack });
findShip(path.resolve(root, '..'));
} catch (e) {
cb(e);
}
});
})(start);
}
/**
* Synchronous version of mothership.
*
* @name mothership::sync
* @function
* @param {string} start full path at which to start looking for the mothership
* @param {function} ismothership invoked with the package object, needs to return true if it is the mothership
* @return {string} full path to package.json that is the mothership or `null` if it wasn't found
*/
go.sync = function sync(start, ismothership) {
return (function findShip (root) {
var packageDir = findParentDir.sync(root, 'package.json')
var pack;
try {
pack = require(path.join(packageDir, 'package.json'));
if (ismothership(pack)) return { path: path.join(packageDir, 'package.json'), pack: pack };
return findShip(path.resolve(root, '..'));
} catch (e) {
return false
}
})(start);
}