index.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. 'use strict';
  2. var _child_process;
  3. function _load_child_process() {
  4. return (_child_process = require('child_process'));
  5. }
  6. var _package;
  7. function _load_package() {
  8. return (_package = require('../package.json'));
  9. }
  10. var _worker;
  11. function _load_worker() {
  12. return (_worker = require('./worker'));
  13. }
  14. var _crypto;
  15. function _load_crypto() {
  16. return (_crypto = _interopRequireDefault(require('crypto')));
  17. }
  18. var _events;
  19. function _load_events() {
  20. return (_events = _interopRequireDefault(require('events')));
  21. }
  22. var _fs;
  23. function _load_fs() {
  24. return (_fs = _interopRequireDefault(require('fs')));
  25. }
  26. var _get_mock_name;
  27. function _load_get_mock_name() {
  28. return (_get_mock_name = _interopRequireDefault(require('./get_mock_name')));
  29. }
  30. var _get_platform_extension;
  31. function _load_get_platform_extension() {
  32. return (_get_platform_extension = _interopRequireDefault(
  33. require('./lib/get_platform_extension')
  34. ));
  35. }
  36. var _constants;
  37. function _load_constants() {
  38. return (_constants = _interopRequireDefault(require('./constants')));
  39. }
  40. var _haste_fs;
  41. function _load_haste_fs() {
  42. return (_haste_fs = _interopRequireDefault(require('./haste_fs')));
  43. }
  44. var _module_map;
  45. function _load_module_map() {
  46. return (_module_map = _interopRequireDefault(require('./module_map')));
  47. }
  48. var _invariant;
  49. function _load_invariant() {
  50. return (_invariant = _interopRequireDefault(require('invariant')));
  51. }
  52. var _node;
  53. function _load_node() {
  54. return (_node = _interopRequireDefault(require('./crawlers/node')));
  55. }
  56. var _normalize_path_sep;
  57. function _load_normalize_path_sep() {
  58. return (_normalize_path_sep = _interopRequireDefault(
  59. require('./lib/normalize_path_sep')
  60. ));
  61. }
  62. var _os;
  63. function _load_os() {
  64. return (_os = _interopRequireDefault(require('os')));
  65. }
  66. var _path;
  67. function _load_path() {
  68. return (_path = _interopRequireDefault(require('path')));
  69. }
  70. var _sane;
  71. function _load_sane() {
  72. return (_sane = _interopRequireDefault(require('sane')));
  73. }
  74. var _jestSerializer;
  75. function _load_jestSerializer() {
  76. return (_jestSerializer = _interopRequireDefault(require('jest-serializer')));
  77. }
  78. var _watchman;
  79. function _load_watchman() {
  80. return (_watchman = _interopRequireDefault(require('./crawlers/watchman')));
  81. }
  82. var _watchman_watcher;
  83. function _load_watchman_watcher() {
  84. return (_watchman_watcher = _interopRequireDefault(
  85. require('./lib/watchman_watcher')
  86. ));
  87. }
  88. var _jestWorker;
  89. function _load_jestWorker() {
  90. return (_jestWorker = _interopRequireDefault(require('jest-worker')));
  91. }
  92. function _interopRequireDefault(obj) {
  93. return obj && obj.__esModule ? obj : {default: obj};
  94. }
  95. const CHANGE_INTERVAL = 30;
  96. // eslint-disable-next-line import/default
  97. // eslint-disable-next-line import/default
  98. /**
  99. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  100. *
  101. * This source code is licensed under the MIT license found in the
  102. * LICENSE file in the root directory of this source tree.
  103. *
  104. *
  105. */
  106. const MAX_WAIT_TIME = 240000;
  107. const NODE_MODULES =
  108. (_path || _load_path()).default.sep +
  109. 'node_modules' +
  110. (_path || _load_path()).default.sep;
  111. const canUseWatchman = (() => {
  112. try {
  113. (0, (_child_process || _load_child_process()).execSync)(
  114. 'watchman --version',
  115. {stdio: ['ignore']}
  116. );
  117. return true;
  118. } catch (e) {}
  119. return false;
  120. })();
  121. const escapePathSeparator = string =>
  122. (_path || _load_path()).default.sep === '\\'
  123. ? string.replace(/(\/|\\)/g, '\\\\')
  124. : string;
  125. const getWhiteList = list => {
  126. if (list && list.length) {
  127. return new RegExp(
  128. '(' +
  129. escapePathSeparator(NODE_MODULES) +
  130. '(?:' +
  131. list.join('|') +
  132. ')(?=$|' +
  133. escapePathSeparator((_path || _load_path()).default.sep) +
  134. '))',
  135. 'g'
  136. );
  137. }
  138. return null;
  139. };
  140. /**
  141. * HasteMap is a JavaScript implementation of Facebook's haste module system.
  142. *
  143. * This implementation is inspired by https://github.com/facebook/node-haste
  144. * and was built with for high-performance in large code repositories with
  145. * hundreds of thousands of files. This implementation is scalable and provides
  146. * predictable performance.
  147. *
  148. * Because the haste map creation and synchronization is critical to startup
  149. * performance and most tasks are blocked by I/O this class makes heavy use of
  150. * synchronous operations. It uses worker processes for parallelizing file
  151. * access and metadata extraction.
  152. *
  153. * The data structures created by `jest-haste-map` can be used directly from the
  154. * cache without further processing. The metadata objects in the `files` and
  155. * `map` objects contain cross-references: a metadata object from one can look
  156. * up the corresponding metadata object in the other map. Note that in most
  157. * projects, the number of files will be greater than the number of haste
  158. * modules one module can refer to many files based on platform extensions.
  159. *
  160. * type HasteMap = {
  161. * clocks: WatchmanClocks,
  162. * files: {[filepath: string]: FileMetaData},
  163. * map: {[id: string]: ModuleMapItem},
  164. * mocks: {[id: string]: string},
  165. * }
  166. *
  167. * // Watchman clocks are used for query synchronization and file system deltas.
  168. * type WatchmanClocks = {[filepath: string]: string};
  169. *
  170. * type FileMetaData = {
  171. * id: ?string, // used to look up module metadata objects in `map`.
  172. * mtime: number, // check for outdated files.
  173. * visited: boolean, // whether the file has been parsed or not.
  174. * dependencies: Array<string>, // all relative dependencies of this file.
  175. * sha1: ?string, // SHA-1 of the file, if requested via options.
  176. * };
  177. *
  178. * // Modules can be targeted to a specific platform based on the file name.
  179. * // Example: platform.ios.js and Platform.android.js will both map to the same
  180. * // `Platform` module. The platform should be specified during resolution.
  181. * type ModuleMapItem = {[platform: string]: ModuleMetaData};
  182. *
  183. * //
  184. * type ModuleMetaData = {
  185. * path: string, // the path to look up the file object in `files`.
  186. * type: string, // the module type (either `package` or `module`).
  187. * };
  188. *
  189. * Note that the data structures described above are conceptual only. The actual
  190. * implementation uses arrays and constant keys for metadata storage. Instead of
  191. * `{id: 'flatMap', mtime: 3421, visited: true, dependencies: []}` the real
  192. * representation is similar to `['flatMap', 3421, 1, []]` to save storage space
  193. * and reduce parse and write time of a big JSON blob.
  194. *
  195. * The HasteMap is created as follows:
  196. * 1. read data from the cache or create an empty structure.
  197. *
  198. * 2. crawl the file system.
  199. * * empty cache: crawl the entire file system.
  200. * * cache available:
  201. * * if watchman is available: get file system delta changes.
  202. * * if watchman is unavailable: crawl the entire file system.
  203. * * build metadata objects for every file. This builds the `files` part of
  204. * the `HasteMap`.
  205. *
  206. * 3. parse and extract metadata from changed files.
  207. * * this is done in parallel over worker processes to improve performance.
  208. * * the worst case is to parse all files.
  209. * * the best case is no file system access and retrieving all data from
  210. * the cache.
  211. * * the average case is a small number of changed files.
  212. *
  213. * 4. serialize the new `HasteMap` in a cache file.
  214. * Worker processes can directly access the cache through `HasteMap.read()`.
  215. *
  216. */
  217. class HasteMap extends (_events || _load_events()).default {
  218. constructor(options) {
  219. super();
  220. this._options = {
  221. cacheDirectory:
  222. options.cacheDirectory || (_os || _load_os()).default.tmpdir(),
  223. computeDependencies:
  224. options.computeDependencies === undefined
  225. ? true
  226. : options.computeDependencies,
  227. computeSha1: options.computeSha1 || false,
  228. extensions: options.extensions,
  229. forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI,
  230. hasteImplModulePath: options.hasteImplModulePath,
  231. ignorePattern: options.ignorePattern,
  232. maxWorkers: options.maxWorkers,
  233. mocksPattern: options.mocksPattern
  234. ? new RegExp(options.mocksPattern)
  235. : null,
  236. name: options.name,
  237. platforms: options.platforms,
  238. resetCache: options.resetCache,
  239. retainAllFiles: options.retainAllFiles,
  240. roots: Array.from(new Set(options.roots)),
  241. throwOnModuleCollision: !!options.throwOnModuleCollision,
  242. useWatchman: options.useWatchman == null ? true : options.useWatchman,
  243. watch: !!options.watch
  244. };
  245. this._console = options.console || global.console;
  246. if (!(options.ignorePattern instanceof RegExp)) {
  247. this._console.warn(
  248. 'jest-haste-map: the `ignorePattern` options as a function is being ' +
  249. 'deprecated. Provide a RegExp instead. See https://github.com/facebook/jest/pull/4063.'
  250. );
  251. }
  252. this._cachePath = HasteMap.getCacheFilePath(
  253. this._options.cacheDirectory,
  254. `haste-map-${this._options.name}`,
  255. (_package || _load_package()).version,
  256. this._options.roots.join(':'),
  257. this._options.extensions.join(':'),
  258. this._options.platforms.join(':'),
  259. this._options.computeSha1.toString(),
  260. options.mocksPattern || '',
  261. options.ignorePattern.toString()
  262. );
  263. this._whitelist = getWhiteList(options.providesModuleNodeModules);
  264. this._buildPromise = null;
  265. this._watchers = [];
  266. this._worker = null;
  267. }
  268. static getCacheFilePath(tmpdir, name) {
  269. for (
  270. var _len = arguments.length,
  271. extra = Array(_len > 2 ? _len - 2 : 0),
  272. _key = 2;
  273. _key < _len;
  274. _key++
  275. ) {
  276. extra[_key - 2] = arguments[_key];
  277. }
  278. const hash = (_crypto || _load_crypto()).default
  279. .createHash('md5')
  280. .update(name + extra.join(''));
  281. return (_path || _load_path()).default.join(
  282. tmpdir,
  283. name.replace(/\W/g, '-') + '-' + hash.digest('hex')
  284. );
  285. }
  286. build() {
  287. if (!this._buildPromise) {
  288. this._buildPromise = this._buildFileMap()
  289. .then(data => this._buildHasteMap(data))
  290. .then(hasteMap => {
  291. this._persist(hasteMap);
  292. const hasteFS = new (_haste_fs || _load_haste_fs()).default(
  293. hasteMap.files
  294. );
  295. const moduleMap = new (_module_map || _load_module_map()).default({
  296. duplicates: hasteMap.duplicates,
  297. map: hasteMap.map,
  298. mocks: hasteMap.mocks
  299. });
  300. const __hasteMapForTest =
  301. (process.env.NODE_ENV === 'test' && hasteMap) || null;
  302. return this._watch(hasteMap, hasteFS, moduleMap).then(() => ({
  303. __hasteMapForTest: __hasteMapForTest,
  304. hasteFS: hasteFS,
  305. moduleMap: moduleMap
  306. }));
  307. });
  308. }
  309. return this._buildPromise;
  310. }
  311. /**
  312. * 1. read data from the cache or create an empty structure.
  313. */
  314. read() {
  315. let hasteMap;
  316. try {
  317. hasteMap = (
  318. _jestSerializer || _load_jestSerializer()
  319. ).default.readFileSync(this._cachePath);
  320. } catch (err) {
  321. hasteMap = this._createEmptyMap();
  322. }
  323. for (const key in hasteMap) {
  324. Object.setPrototypeOf(hasteMap[key], null);
  325. }
  326. return hasteMap;
  327. }
  328. readModuleMap() {
  329. const data = this.read();
  330. return new (_module_map || _load_module_map()).default({
  331. duplicates: data.duplicates,
  332. map: data.map,
  333. mocks: data.mocks
  334. });
  335. }
  336. /**
  337. * 2. crawl the file system.
  338. */
  339. _buildFileMap() {
  340. const read = this._options.resetCache ? this._createEmptyMap : this.read;
  341. return Promise.resolve()
  342. .then(() => read.call(this))
  343. .catch(() => this._createEmptyMap())
  344. .then(cachedHasteMap => {
  345. const cachedFiles = Object.keys(cachedHasteMap.files).map(filePath => {
  346. const moduleName =
  347. cachedHasteMap.files[filePath][
  348. (_constants || _load_constants()).default.ID
  349. ];
  350. return {moduleName: moduleName, path: filePath};
  351. });
  352. return this._crawl(cachedHasteMap).then(hasteMap => {
  353. const deprecatedFiles = cachedFiles.filter(file => {
  354. const fileData = hasteMap.files[file.path];
  355. return (
  356. fileData == null ||
  357. file.moduleName !==
  358. fileData[(_constants || _load_constants()).default.ID]
  359. );
  360. });
  361. return {deprecatedFiles: deprecatedFiles, hasteMap: hasteMap};
  362. });
  363. });
  364. }
  365. /**
  366. * 3. parse and extract metadata from changed files.
  367. */
  368. _processFile(hasteMap, map, mocks, filePath, workerOptions) {
  369. const setModule = (id, module) => {
  370. if (!map[id]) {
  371. // $FlowFixMe
  372. map[id] = Object.create(null);
  373. }
  374. const moduleMap = map[id];
  375. const platform =
  376. (0,
  377. (_get_platform_extension || _load_get_platform_extension()).default)(
  378. module[(_constants || _load_constants()).default.PATH],
  379. this._options.platforms
  380. ) || (_constants || _load_constants()).default.GENERIC_PLATFORM;
  381. const existingModule = moduleMap[platform];
  382. if (
  383. existingModule &&
  384. existingModule[(_constants || _load_constants()).default.PATH] !==
  385. module[(_constants || _load_constants()).default.PATH]
  386. ) {
  387. const message =
  388. `jest-haste-map: @providesModule naming collision:\n` +
  389. ` Duplicate module name: ${id}\n` +
  390. ` Paths: ${
  391. module[(_constants || _load_constants()).default.PATH]
  392. } collides with ` +
  393. `${
  394. existingModule[(_constants || _load_constants()).default.PATH]
  395. }\n\nThis ` +
  396. `${this._options.throwOnModuleCollision ? 'error' : 'warning'} ` +
  397. `is caused by a @providesModule declaration ` +
  398. `with the same name across two different files.`;
  399. if (this._options.throwOnModuleCollision) {
  400. throw new Error(message);
  401. }
  402. this._console.warn(message);
  403. // We do NOT want consumers to use a module that is ambiguous.
  404. delete moduleMap[platform];
  405. if (Object.keys(moduleMap).length === 1) {
  406. delete map[id];
  407. }
  408. let dupsByPlatform = hasteMap.duplicates[id];
  409. if (dupsByPlatform == null) {
  410. dupsByPlatform = hasteMap.duplicates[id] = Object.create(null);
  411. }
  412. const dups = (dupsByPlatform[platform] = Object.create(null));
  413. dups[module[(_constants || _load_constants()).default.PATH]] =
  414. module[(_constants || _load_constants()).default.TYPE];
  415. dups[existingModule[(_constants || _load_constants()).default.PATH]] =
  416. existingModule[(_constants || _load_constants()).default.TYPE];
  417. return;
  418. }
  419. const dupsByPlatform = hasteMap.duplicates[id];
  420. if (dupsByPlatform != null) {
  421. const dups = dupsByPlatform[platform];
  422. if (dups != null) {
  423. dups[module[(_constants || _load_constants()).default.PATH]] =
  424. module[(_constants || _load_constants()).default.TYPE];
  425. }
  426. return;
  427. }
  428. moduleMap[platform] = module;
  429. };
  430. const fileMetadata = hasteMap.files[filePath];
  431. const moduleMetadata =
  432. hasteMap.map[fileMetadata[(_constants || _load_constants()).default.ID]];
  433. const computeSha1 =
  434. this._options.computeSha1 &&
  435. !fileMetadata[(_constants || _load_constants()).default.SHA1];
  436. // Callback called when the response from the worker is successful.
  437. const workerReply = metadata => {
  438. // `1` for truthy values instead of `true` to save cache space.
  439. fileMetadata[(_constants || _load_constants()).default.VISITED] = 1;
  440. const metadataId = metadata.id;
  441. const metadataModule = metadata.module;
  442. if (metadataId && metadataModule) {
  443. fileMetadata[(_constants || _load_constants()).default.ID] = metadataId;
  444. setModule(metadataId, metadataModule);
  445. }
  446. fileMetadata[(_constants || _load_constants()).default.DEPENDENCIES] =
  447. metadata.dependencies || [];
  448. if (computeSha1) {
  449. fileMetadata[(_constants || _load_constants()).default.SHA1] =
  450. metadata.sha1;
  451. }
  452. };
  453. // Callback called when the response from the worker is an error.
  454. const workerError = error => {
  455. if (typeof error !== 'object' || !error.message || !error.stack) {
  456. error = new Error(error);
  457. error.stack = ''; // Remove stack for stack-less errors.
  458. }
  459. // $FlowFixMe: checking error code is OK if error comes from "fs".
  460. if (!['ENOENT', 'EACCES'].includes(error.code)) {
  461. throw error;
  462. }
  463. // If a file cannot be read we remove it from the file list and
  464. // ignore the failure silently.
  465. delete hasteMap.files[filePath];
  466. };
  467. // If we retain all files in the virtual HasteFS representation, we avoid
  468. // reading them if they aren't important (node_modules).
  469. if (this._options.retainAllFiles && this._isNodeModulesDir(filePath)) {
  470. if (computeSha1) {
  471. return this._getWorker(workerOptions)
  472. .getSha1({
  473. computeDependencies: this._options.computeDependencies,
  474. computeSha1: computeSha1,
  475. filePath: filePath,
  476. hasteImplModulePath: this._options.hasteImplModulePath
  477. })
  478. .then(workerReply, workerError);
  479. }
  480. return null;
  481. }
  482. if (
  483. this._options.mocksPattern &&
  484. this._options.mocksPattern.test(filePath)
  485. ) {
  486. const mockPath = (0, (_get_mock_name || _load_get_mock_name()).default)(
  487. filePath
  488. );
  489. if (mocks[mockPath]) {
  490. this._console.warn(
  491. `jest-haste-map: duplicate manual mock found:\n` +
  492. ` Module name: ${mockPath}\n` +
  493. ` Duplicate Mock path: ${filePath}\nThis warning ` +
  494. `is caused by two manual mock files with the same file name.\n` +
  495. `Jest will use the mock file found in: \n` +
  496. `${filePath}\n` +
  497. ` Please delete one of the following two files: \n ` +
  498. `${mocks[mockPath]}\n${filePath}\n\n`
  499. );
  500. }
  501. mocks[mockPath] = filePath;
  502. }
  503. if (fileMetadata[(_constants || _load_constants()).default.VISITED]) {
  504. if (!fileMetadata[(_constants || _load_constants()).default.ID]) {
  505. return null;
  506. }
  507. if (moduleMetadata != null) {
  508. const platform =
  509. (0,
  510. (_get_platform_extension || _load_get_platform_extension()).default)(
  511. filePath,
  512. this._options.platforms
  513. ) || (_constants || _load_constants()).default.GENERIC_PLATFORM;
  514. const module = moduleMetadata[platform];
  515. if (module == null) {
  516. return null;
  517. }
  518. const modulesByPlatform =
  519. map[fileMetadata[(_constants || _load_constants()).default.ID]] ||
  520. (map[
  521. fileMetadata[(_constants || _load_constants()).default.ID]
  522. ] = {});
  523. modulesByPlatform[platform] = module;
  524. return null;
  525. }
  526. }
  527. return this._getWorker(workerOptions)
  528. .worker({
  529. computeDependencies: this._options.computeDependencies,
  530. computeSha1: computeSha1,
  531. filePath: filePath,
  532. hasteImplModulePath: this._options.hasteImplModulePath
  533. })
  534. .then(workerReply, workerError);
  535. }
  536. _buildHasteMap(data) {
  537. const deprecatedFiles = data.deprecatedFiles,
  538. hasteMap = data.hasteMap;
  539. const map = Object.create(null);
  540. const mocks = Object.create(null);
  541. const promises = [];
  542. for (let i = 0; i < deprecatedFiles.length; ++i) {
  543. const file = deprecatedFiles[i];
  544. this._recoverDuplicates(hasteMap, file.path, file.moduleName);
  545. }
  546. for (const filePath in hasteMap.files) {
  547. // SHA-1, if requested, should already be present thanks to the crawler.
  548. const promise = this._processFile(hasteMap, map, mocks, filePath);
  549. if (promise) {
  550. promises.push(promise);
  551. }
  552. }
  553. return Promise.all(promises)
  554. .then(() => {
  555. this._cleanup();
  556. hasteMap.map = map;
  557. hasteMap.mocks = mocks;
  558. return hasteMap;
  559. })
  560. .catch(error => {
  561. this._cleanup();
  562. return Promise.reject(error);
  563. });
  564. }
  565. _cleanup() {
  566. const worker = this._worker;
  567. // $FlowFixMe
  568. if (worker && typeof worker.end === 'function') {
  569. worker.end();
  570. }
  571. this._worker = null;
  572. }
  573. /**
  574. * 4. serialize the new `HasteMap` in a cache file.
  575. */
  576. _persist(hasteMap) {
  577. (_jestSerializer || _load_jestSerializer()).default.writeFileSync(
  578. this._cachePath,
  579. hasteMap
  580. );
  581. }
  582. /**
  583. * Creates workers or parses files and extracts metadata in-process.
  584. */
  585. _getWorker(options) {
  586. if (!this._worker) {
  587. if ((options && options.forceInBand) || this._options.maxWorkers <= 1) {
  588. this._worker = {
  589. getSha1: (_worker || _load_worker()).getSha1,
  590. worker: (_worker || _load_worker()).worker
  591. };
  592. } else {
  593. // $FlowFixMe: assignment of a worker with custom properties.
  594. this._worker = new (_jestWorker || _load_jestWorker()).default(
  595. require.resolve('./worker'),
  596. {
  597. exposedMethods: ['getSha1', 'worker'],
  598. maxRetries: 3,
  599. numWorkers: this._options.maxWorkers
  600. }
  601. );
  602. }
  603. }
  604. return this._worker;
  605. }
  606. _crawl(hasteMap) {
  607. const options = this._options;
  608. const ignore = this._ignore.bind(this);
  609. const crawl =
  610. canUseWatchman && this._options.useWatchman
  611. ? (_watchman || _load_watchman()).default
  612. : (_node || _load_node()).default;
  613. const retry = error => {
  614. if (crawl === (_watchman || _load_watchman()).default) {
  615. this._console.warn(
  616. `jest-haste-map: Watchman crawl failed. Retrying once with node ` +
  617. `crawler.\n` +
  618. ` Usually this happens when watchman isn't running. Create an ` +
  619. `empty \`.watchmanconfig\` file in your project's root folder or ` +
  620. `initialize a git or hg repository in your project.\n` +
  621. ` ` +
  622. error
  623. );
  624. return (0, (_node || _load_node()).default)({
  625. computeSha1: options.computeSha1,
  626. data: hasteMap,
  627. extensions: options.extensions,
  628. forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
  629. ignore: ignore,
  630. roots: options.roots
  631. }).catch(e => {
  632. throw new Error(
  633. `Crawler retry failed:\n` +
  634. ` Original error: ${error.message}\n` +
  635. ` Retry error: ${e.message}\n`
  636. );
  637. });
  638. }
  639. throw error;
  640. };
  641. try {
  642. return crawl({
  643. computeSha1: options.computeSha1,
  644. data: hasteMap,
  645. extensions: options.extensions,
  646. forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
  647. ignore: ignore,
  648. roots: options.roots
  649. }).catch(retry);
  650. } catch (error) {
  651. return retry(error);
  652. }
  653. }
  654. /**
  655. * Watch mode
  656. */
  657. _watch(hasteMap, hasteFS, moduleMap) {
  658. if (!this._options.watch) {
  659. return Promise.resolve();
  660. }
  661. // In watch mode, we'll only warn about module collisions and we'll retain
  662. // all files, even changes to node_modules.
  663. this._options.throwOnModuleCollision = false;
  664. this._options.retainAllFiles = true;
  665. const Watcher =
  666. canUseWatchman && this._options.useWatchman
  667. ? (_watchman_watcher || _load_watchman_watcher()).default
  668. : (_os || _load_os()).default.platform() === 'darwin'
  669. ? (_sane || _load_sane()).default.FSEventsWatcher
  670. : (_sane || _load_sane()).default.NodeWatcher;
  671. const extensions = this._options.extensions;
  672. const ignorePattern = this._options.ignorePattern;
  673. let changeQueue = Promise.resolve();
  674. let eventsQueue = [];
  675. // We only need to copy the entire haste map once on every "frame".
  676. let mustCopy = true;
  677. const createWatcher = root => {
  678. const watcher = new Watcher(root, {
  679. dot: false,
  680. glob: extensions.map(extension => '**/*.' + extension),
  681. ignored: ignorePattern
  682. });
  683. return new Promise((resolve, reject) => {
  684. const rejectTimeout = setTimeout(
  685. () => reject(new Error('Failed to start watch mode.')),
  686. MAX_WAIT_TIME
  687. );
  688. watcher.once('ready', () => {
  689. clearTimeout(rejectTimeout);
  690. watcher.on('all', onChange);
  691. resolve(watcher);
  692. });
  693. });
  694. };
  695. const emitChange = () => {
  696. if (eventsQueue.length) {
  697. mustCopy = true;
  698. this.emit('change', {
  699. eventsQueue: eventsQueue,
  700. hasteFS: new (_haste_fs || _load_haste_fs()).default(hasteMap.files),
  701. moduleMap: new (_module_map || _load_module_map()).default({
  702. duplicates: hasteMap.duplicates,
  703. map: hasteMap.map,
  704. mocks: hasteMap.mocks
  705. })
  706. });
  707. eventsQueue = [];
  708. }
  709. };
  710. const onChange = (type, filePath, root, stat) => {
  711. filePath = (_path || _load_path()).default.join(
  712. root,
  713. (0, (_normalize_path_sep || _load_normalize_path_sep()).default)(
  714. filePath
  715. )
  716. );
  717. if (
  718. (stat && stat.isDirectory()) ||
  719. this._ignore(filePath) ||
  720. !extensions.some(extension => filePath.endsWith(extension))
  721. ) {
  722. return;
  723. }
  724. changeQueue = changeQueue
  725. .then(() => {
  726. // If we get duplicate events for the same file, ignore them.
  727. if (
  728. eventsQueue.find(
  729. event =>
  730. event.type === type &&
  731. event.filePath === filePath &&
  732. ((!event.stat && !stat) ||
  733. (event.stat &&
  734. stat &&
  735. event.stat.mtime.getTime() === stat.mtime.getTime()))
  736. )
  737. ) {
  738. return null;
  739. }
  740. if (mustCopy) {
  741. mustCopy = false;
  742. hasteMap = {
  743. clocks: copy(hasteMap.clocks),
  744. duplicates: copy(hasteMap.duplicates),
  745. files: copy(hasteMap.files),
  746. map: copy(hasteMap.map),
  747. mocks: copy(hasteMap.mocks)
  748. };
  749. }
  750. const add = () =>
  751. eventsQueue.push({filePath: filePath, stat: stat, type: type});
  752. // Delete the file and all of its metadata.
  753. const moduleName =
  754. hasteMap.files[filePath] &&
  755. hasteMap.files[filePath][
  756. (_constants || _load_constants()).default.ID
  757. ];
  758. const platform =
  759. (0,
  760. (_get_platform_extension || _load_get_platform_extension())
  761. .default)(filePath, this._options.platforms) ||
  762. (_constants || _load_constants()).default.GENERIC_PLATFORM;
  763. delete hasteMap.files[filePath];
  764. let moduleMap = hasteMap.map[moduleName];
  765. if (moduleMap != null) {
  766. // We are forced to copy the object because jest-haste-map exposes
  767. // the map as an immutable entity.
  768. moduleMap = copy(moduleMap);
  769. delete moduleMap[platform];
  770. if (Object.keys(moduleMap).length === 0) {
  771. delete hasteMap.map[moduleName];
  772. } else {
  773. hasteMap.map[moduleName] = moduleMap;
  774. }
  775. }
  776. if (
  777. this._options.mocksPattern &&
  778. this._options.mocksPattern.test(filePath)
  779. ) {
  780. const mockName = (0,
  781. (_get_mock_name || _load_get_mock_name()).default)(filePath);
  782. delete hasteMap.mocks[mockName];
  783. }
  784. this._recoverDuplicates(hasteMap, filePath, moduleName);
  785. // If the file was added or changed,
  786. // parse it and update the haste map.
  787. if (type === 'add' || type === 'change') {
  788. (0, (_invariant || _load_invariant()).default)(
  789. stat,
  790. 'since the file exists or changed, it should have stats'
  791. );
  792. const fileMetadata = ['', stat.mtime.getTime(), 0, [], null];
  793. hasteMap.files[filePath] = fileMetadata;
  794. const promise = this._processFile(
  795. hasteMap,
  796. hasteMap.map,
  797. hasteMap.mocks,
  798. filePath,
  799. {forceInBand: true}
  800. );
  801. // Cleanup
  802. this._cleanup();
  803. if (promise) {
  804. return promise.then(add);
  805. } else {
  806. // If a file in node_modules has changed,
  807. // emit an event regardless.
  808. add();
  809. }
  810. } else {
  811. add();
  812. }
  813. return null;
  814. })
  815. .catch(error => {
  816. this._console.error(
  817. `jest-haste-map: watch error:\n ${error.stack}\n`
  818. );
  819. });
  820. };
  821. this._changeInterval = setInterval(emitChange, CHANGE_INTERVAL);
  822. return Promise.all(this._options.roots.map(createWatcher)).then(
  823. watchers => {
  824. this._watchers = watchers;
  825. }
  826. );
  827. }
  828. /**
  829. * This function should be called when the file under `filePath` is removed
  830. * or changed. When that happens, we want to figure out if that file was
  831. * part of a group of files that had the same ID. If it was, we want to
  832. * remove it from the group. Furthermore, if there is only one file
  833. * remaining in the group, then we want to restore that single file as the
  834. * correct resolution for its ID, and cleanup the duplicates index.
  835. */
  836. _recoverDuplicates(hasteMap, filePath, moduleName) {
  837. let dupsByPlatform = hasteMap.duplicates[moduleName];
  838. if (dupsByPlatform == null) {
  839. return;
  840. }
  841. const platform =
  842. (0, (_get_platform_extension || _load_get_platform_extension()).default)(
  843. filePath,
  844. this._options.platforms
  845. ) || (_constants || _load_constants()).default.GENERIC_PLATFORM;
  846. let dups = dupsByPlatform[platform];
  847. if (dups == null) {
  848. return;
  849. }
  850. dupsByPlatform = hasteMap.duplicates[moduleName] = copy(dupsByPlatform);
  851. dups = dupsByPlatform[platform] = copy(dups);
  852. const dedupType = dups[filePath];
  853. delete dups[filePath];
  854. const filePaths = Object.keys(dups);
  855. if (filePaths.length > 1) {
  856. return;
  857. }
  858. let dedupMap = hasteMap.map[moduleName];
  859. if (dedupMap == null) {
  860. dedupMap = hasteMap.map[moduleName] = Object.create(null);
  861. }
  862. dedupMap[platform] = [filePaths[0], dedupType];
  863. delete dupsByPlatform[platform];
  864. if (Object.keys(dupsByPlatform).length === 0) {
  865. delete hasteMap.duplicates[moduleName];
  866. }
  867. }
  868. end() {
  869. clearInterval(this._changeInterval);
  870. if (!this._watchers.length) {
  871. return Promise.resolve();
  872. }
  873. return Promise.all(
  874. this._watchers.map(
  875. watcher => new Promise(resolve => watcher.close(resolve))
  876. )
  877. ).then(() => {
  878. this._watchers = [];
  879. });
  880. }
  881. /**
  882. * Helpers
  883. */
  884. _ignore(filePath) {
  885. const ignorePattern = this._options.ignorePattern;
  886. const ignoreMatched =
  887. ignorePattern instanceof RegExp
  888. ? ignorePattern.test(filePath)
  889. : ignorePattern(filePath);
  890. return (
  891. ignoreMatched ||
  892. (!this._options.retainAllFiles && this._isNodeModulesDir(filePath))
  893. );
  894. }
  895. _isNodeModulesDir(filePath) {
  896. if (!filePath.includes(NODE_MODULES)) {
  897. return false;
  898. }
  899. if (this._whitelist) {
  900. const whitelist = this._whitelist;
  901. const match = whitelist.exec(filePath);
  902. const matchEndIndex = whitelist.lastIndex;
  903. whitelist.lastIndex = 0;
  904. if (!match) {
  905. return true;
  906. }
  907. const filePathInPackage = filePath.substr(matchEndIndex);
  908. return filePathInPackage.startsWith(NODE_MODULES);
  909. }
  910. return true;
  911. }
  912. _createEmptyMap() {
  913. // $FlowFixMe
  914. return {
  915. clocks: Object.create(null),
  916. duplicates: Object.create(null),
  917. files: Object.create(null),
  918. map: Object.create(null),
  919. mocks: Object.create(null)
  920. };
  921. }
  922. }
  923. const copy = object => Object.assign(Object.create(null), object);
  924. HasteMap.H = (_constants || _load_constants()).default;
  925. HasteMap.ModuleMap = (_module_map || _load_module_map()).default;
  926. module.exports = HasteMap;