index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. if (parsed.ext) {
  55. filePath = filePath.slice(0, -parsed.ext.length);
  56. // This ensures that only the original extension is matched.
  57. extArr = [parsed.ext];
  58. }
  59. return {
  60. path: filePath,
  61. basedir: basedir,
  62. findUp: findUp,
  63. extArr: extArr,
  64. extMap: extMap,
  65. };
  66. }
  67. function findWithExpandedPath(expanded) {
  68. var found = expanded.findUp ?
  69. findUpFile(expanded.basedir, expanded.path, expanded.extArr) :
  70. findFile(expanded.basedir, expanded.path, expanded.extArr);
  71. if (!found) {
  72. return null;
  73. }
  74. if (expanded.extMap) {
  75. found.extension = pick(expanded.extMap, found.extension);
  76. }
  77. return found;
  78. }
  79. function findFile(basedir, relpath, extArr) {
  80. var noExtPath = path.resolve(basedir, relpath);
  81. for (var i = 0, n = extArr.length; i < n; i++) {
  82. var filepath = noExtPath + extArr[i];
  83. try {
  84. fs.statSync(filepath);
  85. return { path: filepath, extension: extArr[i] };
  86. } catch (e) {
  87. // Ignore error
  88. }
  89. }
  90. return null;
  91. }
  92. function findUpFile(basedir, filepath, extArr) {
  93. var lastdir;
  94. do {
  95. var found = findFile(basedir, filepath, extArr);
  96. if (found) {
  97. return found;
  98. }
  99. lastdir = basedir;
  100. basedir = path.dirname(basedir);
  101. } while (lastdir !== basedir);
  102. return null;
  103. }
  104. function createExtensionArray(exts) {
  105. if (isString(exts)) {
  106. return [exts];
  107. }
  108. if (Array.isArray(exts)) {
  109. exts = exts.filter(isString);
  110. return (exts.length > 0) ? exts : [''];
  111. }
  112. if (isPlainObject(exts)) {
  113. exts = Object.keys(exts);
  114. return (exts.length > 0) ? exts : [''];
  115. }
  116. return [''];
  117. }
  118. function createExtensionMap(exts) {
  119. if (!isPlainObject(exts)) {
  120. return null;
  121. }
  122. if (isEmpty(exts)) {
  123. return { '': null };
  124. }
  125. return exts;
  126. }
  127. function isEmpty(object) {
  128. return !Object.keys(object).length;
  129. }
  130. function isString(value) {
  131. if (typeof value === 'string') {
  132. return true;
  133. }
  134. if (Object.prototype.toString.call(value) === '[object String]') {
  135. return true;
  136. }
  137. return false;
  138. }
  139. module.exports = fined;