node.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. 'use strict';
  2. function _child_process() {
  3. const data = require('child_process');
  4. _child_process = function () {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function path() {
  10. const data = _interopRequireWildcard(require('path'));
  11. path = function () {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function fs() {
  17. const data = _interopRequireWildcard(require('graceful-fs'));
  18. fs = function () {
  19. return data;
  20. };
  21. return data;
  22. }
  23. var _constants = _interopRequireDefault(require('../constants'));
  24. var fastPath = _interopRequireWildcard(require('../lib/fast_path'));
  25. function _interopRequireDefault(obj) {
  26. return obj && obj.__esModule ? obj : {default: obj};
  27. }
  28. function _getRequireWildcardCache(nodeInterop) {
  29. if (typeof WeakMap !== 'function') return null;
  30. var cacheBabelInterop = new WeakMap();
  31. var cacheNodeInterop = new WeakMap();
  32. return (_getRequireWildcardCache = function (nodeInterop) {
  33. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  34. })(nodeInterop);
  35. }
  36. function _interopRequireWildcard(obj, nodeInterop) {
  37. if (!nodeInterop && obj && obj.__esModule) {
  38. return obj;
  39. }
  40. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  41. return {default: obj};
  42. }
  43. var cache = _getRequireWildcardCache(nodeInterop);
  44. if (cache && cache.has(obj)) {
  45. return cache.get(obj);
  46. }
  47. var newObj = {};
  48. var hasPropertyDescriptor =
  49. Object.defineProperty && Object.getOwnPropertyDescriptor;
  50. for (var key in obj) {
  51. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  52. var desc = hasPropertyDescriptor
  53. ? Object.getOwnPropertyDescriptor(obj, key)
  54. : null;
  55. if (desc && (desc.get || desc.set)) {
  56. Object.defineProperty(newObj, key, desc);
  57. } else {
  58. newObj[key] = obj[key];
  59. }
  60. }
  61. }
  62. newObj.default = obj;
  63. if (cache) {
  64. cache.set(obj, newObj);
  65. }
  66. return newObj;
  67. }
  68. /**
  69. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  70. *
  71. * This source code is licensed under the MIT license found in the
  72. * LICENSE file in the root directory of this source tree.
  73. */
  74. async function hasNativeFindSupport(forceNodeFilesystemAPI) {
  75. if (forceNodeFilesystemAPI) {
  76. return false;
  77. }
  78. try {
  79. return await new Promise(resolve => {
  80. // Check the find binary supports the non-POSIX -iname parameter wrapped in parens.
  81. const args = [
  82. '.',
  83. '-type',
  84. 'f',
  85. '(',
  86. '-iname',
  87. '*.ts',
  88. '-o',
  89. '-iname',
  90. '*.js',
  91. ')'
  92. ];
  93. const child = (0, _child_process().spawn)('find', args, {
  94. cwd: __dirname
  95. });
  96. child.on('error', () => {
  97. resolve(false);
  98. });
  99. child.on('exit', code => {
  100. resolve(code === 0);
  101. });
  102. });
  103. } catch {
  104. return false;
  105. }
  106. }
  107. function find(roots, extensions, ignore, enableSymlinks, callback) {
  108. const result = [];
  109. let activeCalls = 0;
  110. function search(directory) {
  111. activeCalls++;
  112. fs().readdir(
  113. directory,
  114. {
  115. withFileTypes: true
  116. },
  117. (err, entries) => {
  118. activeCalls--;
  119. if (err) {
  120. callback(result);
  121. return;
  122. } // node < v10.10 does not support the withFileTypes option, and
  123. // entry will be a string.
  124. entries.forEach(entry => {
  125. const file = path().join(
  126. directory,
  127. typeof entry === 'string' ? entry : entry.name
  128. );
  129. if (ignore(file)) {
  130. return;
  131. }
  132. if (typeof entry !== 'string') {
  133. if (entry.isSymbolicLink()) {
  134. return;
  135. }
  136. if (entry.isDirectory()) {
  137. search(file);
  138. return;
  139. }
  140. }
  141. activeCalls++;
  142. const stat = enableSymlinks ? fs().stat : fs().lstat;
  143. stat(file, (err, stat) => {
  144. activeCalls--; // This logic is unnecessary for node > v10.10, but leaving it in
  145. // since we need it for backwards-compatibility still.
  146. if (!err && stat && !stat.isSymbolicLink()) {
  147. if (stat.isDirectory()) {
  148. search(file);
  149. } else {
  150. const ext = path().extname(file).substr(1);
  151. if (extensions.indexOf(ext) !== -1) {
  152. result.push([file, stat.mtime.getTime(), stat.size]);
  153. }
  154. }
  155. }
  156. if (activeCalls === 0) {
  157. callback(result);
  158. }
  159. });
  160. });
  161. if (activeCalls === 0) {
  162. callback(result);
  163. }
  164. }
  165. );
  166. }
  167. if (roots.length > 0) {
  168. roots.forEach(search);
  169. } else {
  170. callback(result);
  171. }
  172. }
  173. function findNative(roots, extensions, ignore, enableSymlinks, callback) {
  174. const args = Array.from(roots);
  175. if (enableSymlinks) {
  176. args.push('(', '-type', 'f', '-o', '-type', 'l', ')');
  177. } else {
  178. args.push('-type', 'f');
  179. }
  180. if (extensions.length) {
  181. args.push('(');
  182. }
  183. extensions.forEach((ext, index) => {
  184. if (index) {
  185. args.push('-o');
  186. }
  187. args.push('-iname');
  188. args.push('*.' + ext);
  189. });
  190. if (extensions.length) {
  191. args.push(')');
  192. }
  193. const child = (0, _child_process().spawn)('find', args);
  194. let stdout = '';
  195. if (child.stdout === null) {
  196. throw new Error(
  197. 'stdout is null - this should never happen. Please open up an issue at https://github.com/facebook/jest'
  198. );
  199. }
  200. child.stdout.setEncoding('utf-8');
  201. child.stdout.on('data', data => (stdout += data));
  202. child.stdout.on('close', () => {
  203. const lines = stdout
  204. .trim()
  205. .split('\n')
  206. .filter(x => !ignore(x));
  207. const result = [];
  208. let count = lines.length;
  209. if (!count) {
  210. callback([]);
  211. } else {
  212. lines.forEach(path => {
  213. fs().stat(path, (err, stat) => {
  214. // Filter out symlinks that describe directories
  215. if (!err && stat && !stat.isDirectory()) {
  216. result.push([path, stat.mtime.getTime(), stat.size]);
  217. }
  218. if (--count === 0) {
  219. callback(result);
  220. }
  221. });
  222. });
  223. }
  224. });
  225. }
  226. module.exports = async function nodeCrawl(options) {
  227. const {
  228. data,
  229. extensions,
  230. forceNodeFilesystemAPI,
  231. ignore,
  232. rootDir,
  233. enableSymlinks,
  234. roots
  235. } = options;
  236. const useNativeFind = await hasNativeFindSupport(forceNodeFilesystemAPI);
  237. return new Promise(resolve => {
  238. const callback = list => {
  239. const files = new Map();
  240. const removedFiles = new Map(data.files);
  241. list.forEach(fileData => {
  242. const [filePath, mtime, size] = fileData;
  243. const relativeFilePath = fastPath.relative(rootDir, filePath);
  244. const existingFile = data.files.get(relativeFilePath);
  245. if (existingFile && existingFile[_constants.default.MTIME] === mtime) {
  246. files.set(relativeFilePath, existingFile);
  247. } else {
  248. // See ../constants.js; SHA-1 will always be null and fulfilled later.
  249. files.set(relativeFilePath, ['', mtime, size, 0, '', null]);
  250. }
  251. removedFiles.delete(relativeFilePath);
  252. });
  253. data.files = files;
  254. resolve({
  255. hasteMap: data,
  256. removedFiles
  257. });
  258. };
  259. if (useNativeFind) {
  260. findNative(roots, extensions, ignore, enableSymlinks, callback);
  261. } else {
  262. find(roots, extensions, ignore, enableSymlinks, callback);
  263. }
  264. });
  265. };