Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,4 @@
**/__mocks__/**
**/__tests__/**
src
yarn.lock

View File

@@ -0,0 +1,67 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
const path = require('path');
const micromatch = require('micromatch');
const H = require('./constants');
class HasteFS {
constructor(files) {
this._files = files;
}
getModuleName(file) {
return this._files[file] && this._files[file][H.ID] || null;
}
getDependencies(file) {
return this._files[file] && this._files[file][H.DEPENDENCIES] || null;
}
exists(file) {
return !!this._files[file];
}
getAllFiles() {
return Object.keys(this._files);
}
matchFiles(pattern) {
if (!(pattern instanceof RegExp)) {
pattern = new RegExp(pattern);
}
const files = [];
for (const file in this._files) {
if (pattern.test(file)) {
files.push(file);
}
}
return files;
}
matchFilesWithGlob(globs, root) {
const files = new Set();
for (const file in this._files) {
const filePath = root ? path.relative(root, file) : file;
if (micromatch([filePath], globs).length) {
files.add(file);
}
}
return files;
}}
module.exports = HasteFS;

View File

@@ -0,0 +1,77 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
const H = require('./constants');
class ModuleMap {
constructor(map, mocks) {
this._map = map;
this._mocks = mocks;
}
getModule(
name,
platform,
supportsNativePlatform,
type)
{
if (!type) {
type = H.MODULE;
}
const map = this._map[name];
if (map) {
let module = platform && map[platform];
if (!module && map[H.NATIVE_PLATFORM] && supportsNativePlatform) {
module = map[H.NATIVE_PLATFORM];
} else if (!module) {
module = map[H.GENERIC_PLATFORM];
}
if (module && module[H.TYPE] === type) {
return module[H.PATH];
}
}
return null;
}
getPackage(
name,
platform,
supportsNativePlatform)
{
return this.getModule(name, platform, null, H.PACKAGE);
}
getMockModule(name) {
return this._mocks[name];
}
getRawModuleMap() {
return {
map: this._map,
mocks: this._mocks };
}}
module.exports = ModuleMap;

View File

@@ -0,0 +1,37 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
/* eslint-disable sort-keys */
/*
* This file exports a set of constants that are used for Jest's haste map
* serialization. On very large repositories, the haste map cache becomes very
* large to the point where it is the largest overhead in starting up Jest.
*
* This constant key map allows to keep the map smaller without having to build
* a custom serialization library.
*/
module.exports = {
/* file map attributes */
ID: 0,
MTIME: 1,
VISITED: 2,
DEPENDENCIES: 3,
/* module map attributes */
PATH: 0,
TYPE: 1,
/* module types */
MODULE: 0,
PACKAGE: 1,
/* platforms */
GENERIC_PLATFORM: 'g',
NATIVE_PLATFORM: 'native' };

View File

@@ -0,0 +1,148 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
const fs = require('fs');
const path = require('path');var _require =
require('child_process');const spawn = _require.spawn;
const H = require('../constants');
function find(
roots,
extensions,
ignore,
callback)
{
const result = [];
let activeCalls = 0;
function search(directory) {
activeCalls++;
fs.readdir(directory, (err, names) => {
activeCalls--;
names.forEach(file => {
file = path.join(directory, file);
if (ignore(file)) {
return;
}
activeCalls++;
fs.lstat(file, (err, stat) => {
activeCalls--;
if (!err && stat && !stat.isSymbolicLink()) {
if (stat.isDirectory()) {
search(file);
} else {
const ext = path.extname(file).substr(1);
if (extensions.indexOf(ext) !== -1) {
result.push([file, stat.mtime.getTime()]);
}
}
}
if (activeCalls === 0) {
callback(result);
}
});
});
if (activeCalls === 0) {
callback(result);
}
});
}
roots.forEach(search);
}
function findNative(
roots,
extensions,
ignore,
callback)
{
const args = [].concat(roots);
args.push('-type', 'f');
if (extensions.length) {
args.push('\(');
}
extensions.forEach((ext, index) => {
if (index) {
args.push('-o');
}
args.push('-iname');
args.push('*.' + ext);
});
if (extensions.length) {
args.push('\)');
}
const child = spawn('find', args);
let stdout = '';
child.stdout.setEncoding('utf-8');
child.stdout.on('data', data => stdout += data);
child.stdout.on('close', () => {
const lines = stdout.trim().split('\n').filter(x => !ignore(x));
const result = [];
let count = lines.length;
if (!count) {
callback([]);
} else {
lines.forEach(path => {
fs.stat(path, (err, stat) => {
if (!err && stat) {
result.push([path, stat.mtime.getTime()]);
}
if (--count === 0) {
callback(result);
}
});
});
}
});
}
module.exports = function nodeCrawl(
options)
{const
data = options.data,extensions = options.extensions,forceNodeFilesystemAPI = options.forceNodeFilesystemAPI,ignore = options.ignore,roots = options.roots;
return new Promise(resolve => {
const callback = list => {
const files = Object.create(null);
list.forEach(fileData => {
const name = fileData[0];
const mtime = fileData[1];
const existingFile = data.files[name];
if (existingFile && existingFile[H.MTIME] === mtime) {
files[name] = existingFile;
} else {
// See ../constants.js
files[name] = ['', mtime, 0, []];
}
});
data.files = files;
resolve(data);
};
if (forceNodeFilesystemAPI || process.platform === 'win32') {
find(roots, extensions, ignore, callback);
} else {
findNative(roots, extensions, ignore, callback);
}
});
};

