watchman.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use strict';
  2. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  3. var _normalize_path_sep;
  4. function _load_normalize_path_sep() {
  5. return _normalize_path_sep = _interopRequireDefault(require('../lib/normalize_path_sep'));
  6. }
  7. var _path;
  8. function _load_path() {
  9. return _path = _interopRequireDefault(require('path'));
  10. }
  11. var _fbWatchman;
  12. function _load_fbWatchman() {
  13. return _fbWatchman = _interopRequireDefault(require('fb-watchman'));
  14. }
  15. var _constants;
  16. function _load_constants() {
  17. return _constants = _interopRequireDefault(require('../constants'));
  18. }
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
  21. function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /**
  22. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  23. *
  24. * This source code is licensed under the MIT license found in the
  25. * LICENSE file in the root directory of this source tree.
  26. *
  27. *
  28. */
  29. const watchmanURL = 'https://facebook.github.io/watchman/docs/troubleshooting.html';
  30. function WatchmanError(error) {
  31. error.message = `Watchman error: ${error.message.trim()}. Make sure watchman ` + `is running for this project. See ${watchmanURL}.`;
  32. return error;
  33. }
  34. module.exports = (() => {
  35. var _ref = _asyncToGenerator(function* (options) {
  36. let getWatchmanRoots = (() => {
  37. var _ref2 = _asyncToGenerator(function* (roots) {
  38. const watchmanRoots = new Map();
  39. yield Promise.all(roots.map((() => {
  40. var _ref3 = _asyncToGenerator(function* (root) {
  41. const response = yield cmd('watch-project', root);
  42. const existing = watchmanRoots.get(response.watch);
  43. // A root can only be filtered if it was never seen with a relative_path before
  44. const canBeFiltered = !existing || existing.length > 0;
  45. if (canBeFiltered) {
  46. if (response.relative_path) {
  47. watchmanRoots.set(response.watch, (existing || []).concat(response.relative_path));
  48. } else {
  49. // Make the filter directories an empty array to signal that this root
  50. // was already seen and needs to be watched for all files/directories
  51. watchmanRoots.set(response.watch, []);
  52. }
  53. }
  54. });
  55. return function (_x3) {
  56. return _ref3.apply(this, arguments);
  57. };
  58. })()));
  59. return watchmanRoots;
  60. });
  61. return function getWatchmanRoots(_x2) {
  62. return _ref2.apply(this, arguments);
  63. };
  64. })();
  65. let queryWatchmanForDirs = (() => {
  66. var _ref4 = _asyncToGenerator(function* (rootProjectDirMappings) {
  67. const files = new Map();
  68. let isFresh = false;
  69. yield Promise.all(Array.from(rootProjectDirMappings).map((() => {
  70. var _ref6 = _asyncToGenerator(function* (_ref5) {
  71. var _ref7 = _slicedToArray(_ref5, 2);
  72. let root = _ref7[0],
  73. directoryFilters = _ref7[1];
  74. const expression = Array.from(defaultWatchExpression);
  75. if (directoryFilters.length > 0) {
  76. expression.push(['anyof'].concat(_toConsumableArray(directoryFilters.map(function (dir) {
  77. return ['dirname', dir];
  78. }))));
  79. }
  80. const fields = ['name', 'exists', 'mtime_ms'];
  81. const query = clocks[root] ? // Use the `since` generator if we have a clock available
  82. { expression, fields, since: clocks[root] } : // Otherwise use the `suffix` generator
  83. { expression, fields, suffix: extensions };
  84. const response = yield cmd('query', root, query);
  85. if ('warning' in response) {
  86. console.warn('watchman warning: ', response.warning);
  87. }
  88. isFresh = isFresh || response.is_fresh_instance;
  89. files.set(root, response);
  90. });
  91. return function (_x5) {
  92. return _ref6.apply(this, arguments);
  93. };
  94. })()));
  95. return {
  96. files,
  97. isFresh
  98. };
  99. });
  100. return function queryWatchmanForDirs(_x4) {
  101. return _ref4.apply(this, arguments);
  102. };
  103. })();
  104. const data = options.data,
  105. extensions = options.extensions,
  106. ignore = options.ignore,
  107. roots = options.roots;
  108. const defaultWatchExpression = ['allof', ['type', 'f'], ['anyof'].concat(extensions.map(function (extension) {
  109. return ['suffix', extension];
  110. }))];
  111. const clocks = data.clocks;
  112. const client = new (_fbWatchman || _load_fbWatchman()).default.Client();
  113. let clientError;
  114. client.on('error', function (error) {
  115. return clientError = WatchmanError(error);
  116. });
  117. const cmd = function () {
  118. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  119. args[_key] = arguments[_key];
  120. }
  121. return new Promise(function (resolve, reject) {
  122. return client.command(args, function (error, result) {
  123. return error ? reject(WatchmanError(error)) : resolve(result);
  124. });
  125. });
  126. };
  127. let files = data.files;
  128. let watchmanFiles;
  129. try {
  130. const watchmanRoots = yield getWatchmanRoots(roots);
  131. const watchmanFileResults = yield queryWatchmanForDirs(watchmanRoots);
  132. // Reset the file map if watchman was restarted and sends us a list of files.
  133. if (watchmanFileResults.isFresh) {
  134. files = Object.create(null);
  135. }
  136. watchmanFiles = watchmanFileResults.files;
  137. } finally {
  138. client.end();
  139. }
  140. if (clientError) {
  141. throw clientError;
  142. }
  143. for (const _ref8 of watchmanFiles) {
  144. var _ref9 = _slicedToArray(_ref8, 2);
  145. const watchRoot = _ref9[0];
  146. const response = _ref9[1];
  147. const fsRoot = (0, (_normalize_path_sep || _load_normalize_path_sep()).default)(watchRoot);
  148. clocks[fsRoot] = response.clock;
  149. for (const fileData of response.files) {
  150. const name = fsRoot + (_path || _load_path()).default.sep + (0, (_normalize_path_sep || _load_normalize_path_sep()).default)(fileData.name);
  151. if (!fileData.exists) {
  152. delete files[name];
  153. } else if (!ignore(name)) {
  154. const mtime = typeof fileData.mtime_ms === 'number' ? fileData.mtime_ms : fileData.mtime_ms.toNumber();
  155. const existingFileData = data.files[name];
  156. const isOld = existingFileData && existingFileData[(_constants || _load_constants()).default.MTIME] === mtime;
  157. if (isOld) {
  158. files[name] = existingFileData;
  159. } else {
  160. // See ../constants.js
  161. files[name] = ['', mtime, 0, []];
  162. }
  163. }
  164. }
  165. }
  166. data.files = files;
  167. return data;
  168. });
  169. function watchmanCrawl(_x) {
  170. return _ref.apply(this, arguments);
  171. }
  172. return watchmanCrawl;
  173. })();