選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. const path = require('path');
  3. const locatePath = require('locate-path');
  4. const pathExists = require('path-exists');
  5. const stop = Symbol('findUp.stop');
  6. module.exports = async (name, options = {}) => {
  7. let directory = path.resolve(options.cwd || '');
  8. const {root} = path.parse(directory);
  9. const paths = [].concat(name);
  10. const runMatcher = async locateOptions => {
  11. if (typeof name !== 'function') {
  12. return locatePath(paths, locateOptions);
  13. }
  14. const foundPath = await name(locateOptions.cwd);
  15. if (typeof foundPath === 'string') {
  16. return locatePath([foundPath], locateOptions);
  17. }
  18. return foundPath;
  19. };
  20. // eslint-disable-next-line no-constant-condition
  21. while (true) {
  22. // eslint-disable-next-line no-await-in-loop
  23. const foundPath = await runMatcher({...options, cwd: directory});
  24. if (foundPath === stop) {
  25. return;
  26. }
  27. if (foundPath) {
  28. return path.resolve(directory, foundPath);
  29. }
  30. if (directory === root) {
  31. return;
  32. }
  33. directory = path.dirname(directory);
  34. }
  35. };
  36. module.exports.sync = (name, options = {}) => {
  37. let directory = path.resolve(options.cwd || '');
  38. const {root} = path.parse(directory);
  39. const paths = [].concat(name);
  40. const runMatcher = locateOptions => {
  41. if (typeof name !== 'function') {
  42. return locatePath.sync(paths, locateOptions);
  43. }
  44. const foundPath = name(locateOptions.cwd);
  45. if (typeof foundPath === 'string') {
  46. return locatePath.sync([foundPath], locateOptions);
  47. }
  48. return foundPath;
  49. };
  50. // eslint-disable-next-line no-constant-condition
  51. while (true) {
  52. const foundPath = runMatcher({...options, cwd: directory});
  53. if (foundPath === stop) {
  54. return;
  55. }
  56. if (foundPath) {
  57. return path.resolve(directory, foundPath);
  58. }
  59. if (directory === root) {
  60. return;
  61. }
  62. directory = path.dirname(directory);
  63. }
  64. };
  65. module.exports.exists = pathExists;
  66. module.exports.sync.exists = pathExists.sync;
  67. module.exports.stop = stop;