Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WatchmanWatcher.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = WatchmanWatcher;
  6. function _assert() {
  7. const data = _interopRequireDefault(require('assert'));
  8. _assert = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _events() {
  14. const data = require('events');
  15. _events = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _path() {
  21. const data = _interopRequireDefault(require('path'));
  22. _path = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _fbWatchman() {
  28. const data = _interopRequireDefault(require('fb-watchman'));
  29. _fbWatchman = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function fs() {
  35. const data = _interopRequireWildcard(require('graceful-fs'));
  36. fs = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. var _RecrawlWarning = _interopRequireDefault(require('./RecrawlWarning'));
  42. var _common = _interopRequireDefault(require('./common'));
  43. function _getRequireWildcardCache(nodeInterop) {
  44. if (typeof WeakMap !== 'function') return null;
  45. var cacheBabelInterop = new WeakMap();
  46. var cacheNodeInterop = new WeakMap();
  47. return (_getRequireWildcardCache = function (nodeInterop) {
  48. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  49. })(nodeInterop);
  50. }
  51. function _interopRequireWildcard(obj, nodeInterop) {
  52. if (!nodeInterop && obj && obj.__esModule) {
  53. return obj;
  54. }
  55. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  56. return {default: obj};
  57. }
  58. var cache = _getRequireWildcardCache(nodeInterop);
  59. if (cache && cache.has(obj)) {
  60. return cache.get(obj);
  61. }
  62. var newObj = {};
  63. var hasPropertyDescriptor =
  64. Object.defineProperty && Object.getOwnPropertyDescriptor;
  65. for (var key in obj) {
  66. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  67. var desc = hasPropertyDescriptor
  68. ? Object.getOwnPropertyDescriptor(obj, key)
  69. : null;
  70. if (desc && (desc.get || desc.set)) {
  71. Object.defineProperty(newObj, key, desc);
  72. } else {
  73. newObj[key] = obj[key];
  74. }
  75. }
  76. }
  77. newObj.default = obj;
  78. if (cache) {
  79. cache.set(obj, newObj);
  80. }
  81. return newObj;
  82. }
  83. function _interopRequireDefault(obj) {
  84. return obj && obj.__esModule ? obj : {default: obj};
  85. }
  86. /**
  87. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  88. *
  89. * This source code is licensed under the MIT license found in the
  90. * LICENSE file in the root directory of this source tree.
  91. */
  92. const CHANGE_EVENT = _common.default.CHANGE_EVENT;
  93. const DELETE_EVENT = _common.default.DELETE_EVENT;
  94. const ADD_EVENT = _common.default.ADD_EVENT;
  95. const ALL_EVENT = _common.default.ALL_EVENT;
  96. const SUB_NAME = 'sane-sub';
  97. /**
  98. * Watches `dir`.
  99. *
  100. * @class PollWatcher
  101. * @param String dir
  102. * @param {Object} opts
  103. * @public
  104. */
  105. function WatchmanWatcher(dir, opts) {
  106. _common.default.assignOptions(this, opts);
  107. this.root = _path().default.resolve(dir);
  108. this.init();
  109. } // eslint-disable-next-line no-proto
  110. WatchmanWatcher.prototype.__proto__ = _events().EventEmitter.prototype;
  111. /**
  112. * Run the watchman `watch` command on the root and subscribe to changes.
  113. *
  114. * @private
  115. */
  116. WatchmanWatcher.prototype.init = function () {
  117. if (this.client) {
  118. this.client.removeAllListeners();
  119. }
  120. const self = this;
  121. this.client = new (_fbWatchman().default.Client)();
  122. this.client.on('error', error => {
  123. self.emit('error', error);
  124. });
  125. this.client.on('subscription', this.handleChangeEvent.bind(this));
  126. this.client.on('end', () => {
  127. console.warn('[sane] Warning: Lost connection to watchman, reconnecting..');
  128. self.init();
  129. });
  130. this.watchProjectInfo = null;
  131. function getWatchRoot() {
  132. return self.watchProjectInfo ? self.watchProjectInfo.root : self.root;
  133. }
  134. function onCapability(error, resp) {
  135. if (handleError(self, error)) {
  136. // The Watchman watcher is unusable on this system, we cannot continue
  137. return;
  138. }
  139. handleWarning(resp);
  140. self.capabilities = resp.capabilities;
  141. if (self.capabilities.relative_root) {
  142. self.client.command(['watch-project', getWatchRoot()], onWatchProject);
  143. } else {
  144. self.client.command(['watch', getWatchRoot()], onWatch);
  145. }
  146. }
  147. function onWatchProject(error, resp) {
  148. if (handleError(self, error)) {
  149. return;
  150. }
  151. handleWarning(resp);
  152. self.watchProjectInfo = {
  153. relativePath: resp.relative_path ? resp.relative_path : '',
  154. root: resp.watch
  155. };
  156. self.client.command(['clock', getWatchRoot()], onClock);
  157. }
  158. function onWatch(error, resp) {
  159. if (handleError(self, error)) {
  160. return;
  161. }
  162. handleWarning(resp);
  163. self.client.command(['clock', getWatchRoot()], onClock);
  164. }
  165. function onClock(error, resp) {
  166. if (handleError(self, error)) {
  167. return;
  168. }
  169. handleWarning(resp);
  170. const options = {
  171. fields: ['name', 'exists', 'new'],
  172. since: resp.clock
  173. }; // If the server has the wildmatch capability available it supports
  174. // the recursive **/*.foo style match and we can offload our globs
  175. // to the watchman server. This saves both on data size to be
  176. // communicated back to us and compute for evaluating the globs
  177. // in our node process.
  178. if (self.capabilities.wildmatch) {
  179. if (self.globs.length === 0) {
  180. if (!self.dot) {
  181. // Make sure we honor the dot option if even we're not using globs.
  182. options.expression = [
  183. 'match',
  184. '**',
  185. 'wholename',
  186. {
  187. includedotfiles: false
  188. }
  189. ];
  190. }
  191. } else {
  192. options.expression = ['anyof'];
  193. for (const i in self.globs) {
  194. options.expression.push([
  195. 'match',
  196. self.globs[i],
  197. 'wholename',
  198. {
  199. includedotfiles: self.dot
  200. }
  201. ]);
  202. }
  203. }
  204. }
  205. if (self.capabilities.relative_root) {
  206. options.relative_root = self.watchProjectInfo.relativePath;
  207. }
  208. self.client.command(
  209. ['subscribe', getWatchRoot(), SUB_NAME, options],
  210. onSubscribe
  211. );
  212. }
  213. function onSubscribe(error, resp) {
  214. if (handleError(self, error)) {
  215. return;
  216. }
  217. handleWarning(resp);
  218. self.emit('ready');
  219. }
  220. self.client.capabilityCheck(
  221. {
  222. optional: ['wildmatch', 'relative_root']
  223. },
  224. onCapability
  225. );
  226. };
  227. /**
  228. * Handles a change event coming from the subscription.
  229. *
  230. * @param {Object} resp
  231. * @private
  232. */
  233. WatchmanWatcher.prototype.handleChangeEvent = function (resp) {
  234. _assert().default.equal(
  235. resp.subscription,
  236. SUB_NAME,
  237. 'Invalid subscription event.'
  238. );
  239. if (resp.is_fresh_instance) {
  240. this.emit('fresh_instance');
  241. }
  242. if (resp.is_fresh_instance) {
  243. this.emit('fresh_instance');
  244. }
  245. if (Array.isArray(resp.files)) {
  246. resp.files.forEach(this.handleFileChange, this);
  247. }
  248. };
  249. /**
  250. * Handles a single change event record.
  251. *
  252. * @param {Object} changeDescriptor
  253. * @private
  254. */
  255. WatchmanWatcher.prototype.handleFileChange = function (changeDescriptor) {
  256. const self = this;
  257. let absPath;
  258. let relativePath;
  259. if (this.capabilities.relative_root) {
  260. relativePath = changeDescriptor.name;
  261. absPath = _path().default.join(
  262. this.watchProjectInfo.root,
  263. this.watchProjectInfo.relativePath,
  264. relativePath
  265. );
  266. } else {
  267. absPath = _path().default.join(this.root, changeDescriptor.name);
  268. relativePath = changeDescriptor.name;
  269. }
  270. if (
  271. !(self.capabilities.wildmatch && !this.hasIgnore) &&
  272. !_common.default.isFileIncluded(
  273. this.globs,
  274. this.dot,
  275. this.doIgnore,
  276. relativePath
  277. )
  278. ) {
  279. return;
  280. }
  281. if (!changeDescriptor.exists) {
  282. self.emitEvent(DELETE_EVENT, relativePath, self.root);
  283. } else {
  284. fs().lstat(absPath, (error, stat) => {
  285. // Files can be deleted between the event and the lstat call
  286. // the most reliable thing to do here is to ignore the event.
  287. if (error && error.code === 'ENOENT') {
  288. return;
  289. }
  290. if (handleError(self, error)) {
  291. return;
  292. }
  293. const eventType = changeDescriptor.new ? ADD_EVENT : CHANGE_EVENT; // Change event on dirs are mostly useless.
  294. if (!(eventType === CHANGE_EVENT && stat.isDirectory())) {
  295. self.emitEvent(eventType, relativePath, self.root, stat);
  296. }
  297. });
  298. }
  299. };
  300. /**
  301. * Dispatches the event.
  302. *
  303. * @param {string} eventType
  304. * @param {string} filepath
  305. * @param {string} root
  306. * @param {fs.Stat} stat
  307. * @private
  308. */
  309. WatchmanWatcher.prototype.emitEvent = function (
  310. eventType,
  311. filepath,
  312. root,
  313. stat
  314. ) {
  315. this.emit(eventType, filepath, root, stat);
  316. this.emit(ALL_EVENT, eventType, filepath, root, stat);
  317. };
  318. /**
  319. * Closes the watcher.
  320. *
  321. */
  322. WatchmanWatcher.prototype.close = function () {
  323. this.client.removeAllListeners();
  324. this.client.end();
  325. return Promise.resolve();
  326. };
  327. /**
  328. * Handles an error and returns true if exists.
  329. *
  330. * @param {WatchmanWatcher} self
  331. * @param {Error} error
  332. * @private
  333. */
  334. function handleError(self, error) {
  335. if (error != null) {
  336. self.emit('error', error);
  337. return true;
  338. } else {
  339. return false;
  340. }
  341. }
  342. /**
  343. * Handles a warning in the watchman resp object.
  344. *
  345. * @param {object} resp
  346. * @private
  347. */
  348. function handleWarning(resp) {
  349. if ('warning' in resp) {
  350. if (_RecrawlWarning.default.isRecrawlWarningDupe(resp.warning)) {
  351. return true;
  352. }
  353. console.warn(resp.warning);
  354. return true;
  355. } else {
  356. return false;
  357. }
  358. }