watchman.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. 'use strict';
  2. var _slicedToArray = (function() {
  3. function sliceIterator(arr, i) {
  4. var _arr = [];
  5. var _n = true;
  6. var _d = false;
  7. var _e = undefined;
  8. try {
  9. for (
  10. var _i = arr[Symbol.iterator](), _s;
  11. !(_n = (_s = _i.next()).done);
  12. _n = true
  13. ) {
  14. _arr.push(_s.value);
  15. if (i && _arr.length === i) break;
  16. }
  17. } catch (err) {
  18. _d = true;
  19. _e = err;
  20. } finally {
  21. try {
  22. if (!_n && _i['return']) _i['return']();
  23. } finally {
  24. if (_d) throw _e;
  25. }
  26. }
  27. return _arr;
  28. }
  29. return function(arr, i) {
  30. if (Array.isArray(arr)) {
  31. return arr;
  32. } else if (Symbol.iterator in Object(arr)) {
  33. return sliceIterator(arr, i);
  34. } else {
  35. throw new TypeError(
  36. 'Invalid attempt to destructure non-iterable instance'
  37. );
  38. }
  39. };
  40. })();
  41. var _normalize_path_sep;
  42. function _load_normalize_path_sep() {
  43. return (_normalize_path_sep = _interopRequireDefault(
  44. require('../lib/normalize_path_sep')
  45. ));
  46. }
  47. var _path;
  48. function _load_path() {
  49. return (_path = _interopRequireDefault(require('path')));
  50. }
  51. var _fbWatchman;
  52. function _load_fbWatchman() {
  53. return (_fbWatchman = _interopRequireDefault(require('fb-watchman')));
  54. }
  55. var _constants;
  56. function _load_constants() {
  57. return (_constants = _interopRequireDefault(require('../constants')));
  58. }
  59. function _interopRequireDefault(obj) {
  60. return obj && obj.__esModule ? obj : {default: obj};
  61. }
  62. function _toConsumableArray(arr) {
  63. if (Array.isArray(arr)) {
  64. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++)
  65. arr2[i] = arr[i];
  66. return arr2;
  67. } else {
  68. return Array.from(arr);
  69. }
  70. }
  71. function _asyncToGenerator(fn) {
  72. return function() {
  73. var gen = fn.apply(this, arguments);
  74. return new Promise(function(resolve, reject) {
  75. function step(key, arg) {
  76. try {
  77. var info = gen[key](arg);
  78. var value = info.value;
  79. } catch (error) {
  80. reject(error);
  81. return;
  82. }
  83. if (info.done) {
  84. resolve(value);
  85. } else {
  86. return Promise.resolve(value).then(
  87. function(value) {
  88. step('next', value);
  89. },
  90. function(err) {
  91. step('throw', err);
  92. }
  93. );
  94. }
  95. }
  96. return step('next');
  97. });
  98. };
  99. }
  100. /**
  101. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  102. *
  103. * This source code is licensed under the MIT license found in the
  104. * LICENSE file in the root directory of this source tree.
  105. *
  106. *
  107. */
  108. const watchmanURL =
  109. 'https://facebook.github.io/watchman/docs/troubleshooting.html';
  110. function WatchmanError(error) {
  111. error.message =
  112. `Watchman error: ${error.message.trim()}. Make sure watchman ` +
  113. `is running for this project. See ${watchmanURL}.`;
  114. return error;
  115. }
  116. module.exports = (() => {
  117. var _ref = _asyncToGenerator(function*(options) {
  118. let getWatchmanRoots = (() => {
  119. var _ref3 = _asyncToGenerator(function*(roots) {
  120. const watchmanRoots = new Map();
  121. yield Promise.all(
  122. roots.map(
  123. (() => {
  124. var _ref4 = _asyncToGenerator(function*(root) {
  125. const response = yield cmd('watch-project', root);
  126. const existing = watchmanRoots.get(response.watch);
  127. // A root can only be filtered if it was never seen with a
  128. // relative_path before.
  129. const canBeFiltered = !existing || existing.length > 0;
  130. if (canBeFiltered) {
  131. if (response.relative_path) {
  132. watchmanRoots.set(
  133. response.watch,
  134. (existing || []).concat(response.relative_path)
  135. );
  136. } else {
  137. // Make the filter directories an empty array to signal that this
  138. // root was already seen and needs to be watched for all files or
  139. // directories.
  140. watchmanRoots.set(response.watch, []);
  141. }
  142. }
  143. });
  144. return function(_x3) {
  145. return _ref4.apply(this, arguments);
  146. };
  147. })()
  148. )
  149. );
  150. return watchmanRoots;
  151. });
  152. return function getWatchmanRoots(_x2) {
  153. return _ref3.apply(this, arguments);
  154. };
  155. })();
  156. let queryWatchmanForDirs = (() => {
  157. var _ref5 = _asyncToGenerator(function*(rootProjectDirMappings) {
  158. const files = new Map();
  159. let isFresh = false;
  160. yield Promise.all(
  161. Array.from(rootProjectDirMappings).map(
  162. (() => {
  163. var _ref7 = _asyncToGenerator(function*(_ref6) {
  164. var _ref8 = _slicedToArray(_ref6, 2);
  165. let root = _ref8[0],
  166. directoryFilters = _ref8[1];
  167. const expression = Array.from(defaultWatchExpression);
  168. const glob = [];
  169. if (directoryFilters.length > 0) {
  170. expression.push(
  171. ['anyof'].concat(
  172. _toConsumableArray(
  173. directoryFilters.map(function(dir) {
  174. return ['dirname', dir];
  175. })
  176. )
  177. )
  178. );
  179. for (const directory of directoryFilters) {
  180. for (const extension of extensions) {
  181. glob.push(`${directory}/**/*.${extension}`);
  182. }
  183. }
  184. } else {
  185. for (const extension of extensions) {
  186. glob.push(`**/*.${extension}`);
  187. }
  188. }
  189. const query = clocks[root] // Use the `since` generator if we have a clock available
  190. ? {
  191. expression: expression,
  192. fields: fields,
  193. since: clocks[root]
  194. } // Otherwise use the `glob` filter
  195. : {expression: expression, fields: fields, glob: glob};
  196. const response = yield cmd('query', root, query);
  197. if ('warning' in response) {
  198. console.warn('watchman warning: ', response.warning);
  199. }
  200. isFresh = isFresh || response.is_fresh_instance;
  201. files.set(root, response);
  202. });
  203. return function(_x5) {
  204. return _ref7.apply(this, arguments);
  205. };
  206. })()
  207. )
  208. );
  209. return {
  210. files: files,
  211. isFresh: isFresh
  212. };
  213. });
  214. return function queryWatchmanForDirs(_x4) {
  215. return _ref5.apply(this, arguments);
  216. };
  217. })();
  218. const fields = ['name', 'exists', 'mtime_ms'];
  219. const data = options.data,
  220. extensions = options.extensions,
  221. ignore = options.ignore,
  222. roots = options.roots;
  223. const defaultWatchExpression = [
  224. 'allof',
  225. ['type', 'f'],
  226. ['anyof'].concat(
  227. extensions.map(function(extension) {
  228. return ['suffix', extension];
  229. })
  230. )
  231. ];
  232. const clocks = data.clocks;
  233. const client = new (_fbWatchman || _load_fbWatchman()).default.Client();
  234. let clientError;
  235. client.on('error', function(error) {
  236. return (clientError = WatchmanError(error));
  237. });
  238. const cmd = function() {
  239. for (
  240. var _len = arguments.length, args = Array(_len), _key = 0;
  241. _key < _len;
  242. _key++
  243. ) {
  244. args[_key] = arguments[_key];
  245. }
  246. return new Promise(function(resolve, reject) {
  247. return client.command(args, function(error, result) {
  248. return error ? reject(WatchmanError(error)) : resolve(result);
  249. });
  250. });
  251. };
  252. if (options.computeSha1) {
  253. var _ref2 = yield cmd('list-capabilities');
  254. const capabilities = _ref2.capabilities;
  255. if (capabilities.indexOf('field-content.sha1hex') !== -1) {
  256. fields.push('content.sha1hex');
  257. }
  258. }
  259. let files = data.files;
  260. let watchmanFiles;
  261. try {
  262. const watchmanRoots = yield getWatchmanRoots(roots);
  263. const watchmanFileResults = yield queryWatchmanForDirs(watchmanRoots);
  264. // Reset the file map if watchman was restarted and sends us a list of
  265. // files.
  266. if (watchmanFileResults.isFresh) {
  267. files = Object.create(null);
  268. }
  269. watchmanFiles = watchmanFileResults.files;
  270. } finally {
  271. client.end();
  272. }
  273. if (clientError) {
  274. throw clientError;
  275. }
  276. for (const _ref9 of watchmanFiles) {
  277. var _ref10 = _slicedToArray(_ref9, 2);
  278. const watchRoot = _ref10[0];
  279. const response = _ref10[1];
  280. const fsRoot = (0,
  281. (_normalize_path_sep || _load_normalize_path_sep()).default)(watchRoot);
  282. clocks[fsRoot] = response.clock;
  283. for (const fileData of response.files) {
  284. const name =
  285. fsRoot +
  286. (_path || _load_path()).default.sep +
  287. (0, (_normalize_path_sep || _load_normalize_path_sep()).default)(
  288. fileData.name
  289. );
  290. if (!fileData.exists) {
  291. delete files[name];
  292. } else if (!ignore(name)) {
  293. const mtime =
  294. typeof fileData.mtime_ms === 'number'
  295. ? fileData.mtime_ms
  296. : fileData.mtime_ms.toNumber();
  297. const existingFileData = data.files[name];
  298. const isOld =
  299. existingFileData &&
  300. existingFileData[
  301. (_constants || _load_constants()).default.MTIME
  302. ] === mtime;
  303. if (isOld) {
  304. files[name] = existingFileData;
  305. } else {
  306. let sha1hex = fileData['content.sha1hex'];
  307. if (typeof sha1hex !== 'string' || sha1hex.length !== 40) {
  308. sha1hex = null;
  309. }
  310. // See ../constants.js
  311. files[name] = ['', mtime, 0, [], sha1hex];
  312. }
  313. }
  314. }
  315. }
  316. data.files = files;
  317. return data;
  318. });
  319. function watchmanCrawl(_x) {
  320. return _ref.apply(this, arguments);
  321. }
  322. return watchmanCrawl;
  323. })();