1、pkg-dir

  1. 'use strict';
  2. const path = require('path');
  3. const findUp = require('find-up');
  4. const pkgDir = async cwd => {
  5. const filePath = await findUp('package.json', {cwd});
  6. return filePath && path.dirname(filePath);
  7. };
  8. module.exports = pkgDir;
  9. // TODO: Remove this for the next major release
  10. module.exports.default = pkgDir;
  11. module.exports.sync = cwd => {
  12. //该库用于在某个目录下向上找指定文件,找到返回该文件路径,找不到返回null
  13. //接收两个参数 第一要寻找的文件,第二个 路径
  14. const filePath = findUp.sync('package.json', {cwd});
  15. return filePath && path.dirname(filePath);
  16. };

image.png

2、find-up

  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. //运行匹配器
  41. const runMatcher = locateOptions => {
  42. if (typeof name !== 'function') {
  43. //当前库的作用就是判断某个文件名称(带后缀)数组中是否有一个文件是否在某个目录下,
  44. //如果在,则返回这个文件名称(带后缀),如果不在则返回undefined
  45. return locatePath.sync(paths, locateOptions);
  46. }
  47. const _l=locateOptions
  48. const foundPath = name(locateOptions.cwd);
  49. if (typeof foundPath === 'string') {
  50. return locatePath.sync([foundPath], locateOptions);
  51. }
  52. return foundPath;
  53. };
  54. // eslint-disable-next-line no-constant-condition
  55. while (true) {
  56. const foundPath = runMatcher({...options, cwd: directory});
  57. if (foundPath === stop) {
  58. return;
  59. }
  60. if (foundPath) {
  61. //如果foundPath存在,返回相对路径
  62. return path.resolve(directory, foundPath);
  63. }
  64. if (directory === root) {
  65. return;
  66. }
  67. directory = path.dirname(directory);
  68. }
  69. };
  70. module.exports.exists = pathExists;
  71. module.exports.sync.exists = pathExists.sync;
  72. module.exports.stop = stop;