UNPKG

2.26 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.findRepos = exports.getChangedFilesForRoots = void 0;
7
8function _throat() {
9 const data = _interopRequireDefault(require('throat'));
10
11 _throat = function () {
12 return data;
13 };
14
15 return data;
16}
17
18var _git = _interopRequireDefault(require('./git'));
19
20var _hg = _interopRequireDefault(require('./hg'));
21
22function _interopRequireDefault(obj) {
23 return obj && obj.__esModule ? obj : {default: obj};
24}
25
26/**
27 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
28 *
29 * This source code is licensed under the MIT license found in the
30 * LICENSE file in the root directory of this source tree.
31 *
32 */
33function notEmpty(value) {
34 return value != null;
35} // This is an arbitrary number. The main goal is to prevent projects with
36// many roots (50+) from spawning too many processes at once.
37
38const mutex = (0, _throat().default)(5);
39
40const findGitRoot = dir => mutex(() => _git.default.getRoot(dir));
41
42const findHgRoot = dir => mutex(() => _hg.default.getRoot(dir));
43
44const getChangedFilesForRoots = async (roots, options) => {
45 const repos = await findRepos(roots);
46 const changedFilesOptions = {
47 includePaths: roots,
48 ...options
49 };
50 const gitPromises = Array.from(repos.git).map(repo =>
51 _git.default.findChangedFiles(repo, changedFilesOptions)
52 );
53 const hgPromises = Array.from(repos.hg).map(repo =>
54 _hg.default.findChangedFiles(repo, changedFilesOptions)
55 );
56 const changedFiles = (
57 await Promise.all(gitPromises.concat(hgPromises))
58 ).reduce((allFiles, changedFilesInTheRepo) => {
59 for (const file of changedFilesInTheRepo) {
60 allFiles.add(file);
61 }
62
63 return allFiles;
64 }, new Set());
65 return {
66 changedFiles,
67 repos
68 };
69};
70
71exports.getChangedFilesForRoots = getChangedFilesForRoots;
72
73const findRepos = async roots => {
74 const gitRepos = await Promise.all(
75 roots.reduce((promises, root) => promises.concat(findGitRoot(root)), [])
76 );
77 const hgRepos = await Promise.all(
78 roots.reduce((promises, root) => promises.concat(findHgRoot(root)), [])
79 );
80 return {
81 git: new Set(gitRepos.filter(notEmpty)),
82 hg: new Set(hgRepos.filter(notEmpty))
83 };
84};
85
86exports.findRepos = findRepos;