UNPKG

939 BJavaScriptView Raw
1'use strict';
2var path = require('path');
3var pathExists = require('path-exists');
4var Promise = require('pinkie-promise');
5
6function splitPath(x) {
7 return path.resolve(x || '').split(path.sep);
8}
9
10function join(parts, filename) {
11 return parts.join(path.sep) + path.sep + filename;
12}
13
14module.exports = function (filename, opts) {
15 opts = opts || {};
16
17 var parts = splitPath(opts.cwd);
18
19 return new Promise(function (resolve) {
20 (function find() {
21 var fp = join(parts, filename);
22
23 pathExists(fp).then(function (exists) {
24 if (exists) {
25 resolve(fp);
26 } else if (!parts.pop()) {
27 resolve(null);
28 } else {
29 find();
30 }
31 });
32 })();
33 });
34};
35
36module.exports.sync = function (filename, opts) {
37 opts = opts || {};
38
39 var parts = splitPath(opts.cwd);
40 var len = parts.length;
41
42 while (len--) {
43 var fp = join(parts, filename);
44
45 if (pathExists.sync(fp)) {
46 return fp;
47 }
48
49 parts.pop();
50 }
51
52 return null;
53};