index.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. 'use strict';
  2. function _child_process() {
  3. const data = require('child_process');
  4. _child_process = function _child_process() {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function _crypto() {
  10. const data = _interopRequireDefault(require('crypto'));
  11. _crypto = function _crypto() {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _events() {
  17. const data = _interopRequireDefault(require('events'));
  18. _events = function _events() {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function _os() {
  24. const data = _interopRequireDefault(require('os'));
  25. _os = function _os() {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function _path() {
  31. const data = _interopRequireDefault(require('path'));
  32. _path = function _path() {
  33. return data;
  34. };
  35. return data;
  36. }
  37. function _sane() {
  38. const data = _interopRequireDefault(require('sane'));
  39. _sane = function _sane() {
  40. return data;
  41. };
  42. return data;
  43. }
  44. function _invariant() {
  45. const data = _interopRequireDefault(require('invariant'));
  46. _invariant = function _invariant() {
  47. return data;
  48. };
  49. return data;
  50. }
  51. function _jestSerializer() {
  52. const data = _interopRequireDefault(require('jest-serializer'));
  53. _jestSerializer = function _jestSerializer() {
  54. return data;
  55. };
  56. return data;
  57. }
  58. function _jestWorker() {
  59. const data = _interopRequireDefault(require('jest-worker'));
  60. _jestWorker = function _jestWorker() {
  61. return data;
  62. };
  63. return data;
  64. }
  65. var _worker = require('./worker');
  66. var _getMockName = _interopRequireDefault(require('./getMockName'));
  67. var _getPlatformExtension = _interopRequireDefault(
  68. require('./lib/getPlatformExtension')
  69. );
  70. var _constants = _interopRequireDefault(require('./constants'));
  71. var _HasteFS = _interopRequireDefault(require('./HasteFS'));
  72. var _ModuleMap = _interopRequireDefault(require('./ModuleMap'));
  73. var _node = _interopRequireDefault(require('./crawlers/node'));
  74. var _normalizePathSep = _interopRequireDefault(
  75. require('./lib/normalizePathSep')
  76. );
  77. var _watchman = _interopRequireDefault(require('./crawlers/watchman'));
  78. var _WatchmanWatcher = _interopRequireDefault(require('./lib/WatchmanWatcher'));
  79. var _FSEventsWatcher = _interopRequireDefault(require('./lib/FSEventsWatcher'));
  80. var fastPath = _interopRequireWildcard(require('./lib/fast_path'));
  81. function _interopRequireWildcard(obj) {
  82. if (obj && obj.__esModule) {
  83. return obj;
  84. } else {
  85. var newObj = {};
  86. if (obj != null) {
  87. for (var key in obj) {
  88. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  89. var desc =
  90. Object.defineProperty && Object.getOwnPropertyDescriptor
  91. ? Object.getOwnPropertyDescriptor(obj, key)
  92. : {};
  93. if (desc.get || desc.set) {
  94. Object.defineProperty(newObj, key, desc);
  95. } else {
  96. newObj[key] = obj[key];
  97. }
  98. }
  99. }
  100. }
  101. newObj.default = obj;
  102. return newObj;
  103. }
  104. }
  105. function _interopRequireDefault(obj) {
  106. return obj && obj.__esModule ? obj : {default: obj};
  107. }
  108. function _slicedToArray(arr, i) {
  109. return (
  110. _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest()
  111. );
  112. }
  113. function _nonIterableRest() {
  114. throw new TypeError('Invalid attempt to destructure non-iterable instance');
  115. }
  116. function _iterableToArrayLimit(arr, i) {
  117. var _arr = [];
  118. var _n = true;
  119. var _d = false;
  120. var _e = undefined;
  121. try {
  122. for (
  123. var _i = arr[Symbol.iterator](), _s;
  124. !(_n = (_s = _i.next()).done);
  125. _n = true
  126. ) {
  127. _arr.push(_s.value);
  128. if (i && _arr.length === i) break;
  129. }
  130. } catch (err) {
  131. _d = true;
  132. _e = err;
  133. } finally {
  134. try {
  135. if (!_n && _i['return'] != null) _i['return']();
  136. } finally {
  137. if (_d) throw _e;
  138. }
  139. }
  140. return _arr;
  141. }
  142. function _arrayWithHoles(arr) {
  143. if (Array.isArray(arr)) return arr;
  144. }
  145. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  146. try {
  147. var info = gen[key](arg);
  148. var value = info.value;
  149. } catch (error) {
  150. reject(error);
  151. return;
  152. }
  153. if (info.done) {
  154. resolve(value);
  155. } else {
  156. Promise.resolve(value).then(_next, _throw);
  157. }
  158. }
  159. function _asyncToGenerator(fn) {
  160. return function() {
  161. var self = this,
  162. args = arguments;
  163. return new Promise(function(resolve, reject) {
  164. var gen = fn.apply(self, args);
  165. function _next(value) {
  166. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'next', value);
  167. }
  168. function _throw(err) {
  169. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'throw', err);
  170. }
  171. _next(undefined);
  172. });
  173. };
  174. }
  175. function _defineProperty(obj, key, value) {
  176. if (key in obj) {
  177. Object.defineProperty(obj, key, {
  178. value: value,
  179. enumerable: true,
  180. configurable: true,
  181. writable: true
  182. });
  183. } else {
  184. obj[key] = value;
  185. }
  186. return obj;
  187. }
  188. const CHANGE_INTERVAL = 30;
  189. const MAX_WAIT_TIME = 240000;
  190. const NODE_MODULES = _path().default.sep + 'node_modules' + _path().default.sep;
  191. const PACKAGE_JSON = _path().default.sep + 'package.json'; // TypeScript doesn't like us importing from outside `rootDir`, but it doesn't
  192. // understand `require`.
  193. const _require = require('../package.json'),
  194. VERSION = _require.version;
  195. const canUseWatchman = (() => {
  196. try {
  197. (0, _child_process().execSync)('watchman --version', {
  198. stdio: ['ignore']
  199. });
  200. return true;
  201. } catch (e) {}
  202. return false;
  203. })();
  204. const escapePathSeparator = string =>
  205. _path().default.sep === '\\' ? string.replace(/(\/|\\)/g, '\\\\') : string;
  206. const getWhiteList = list => {
  207. if (list && list.length) {
  208. const newList = list.map(item =>
  209. escapePathSeparator(item.replace(/(\/)/g, _path().default.sep))
  210. );
  211. return new RegExp(
  212. '(' +
  213. escapePathSeparator(NODE_MODULES) +
  214. '(?:' +
  215. newList.join('|') +
  216. ')(?=$|' +
  217. escapePathSeparator(_path().default.sep) +
  218. '))',
  219. 'g'
  220. );
  221. }
  222. return null;
  223. };
  224. /**
  225. * HasteMap is a JavaScript implementation of Facebook's haste module system.
  226. *
  227. * This implementation is inspired by https://github.com/facebook/node-haste
  228. * and was built with for high-performance in large code repositories with
  229. * hundreds of thousands of files. This implementation is scalable and provides
  230. * predictable performance.
  231. *
  232. * Because the haste map creation and synchronization is critical to startup
  233. * performance and most tasks are blocked by I/O this class makes heavy use of
  234. * synchronous operations. It uses worker processes for parallelizing file
  235. * access and metadata extraction.
  236. *
  237. * The data structures created by `jest-haste-map` can be used directly from the
  238. * cache without further processing. The metadata objects in the `files` and
  239. * `map` objects contain cross-references: a metadata object from one can look
  240. * up the corresponding metadata object in the other map. Note that in most
  241. * projects, the number of files will be greater than the number of haste
  242. * modules one module can refer to many files based on platform extensions.
  243. *
  244. * type HasteMap = {
  245. * clocks: WatchmanClocks,
  246. * files: {[filepath: string]: FileMetaData},
  247. * map: {[id: string]: ModuleMapItem},
  248. * mocks: {[id: string]: string},
  249. * }
  250. *
  251. * // Watchman clocks are used for query synchronization and file system deltas.
  252. * type WatchmanClocks = {[filepath: string]: string};
  253. *
  254. * type FileMetaData = {
  255. * id: ?string, // used to look up module metadata objects in `map`.
  256. * mtime: number, // check for outdated files.
  257. * size: number, // size of the file in bytes.
  258. * visited: boolean, // whether the file has been parsed or not.
  259. * dependencies: Array<string>, // all relative dependencies of this file.
  260. * sha1: ?string, // SHA-1 of the file, if requested via options.
  261. * };
  262. *
  263. * // Modules can be targeted to a specific platform based on the file name.
  264. * // Example: platform.ios.js and Platform.android.js will both map to the same
  265. * // `Platform` module. The platform should be specified during resolution.
  266. * type ModuleMapItem = {[platform: string]: ModuleMetaData};
  267. *
  268. * //
  269. * type ModuleMetaData = {
  270. * path: string, // the path to look up the file object in `files`.
  271. * type: string, // the module type (either `package` or `module`).
  272. * };
  273. *
  274. * Note that the data structures described above are conceptual only. The actual
  275. * implementation uses arrays and constant keys for metadata storage. Instead of
  276. * `{id: 'flatMap', mtime: 3421, size: 42, visited: true, dependencies: []}` the real
  277. * representation is similar to `['flatMap', 3421, 42, 1, []]` to save storage space
  278. * and reduce parse and write time of a big JSON blob.
  279. *
  280. * The HasteMap is created as follows:
  281. * 1. read data from the cache or create an empty structure.
  282. *
  283. * 2. crawl the file system.
  284. * * empty cache: crawl the entire file system.
  285. * * cache available:
  286. * * if watchman is available: get file system delta changes.
  287. * * if watchman is unavailable: crawl the entire file system.
  288. * * build metadata objects for every file. This builds the `files` part of
  289. * the `HasteMap`.
  290. *
  291. * 3. parse and extract metadata from changed files.
  292. * * this is done in parallel over worker processes to improve performance.
  293. * * the worst case is to parse all files.
  294. * * the best case is no file system access and retrieving all data from
  295. * the cache.
  296. * * the average case is a small number of changed files.
  297. *
  298. * 4. serialize the new `HasteMap` in a cache file.
  299. * Worker processes can directly access the cache through `HasteMap.read()`.
  300. *
  301. */
  302. /* eslint-disable-next-line no-redeclare */
  303. class HasteMap extends _events().default {
  304. constructor(options) {
  305. super();
  306. _defineProperty(this, '_buildPromise', void 0);
  307. _defineProperty(this, '_cachePath', void 0);
  308. _defineProperty(this, '_changeInterval', void 0);
  309. _defineProperty(this, '_console', void 0);
  310. _defineProperty(this, '_options', void 0);
  311. _defineProperty(this, '_watchers', void 0);
  312. _defineProperty(this, '_whitelist', void 0);
  313. _defineProperty(this, '_worker', void 0);
  314. this._options = {
  315. cacheDirectory: options.cacheDirectory || _os().default.tmpdir(),
  316. computeDependencies:
  317. options.computeDependencies === undefined
  318. ? true
  319. : options.computeDependencies,
  320. computeSha1: options.computeSha1 || false,
  321. dependencyExtractor: options.dependencyExtractor,
  322. extensions: options.extensions,
  323. forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI,
  324. hasteImplModulePath: options.hasteImplModulePath,
  325. ignorePattern: options.ignorePattern,
  326. mapper: options.mapper,
  327. maxWorkers: options.maxWorkers,
  328. mocksPattern: options.mocksPattern
  329. ? new RegExp(options.mocksPattern)
  330. : null,
  331. name: options.name,
  332. platforms: options.platforms,
  333. resetCache: options.resetCache,
  334. retainAllFiles: options.retainAllFiles,
  335. rootDir: options.rootDir,
  336. roots: Array.from(new Set(options.roots)),
  337. skipPackageJson: !!options.skipPackageJson,
  338. throwOnModuleCollision: !!options.throwOnModuleCollision,
  339. useWatchman: options.useWatchman == null ? true : options.useWatchman,
  340. watch: !!options.watch
  341. };
  342. this._console = options.console || global.console;
  343. if (options.ignorePattern && !(options.ignorePattern instanceof RegExp)) {
  344. this._console.warn(
  345. 'jest-haste-map: the `ignorePattern` options as a function is being ' +
  346. 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.'
  347. );
  348. }
  349. const rootDirHash = _crypto()
  350. .default.createHash('md5')
  351. .update(options.rootDir)
  352. .digest('hex');
  353. let hasteImplHash = '';
  354. let dependencyExtractorHash = '';
  355. if (options.hasteImplModulePath) {
  356. const hasteImpl = require(options.hasteImplModulePath);
  357. if (hasteImpl.getCacheKey) {
  358. hasteImplHash = String(hasteImpl.getCacheKey());
  359. }
  360. }
  361. if (options.dependencyExtractor) {
  362. const dependencyExtractor = require(options.dependencyExtractor);
  363. if (dependencyExtractor.getCacheKey) {
  364. dependencyExtractorHash = String(dependencyExtractor.getCacheKey());
  365. }
  366. }
  367. this._cachePath = HasteMap.getCacheFilePath(
  368. this._options.cacheDirectory,
  369. `haste-map-${this._options.name}-${rootDirHash}`,
  370. VERSION,
  371. this._options.name,
  372. this._options.roots
  373. .map(root => fastPath.relative(options.rootDir, root))
  374. .join(':'),
  375. this._options.extensions.join(':'),
  376. this._options.platforms.join(':'),
  377. this._options.computeSha1.toString(),
  378. options.mocksPattern || '',
  379. (options.ignorePattern || '').toString(),
  380. hasteImplHash,
  381. dependencyExtractorHash
  382. );
  383. this._whitelist = getWhiteList(options.providesModuleNodeModules);
  384. this._buildPromise = null;
  385. this._watchers = [];
  386. this._worker = null;
  387. }
  388. static getCacheFilePath(tmpdir, name, ...extra) {
  389. const hash = _crypto()
  390. .default.createHash('md5')
  391. .update(extra.join(''));
  392. return _path().default.join(
  393. tmpdir,
  394. name.replace(/\W/g, '-') + '-' + hash.digest('hex')
  395. );
  396. }
  397. getCacheFilePath() {
  398. return this._cachePath;
  399. }
  400. build() {
  401. var _this = this;
  402. if (!this._buildPromise) {
  403. this._buildPromise = _asyncToGenerator(function*() {
  404. const data = yield _this._buildFileMap(); // Persist when we don't know if files changed (changedFiles undefined)
  405. // or when we know a file was changed or deleted.
  406. let hasteMap;
  407. if (
  408. data.changedFiles === undefined ||
  409. data.changedFiles.size > 0 ||
  410. data.removedFiles.size > 0
  411. ) {
  412. hasteMap = yield _this._buildHasteMap(data);
  413. _this._persist(hasteMap);
  414. } else {
  415. hasteMap = data.hasteMap;
  416. }
  417. const rootDir = _this._options.rootDir;
  418. const hasteFS = new _HasteFS.default({
  419. files: hasteMap.files,
  420. rootDir
  421. });
  422. const moduleMap = new _ModuleMap.default({
  423. duplicates: hasteMap.duplicates,
  424. map: hasteMap.map,
  425. mocks: hasteMap.mocks,
  426. rootDir
  427. });
  428. const __hasteMapForTest =
  429. (process.env.NODE_ENV === 'test' && hasteMap) || null;
  430. yield _this._watch(hasteMap);
  431. return {
  432. __hasteMapForTest,
  433. hasteFS,
  434. moduleMap
  435. };
  436. })();
  437. }
  438. return this._buildPromise;
  439. }
  440. /**
  441. * 1. read data from the cache or create an empty structure.
  442. */
  443. read() {
  444. let hasteMap;
  445. try {
  446. hasteMap = _jestSerializer().default.readFileSync(this._cachePath);
  447. } catch (err) {
  448. hasteMap = this._createEmptyMap();
  449. }
  450. return hasteMap;
  451. }
  452. readModuleMap() {
  453. const data = this.read();
  454. return new _ModuleMap.default({
  455. duplicates: data.duplicates,
  456. map: data.map,
  457. mocks: data.mocks,
  458. rootDir: this._options.rootDir
  459. });
  460. }
  461. /**
  462. * 2. crawl the file system.
  463. */
  464. _buildFileMap() {
  465. var _this2 = this;
  466. return _asyncToGenerator(function*() {
  467. let hasteMap;
  468. try {
  469. const read = _this2._options.resetCache
  470. ? _this2._createEmptyMap
  471. : _this2.read;
  472. hasteMap = yield read.call(_this2);
  473. } catch (_unused) {
  474. hasteMap = _this2._createEmptyMap();
  475. }
  476. return _this2._crawl(hasteMap);
  477. })();
  478. }
  479. /**
  480. * 3. parse and extract metadata from changed files.
  481. */
  482. _processFile(hasteMap, map, mocks, filePath, workerOptions) {
  483. const rootDir = this._options.rootDir;
  484. const setModule = (id, module) => {
  485. let moduleMap = map.get(id);
  486. if (!moduleMap) {
  487. moduleMap = Object.create(null);
  488. map.set(id, moduleMap);
  489. }
  490. const platform =
  491. (0, _getPlatformExtension.default)(
  492. module[_constants.default.PATH],
  493. this._options.platforms
  494. ) || _constants.default.GENERIC_PLATFORM;
  495. const existingModule = moduleMap[platform];
  496. if (
  497. existingModule &&
  498. existingModule[_constants.default.PATH] !==
  499. module[_constants.default.PATH]
  500. ) {
  501. const method = this._options.throwOnModuleCollision ? 'error' : 'warn';
  502. this._console[method](
  503. [
  504. 'jest-haste-map: Haste module naming collision: ' + id,
  505. ' The following files share their name; please adjust your hasteImpl:',
  506. ' * <rootDir>' +
  507. _path().default.sep +
  508. existingModule[_constants.default.PATH],
  509. ' * <rootDir>' +
  510. _path().default.sep +
  511. module[_constants.default.PATH],
  512. ''
  513. ].join('\n')
  514. );
  515. if (this._options.throwOnModuleCollision) {
  516. throw new DuplicateError(
  517. existingModule[_constants.default.PATH],
  518. module[_constants.default.PATH]
  519. );
  520. } // We do NOT want consumers to use a module that is ambiguous.
  521. delete moduleMap[platform];
  522. if (Object.keys(moduleMap).length === 1) {
  523. map.delete(id);
  524. }
  525. let dupsByPlatform = hasteMap.duplicates.get(id);
  526. if (dupsByPlatform == null) {
  527. dupsByPlatform = new Map();
  528. hasteMap.duplicates.set(id, dupsByPlatform);
  529. }
  530. const dups = new Map([
  531. [module[_constants.default.PATH], module[_constants.default.TYPE]],
  532. [
  533. existingModule[_constants.default.PATH],
  534. existingModule[_constants.default.TYPE]
  535. ]
  536. ]);
  537. dupsByPlatform.set(platform, dups);
  538. return;
  539. }
  540. const dupsByPlatform = hasteMap.duplicates.get(id);
  541. if (dupsByPlatform != null) {
  542. const dups = dupsByPlatform.get(platform);
  543. if (dups != null) {
  544. dups.set(
  545. module[_constants.default.PATH],
  546. module[_constants.default.TYPE]
  547. );
  548. }
  549. return;
  550. }
  551. moduleMap[platform] = module;
  552. };
  553. const relativeFilePath = fastPath.relative(rootDir, filePath);
  554. const fileMetadata = hasteMap.files.get(relativeFilePath);
  555. if (!fileMetadata) {
  556. throw new Error(
  557. 'jest-haste-map: File to process was not found in the haste map.'
  558. );
  559. }
  560. const moduleMetadata = hasteMap.map.get(
  561. fileMetadata[_constants.default.ID]
  562. );
  563. const computeSha1 =
  564. this._options.computeSha1 && !fileMetadata[_constants.default.SHA1]; // Callback called when the response from the worker is successful.
  565. const workerReply = metadata => {
  566. // `1` for truthy values instead of `true` to save cache space.
  567. fileMetadata[_constants.default.VISITED] = 1;
  568. const metadataId = metadata.id;
  569. const metadataModule = metadata.module;
  570. if (metadataId && metadataModule) {
  571. fileMetadata[_constants.default.ID] = metadataId;
  572. setModule(metadataId, metadataModule);
  573. }
  574. fileMetadata[_constants.default.DEPENDENCIES] = metadata.dependencies
  575. ? metadata.dependencies.join(_constants.default.DEPENDENCY_DELIM)
  576. : '';
  577. if (computeSha1) {
  578. fileMetadata[_constants.default.SHA1] = metadata.sha1;
  579. }
  580. }; // Callback called when the response from the worker is an error.
  581. const workerError = error => {
  582. if (typeof error !== 'object' || !error.message || !error.stack) {
  583. error = new Error(error);
  584. error.stack = ''; // Remove stack for stack-less errors.
  585. } // @ts-ignore: checking error code is OK if error comes from "fs".
  586. if (!['ENOENT', 'EACCES'].includes(error.code)) {
  587. throw error;
  588. } // If a file cannot be read we remove it from the file list and
  589. // ignore the failure silently.
  590. hasteMap.files.delete(relativeFilePath);
  591. }; // If we retain all files in the virtual HasteFS representation, we avoid
  592. // reading them if they aren't important (node_modules).
  593. if (this._options.retainAllFiles && this._isNodeModulesDir(filePath)) {
  594. if (computeSha1) {
  595. return this._getWorker(workerOptions)
  596. .getSha1({
  597. computeDependencies: this._options.computeDependencies,
  598. computeSha1,
  599. dependencyExtractor: this._options.dependencyExtractor,
  600. filePath,
  601. hasteImplModulePath: this._options.hasteImplModulePath,
  602. rootDir
  603. })
  604. .then(workerReply, workerError);
  605. }
  606. return null;
  607. }
  608. if (
  609. this._options.mocksPattern &&
  610. this._options.mocksPattern.test(filePath)
  611. ) {
  612. const mockPath = (0, _getMockName.default)(filePath);
  613. const existingMockPath = mocks.get(mockPath);
  614. if (existingMockPath) {
  615. const secondMockPath = fastPath.relative(rootDir, filePath);
  616. if (existingMockPath !== secondMockPath) {
  617. const method = this._options.throwOnModuleCollision
  618. ? 'error'
  619. : 'warn';
  620. this._console[method](
  621. [
  622. 'jest-haste-map: duplicate manual mock found: ' + mockPath,
  623. ' The following files share their name; please delete one of them:',
  624. ' * <rootDir>' + _path().default.sep + existingMockPath,
  625. ' * <rootDir>' + _path().default.sep + secondMockPath,
  626. ''
  627. ].join('\n')
  628. );
  629. if (this._options.throwOnModuleCollision) {
  630. throw new DuplicateError(existingMockPath, secondMockPath);
  631. }
  632. }
  633. }
  634. mocks.set(mockPath, relativeFilePath);
  635. }
  636. if (fileMetadata[_constants.default.VISITED]) {
  637. if (!fileMetadata[_constants.default.ID]) {
  638. return null;
  639. }
  640. if (moduleMetadata != null) {
  641. const platform =
  642. (0, _getPlatformExtension.default)(
  643. filePath,
  644. this._options.platforms
  645. ) || _constants.default.GENERIC_PLATFORM;
  646. const module = moduleMetadata[platform];
  647. if (module == null) {
  648. return null;
  649. }
  650. const moduleId = fileMetadata[_constants.default.ID];
  651. let modulesByPlatform = map.get(moduleId);
  652. if (!modulesByPlatform) {
  653. modulesByPlatform = Object.create(null);
  654. map.set(moduleId, modulesByPlatform);
  655. }
  656. modulesByPlatform[platform] = module;
  657. return null;
  658. }
  659. }
  660. return this._getWorker(workerOptions)
  661. .worker({
  662. computeDependencies: this._options.computeDependencies,
  663. computeSha1,
  664. dependencyExtractor: this._options.dependencyExtractor,
  665. filePath,
  666. hasteImplModulePath: this._options.hasteImplModulePath,
  667. rootDir
  668. })
  669. .then(workerReply, workerError);
  670. }
  671. _buildHasteMap(data) {
  672. const removedFiles = data.removedFiles,
  673. changedFiles = data.changedFiles,
  674. hasteMap = data.hasteMap; // If any files were removed or we did not track what files changed, process
  675. // every file looking for changes. Otherwise, process only changed files.
  676. let map;
  677. let mocks;
  678. let filesToProcess;
  679. if (changedFiles === undefined || removedFiles.size) {
  680. map = new Map();
  681. mocks = new Map();
  682. filesToProcess = hasteMap.files;
  683. } else {
  684. map = hasteMap.map;
  685. mocks = hasteMap.mocks;
  686. filesToProcess = changedFiles;
  687. }
  688. var _iteratorNormalCompletion = true;
  689. var _didIteratorError = false;
  690. var _iteratorError = undefined;
  691. try {
  692. for (
  693. var _iterator = removedFiles[Symbol.iterator](), _step;
  694. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
  695. _iteratorNormalCompletion = true
  696. ) {
  697. const _step$value = _slicedToArray(_step.value, 2),
  698. relativeFilePath = _step$value[0],
  699. fileMetadata = _step$value[1];
  700. this._recoverDuplicates(
  701. hasteMap,
  702. relativeFilePath,
  703. fileMetadata[_constants.default.ID]
  704. );
  705. }
  706. } catch (err) {
  707. _didIteratorError = true;
  708. _iteratorError = err;
  709. } finally {
  710. try {
  711. if (!_iteratorNormalCompletion && _iterator.return != null) {
  712. _iterator.return();
  713. }
  714. } finally {
  715. if (_didIteratorError) {
  716. throw _iteratorError;
  717. }
  718. }
  719. }
  720. const promises = [];
  721. var _iteratorNormalCompletion2 = true;
  722. var _didIteratorError2 = false;
  723. var _iteratorError2 = undefined;
  724. try {
  725. for (
  726. var _iterator2 = filesToProcess.keys()[Symbol.iterator](), _step2;
  727. !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done);
  728. _iteratorNormalCompletion2 = true
  729. ) {
  730. const relativeFilePath = _step2.value;
  731. if (
  732. this._options.skipPackageJson &&
  733. relativeFilePath.endsWith(PACKAGE_JSON)
  734. ) {
  735. continue;
  736. } // SHA-1, if requested, should already be present thanks to the crawler.
  737. const filePath = fastPath.resolve(
  738. this._options.rootDir,
  739. relativeFilePath
  740. );
  741. const promise = this._processFile(hasteMap, map, mocks, filePath);
  742. if (promise) {
  743. promises.push(promise);
  744. }
  745. }
  746. } catch (err) {
  747. _didIteratorError2 = true;
  748. _iteratorError2 = err;
  749. } finally {
  750. try {
  751. if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
  752. _iterator2.return();
  753. }
  754. } finally {
  755. if (_didIteratorError2) {
  756. throw _iteratorError2;
  757. }
  758. }
  759. }
  760. return Promise.all(promises).then(
  761. () => {
  762. this._cleanup();
  763. hasteMap.map = map;
  764. hasteMap.mocks = mocks;
  765. return hasteMap;
  766. },
  767. error => {
  768. this._cleanup();
  769. throw error;
  770. }
  771. );
  772. }
  773. _cleanup() {
  774. const worker = this._worker; // @ts-ignore
  775. if (worker && typeof worker.end === 'function') {
  776. // @ts-ignore
  777. worker.end();
  778. }
  779. this._worker = null;
  780. }
  781. /**
  782. * 4. serialize the new `HasteMap` in a cache file.
  783. */
  784. _persist(hasteMap) {
  785. _jestSerializer().default.writeFileSync(this._cachePath, hasteMap);
  786. }
  787. /**
  788. * Creates workers or parses files and extracts metadata in-process.
  789. */
  790. _getWorker(options) {
  791. if (!this._worker) {
  792. if ((options && options.forceInBand) || this._options.maxWorkers <= 1) {
  793. this._worker = {
  794. getSha1: _worker.getSha1,
  795. worker: _worker.worker
  796. };
  797. } else {
  798. // @ts-ignore: assignment of a worker with custom properties.
  799. this._worker = new (_jestWorker()).default(
  800. require.resolve('./worker'),
  801. {
  802. exposedMethods: ['getSha1', 'worker'],
  803. maxRetries: 3,
  804. numWorkers: this._options.maxWorkers
  805. }
  806. );
  807. }
  808. }
  809. return this._worker;
  810. }
  811. _crawl(hasteMap) {
  812. const options = this._options;
  813. const ignore = this._ignore.bind(this);
  814. const crawl =
  815. canUseWatchman && this._options.useWatchman
  816. ? _watchman.default
  817. : _node.default;
  818. const crawlerOptions = {
  819. computeSha1: options.computeSha1,
  820. data: hasteMap,
  821. extensions: options.extensions,
  822. forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
  823. ignore,
  824. mapper: options.mapper,
  825. rootDir: options.rootDir,
  826. roots: options.roots
  827. };
  828. const retry = error => {
  829. if (crawl === _watchman.default) {
  830. this._console.warn(
  831. `jest-haste-map: Watchman crawl failed. Retrying once with node ` +
  832. `crawler.\n` +
  833. ` Usually this happens when watchman isn't running. Create an ` +
  834. `empty \`.watchmanconfig\` file in your project's root folder or ` +
  835. `initialize a git or hg repository in your project.\n` +
  836. ` ` +
  837. error
  838. );
  839. return (0, _node.default)(crawlerOptions).catch(e => {
  840. throw new Error(
  841. `Crawler retry failed:\n` +
  842. ` Original error: ${error.message}\n` +
  843. ` Retry error: ${e.message}\n`
  844. );
  845. });
  846. }
  847. throw error;
  848. };
  849. try {
  850. return crawl(crawlerOptions).catch(retry);
  851. } catch (error) {
  852. return retry(error);
  853. }
  854. }
  855. /**
  856. * Watch mode
  857. */
  858. _watch(hasteMap) {
  859. if (!this._options.watch) {
  860. return Promise.resolve();
  861. } // In watch mode, we'll only warn about module collisions and we'll retain
  862. // all files, even changes to node_modules.
  863. this._options.throwOnModuleCollision = false;
  864. this._options.retainAllFiles = true; // WatchmanWatcher > FSEventsWatcher > sane.NodeWatcher
  865. const Watcher =
  866. canUseWatchman && this._options.useWatchman
  867. ? _WatchmanWatcher.default
  868. : _FSEventsWatcher.default.isSupported()
  869. ? _FSEventsWatcher.default
  870. : _sane().default.NodeWatcher;
  871. const extensions = this._options.extensions;
  872. const ignorePattern = this._options.ignorePattern;
  873. const rootDir = this._options.rootDir;
  874. let changeQueue = Promise.resolve();
  875. let eventsQueue = []; // We only need to copy the entire haste map once on every "frame".
  876. let mustCopy = true;
  877. const createWatcher = root => {
  878. // @ts-ignore: TODO how? "Cannot use 'new' with an expression whose type lacks a call or construct signature."
  879. const watcher = new Watcher(root, {
  880. dot: false,
  881. glob: extensions.map(extension => '**/*.' + extension),
  882. ignored: ignorePattern
  883. });
  884. return new Promise((resolve, reject) => {
  885. const rejectTimeout = setTimeout(
  886. () => reject(new Error('Failed to start watch mode.')),
  887. MAX_WAIT_TIME
  888. );
  889. watcher.once('ready', () => {
  890. clearTimeout(rejectTimeout);
  891. watcher.on('all', onChange);
  892. resolve(watcher);
  893. });
  894. });
  895. };
  896. const emitChange = () => {
  897. if (eventsQueue.length) {
  898. mustCopy = true;
  899. const changeEvent = {
  900. eventsQueue,
  901. hasteFS: new _HasteFS.default({
  902. files: hasteMap.files,
  903. rootDir
  904. }),
  905. moduleMap: new _ModuleMap.default({
  906. duplicates: hasteMap.duplicates,
  907. map: hasteMap.map,
  908. mocks: hasteMap.mocks,
  909. rootDir
  910. })
  911. };
  912. this.emit('change', changeEvent);
  913. eventsQueue = [];
  914. }
  915. };
  916. const onChange = (type, filePath, root, stat) => {
  917. filePath = _path().default.join(
  918. root,
  919. (0, _normalizePathSep.default)(filePath)
  920. );
  921. if (
  922. (stat && stat.isDirectory()) ||
  923. this._ignore(filePath) ||
  924. !extensions.some(extension => filePath.endsWith(extension))
  925. ) {
  926. return;
  927. }
  928. changeQueue = changeQueue
  929. .then(() => {
  930. // If we get duplicate events for the same file, ignore them.
  931. if (
  932. eventsQueue.find(
  933. event =>
  934. event.type === type &&
  935. event.filePath === filePath &&
  936. ((!event.stat && !stat) ||
  937. (!!event.stat &&
  938. !!stat &&
  939. event.stat.mtime.getTime() === stat.mtime.getTime()))
  940. )
  941. ) {
  942. return null;
  943. }
  944. if (mustCopy) {
  945. mustCopy = false;
  946. hasteMap = {
  947. clocks: new Map(hasteMap.clocks),
  948. duplicates: new Map(hasteMap.duplicates),
  949. files: new Map(hasteMap.files),
  950. map: new Map(hasteMap.map),
  951. mocks: new Map(hasteMap.mocks)
  952. };
  953. }
  954. const add = () => {
  955. eventsQueue.push({
  956. filePath,
  957. stat,
  958. type
  959. });
  960. return null;
  961. };
  962. const relativeFilePath = fastPath.relative(rootDir, filePath);
  963. const fileMetadata = hasteMap.files.get(relativeFilePath); // If it's not an addition, delete the file and all its metadata
  964. if (fileMetadata != null) {
  965. const moduleName = fileMetadata[_constants.default.ID];
  966. const platform =
  967. (0, _getPlatformExtension.default)(
  968. filePath,
  969. this._options.platforms
  970. ) || _constants.default.GENERIC_PLATFORM;
  971. hasteMap.files.delete(relativeFilePath);
  972. let moduleMap = hasteMap.map.get(moduleName);
  973. if (moduleMap != null) {
  974. // We are forced to copy the object because jest-haste-map exposes
  975. // the map as an immutable entity.
  976. moduleMap = copy(moduleMap);
  977. delete moduleMap[platform];
  978. if (Object.keys(moduleMap).length === 0) {
  979. hasteMap.map.delete(moduleName);
  980. } else {
  981. hasteMap.map.set(moduleName, moduleMap);
  982. }
  983. }
  984. if (
  985. this._options.mocksPattern &&
  986. this._options.mocksPattern.test(filePath)
  987. ) {
  988. const mockName = (0, _getMockName.default)(filePath);
  989. hasteMap.mocks.delete(mockName);
  990. }
  991. this._recoverDuplicates(hasteMap, relativeFilePath, moduleName);
  992. } // If the file was added or changed,
  993. // parse it and update the haste map.
  994. if (type === 'add' || type === 'change') {
  995. (0, _invariant().default)(
  996. stat,
  997. 'since the file exists or changed, it should have stats'
  998. );
  999. const fileMetadata = [
  1000. '',
  1001. stat ? stat.mtime.getTime() : -1,
  1002. stat ? stat.size : 0,
  1003. 0,
  1004. '',
  1005. null
  1006. ];
  1007. hasteMap.files.set(relativeFilePath, fileMetadata);
  1008. const promise = this._processFile(
  1009. hasteMap,
  1010. hasteMap.map,
  1011. hasteMap.mocks,
  1012. filePath,
  1013. {
  1014. forceInBand: true
  1015. }
  1016. ); // Cleanup
  1017. this._cleanup();
  1018. if (promise) {
  1019. return promise.then(add);
  1020. } else {
  1021. // If a file in node_modules has changed,
  1022. // emit an event regardless.
  1023. add();
  1024. }
  1025. } else {
  1026. add();
  1027. }
  1028. return null;
  1029. })
  1030. .catch(error => {
  1031. this._console.error(
  1032. `jest-haste-map: watch error:\n ${error.stack}\n`
  1033. );
  1034. });
  1035. };
  1036. this._changeInterval = setInterval(emitChange, CHANGE_INTERVAL);
  1037. return Promise.all(this._options.roots.map(createWatcher)).then(
  1038. watchers => {
  1039. this._watchers = watchers;
  1040. }
  1041. );
  1042. }
  1043. /**
  1044. * This function should be called when the file under `filePath` is removed
  1045. * or changed. When that happens, we want to figure out if that file was
  1046. * part of a group of files that had the same ID. If it was, we want to
  1047. * remove it from the group. Furthermore, if there is only one file
  1048. * remaining in the group, then we want to restore that single file as the
  1049. * correct resolution for its ID, and cleanup the duplicates index.
  1050. */
  1051. _recoverDuplicates(hasteMap, relativeFilePath, moduleName) {
  1052. let dupsByPlatform = hasteMap.duplicates.get(moduleName);
  1053. if (dupsByPlatform == null) {
  1054. return;
  1055. }
  1056. const platform =
  1057. (0, _getPlatformExtension.default)(
  1058. relativeFilePath,
  1059. this._options.platforms
  1060. ) || _constants.default.GENERIC_PLATFORM;
  1061. let dups = dupsByPlatform.get(platform);
  1062. if (dups == null) {
  1063. return;
  1064. }
  1065. dupsByPlatform = copyMap(dupsByPlatform);
  1066. hasteMap.duplicates.set(moduleName, dupsByPlatform);
  1067. dups = copyMap(dups);
  1068. dupsByPlatform.set(platform, dups);
  1069. dups.delete(relativeFilePath);
  1070. if (dups.size !== 1) {
  1071. return;
  1072. }
  1073. const uniqueModule = dups.entries().next().value;
  1074. if (!uniqueModule) {
  1075. return;
  1076. }
  1077. let dedupMap = hasteMap.map.get(moduleName);
  1078. if (dedupMap == null) {
  1079. dedupMap = Object.create(null);
  1080. hasteMap.map.set(moduleName, dedupMap);
  1081. }
  1082. dedupMap[platform] = uniqueModule;
  1083. dupsByPlatform.delete(platform);
  1084. if (dupsByPlatform.size === 0) {
  1085. hasteMap.duplicates.delete(moduleName);
  1086. }
  1087. }
  1088. end() {
  1089. // @ts-ignore: TODO TS cannot decide if `setInterval` and `clearInterval` comes from NodeJS or the DOM
  1090. clearInterval(this._changeInterval);
  1091. if (!this._watchers.length) {
  1092. return Promise.resolve();
  1093. }
  1094. return Promise.all(
  1095. this._watchers.map(
  1096. watcher => new Promise(resolve => watcher.close(resolve))
  1097. )
  1098. ).then(() => {
  1099. this._watchers = [];
  1100. });
  1101. }
  1102. /**
  1103. * Helpers
  1104. */
  1105. _ignore(filePath) {
  1106. const ignorePattern = this._options.ignorePattern;
  1107. const ignoreMatched =
  1108. ignorePattern instanceof RegExp
  1109. ? ignorePattern.test(filePath)
  1110. : ignorePattern && ignorePattern(filePath);
  1111. return (
  1112. ignoreMatched ||
  1113. (!this._options.retainAllFiles && this._isNodeModulesDir(filePath))
  1114. );
  1115. }
  1116. _isNodeModulesDir(filePath) {
  1117. if (!filePath.includes(NODE_MODULES)) {
  1118. return false;
  1119. }
  1120. if (this._whitelist) {
  1121. const whitelist = this._whitelist;
  1122. const match = whitelist.exec(filePath);
  1123. const matchEndIndex = whitelist.lastIndex;
  1124. whitelist.lastIndex = 0;
  1125. if (!match) {
  1126. return true;
  1127. }
  1128. const filePathInPackage = filePath.substr(matchEndIndex);
  1129. return filePathInPackage.startsWith(NODE_MODULES);
  1130. }
  1131. return true;
  1132. }
  1133. _createEmptyMap() {
  1134. return {
  1135. clocks: new Map(),
  1136. duplicates: new Map(),
  1137. files: new Map(),
  1138. map: new Map(),
  1139. mocks: new Map()
  1140. };
  1141. }
  1142. }
  1143. _defineProperty(HasteMap, 'H', void 0);
  1144. _defineProperty(HasteMap, 'DuplicateError', void 0);
  1145. _defineProperty(HasteMap, 'ModuleMap', void 0);
  1146. class DuplicateError extends Error {
  1147. constructor(mockPath1, mockPath2) {
  1148. super('Duplicated files or mocks. Please check the console for more info');
  1149. _defineProperty(this, 'mockPath1', void 0);
  1150. _defineProperty(this, 'mockPath2', void 0);
  1151. this.mockPath1 = mockPath1;
  1152. this.mockPath2 = mockPath2;
  1153. }
  1154. }
  1155. function copy(object) {
  1156. return Object.assign(Object.create(null), object);
  1157. }
  1158. function copyMap(input) {
  1159. return new Map(input);
  1160. }
  1161. HasteMap.H = _constants.default;
  1162. HasteMap.DuplicateError = DuplicateError;
  1163. HasteMap.ModuleMap = _ModuleMap.default;
  1164. module.exports = HasteMap;