HasteFS.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(nodeInterop) {
  16. if (typeof WeakMap !== 'function') return null;
  17. var cacheBabelInterop = new WeakMap();
  18. var cacheNodeInterop = new WeakMap();
  19. return (_getRequireWildcardCache = function (nodeInterop) {
  20. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  21. })(nodeInterop);
  22. }
  23. function _interopRequireWildcard(obj, nodeInterop) {
  24. if (!nodeInterop && 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(nodeInterop);
  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 (key !== 'default' && 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. /**
  59. * Copyright (c) Meta Platforms, Inc. and affiliates.
  60. *
  61. * This source code is licensed under the MIT license found in the
  62. * LICENSE file in the root directory of this source tree.
  63. */
  64. class HasteFS {
  65. _rootDir;
  66. _files;
  67. constructor({rootDir, files}) {
  68. this._rootDir = rootDir;
  69. this._files = files;
  70. }
  71. getModuleName(file) {
  72. const fileMetadata = this._getFileData(file);
  73. return (fileMetadata && fileMetadata[_constants.default.ID]) || null;
  74. }
  75. getSize(file) {
  76. const fileMetadata = this._getFileData(file);
  77. return (fileMetadata && fileMetadata[_constants.default.SIZE]) || null;
  78. }
  79. getDependencies(file) {
  80. const fileMetadata = this._getFileData(file);
  81. if (fileMetadata) {
  82. return fileMetadata[_constants.default.DEPENDENCIES]
  83. ? fileMetadata[_constants.default.DEPENDENCIES].split(
  84. _constants.default.DEPENDENCY_DELIM
  85. )
  86. : [];
  87. } else {
  88. return null;
  89. }
  90. }
  91. getSha1(file) {
  92. const fileMetadata = this._getFileData(file);
  93. return (fileMetadata && fileMetadata[_constants.default.SHA1]) || null;
  94. }
  95. exists(file) {
  96. return this._getFileData(file) != null;
  97. }
  98. getAllFiles() {
  99. return Array.from(this.getAbsoluteFileIterator());
  100. }
  101. getFileIterator() {
  102. return this._files.keys();
  103. }
  104. *getAbsoluteFileIterator() {
  105. for (const file of this.getFileIterator()) {
  106. yield fastPath.resolve(this._rootDir, file);
  107. }
  108. }
  109. matchFiles(pattern) {
  110. if (!(pattern instanceof RegExp)) {
  111. pattern = new RegExp(pattern);
  112. }
  113. const files = [];
  114. for (const file of this.getAbsoluteFileIterator()) {
  115. if (pattern.test(file)) {
  116. files.push(file);
  117. }
  118. }
  119. return files;
  120. }
  121. matchFilesWithGlob(globs, root) {
  122. const files = new Set();
  123. const matcher = (0, _jestUtil().globsToMatcher)(globs);
  124. for (const file of this.getAbsoluteFileIterator()) {
  125. const filePath = root ? fastPath.relative(root, file) : file;
  126. if (matcher((0, _jestUtil().replacePathSepForGlob)(filePath))) {
  127. files.add(file);
  128. }
  129. }
  130. return files;
  131. }
  132. _getFileData(file) {
  133. const relativePath = fastPath.relative(this._rootDir, file);
  134. return this._files.get(relativePath);
  135. }
  136. }
  137. exports.default = HasteFS;