View File

@@ -0,0 +1,137 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
const path = require('path');
const watchman = require('fb-watchman');
const H = require('../constants');
const watchmanURL =
'https://facebook.github.io/watchman/docs/troubleshooting.html';
function isDescendant(root, child) {
return child.startsWith(root);
}
function WatchmanError(error) {
return new Error(
`Watchman error: ${error.message.trim()}. Make sure watchman ` +
`is running for this project. See ${watchmanURL}.`);
}
module.exports = function watchmanCrawl(
options)
{const
data = options.data,extensions = options.extensions,ignore = options.ignore,roots = options.roots;
return new Promise((resolve, reject) => {
const client = new watchman.Client();
client.on('error', error => reject(error));
const cmd = args =>
new Promise((resolve, reject) => {
client.command(args, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
const clocks = data.clocks;
let files = data.files;
return Promise.all(roots.map(root => cmd(['watch-project', root]))).
then(responses => {
const watchmanRoots = Array.from(
new Set(responses.map(response => response.watch)));
return Promise.all(
watchmanRoots.map(root => {
// Build an expression to filter the output by the relevant roots.
const dirExpr = ['anyof'];
roots.forEach(subRoot => {
if (isDescendant(root, subRoot)) {
dirExpr.push(['dirname', path.relative(root, subRoot)]);
}
});
const expression = [
'allof',
['type', 'f'],
['anyof'].concat(
extensions.map(extension => ['suffix', extension]))];
if (dirExpr.length > 1) {
expression.push(dirExpr);
}
const fields = ['name', 'exists', 'mtime_ms'];
const query = clocks[root] ?
// Use the `since` generator if we have a clock available
{ expression, fields, since: clocks[root] } :
// Otherwise use the `suffix` generator
{ expression, fields, suffix: extensions };
return cmd(['query', root, query]).then(response => ({
response,
root }));
})).
then(pairs => {
// Reset the file map if watchman was restarted and sends us a list of
// files.
if (pairs.some(pair => pair.response.is_fresh_instance)) {
files = Object.create(null);
}
pairs.forEach(pair => {
const root = pair.root;
const response = pair.response;
if ('warning' in response) {
console.warn('watchman warning: ', response.warning);
}
clocks[root] = response.clock;
response.files.forEach(fileData => {
const name = root + path.sep + fileData.name;
if (!fileData.exists) {
delete files[name];
} else if (!ignore(name)) {
const mtime = typeof fileData.mtime_ms === 'number' ?
fileData.mtime_ms :
fileData.mtime_ms.toNumber();
const isNew =
!data.files[name] || data.files[name][H.MTIME] !== mtime;
if (isNew) {
// See ../constants.js
files[name] = ['', mtime, 0, []];
} else {
files[name] = data.files[name];
}
}
});
});
});
}).
then(() => {
client.end();
data.files = files;
resolve(data);
}).
catch(error => {
client.end();
reject(WatchmanError(error));
});
});
};

View File

@@ -0,0 +1,20 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
const path = require('path');
const MOCKS_PATTERN = path.sep + '__mocks__' + path.sep;
const getMockName = filePath => {
const mockPath = filePath.split(MOCKS_PATTERN)[1];
return mockPath.substring(0, mockPath.lastIndexOf(path.extname(mockPath)));
};
module.exports = getMockName;

View File

@@ -0,0 +1,823 @@
'use strict';
const EventEmitter = require('events'); /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/const os = require('os');const path = require('path');const crypto = require('crypto');var _require = require('child_process');const execSync = _require.execSync;const fs = require('graceful-fs');const sane = require('sane');const workerFarm = require('worker-farm');
const VERSION = require('../package.json').version;
const H = require('./constants');
const HasteFS = require('./HasteFS');
const HasteModuleMap = require('./ModuleMap');
const getMockName = require('./getMockName');
const getPlatformExtension = require('./lib/getPlatformExtension');
const nodeCrawl = require('./crawlers/node');
const watchmanCrawl = require('./crawlers/watchman');
const worker = require('./worker');
const CHANGE_INTERVAL = 30;
const MAX_WAIT_TIME = 240000;
const NODE_MODULES = path.sep + 'node_modules' + path.sep;
const canUseWatchman = (() => {
try {
execSync('watchman --version', { stdio: ['ignore'] });
return true;
} catch (e) {}
return false;
})();
const escapePathSeparator = string =>
path.sep === '\\' ? string.replace(/(\/|\\)/g, '\\\\') : string;
const getWhiteList = list => {
if (list && list.length) {
return new RegExp(
'(' +
escapePathSeparator(NODE_MODULES) +
'(?:' +
list.join('|') +
')(?=$|' +
escapePathSeparator(path.sep) +
'))',
'g');
}
return null;
};
/**
* HasteMap is a JavaScript implementation of Facebook's haste module system.
*
* This implementation is inspired by https://github.com/facebook/node-haste
* and was built with for high-performance in large code repositories with
* hundreds of thousands of files. This implementation is scalable and provides
* predictable performance.
*
* Because the haste map creation and synchronization is critical to startup
* performance and most tasks are blocked by I/O this class makes heavy use of
* synchronous operations. It uses worker processes for parallelizing file
* access and metadata extraction.
*
* The data structures created by `jest-haste-map` can be used directly from the
* cache without further processing. The metadata objects in the `files` and
* `map` objects contain cross-references: a metadata object from one can look
* up the corresponding metadata object in the other map. Note that in most
* projects, the number of files will be greater than the number of haste
* modules one module can refer to many files based on platform extensions.
*
* type HasteMap = {
* clocks: WatchmanClocks,
* files: {[filepath: string]: FileMetaData},
* map: {[id: string]: ModuleMapItem},
* mocks: {[id: string]: string},
* }
*
* // Watchman clocks are used for query synchronization and file system deltas.
* type WatchmanClocks = {[filepath: string]: string};
*
* type FileMetaData = {
* id: ?string, // used to look up module metadata objects in `map`.
* mtime: number, // check for outdated files.
* visited: boolean, // whether the file has been parsed or not.
* dependencies: Array<string>, // all relative dependencies of this file.
* };
*
* // Modules can be targeted to a specific platform based on the file name.
* // Example: Platform.ios.js and Platform.android.js will both map to the same
* // `Platform` module. The platform should be specified during resolution.
* type ModuleMapItem = {[platform: string]: ModuleMetaData};
*
* //
* type ModuleMetaData = {
* path: string, // the path to look up the file object in `files`.
* type: string, // the module type (either `package` or `module`).
* };
*
* Note that the data structures described above are conceptual only. The actual
* implementation uses arrays and constant keys for metadata storage. Instead of
* `{id: 'flatMap', mtime: 3421, visited: true, dependencies: []}` the real
* representation is similar to `['flatMap', 3421, 1, []]` to save storage space
* and reduce parse and write time of a big JSON blob.
*
* The HasteMap is created as follows:
* 1. read data from the cache or create an empty structure.
* 2. crawl the file system.
* * empty cache: crawl the entire file system.
* * cache available:
* * if watchman is available: get file system delta changes.
* * if watchman is unavailable: crawl the entire file system.
* * build metadata objects for every file. This builds the `files` part of
* the `HasteMap`.
* 3. parse and extract metadata from changed files.
* * this is done in parallel over worker processes to improve performance.
* * the worst case is to parse all files.
* * the best case is no file system access and retrieving all data from
* the cache.
* * the average case is a small number of changed files.
* 4. serialize the new `HasteMap` in a cache file.
* Worker processes can directly access the cache through `HasteMap.read()`.
*
*/
class HasteMap extends EventEmitter {
constructor(options) {
super();
this._options = {
cacheDirectory: options.cacheDirectory || os.tmpdir(),
extensions: options.extensions,
forceNodeFilesystemAPI: !!options.forceNodeFilesystemAPI,
hasteImplModulePath: options.hasteImplModulePath,
ignorePattern: options.ignorePattern,
maxWorkers: options.maxWorkers,
mocksPattern: options.mocksPattern ?
new RegExp(options.mocksPattern) :
null,
name: options.name,
platforms: options.platforms,
resetCache: options.resetCache,
retainAllFiles: options.retainAllFiles,
roots: Array.from(new Set(options.roots)),
throwOnModuleCollision: !!options.throwOnModuleCollision,
useWatchman: options.useWatchman == null ? true : options.useWatchman,
watch: !!options.watch };
this._console = options.console || global.console;
this._cachePath = HasteMap.getCacheFilePath(
this._options.cacheDirectory,
`haste-map-${this._options.name}`,
VERSION,
this._options.roots.join(':'),
this._options.extensions.join(':'),
this._options.platforms.join(':'),
options.mocksPattern || '');
this._whitelist = getWhiteList(options.providesModuleNodeModules);
this._buildPromise = null;
this._workerPromise = null;
this._workerFarm = null;
this._watchers = [];
}
static getCacheFilePath(tmpdir, name) {
const hash = crypto.createHash('md5');
Array.from(arguments).slice(1).forEach(arg => hash.update(arg));
return path.join(
tmpdir,
name.replace(/\W/g, '-') + '-' + hash.digest('hex'));
}
build() {
if (!this._buildPromise) {
this._buildPromise = this._buildFileMap().
then(fileMap => this._buildHasteMap(fileMap)).
then(hasteMap => {
this._persist(hasteMap);
const hasteFS = new HasteFS(hasteMap.files);
const moduleMap = new HasteModuleMap(hasteMap.map, hasteMap.mocks);
const __hasteMapForTest =
process.env.NODE_ENV === 'test' && hasteMap || null;
return this._watch(hasteMap, hasteFS, moduleMap).then(() => ({
__hasteMapForTest,
hasteFS,
moduleMap }));
});
}
return this._buildPromise;
}
/**
* 1. read data from the cache or create an empty structure.
*/
read() {
return this._parse(fs.readFileSync(this._cachePath, 'utf8'));
}
readModuleMap() {
const data = this.read();
return new HasteModuleMap(data.map, data.mocks);
}
/**
* 2. crawl the file system.
*/
_buildFileMap() {
const read = this._options.resetCache ? this._createEmptyMap : this.read;
return Promise.resolve().
then(() => read.call(this)).
catch(() => this._createEmptyMap()).
then(hasteMap => this._crawl(hasteMap));
}
/**
* 3. parse and extract metadata from changed files.
*/
_processFile(
hasteMap,
map,
mocks,
filePath,
workerOptions)
{
const setModule = (id, module) => {
if (!map[id]) {
map[id] = Object.create(null);
}
const moduleMap = map[id];
const platform =
getPlatformExtension(module[H.PATH], this._options.platforms) ||
H.GENERIC_PLATFORM;
const existingModule = moduleMap[platform];
if (existingModule && existingModule[H.PATH] !== module[H.PATH]) {
const message =
`jest-haste-map: @providesModule naming collision:\n` +
` Duplicate module name: ${id}\n` +
` Paths: ${module[H.PATH]} collides with ` +
`${existingModule[H.PATH]}\n\nThis ` +
`${this._options.throwOnModuleCollision ? 'error' : 'warning'} ` +
`is caused by a @providesModule declaration ` +
`with the same name across two different files.`;
if (this._options.throwOnModuleCollision) {
throw new Error(message);
}
this._console.warn(message);
// We do NOT want consumers to use a module that is ambiguous.
delete moduleMap[platform];
if (Object.keys(moduleMap).length === 1) {
delete map[id];
}
let dupsByPlatform = hasteMap.duplicates[id];
if (dupsByPlatform == null) {
dupsByPlatform = hasteMap.duplicates[id] = Object.create(null);
}
const dups = dupsByPlatform[platform] = Object.create(null);
dups[module[H.PATH]] = module[H.TYPE];
dups[existingModule[H.PATH]] = existingModule[H.TYPE];
return;
}
const dupsByPlatform = hasteMap.duplicates[id];
if (dupsByPlatform != null) {
const dups = dupsByPlatform[platform];
if (dups != null) {
dups[module[H.PATH]] = module[H.TYPE];
}
return;
}
moduleMap[platform] = module;
};
// If we retain all files in the virtual HasteFS representation, we avoid
// reading them if they aren't important (node_modules).
if (this._options.retainAllFiles && this._isNodeModulesDir(filePath)) {
return null;
}
if (
this._options.mocksPattern &&
this._options.mocksPattern.test(filePath))
{
const mockPath = getMockName(filePath);
if (mocks[mockPath]) {
this._console.warn(
`jest-haste-map: duplicate manual mock found:\n` +
` Module name: ${mockPath}\n` +
` Duplicate Mock path: ${filePath}\nThis warning ` +
`is caused by two manual mock files with the same file name.\n` +
`Jest will use the mock file found in: \n` +
`${filePath}\n` +
` Please delete one of the following two files: \n ` +
`${mocks[mockPath]}\n${filePath}\n\n`);
}
mocks[mockPath] = filePath;
}
const fileMetadata = hasteMap.files[filePath];
const moduleMetadata = hasteMap.map[fileMetadata[H.ID]];
if (fileMetadata[H.VISITED]) {
if (!fileMetadata[H.ID]) {
return null;
} else if (fileMetadata[H.ID] && moduleMetadata) {
map[fileMetadata[H.ID]] = moduleMetadata;
return null;
}
}
return this._getWorker(workerOptions)({
filePath,
hasteImplModulePath: this._options.hasteImplModulePath }).
then(
metadata => {
// `1` for truthy values instead of `true` to save cache space.
fileMetadata[H.VISITED] = 1;
const metadataId = metadata.id;
const metadataModule = metadata.module;
if (metadataId && metadataModule) {
fileMetadata[H.ID] = metadataId;
setModule(metadataId, metadataModule);
}
fileMetadata[H.DEPENDENCIES] = metadata.dependencies || [];
},
error => {
// If a file cannot be read we remove it from the file list and
// ignore the failure silently.
delete hasteMap.files[filePath];
});
}
_buildHasteMap(hasteMap) {
const map = Object.create(null);
const mocks = Object.create(null);
const promises = [];
for (const filePath in hasteMap.files) {
const promise = this._processFile(hasteMap, map, mocks, filePath);
if (promise) {
promises.push(promise);
}
}
const cleanup = () => {
if (this._workerFarm) {
workerFarm.end(this._workerFarm);
}
this._workerFarm = null;
this._workerPromise = null;
};
return Promise.all(promises).
then(cleanup).
then(() => {
hasteMap.map = map;
hasteMap.mocks = mocks;
return hasteMap;
}).
catch(error => {
cleanup();
return Promise.reject(error);
});
}
/**
* 4. serialize the new `HasteMap` in a cache file.
*/
_persist(hasteMap) {
fs.writeFileSync(this._cachePath, JSON.stringify(hasteMap), 'utf8');
}
/**
* Creates workers or parses files and extracts metadata in-process.
*/
_getWorker(
options)
{
if (!this._workerPromise) {
let workerFn;
if (options && options.forceInBand || this._options.maxWorkers <= 1) {
workerFn = worker;
} else {
this._workerFarm = workerFarm(
{
maxConcurrentWorkers: this._options.maxWorkers },
require.resolve('./worker'));
workerFn = this._workerFarm;
}
this._workerPromise = message =>
new Promise((resolve, reject) =>
workerFn(message, (error, metadata) => {
if (error || !metadata) {
reject(error);
} else {
resolve(metadata);
}
}));
}
return this._workerPromise;
}
_parse(hasteMapPath) {
const hasteMap = JSON.parse(hasteMapPath);
for (const key in hasteMap) {
Object.setPrototypeOf(hasteMap[key], null);
}
return hasteMap;
}
_crawl(hasteMap) {
const options = this._options;
const ignore = this._ignore.bind(this);
const crawl = canUseWatchman && this._options.useWatchman ?
watchmanCrawl :
nodeCrawl;
const retry = error => {
if (crawl === watchmanCrawl) {
this._console.warn(
`jest-haste-map: Watchman crawl failed. Retrying once with node ` +
`crawler.\n` +
` Usually this happens when watchman isn't running. Create an ` +
`empty \`.watchmanconfig\` file in your project's root folder or ` +
`initialize a git or hg repository in your project.\n` +
` ` +
error);
return nodeCrawl({
data: hasteMap,
extensions: options.extensions,
forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
ignore,
roots: options.roots }).
catch(e => {
throw new Error(
`Crawler retry failed:\n` +
` Original error: ${error.message}\n` +
` Retry error: ${e.message}\n`);
});
}
throw error;
};
try {
return crawl({
data: hasteMap,
extensions: options.extensions,
forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
ignore,
roots: options.roots }).
catch(retry);
} catch (error) {
return retry(error);
}
}
/**
* Watch mode
*/
_watch(
hasteMap,
hasteFS,
moduleMap)
{
if (!this._options.watch) {
return Promise.resolve();
}
// In watch mode, we'll only warn about module collisions and we'll retain
// all files, even changes to node_modules.
this._options.throwOnModuleCollision = false;
this._options.retainAllFiles = true;
const Watcher = canUseWatchman && this._options.useWatchman ?
sane.WatchmanWatcher :
sane.NodeWatcher;
const extensions = this._options.extensions;
const ignorePattern = this._options.ignorePattern;
let changeQueue = Promise.resolve();
let eventsQueue = [];
// We only need to copy the entire haste map once on every "frame".
let mustCopy = true;
const createWatcher = root => {
const watcher = new Watcher(root, {
dot: false,
glob: extensions.map(extension => '**/*.' + extension),
ignored: ignorePattern });
return new Promise((resolve, reject) => {
const rejectTimeout = setTimeout(
() => reject(new Error('Failed to start watch mode.')),
MAX_WAIT_TIME);
watcher.once('ready', () => {
clearTimeout(rejectTimeout);
watcher.on('all', onChange);
resolve(watcher);
});
});
};
const emitChange = () => {
if (eventsQueue.length) {
mustCopy = true;
this.emit('change', {
eventsQueue,
hasteFS: new HasteFS(hasteMap.files),
moduleMap: new HasteModuleMap(hasteMap.map, hasteMap.mocks) });
eventsQueue = [];
}
};
const onChange = (
type,
filePath,
root,
stat) =>
{
filePath = path.join(root, filePath);
if (
this._ignore(filePath) ||
!extensions.some(extension => filePath.endsWith(extension)))
{
return;
}
changeQueue = changeQueue.
then(() => {
// If we get duplicate events for the same file, ignore them.
if (
eventsQueue.find(
event =>
event.type === type &&
event.filePath === filePath && (
!event.stat && !stat ||
event.stat &&
stat &&
event.stat.mtime.getTime() === stat.mtime.getTime())))
{
return null;
}
if (mustCopy) {
mustCopy = false;
hasteMap = {
clocks: copy(hasteMap.clocks),
duplicates: copy(hasteMap.duplicates),
files: copy(hasteMap.files),
map: copy(hasteMap.map),
mocks: copy(hasteMap.mocks) };
}
const add = () => eventsQueue.push({ filePath, stat, type });
// Delete the file and all of its metadata.
const moduleName =
hasteMap.files[filePath] && hasteMap.files[filePath][H.ID];
delete hasteMap.files[filePath];
delete hasteMap.map[moduleName];
if (
this._options.mocksPattern &&
this._options.mocksPattern.test(filePath))
{
const mockName = getMockName(filePath);
delete hasteMap.mocks[mockName];
}
this._recoverDuplicates(hasteMap, filePath, moduleName);
// If the file was added or changed,
// parse it and update the haste map.
if (type === 'add' || type === 'change') {
const fileMetadata = ['', stat.mtime.getTime(), 0, []];
hasteMap.files[filePath] = fileMetadata;
const promise = this._processFile(
hasteMap,
hasteMap.map,
hasteMap.mocks,
filePath,
{
forceInBand: true });
// Cleanup
this._workerPromise = null;
if (promise) {
return promise.then(add);
} else {
// If a file in node_modules has changed,
// emit an event regardless.
add();
}
} else {
add();
}
return null;
}).
catch(error => {
this._console.error(
`jest-haste-map: watch error:\n ${error.stack}\n`);
});
};
this._changeInterval = setInterval(emitChange, CHANGE_INTERVAL);
return Promise.all(
this._options.roots.map(createWatcher)).
then(watchers => {
this._watchers = watchers;
});
}
/**
* This function should be called when the file under `filePath` is removed
* or changed. When that happens, we want to figure out if that file was
* part of a group of files that had the same ID. If it was, we want to
* remove it from the group. Furthermore, if there is only one file
* remaining in the group, then we want to restore that single file as the
* correct resolution for its ID, and cleanup the duplicates index.
*/
_recoverDuplicates(
hasteMap,
filePath,
moduleName)
{
let dupsByPlatform = hasteMap.duplicates[moduleName];
if (dupsByPlatform == null) {
return;
}
const platform =
getPlatformExtension(filePath, this._options.platforms) ||
H.GENERIC_PLATFORM;
let dups = dupsByPlatform[platform];
if (dups == null) {
return;
}
dupsByPlatform = hasteMap.duplicates[moduleName] = copy(
dupsByPlatform);
dups = dupsByPlatform[platform] = copy(dups);
const dedupType = dups[filePath];
delete dups[filePath];
const filePaths = Object.keys(dups);
if (filePaths.length > 1) {
return;
}
let dedupMap = hasteMap.map[moduleName];
if (dedupMap == null) {
dedupMap = hasteMap.map[moduleName] = Object.create(null);
}
dedupMap[platform] = [filePaths[0], dedupType];
delete dupsByPlatform[platform];
if (Object.keys(dupsByPlatform).length === 0) {
delete hasteMap.duplicates[moduleName];
}
}
end() {
clearInterval(this._changeInterval);
if (!this._watchers.length) {
return Promise.resolve();
}
return Promise.all(
this._watchers.map(
watcher => new Promise(resolve => watcher.close(resolve)))).
then(() => this._watchers = []);
}
/**
* Helpers
*/
_ignore(filePath) {
const ignorePattern = this._options.ignorePattern;
const ignoreMatched = ignorePattern instanceof RegExp ?
ignorePattern.test(filePath) :
ignorePattern(filePath);
return (
ignoreMatched ||
!this._options.retainAllFiles && this._isNodeModulesDir(filePath));
}
_isNodeModulesDir(filePath) {
if (!filePath.includes(NODE_MODULES)) {
return false;
}
if (this._whitelist) {
const whitelist = this._whitelist;
const match = whitelist.exec(filePath);
const matchEndIndex = whitelist.lastIndex;
whitelist.lastIndex = 0;
if (!match) {
return true;
}
const filePathInPackage = filePath.substr(matchEndIndex);
return filePathInPackage.startsWith(NODE_MODULES);
}
return true;
}
_createEmptyMap() {
return {
clocks: Object.create(null),
duplicates: Object.create(null),
files: Object.create(null),
map: Object.create(null),
mocks: Object.create(null) };
}}
const copy = object => Object.assign(Object.create(null), object);
HasteMap.H = H;
HasteMap.ModuleMap = HasteModuleMap;
module.exports = HasteMap;

View File

@@ -0,0 +1,39 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
const blockCommentRe = /\/\*[^]*?\*\//g;
const lineCommentRe = /\/\/.*/g;
const replacePatterns = {
EXPORT_RE: /(\bexport\s+(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g,
IMPORT_RE: /(\bimport\s+(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g,
REQUIRE_EXTENSIONS_PATTERN: /(\b(?:require\s*?\.\s*?(?:requireActual|requireMock)|jest\s*?\.\s*?genMockFromModule)\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
REQUIRE_RE: /(\brequire\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g };
function extractRequires(code) {
const dependencies = new Set();
const addDependency = (match, pre, quot, dep, post) => {
dependencies.add(dep);
return match;
};
code.
replace(blockCommentRe, '').
replace(lineCommentRe, '').
replace(replacePatterns.EXPORT_RE, addDependency).
replace(replacePatterns.IMPORT_RE, addDependency).
replace(replacePatterns.REQUIRE_EXTENSIONS_PATTERN, addDependency).
replace(replacePatterns.REQUIRE_RE, addDependency);
return Array.from(dependencies);
}
module.exports = extractRequires;

View File

@@ -0,0 +1,37 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
const SUPPORTED_PLATFORM_EXTS = {
android: true,
ios: true,
native: true,
web: true };
// Extract platform extension: index.ios.js -> ios
function getPlatformExtension(
file,
platforms)
{
const last = file.lastIndexOf('.');
const secondToLast = file.lastIndexOf('.', last - 1);
if (secondToLast === -1) {
return null;
}
const platform = file.substring(secondToLast + 1, last);
// If an overriding platform array is passed, check that first
if (platforms && platforms.indexOf(platform) !== -1) {
return platform;
}
return SUPPORTED_PLATFORM_EXTS[platform] ? platform : null;
}
module.exports = getPlatformExtension;

View File

@@ -0,0 +1 @@
'use strict';

View File

@@ -0,0 +1,86 @@
'use strict'; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
const path = require('path');
const docblock = require('jest-docblock');
const fs = require('graceful-fs');
const H = require('./constants');
const extractRequires = require('./lib/extractRequires');
const JSON_EXTENSION = '.json';
const PACKAGE_JSON = path.sep + 'package' + JSON_EXTENSION;
let hasteImpl = null;
let hasteImplModulePath = null;
const formatError = error => {
if (typeof error === 'string') {
return {
message: error,
stack: null,
type: 'Error' };
}
return {
message: error.message,
stack: error.stack,
type: 'Error' };
};
module.exports = (data, callback) => {
try {
if (
data.hasteImplModulePath &&
data.hasteImplModulePath !== hasteImplModulePath)
{
if (hasteImpl) {
throw new Error('jest-haste-map: hasteImplModulePath changed');
}
hasteImplModulePath = data.hasteImplModulePath;
hasteImpl =
// $FlowFixMe: dynamic require
require(hasteImplModulePath);
}
const filePath = data.filePath;
const content = fs.readFileSync(filePath, 'utf8');
let module;
let id;
let dependencies;
if (filePath.endsWith(PACKAGE_JSON)) {
const fileData = JSON.parse(content);
if (fileData.name) {
id = fileData.name;
module = [filePath, H.PACKAGE];
}
} else if (!filePath.endsWith(JSON_EXTENSION)) {
if (hasteImpl) {
id = hasteImpl.getHasteName(filePath);
} else {
const doc = docblock.parse(docblock.extract(content));
id = doc.providesModule || doc.provides;
}
dependencies = extractRequires(content);
if (id) {
module = [filePath, H.MODULE];
}
}
callback(null, { dependencies, id, module });
} catch (error) {
callback(formatError(error));
}
};

View File

@@ -0,0 +1,51 @@
{
"_args": [
[
"jest-haste-map@20.0.5",
"C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project"
]
],
"_from": "jest-haste-map@20.0.5",
"_id": "jest-haste-map@20.0.5",
"_inBundle": false,
"_integrity": "sha512-0IKAQjUvuZjMCNi/0VNQQF74/H9KB67hsHJqGiwTWQC6XO5Azs7kLWm+6Q/dwuhvDUvABDOBMFK2/FwZ3sZ07Q==",
"_location": "/react-scripts/jest-haste-map",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "jest-haste-map@20.0.5",
"name": "jest-haste-map",
"escapedName": "jest-haste-map",
"rawSpec": "20.0.5",
"saveSpec": null,
"fetchSpec": "20.0.5"
},
"_requiredBy": [
"/react-scripts/jest-runtime",
"/react-scripts/jest/jest-cli"
],
"_resolved": "https://registry.npmjs.org/jest-haste-map/-/jest-haste-map-20.0.5.tgz",
"_spec": "20.0.5",
"_where": "C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project",
"bugs": {
"url": "https://github.com/facebook/jest/issues"
},
"dependencies": {
"fb-watchman": "^2.0.0",
"graceful-fs": "^4.1.11",
"jest-docblock": "^20.0.3",
"micromatch": "^2.3.11",
"sane": "~1.6.0",
"worker-farm": "^1.3.1"
},
"homepage": "https://github.com/facebook/jest#readme",
"license": "BSD-3-Clause",
"main": "build/index.js",
"name": "jest-haste-map",
"repository": {
"type": "git",
"url": "git+https://github.com/facebook/jest.git"
},
"version": "20.0.5"
}