index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. Object.defineProperty(exports, 'ChangedFiles', {
  6. enumerable: true,
  7. get: function get() {
  8. return _types.ChangedFiles;
  9. }
  10. });
  11. Object.defineProperty(exports, 'ChangedFilesPromise', {
  12. enumerable: true,
  13. get: function get() {
  14. return _types.ChangedFilesPromise;
  15. }
  16. });
  17. exports.findRepos = exports.getChangedFilesForRoots = void 0;
  18. function _throat() {
  19. const data = _interopRequireDefault(require('throat'));
  20. _throat = function _throat() {
  21. return data;
  22. };
  23. return data;
  24. }
  25. var _types = require('./types');
  26. var _git = _interopRequireDefault(require('./git'));
  27. var _hg = _interopRequireDefault(require('./hg'));
  28. function _interopRequireDefault(obj) {
  29. return obj && obj.__esModule ? obj : {default: obj};
  30. }
  31. function _objectSpread(target) {
  32. for (var i = 1; i < arguments.length; i++) {
  33. var source = arguments[i] != null ? arguments[i] : {};
  34. var ownKeys = Object.keys(source);
  35. if (typeof Object.getOwnPropertySymbols === 'function') {
  36. ownKeys = ownKeys.concat(
  37. Object.getOwnPropertySymbols(source).filter(function(sym) {
  38. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  39. })
  40. );
  41. }
  42. ownKeys.forEach(function(key) {
  43. _defineProperty(target, key, source[key]);
  44. });
  45. }
  46. return target;
  47. }
  48. function _defineProperty(obj, key, value) {
  49. if (key in obj) {
  50. Object.defineProperty(obj, key, {
  51. value: value,
  52. enumerable: true,
  53. configurable: true,
  54. writable: true
  55. });
  56. } else {
  57. obj[key] = value;
  58. }
  59. return obj;
  60. }
  61. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  62. try {
  63. var info = gen[key](arg);
  64. var value = info.value;
  65. } catch (error) {
  66. reject(error);
  67. return;
  68. }
  69. if (info.done) {
  70. resolve(value);
  71. } else {
  72. Promise.resolve(value).then(_next, _throw);
  73. }
  74. }
  75. function _asyncToGenerator(fn) {
  76. return function() {
  77. var self = this,
  78. args = arguments;
  79. return new Promise(function(resolve, reject) {
  80. var gen = fn.apply(self, args);
  81. function _next(value) {
  82. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'next', value);
  83. }
  84. function _throw(err) {
  85. asyncGeneratorStep(gen, resolve, reject, _next, _throw, 'throw', err);
  86. }
  87. _next(undefined);
  88. });
  89. };
  90. }
  91. function notEmpty(value) {
  92. return value != null;
  93. } // This is an arbitrary number. The main goal is to prevent projects with
  94. // many roots (50+) from spawning too many processes at once.
  95. const mutex = (0, _throat().default)(5);
  96. const findGitRoot = dir => mutex(() => _git.default.getRoot(dir));
  97. const findHgRoot = dir => mutex(() => _hg.default.getRoot(dir));
  98. const getChangedFilesForRoots =
  99. /*#__PURE__*/
  100. (function() {
  101. var _ref = _asyncToGenerator(function*(roots, options) {
  102. const repos = yield findRepos(roots);
  103. const changedFilesOptions = _objectSpread(
  104. {
  105. includePaths: roots
  106. },
  107. options
  108. );
  109. const gitPromises = Array.from(repos.git).map(repo =>
  110. _git.default.findChangedFiles(repo, changedFilesOptions)
  111. );
  112. const hgPromises = Array.from(repos.hg).map(repo =>
  113. _hg.default.findChangedFiles(repo, changedFilesOptions)
  114. );
  115. const changedFiles = (yield Promise.all(
  116. gitPromises.concat(hgPromises)
  117. )).reduce((allFiles, changedFilesInTheRepo) => {
  118. var _iteratorNormalCompletion = true;
  119. var _didIteratorError = false;
  120. var _iteratorError = undefined;
  121. try {
  122. for (
  123. var _iterator = changedFilesInTheRepo[Symbol.iterator](), _step;
  124. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
  125. _iteratorNormalCompletion = true
  126. ) {
  127. const file = _step.value;
  128. allFiles.add(file);
  129. }
  130. } catch (err) {
  131. _didIteratorError = true;
  132. _iteratorError = err;
  133. } finally {
  134. try {
  135. if (!_iteratorNormalCompletion && _iterator.return != null) {
  136. _iterator.return();
  137. }
  138. } finally {
  139. if (_didIteratorError) {
  140. throw _iteratorError;
  141. }
  142. }
  143. }
  144. return allFiles;
  145. }, new Set());
  146. return {
  147. changedFiles,
  148. repos
  149. };
  150. });
  151. return function getChangedFilesForRoots(_x, _x2) {
  152. return _ref.apply(this, arguments);
  153. };
  154. })();
  155. exports.getChangedFilesForRoots = getChangedFilesForRoots;
  156. const findRepos =
  157. /*#__PURE__*/
  158. (function() {
  159. var _ref2 = _asyncToGenerator(function*(roots) {
  160. const gitRepos = yield Promise.all(
  161. roots.reduce((promises, root) => promises.concat(findGitRoot(root)), [])
  162. );
  163. const hgRepos = yield Promise.all(
  164. roots.reduce((promises, root) => promises.concat(findHgRoot(root)), [])
  165. );
  166. return {
  167. git: new Set(gitRepos.filter(notEmpty)),
  168. hg: new Set(hgRepos.filter(notEmpty))
  169. };
  170. });
  171. return function findRepos(_x3) {
  172. return _ref2.apply(this, arguments);
  173. };
  174. })();
  175. exports.findRepos = findRepos;