Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 35KB

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