You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. const path = require('path');
  3. const locatePath = require('locate-path');
  4. module.exports = (filename, opts = {}) => {
  5. const startDir = path.resolve(opts.cwd || '');
  6. const {root} = path.parse(startDir);
  7. const filenames = [].concat(filename);
  8. return new Promise(resolve => {
  9. (function find(dir) {
  10. locatePath(filenames, {cwd: dir}).then(file => {
  11. if (file) {
  12. resolve(path.join(dir, file));
  13. } else if (dir === root) {
  14. resolve(null);
  15. } else {
  16. find(path.dirname(dir));
  17. }
  18. });
  19. })(startDir);
  20. });
  21. };
  22. module.exports.sync = (filename, opts = {}) => {
  23. let dir = path.resolve(opts.cwd || '');
  24. const {root} = path.parse(dir);
  25. const filenames = [].concat(filename);
  26. // eslint-disable-next-line no-constant-condition
  27. while (true) {
  28. const file = locatePath.sync(filenames, {cwd: dir});
  29. if (file) {
  30. return path.join(dir, file);
  31. }
  32. if (dir === root) {
  33. return null;
  34. }
  35. dir = path.dirname(dir);
  36. }
  37. };