1 | 'use strict';
|
2 | var path = require('path');
|
3 | var pathExists = require('path-exists');
|
4 | var Promise = require('pinkie-promise');
|
5 |
|
6 | function splitPath(x) {
|
7 | return path.resolve(x || '').split(path.sep);
|
8 | }
|
9 |
|
10 | function join(parts, filename) {
|
11 | return parts.join(path.sep) + path.sep + filename;
|
12 | }
|
13 |
|
14 | module.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 |
|
36 | module.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 | };
|