fileWalkers.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.clearFsCache = clearFsCache;
  6. exports.findClosestPackageJson = findClosestPackageJson;
  7. exports.isDirectory = isDirectory;
  8. exports.isFile = isFile;
  9. exports.readPackageCached = readPackageCached;
  10. exports.realpathSync = realpathSync;
  11. function _path() {
  12. const data = require('path');
  13. _path = function () {
  14. return data;
  15. };
  16. return data;
  17. }
  18. function fs() {
  19. const data = _interopRequireWildcard(require('graceful-fs'));
  20. fs = function () {
  21. return data;
  22. };
  23. return data;
  24. }
  25. function _jestUtil() {
  26. const data = require('jest-util');
  27. _jestUtil = function () {
  28. return data;
  29. };
  30. return data;
  31. }
  32. function _getRequireWildcardCache(nodeInterop) {
  33. if (typeof WeakMap !== 'function') return null;
  34. var cacheBabelInterop = new WeakMap();
  35. var cacheNodeInterop = new WeakMap();
  36. return (_getRequireWildcardCache = function (nodeInterop) {
  37. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  38. })(nodeInterop);
  39. }
  40. function _interopRequireWildcard(obj, nodeInterop) {
  41. if (!nodeInterop && obj && obj.__esModule) {
  42. return obj;
  43. }
  44. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  45. return {default: obj};
  46. }
  47. var cache = _getRequireWildcardCache(nodeInterop);
  48. if (cache && cache.has(obj)) {
  49. return cache.get(obj);
  50. }
  51. var newObj = {};
  52. var hasPropertyDescriptor =
  53. Object.defineProperty && Object.getOwnPropertyDescriptor;
  54. for (var key in obj) {
  55. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  56. var desc = hasPropertyDescriptor
  57. ? Object.getOwnPropertyDescriptor(obj, key)
  58. : null;
  59. if (desc && (desc.get || desc.set)) {
  60. Object.defineProperty(newObj, key, desc);
  61. } else {
  62. newObj[key] = obj[key];
  63. }
  64. }
  65. }
  66. newObj.default = obj;
  67. if (cache) {
  68. cache.set(obj, newObj);
  69. }
  70. return newObj;
  71. }
  72. /**
  73. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  74. *
  75. * This source code is licensed under the MIT license found in the
  76. * LICENSE file in the root directory of this source tree.
  77. */
  78. function clearFsCache() {
  79. checkedPaths.clear();
  80. checkedRealpathPaths.clear();
  81. packageContents.clear();
  82. }
  83. var IPathType;
  84. (function (IPathType) {
  85. IPathType[(IPathType['FILE'] = 1)] = 'FILE';
  86. IPathType[(IPathType['DIRECTORY'] = 2)] = 'DIRECTORY';
  87. IPathType[(IPathType['OTHER'] = 3)] = 'OTHER';
  88. })(IPathType || (IPathType = {}));
  89. const checkedPaths = new Map();
  90. function statSyncCached(path) {
  91. const result = checkedPaths.get(path);
  92. if (result != null) {
  93. return result;
  94. }
  95. let stat;
  96. try {
  97. // @ts-expect-error TS2554 - throwIfNoEntry is only available in recent version of node, but inclusion of the option is a backward compatible no-op.
  98. stat = fs().statSync(path, {
  99. throwIfNoEntry: false
  100. });
  101. } catch (e) {
  102. if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {
  103. throw e;
  104. }
  105. }
  106. if (stat) {
  107. if (stat.isFile() || stat.isFIFO()) {
  108. checkedPaths.set(path, IPathType.FILE);
  109. return IPathType.FILE;
  110. } else if (stat.isDirectory()) {
  111. checkedPaths.set(path, IPathType.DIRECTORY);
  112. return IPathType.DIRECTORY;
  113. }
  114. }
  115. checkedPaths.set(path, IPathType.OTHER);
  116. return IPathType.OTHER;
  117. }
  118. const checkedRealpathPaths = new Map();
  119. function realpathCached(path) {
  120. let result = checkedRealpathPaths.get(path);
  121. if (result != null) {
  122. return result;
  123. }
  124. result = (0, _jestUtil().tryRealpath)(path);
  125. checkedRealpathPaths.set(path, result);
  126. if (path !== result) {
  127. // also cache the result in case it's ever referenced directly - no reason to `realpath` that as well
  128. checkedRealpathPaths.set(result, result);
  129. }
  130. return result;
  131. }
  132. const packageContents = new Map();
  133. function readPackageCached(path) {
  134. let result = packageContents.get(path);
  135. if (result != null) {
  136. return result;
  137. }
  138. result = JSON.parse(fs().readFileSync(path, 'utf8'));
  139. packageContents.set(path, result);
  140. return result;
  141. } // adapted from
  142. // https://github.com/lukeed/escalade/blob/2477005062cdbd8407afc90d3f48f4930354252b/src/sync.js
  143. // to use cached `fs` calls
  144. function findClosestPackageJson(start) {
  145. let dir = (0, _path().resolve)('.', start);
  146. if (!isDirectory(dir)) {
  147. dir = (0, _path().dirname)(dir);
  148. }
  149. while (true) {
  150. const pkgJsonFile = (0, _path().resolve)(dir, './package.json');
  151. const hasPackageJson = isFile(pkgJsonFile);
  152. if (hasPackageJson) {
  153. return pkgJsonFile;
  154. }
  155. const prevDir = dir;
  156. dir = (0, _path().dirname)(dir);
  157. if (prevDir === dir) {
  158. return undefined;
  159. }
  160. }
  161. }
  162. /*
  163. * helper functions
  164. */
  165. function isFile(file) {
  166. return statSyncCached(file) === IPathType.FILE;
  167. }
  168. function isDirectory(dir) {
  169. return statSyncCached(dir) === IPathType.DIRECTORY;
  170. }
  171. function realpathSync(file) {
  172. return realpathCached(file);
  173. }