From 537598d705b2391d0dfe0b46fe9072d4a62f6ac2 Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Mon, 14 Dec 2015 11:12:54 -0800 Subject: [PATCH] Move FileWatcher into node-haste Reviewed By: davidaurelio Differential Revision: D2752711 fb-gh-sync-id: e656a3019b95c7677d5b27e74dc921ef62ba5c83 --- .../FileWatcher/__mocks__/sane.js | 2 +- .../FileWatcher/__tests__/FileWatcher-test.js | 23 +++++++++++++------ .../FileWatcher/index.js | 8 ++----- .../src/Server/__tests__/Server-test.js | 2 +- packager/react-packager/src/Server/index.js | 2 +- 5 files changed, 21 insertions(+), 16 deletions(-) rename packager/react-packager/src/{ => DependencyResolver}/FileWatcher/__mocks__/sane.js (98%) rename packager/react-packager/src/{ => DependencyResolver}/FileWatcher/__tests__/FileWatcher-test.js (80%) rename packager/react-packager/src/{ => DependencyResolver}/FileWatcher/index.js (94%) diff --git a/packager/react-packager/src/FileWatcher/__mocks__/sane.js b/packager/react-packager/src/DependencyResolver/FileWatcher/__mocks__/sane.js similarity index 98% rename from packager/react-packager/src/FileWatcher/__mocks__/sane.js rename to packager/react-packager/src/DependencyResolver/FileWatcher/__mocks__/sane.js index 9823a930d..2a36bb39d 100644 --- a/packager/react-packager/src/FileWatcher/__mocks__/sane.js +++ b/packager/react-packager/src/DependencyResolver/FileWatcher/__mocks__/sane.js @@ -9,5 +9,5 @@ 'use strict'; module.exports = { - WatchmanWatcher: jest.genMockFromModule('sane/src/watchman_watcher') + WatchmanWatcher: jest.genMockFromModule('sane/src/watchman_watcher'), }; diff --git a/packager/react-packager/src/FileWatcher/__tests__/FileWatcher-test.js b/packager/react-packager/src/DependencyResolver/FileWatcher/__tests__/FileWatcher-test.js similarity index 80% rename from packager/react-packager/src/FileWatcher/__tests__/FileWatcher-test.js rename to packager/react-packager/src/DependencyResolver/FileWatcher/__tests__/FileWatcher-test.js index 973c4f780..b0d2fd090 100644 --- a/packager/react-packager/src/FileWatcher/__tests__/FileWatcher-test.js +++ b/packager/react-packager/src/DependencyResolver/FileWatcher/__tests__/FileWatcher-test.js @@ -12,28 +12,37 @@ jest .dontMock('util') .dontMock('events') .dontMock('../') - .dontMock('q') .setMock('child_process', { exec: function(cmd, cb) { cb(null, '/usr/bin/watchman'); - } + }, }); -var FileWatcher = require('../'); var sane = require('sane'); describe('FileWatcher', function() { var Watcher; + var FileWatcher; + var config; beforeEach(function() { Watcher = sane.WatchmanWatcher; Watcher.prototype.once.mockImplementation(function(type, callback) { callback(); }); + FileWatcher = require('../'); + + config = [{ + dir: 'rootDir', + globs: [ + '**/*.js', + '**/*.json', + ], + }]; }); pit('it should get the watcher instance when ready', function() { - var fileWatcher = new FileWatcher(['rootDir']); + var fileWatcher = new FileWatcher(config); return fileWatcher.getWatchers().then(function(watchers) { watchers.forEach(function(watcher) { expect(watcher instanceof Watcher).toBe(true); @@ -46,10 +55,10 @@ describe('FileWatcher', function() { Watcher.prototype.on.mockImplementation(function(type, callback) { cb = callback; }); - var fileWatcher = new FileWatcher(['rootDir']); + var fileWatcher = new FileWatcher(config); var handler = jest.genMockFn(); fileWatcher.on('all', handler); - return fileWatcher.getWatchers().then(function(){ + return fileWatcher.getWatchers().then(function() { cb(1, 2, 3, 4); jest.runAllTimers(); expect(handler.mock.calls[0]).toEqual([1, 2, 3, 4]); @@ -57,7 +66,7 @@ describe('FileWatcher', function() { }); pit('it should end the watcher', function() { - var fileWatcher = new FileWatcher(['rootDir']); + var fileWatcher = new FileWatcher(config); Watcher.prototype.close.mockImplementation(function(callback) { callback(); }); diff --git a/packager/react-packager/src/FileWatcher/index.js b/packager/react-packager/src/DependencyResolver/FileWatcher/index.js similarity index 94% rename from packager/react-packager/src/FileWatcher/index.js rename to packager/react-packager/src/DependencyResolver/FileWatcher/index.js index e32c36963..227687e1c 100644 --- a/packager/react-packager/src/FileWatcher/index.js +++ b/packager/react-packager/src/DependencyResolver/FileWatcher/index.js @@ -12,11 +12,10 @@ const EventEmitter = require('events').EventEmitter; const sane = require('sane'); const Promise = require('promise'); const exec = require('child_process').exec; -const _ = require('underscore'); const MAX_WAIT_TIME = 25000; -// TODO(amasad): can we use watchman version command instead?r +// TODO(amasad): can we use watchman version command instead? const detectingWatcherClass = new Promise(function(resolve) { exec('which watchman', function(err, out) { if (err || out.length === 0) { @@ -81,13 +80,10 @@ class FileWatcher extends EventEmitter { } static createDummyWatcher() { - const ev = new EventEmitter(); - _.extend(ev, { + return Object.assign(new EventEmitter(), { isWatchman: () => Promise.resolve(false), end: () => Promise.resolve(), }); - - return ev; } } diff --git a/packager/react-packager/src/Server/__tests__/Server-test.js b/packager/react-packager/src/Server/__tests__/Server-test.js index b8fee5fad..2fdb5e738 100644 --- a/packager/react-packager/src/Server/__tests__/Server-test.js +++ b/packager/react-packager/src/Server/__tests__/Server-test.js @@ -19,7 +19,7 @@ jest.setMock('worker-farm', function() { return () => {}; }) const Promise = require('promise'); var Bundler = require('../../Bundler'); -var FileWatcher = require('../../FileWatcher'); +var FileWatcher = require('../../DependencyResolver/FileWatcher'); var Server = require('../'); var Server = require('../../Server'); var AssetServer = require('../../AssetServer'); diff --git a/packager/react-packager/src/Server/index.js b/packager/react-packager/src/Server/index.js index b2ca6d73b..121dcddb8 100644 --- a/packager/react-packager/src/Server/index.js +++ b/packager/react-packager/src/Server/index.js @@ -10,7 +10,7 @@ const Activity = require('../Activity'); const AssetServer = require('../AssetServer'); -const FileWatcher = require('../FileWatcher'); +const FileWatcher = require('../DependencyResolver/FileWatcher'); const getPlatformExtension = require('../DependencyResolver/lib/getPlatformExtension'); const Bundler = require('../Bundler'); const Promise = require('promise');