HasteFS.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _jestUtil() {
  7. const data = require('jest-util');
  8. _jestUtil = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. var _constants = _interopRequireDefault(require('./constants'));
  14. var fastPath = _interopRequireWildcard(require('./lib/fast_path'));
  15. function _getRequireWildcardCache() {
  16. if (typeof WeakMap !== 'function') return null;
  17. var cache = new WeakMap();
  18. _getRequireWildcardCache = function () {
  19. return cache;
  20. };
  21. return cache;
  22. }
  23. function _interopRequireWildcard(obj) {
  24. if (obj && obj.__esModule) {
  25. return obj;
  26. }
  27. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  28. return {default: obj};
  29. }
  30. var cache = _getRequireWildcardCache();
  31. if (cache && cache.has(obj)) {
  32. return cache.get(obj);
  33. }
  34. var newObj = {};
  35. var hasPropertyDescriptor =
  36. Object.defineProperty && Object.getOwnPropertyDescriptor;
  37. for (var key in obj) {
  38. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  39. var desc = hasPropertyDescriptor
  40. ? Object.getOwnPropertyDescriptor(obj, key)
  41. : null;
  42. if (desc && (desc.get || desc.set)) {
  43. Object.defineProperty(newObj, key, desc);
  44. } else {
  45. newObj[key] = obj[key];
  46. }
  47. }
  48. }
  49. newObj.default = obj;
  50. if (cache) {
  51. cache.set(obj, newObj);
  52. }
  53. return newObj;
  54. }
  55. function _interopRequireDefault(obj) {
  56. return obj && obj.__esModule ? obj : {default: obj};
  57. }
  58. function _defineProperty(obj, key, value) {
  59. if (key in obj) {
  60. Object.defineProperty(obj, key, {
  61. value: value,
  62. enumerable: true,
  63. configurable: true,
  64. writable: true
  65. });
  66. } else {
  67. obj[key] = value;
  68. }
  69. return obj;
  70. }
  71. class HasteFS {
  72. constructor({rootDir, files}) {
  73. _defineProperty(this, '_rootDir', void 0);
  74. _defineProperty(this, '_files', void 0);
  75. this._rootDir = rootDir;
  76. this._files = files;
  77. }
  78. getModuleName(file) {
  79. const fileMetadata = this._getFileData(file);
  80. return (fileMetadata && fileMetadata[_constants.default.ID]) || null;
  81. }
  82. getSize(file) {
  83. const fileMetadata = this._getFileData(file);
  84. return (fileMetadata && fileMetadata[_constants.default.SIZE]) || null;
  85. }
  86. getDependencies(file) {
  87. const fileMetadata = this._getFileData(file);
  88. if (fileMetadata) {
  89. return fileMetadata[_constants.default.DEPENDENCIES]
  90. ? fileMetadata[_constants.default.DEPENDENCIES].split(
  91. _constants.default.DEPENDENCY_DELIM
  92. )
  93. : [];
  94. } else {
  95. return null;
  96. }
  97. }
  98. getSha1(file) {
  99. const fileMetadata = this._getFileData(file);
  100. return (fileMetadata && fileMetadata[_constants.default.SHA1]) || null;
  101. }
  102. exists(file) {
  103. return this._getFileData(file) != null;
  104. }
  105. getAllFiles() {
  106. return Array.from(this.getAbsoluteFileIterator());
  107. }
  108. getFileIterator() {
  109. return this._files.keys();
  110. }
  111. *getAbsoluteFileIterator() {
  112. for (const file of this.getFileIterator()) {
  113. yield fastPath.resolve(this._rootDir, file);
  114. }
  115. }
  116. matchFiles(pattern) {
  117. if (!(pattern instanceof RegExp)) {
  118. pattern = new RegExp(pattern);
  119. }
  120. const files = [];
  121. for (const file of this.getAbsoluteFileIterator()) {
  122. if (pattern.test(file)) {
  123. files.push(file);
  124. }
  125. }
  126. return files;
  127. }
  128. matchFilesWithGlob(globs, root) {
  129. const files = new Set();
  130. const matcher = (0, _jestUtil().globsToMatcher)(globs);
  131. for (const file of this.getAbsoluteFileIterator()) {
  132. const filePath = root ? fastPath.relative(root, file) : file;
  133. if (matcher((0, _jestUtil().replacePathSepForGlob)(filePath))) {
  134. files.add(file);
  135. }
  136. }
  137. return files;
  138. }
  139. _getFileData(file) {
  140. const relativePath = fastPath.relative(this._rootDir, file);
  141. return this._files.get(relativePath);
  142. }
  143. }
  144. exports.default = HasteFS;