wallet-core/node_modules/find-up/index.js

49 lines
1008 B
JavaScript
Raw Normal View History

2016-10-10 03:43:44 +02:00
'use strict';
2017-08-14 05:01:11 +02:00
const path = require('path');
const locatePath = require('locate-path');
2016-10-10 03:43:44 +02:00
2017-08-14 05:01:11 +02:00
module.exports = (filename, opts) => {
2016-10-10 03:43:44 +02:00
opts = opts || {};
2017-08-14 05:01:11 +02:00
const startDir = path.resolve(opts.cwd || '');
const root = path.parse(startDir).root;
2016-10-10 03:43:44 +02:00
2017-08-14 05:01:11 +02:00
const filenames = [].concat(filename);
2016-10-10 03:43:44 +02:00
2017-08-14 05:01:11 +02:00
return new Promise(resolve => {
(function find(dir) {
locatePath(filenames, {cwd: dir}).then(file => {
if (file) {
resolve(path.join(dir, file));
} else if (dir === root) {
2016-10-10 03:43:44 +02:00
resolve(null);
2017-08-14 05:01:11 +02:00
} else {
find(path.dirname(dir));
2016-10-10 03:43:44 +02:00
}
});
2017-08-14 05:01:11 +02:00
})(startDir);
2016-10-10 03:43:44 +02:00
});
};
2017-08-14 05:01:11 +02:00
module.exports.sync = (filename, opts) => {
2016-10-10 03:43:44 +02:00
opts = opts || {};
2017-08-14 05:01:11 +02:00
let dir = path.resolve(opts.cwd || '');
const root = path.parse(dir).root;
const filenames = [].concat(filename);
2016-10-10 03:43:44 +02:00
2017-08-14 05:01:11 +02:00
// eslint-disable-next-line no-constant-condition
while (true) {
const file = locatePath.sync(filenames, {cwd: dir});
2016-10-10 03:43:44 +02:00
2017-08-14 05:01:11 +02:00
if (file) {
return path.join(dir, file);
} else if (dir === root) {
return null;
2016-10-10 03:43:44 +02:00
}
2017-08-14 05:01:11 +02:00
dir = path.dirname(dir);
2016-10-10 03:43:44 +02:00
}
};