node.js 7.1 KB

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