index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. var isPlainObject = require('is-plain-object');
  5. var pick = require('object.pick');
  6. var defaults = require('object.defaults/immutable');
  7. var expandTilde = require('expand-tilde');
  8. var parsePath = require('parse-filepath');
  9. function fined(pathObj, defaultObj) {
  10. var expandedPath = expandPath(pathObj, defaultObj);
  11. return expandedPath ? findWithExpandedPath(expandedPath) : null;
  12. }
  13. function expandPath(pathObj, defaultObj) {
  14. if (!isPlainObject(defaultObj)) {
  15. defaultObj = {};
  16. }
  17. if (isString(pathObj)) {
  18. pathObj = { path: pathObj };
  19. }
  20. if (!isPlainObject(pathObj)) {
  21. pathObj = {};
  22. }
  23. pathObj = defaults(pathObj, defaultObj);
  24. var filePath;
  25. if (!isString(pathObj.path)) {
  26. return null;
  27. }
  28. // Execution of toString is for a String object.
  29. if (isString(pathObj.name) && pathObj.name) {
  30. if (pathObj.path) {
  31. filePath = expandTilde(pathObj.path.toString());
  32. filePath = path.join(filePath, pathObj.name.toString());
  33. } else {
  34. filePath = pathObj.name.toString();
  35. }
  36. } else {
  37. filePath = expandTilde(pathObj.path.toString());
  38. }
  39. var extArr = createExtensionArray(pathObj.extensions);
  40. var extMap = createExtensionMap(pathObj.extensions);
  41. var basedir = isString(pathObj.cwd) ? pathObj.cwd.toString() : '.';
  42. basedir = path.resolve(expandTilde(basedir));
  43. var findUp = !!pathObj.findUp;
  44. var parsed = parsePath(filePath);
  45. if (parsed.isAbsolute) {
  46. filePath = filePath.slice(parsed.root.length);
  47. findUp = false;
  48. basedir = parsed.root;
  49. /* istanbul ignore if */
  50. } else if (parsed.root) { // Expanded path has a drive letter on Windows.
  51. filePath = filePath.slice(parsed.root.length);
  52. basedir = path.resolve(parsed.root);
  53. }
  54. return {
  55. path: filePath,
  56. basedir: basedir,
  57. findUp: findUp,
  58. extArr: extArr,
  59. extMap: extMap,
  60. };
  61. }
  62. function findWithExpandedPath(expanded) {
  63. var found = expanded.findUp ?
  64. findUpFile(expanded.basedir, expanded.path, expanded.extArr) :
  65. findFile(expanded.basedir, expanded.path, expanded.extArr);
  66. if (!found) {
  67. return null;
  68. }
  69. if (expanded.extMap) {
  70. found.extension = pick(expanded.extMap, found.extension);
  71. }
  72. return found;
  73. }
  74. function findFile(basedir, relpath, extArr) {
  75. var noExtPath = path.resolve(basedir, relpath);
  76. for (var i = 0, n = extArr.length; i < n; i++) {
  77. var filepath = noExtPath + extArr[i];
  78. try {
  79. fs.statSync(filepath);
  80. return { path: filepath, extension: extArr[i] };
  81. } catch (e) {}
  82. }
  83. return null;
  84. }
  85. function findUpFile(basedir, filepath, extArr) {
  86. var lastdir;
  87. do {
  88. var found = findFile(basedir, filepath, extArr);
  89. if (found) {
  90. return found;
  91. }
  92. lastdir = basedir;
  93. basedir = path.dirname(basedir);
  94. } while (lastdir !== basedir);
  95. return null;
  96. }
  97. function createExtensionArray(exts) {
  98. if (isString(exts)) {
  99. return [exts];
  100. }
  101. if (Array.isArray(exts)) {
  102. exts = exts.filter(isString);
  103. return (exts.length > 0) ? exts : [''];
  104. }
  105. if (isPlainObject(exts)) {
  106. exts = Object.keys(exts);
  107. return (exts.length > 0) ? exts : [''];
  108. }
  109. return [''];
  110. }
  111. function createExtensionMap(exts) {
  112. if (!isPlainObject(exts)) {
  113. return null;
  114. }
  115. if (isEmpty(exts)) {
  116. return { '': null };
  117. }
  118. return exts;
  119. }
  120. function isEmpty(object) {
  121. return !Object.keys(object).length;
  122. }
  123. function isString(value) {
  124. if (typeof value === 'string') {
  125. return true;
  126. }
  127. if (Object.prototype.toString.call(value) === '[object String]') {
  128. return true;
  129. }
  130. return false;
  131. }
  132. module.exports = fined;