node.js 7.0 KB

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