123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.default = WatchmanWatcher;
- var _fs;
- function _load_fs() {
- return (_fs = _interopRequireDefault(require('fs')));
- }
- var _path;
- function _load_path() {
- return (_path = _interopRequireDefault(require('path')));
- }
- var _assert;
- function _load_assert() {
- return (_assert = _interopRequireDefault(require('assert')));
- }
- var _common;
- function _load_common() {
- return (_common = _interopRequireDefault(require('sane/src/common')));
- }
- var _fbWatchman;
- function _load_fbWatchman() {
- return (_fbWatchman = _interopRequireDefault(require('fb-watchman')));
- }
- var _events;
- function _load_events() {
- return (_events = require('events'));
- }
- var _recrawlWarningDedupe;
- function _load_recrawlWarningDedupe() {
- return (_recrawlWarningDedupe = _interopRequireDefault(
- require('sane/src/utils/recrawl-warning-dedupe')
- ));
- }
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {default: obj};
- }
- const CHANGE_EVENT = (_common || _load_common()).default.CHANGE_EVENT;
- /**
- * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- */
- const DELETE_EVENT = (_common || _load_common()).default.DELETE_EVENT;
- const ADD_EVENT = (_common || _load_common()).default.ADD_EVENT;
- const ALL_EVENT = (_common || _load_common()).default.ALL_EVENT;
- const SUB_NAME = 'sane-sub';
- /**
- * Watches `dir`.
- *
- * @class PollWatcher
- * @param String dir
- * @param {Object} opts
- * @public
- */
- function WatchmanWatcher(dir, opts) {
- (_common || _load_common()).default.assignOptions(this, opts);
- this.root = (_path || _load_path()).default.resolve(dir);
- this.init();
- }
- // eslint-disable-next-line no-proto
- WatchmanWatcher.prototype.__proto__ = (
- _events || _load_events()
- ).EventEmitter.prototype;
- /**
- * Run the watchman `watch` command on the root and subscribe to changes.
- *
- * @private
- */
- WatchmanWatcher.prototype.init = function() {
- if (this.client) {
- this.client.removeAllListeners();
- }
- const self = this;
- this.client = new (_fbWatchman || _load_fbWatchman()).default.Client();
- this.client.on('error', error => {
- self.emit('error', error);
- });
- this.client.on('subscription', this.handleChangeEvent.bind(this));
- this.client.on('end', () => {
- console.warn('[sane] Warning: Lost connection to watchman, reconnecting..');
- self.init();
- });
- this.watchProjectInfo = null;
- function getWatchRoot() {
- return self.watchProjectInfo ? self.watchProjectInfo.root : self.root;
- }
- function onCapability(error, resp) {
- if (handleError(self, error)) {
- // The Watchman watcher is unusable on this system, we cannot continue
- return;
- }
- handleWarning(resp);
- self.capabilities = resp.capabilities;
- if (self.capabilities.relative_root) {
- self.client.command(['watch-project', getWatchRoot()], onWatchProject);
- } else {
- self.client.command(['watch', getWatchRoot()], onWatch);
- }
- }
- function onWatchProject(error, resp) {
- if (handleError(self, error)) {
- return;
- }
- handleWarning(resp);
- self.watchProjectInfo = {
- relativePath: resp.relative_path ? resp.relative_path : '',
- root: resp.watch
- };
- self.client.command(['clock', getWatchRoot()], onClock);
- }
- function onWatch(error, resp) {
- if (handleError(self, error)) {
- return;
- }
- handleWarning(resp);
- self.client.command(['clock', getWatchRoot()], onClock);
- }
- function onClock(error, resp) {
- if (handleError(self, error)) {
- return;
- }
- handleWarning(resp);
- const options = {
- fields: ['name', 'exists', 'new'],
- since: resp.clock
- };
- // If the server has the wildmatch capability available it supports
- // the recursive **/*.foo style match and we can offload our globs
- // to the watchman server. This saves both on data size to be
- // communicated back to us and compute for evaluating the globs
- // in our node process.
- if (self.capabilities.wildmatch) {
- if (self.globs.length === 0) {
- if (!self.dot) {
- // Make sure we honor the dot option if even we're not using globs.
- options.expression = [
- 'match',
- '**',
- 'wholename',
- {
- includedotfiles: false
- }
- ];
- }
- } else {
- options.expression = ['anyof'];
- for (const i in self.globs) {
- options.expression.push([
- 'match',
- self.globs[i],
- 'wholename',
- {
- includedotfiles: self.dot
- }
- ]);
- }
- }
- }
- if (self.capabilities.relative_root) {
- options.relative_root = self.watchProjectInfo.relativePath;
- }
- self.client.command(
- ['subscribe', getWatchRoot(), SUB_NAME, options],
- onSubscribe
- );
- }
- function onSubscribe(error, resp) {
- if (handleError(self, error)) {
- return;
- }
- handleWarning(resp);
- self.emit('ready');
- }
- self.client.capabilityCheck(
- {
- optional: ['wildmatch', 'relative_root']
- },
- onCapability
- );
- };
- /**
- * Handles a change event coming from the subscription.
- *
- * @param {Object} resp
- * @private
- */
- WatchmanWatcher.prototype.handleChangeEvent = function(resp) {
- (_assert || _load_assert()).default.equal(
- resp.subscription,
- SUB_NAME,
- 'Invalid subscription event.'
- );
- if (resp.is_fresh_instance) {
- this.emit('fresh_instance');
- }
- if (resp.is_fresh_instance) {
- this.emit('fresh_instance');
- }
- if (Array.isArray(resp.files)) {
- resp.files.forEach(this.handleFileChange, this);
- }
- };
- /**
- * Handles a single change event record.
- *
- * @param {Object} changeDescriptor
- * @private
- */
- WatchmanWatcher.prototype.handleFileChange = function(changeDescriptor) {
- const self = this;
- let absPath;
- let relativePath;
- if (this.capabilities.relative_root) {
- relativePath = changeDescriptor.name;
- absPath = (_path || _load_path()).default.join(
- this.watchProjectInfo.root,
- this.watchProjectInfo.relativePath,
- relativePath
- );
- } else {
- absPath = (_path || _load_path()).default.join(
- this.root,
- changeDescriptor.name
- );
- relativePath = changeDescriptor.name;
- }
- if (
- !(self.capabilities.wildmatch && !this.hasIgnore) &&
- !(_common || _load_common()).default.isFileIncluded(
- this.globs,
- this.dot,
- this.doIgnore,
- relativePath
- )
- ) {
- return;
- }
- if (!changeDescriptor.exists) {
- self.emitEvent(DELETE_EVENT, relativePath, self.root);
- } else {
- (_fs || _load_fs()).default.lstat(absPath, (error, stat) => {
- // Files can be deleted between the event and the lstat call
- // the most reliable thing to do here is to ignore the event.
- if (error && error.code === 'ENOENT') {
- return;
- }
- if (handleError(self, error)) {
- return;
- }
- const eventType = changeDescriptor.new ? ADD_EVENT : CHANGE_EVENT;
- // Change event on dirs are mostly useless.
- if (!(eventType === CHANGE_EVENT && stat.isDirectory())) {
- self.emitEvent(eventType, relativePath, self.root, stat);
- }
- });
- }
- };
- /**
- * Dispatches the event.
- *
- * @param {string} eventType
- * @param {string} filepath
- * @param {string} root
- * @param {fs.Stat} stat
- * @private
- */
- WatchmanWatcher.prototype.emitEvent = function(
- eventType,
- filepath,
- root,
- stat
- ) {
- this.emit(eventType, filepath, root, stat);
- this.emit(ALL_EVENT, eventType, filepath, root, stat);
- };
- /**
- * Closes the watcher.
- *
- * @param {function} callback
- * @private
- */
- WatchmanWatcher.prototype.close = function(callback) {
- this.client.removeAllListeners();
- this.client.end();
- callback && callback(null, true);
- };
- /**
- * Handles an error and returns true if exists.
- *
- * @param {WatchmanWatcher} self
- * @param {Error} error
- * @private
- */
- function handleError(self, error) {
- if (error != null) {
- self.emit('error', error);
- return true;
- } else {
- return false;
- }
- }
- /**
- * Handles a warning in the watchman resp object.
- *
- * @param {object} resp
- * @private
- */
- function handleWarning(resp) {
- if ('warning' in resp) {
- if (
- (
- _recrawlWarningDedupe || _load_recrawlWarningDedupe()
- ).default.isRecrawlWarningDupe(resp.warning)
- ) {
- return true;
- }
- console.warn(resp.warning);
- return true;
- } else {
- return false;
- }
- }
|