From e9353fc63477f57acd4ff077b3030055e707e40b Mon Sep 17 00:00:00 2001 From: em Date: Sat, 25 Aug 2018 17:30:17 -0700 Subject: [PATCH 1/5] Make watchman-processor librar-ish. --- bin/watchman-processor | 16 ++++++++++++- rollup.config.js | 3 ++- src/WatchmanProcessor.ts | 41 +++++++++++++++++----------------- src/index.ts | 8 +++++-- src/ioc.bindings.ts | 1 + src/ioc.config.ts | 2 ++ test/WatchmanProcessor-test.ts | 34 ++++++++++++++++------------ 7 files changed, 67 insertions(+), 38 deletions(-) diff --git a/bin/watchman-processor b/bin/watchman-processor index 02aeb7c..cd6e4f1 100755 --- a/bin/watchman-processor +++ b/bin/watchman-processor @@ -1,8 +1,22 @@ #!/usr/bin/env node process.title = 'watchman-processor'; -var watchman = require('../index'); + +var index = require('../index'); +var watchman = index.processor; +var terminal = index.terminal; + if (watchman && typeof watchman.start === 'function') { + watchman.emitter.on('error', function (params) { + terminal.error(params.err); + }); + watchman.emitter.on('debug', function (params) { + terminal.debug(params.msg); + }); + watchman.emitter.on('setState', function (params) { + terminal.setState(params.configEntry, params.state, params.statusMessage); + }); + watchman.emitter.on('render', terminal.render.bind(terminal)); watchman.start() } process.on('SIGINT', function() { diff --git a/rollup.config.js b/rollup.config.js index 9b607e0..eadc9d4 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -12,6 +12,7 @@ module.exports = { external: [ 'chai', 'child_process', + 'events', 'fb-watchman', 'fs', 'inversify', @@ -26,4 +27,4 @@ module.exports = { typescript: require('typescript') }), ] -}; \ No newline at end of file +}; diff --git a/src/WatchmanProcessor.ts b/src/WatchmanProcessor.ts index 0eebe49..816f1b1 100644 --- a/src/WatchmanProcessor.ts +++ b/src/WatchmanProcessor.ts @@ -1,7 +1,8 @@ +import { EventEmitter } from 'events'; import { Client, SubscriptionResponse, SubscriptionResponseFile } from 'fb-watchman'; import { inject, injectable } from 'inversify'; import { resolve as resolvePath } from 'path'; -import { Config, SubConfig, Sync, Terminal, WatchmanExpression, WatchmanProcessor } from '../interfaces'; +import { Config, SubConfig, Sync, WatchmanExpression, WatchmanProcessor } from '../interfaces'; import { Bindings } from './ioc.bindings'; @injectable() @@ -11,16 +12,16 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { private config: Config, @inject(Bindings.WatchmanClient) private client: Client, - @inject(Bindings.Terminal) - private terminal: Terminal, + @inject(Bindings.Emitter) + private emitter: EventEmitter, @inject(Bindings.Sync) private sync: Sync, ) { } public start(): void { - const { client, terminal } = this; + const { client, emitter } = this; - terminal.debug('watchman: initialize'); + emitter.emit('debug', {msg: 'watchman: initialize'}); const onCapabilityCheck = this.onCapabilityCheck.bind(this); client.capabilityCheck({}, onCapabilityCheck); } @@ -46,12 +47,12 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { } private onCapabilityCheck(error?: string | Error): void { - const terminal = this.terminal; + const emitter = this.emitter; if (error) { - terminal.error(error); + emitter.emit('error', {err: error }); return; } - terminal.render(); + emitter.emit('render'); const client = this.client; const onSubscription = this.onSubscription.bind(this); @@ -68,8 +69,8 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { promises.push(this.subscribe(resolvePath(sub.source), name, expression)); } - const render = terminal.render.bind(terminal); - const errHandler = terminal.error.bind(terminal); + const render = emitter.emit.bind(emitter, 'rednder'); + const errHandler = emitter.emit.bind(emitter, 'error'); Promise.all(promises).then(render).catch(errHandler); // subscription is fired regardless of which subscriber fired it @@ -86,21 +87,21 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { } private syncFiles(subConfig: SubConfig, files: SubscriptionResponseFile[]): void { - const {sync, terminal } = this; - terminal.setState(subConfig, 'running'); + const {sync, emitter } = this; + emitter.emit('setState', {configEntry: subConfig, state: 'running' }); const fileNames = (files || []).map(file => file.name); sync.syncFiles(subConfig, fileNames) .then(() => { - terminal.setState(subConfig, 'good'); + emitter.emit('setState', {configEntry: subConfig, state: 'good' }); }) .catch(err => { - terminal.setState(subConfig, 'error', err); + emitter.emit('setState', {configEntry: subConfig, state: 'error', statusMessage: err}); }); } private subscribe(folder: string, name: string, expression: WatchmanExpression): Promise { - const terminal = this.terminal; + const emitter = this.emitter; const client = this.client; const sub = { expression, @@ -108,7 +109,7 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { relative_root: '', }; - terminal.debug(`subscribe: ${name}`); + emitter.emit('debug', {msg: `subscribe: ${name}` }); return new Promise((resolve, reject) => { client.command(['subscribe', folder, name, sub], (error: string) => { @@ -118,20 +119,20 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { } private unsubscribe(folder: string, name: string): Promise { - const terminal = this.terminal; + const emitter = this.emitter; const client = this.client; - terminal.debug(`unsubscribe: ${name}`); + emitter.emit('debug', {msg: `unsubscribe: ${name}` }); return new Promise(resolve => { client.command(['unsubscribe', folder, name], resolve); }); } private shutdown(): Promise { - const terminal = this.terminal; + const emitter = this.emitter; const client = this.client; - terminal.debug(`watchman: shutdown`); + emitter.emit('debug', {msg: `watchman: shutdown` }); return new Promise(resolve => { client.command(['shutdown-server'], resolve); }); diff --git a/src/index.ts b/src/index.ts index c90f571..aeaf458 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,13 @@ import 'reflect-metadata'; -import { Cli, ConfigManager, WatchmanProcessor } from '../interfaces'; +import { Cli, ConfigManager, Terminal, WatchmanProcessor } from '../interfaces'; import { Bindings } from './ioc.bindings'; import { container } from './ioc.config'; const cli = container.get(Bindings.Cli); const configManager = container.get(Bindings.ConfigManager); const watchmanProcessor = container.get(Bindings.WatchmanProcessor); +const terminal = container.get(Bindings.Terminal); const args = cli.getArguments(); let processor = watchmanProcessor; @@ -28,4 +29,7 @@ if (args.init) { } } -export default processor; +export default { + processor, + terminal, +}; diff --git a/src/ioc.bindings.ts b/src/ioc.bindings.ts index 52f9ab0..c2e09bd 100644 --- a/src/ioc.bindings.ts +++ b/src/ioc.bindings.ts @@ -2,6 +2,7 @@ export const Bindings = { Cli: Symbol('Cli'), Config: Symbol('Config'), ConfigManager: Symbol('ConfigManager'), + Emitter: Symbol('Emitter'), Process: Symbol('Process'), Require: Symbol('require'), Spawn: Symbol('spawn'), diff --git a/src/ioc.config.ts b/src/ioc.config.ts index 724b39b..c93aba8 100644 --- a/src/ioc.config.ts +++ b/src/ioc.config.ts @@ -1,4 +1,5 @@ import { spawn } from 'child_process'; +import { EventEmitter } from 'events'; import { Client } from 'fb-watchman'; import { Container } from 'inversify'; import * as interfaces from '../interfaces'; @@ -16,6 +17,7 @@ container.bind(Bindings.Process).toConstantValue(process); container.bind(Bindings.Spawn).toConstantValue(spawn); container.bind(Bindings.Require).toConstantValue(require); container.bind(Bindings.WatchmanClient).toConstantValue(new Client()); +container.bind(Bindings.Emitter).toConstantValue(new EventEmitter()); // setup the main classes container.bind(Bindings.Cli).to(CliImpl); diff --git a/test/WatchmanProcessor-test.ts b/test/WatchmanProcessor-test.ts index 1390d51..a00111c 100644 --- a/test/WatchmanProcessor-test.ts +++ b/test/WatchmanProcessor-test.ts @@ -1,15 +1,15 @@ import * as chai from 'chai'; +import { EventEmitter } from 'events'; import { Client } from 'fb-watchman'; import 'reflect-metadata'; import * as sinon from 'sinon'; import 'ts-helpers'; import { Config } from '../interfaces'; import { SyncImpl as Sync } from '../src/Sync'; -import { TerminalImpl as Terminal } from '../src/Terminal'; import { WatchmanProcessorImpl as Watchman } from '../src/WatchmanProcessor'; -const mockTerminal = sinon.mock(Terminal); -const terminal: Terminal = mockTerminal as any; +const mockEventEmitter = sinon.mock(EventEmitter); +const emitter = mockEventEmitter as any; const mockSync = { end: sinon.stub(), syncFiles: sinon.stub(), @@ -38,10 +38,7 @@ describe('Watchman', () => { beforeEach(() => { // mock all the defaults before - terminal.render = sinon.stub(); - terminal.error = sinon.stub(); - terminal.debug = sinon.stub(); - terminal.setState = sinon.stub(); + emitter.emit = sinon.spy(); mockSync.syncFiles = sinon.stub().returns(new Promise(resolve => resolve())); mockWatchmanClient.capabilityCheck = sinon.stub().callsArg(1); @@ -50,7 +47,7 @@ describe('Watchman', () => { }); it('should start watchman', () => { - const watchman = new Watchman(config, watchmanClient, terminal, sync); + const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.start(); chai.assert.isObject(watchman, 'watchman is an object'); @@ -59,25 +56,32 @@ describe('Watchman', () => { it('should log errors from watchman.capabilityCheck', () => { mockWatchmanClient.capabilityCheck.callsArgWith(1, 'error'); - const watchman = new Watchman(config, watchmanClient, terminal, sync); + const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.start(); + chai.assert.deepEqual(emitter.emit.getCall(0).args, ['debug', { msg: 'watchman: initialize' }]); + chai.assert.deepEqual(emitter.emit.getCall(1).args, ['error', { err: 'error' }]); chai.assert.isObject(watchman, 'watchman is an object'); }); it('should log errors from watchman.command', () => { mockWatchmanClient.command.callsArgWith(1, 'error'); - const watchman = new Watchman(config, watchmanClient, terminal, sync); + const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.start(); + chai.assert.deepEqual(emitter.emit.getCall(0).args, ['debug', { msg: 'watchman: initialize' }]); + chai.assert.deepEqual(emitter.emit.getCall(1).args, ['render']); + chai.assert.deepEqual(emitter.emit.getCall(2).args, ['debug', { msg: 'subscribe: example1' }]); + chai.assert.deepEqual(emitter.emit.getCall(3).args, ['setState', + { configEntry: config.subscriptions.example1, state: 'running' }]); chai.assert.isObject(watchman, 'watchman is an object'); }); it('should log errors from sync.syncFiles', () => { mockSync.syncFiles.returns(new Promise(() => { throw new Error('error'); })); - const watchman = new Watchman(config, watchmanClient, terminal, sync); + const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.start(); chai.assert.isObject(watchman, 'watchman is an object'); @@ -85,7 +89,7 @@ describe('Watchman', () => { it('should attempt to sync files', () => { mockWatchmanClient.on = sinon.stub().callsArgWith(1, {subscription: 'example1'}); - const watchman = new Watchman(config, watchmanClient, terminal, sync); + const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.start(); chai.assert.isObject(watchman, 'watchman is an object'); @@ -93,14 +97,16 @@ describe('Watchman', () => { it('should end', () => { mockWatchmanClient.end = sinon.stub(); - const watchman = new Watchman(config, watchmanClient, terminal, sync); + const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.end(); + + chai.assert.deepEqual(emitter.emit.getCall(0).args, ['debug', { msg: 'unsubscribe: example1' }]); }); it('should end and shutdown', () => { mockWatchmanClient.end = sinon.stub(); const newConfig = { controlWatchman: true, subscriptions: {} } as any; - const watchman = new Watchman(newConfig, watchmanClient, terminal, sync); + const watchman = new Watchman(newConfig, watchmanClient, emitter, sync); watchman.end(); }); From 2d62220e780c57a31fcf30cf94299a04d7479116 Mon Sep 17 00:00:00 2001 From: em Date: Sun, 26 Aug 2018 09:43:52 -0700 Subject: [PATCH 2/5] Add subscription param to setState event. --- src/WatchmanProcessor.ts | 10 +++++----- test/WatchmanProcessor-test.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/WatchmanProcessor.ts b/src/WatchmanProcessor.ts index 816f1b1..04c8fe5 100644 --- a/src/WatchmanProcessor.ts +++ b/src/WatchmanProcessor.ts @@ -83,20 +83,20 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { const files = resp.files; const subConfig = config.subscriptions[subscription]; - this.syncFiles(subConfig, files); + this.syncFiles(subConfig, files, subscription); } - private syncFiles(subConfig: SubConfig, files: SubscriptionResponseFile[]): void { + private syncFiles(subConfig: SubConfig, files: SubscriptionResponseFile[], subscription: string): void { const {sync, emitter } = this; - emitter.emit('setState', {configEntry: subConfig, state: 'running' }); + emitter.emit('setState', {subscription, configEntry: subConfig, state: 'running' }); const fileNames = (files || []).map(file => file.name); sync.syncFiles(subConfig, fileNames) .then(() => { - emitter.emit('setState', {configEntry: subConfig, state: 'good' }); + emitter.emit('setState', {subscription, configEntry: subConfig, state: 'good' }); }) .catch(err => { - emitter.emit('setState', {configEntry: subConfig, state: 'error', statusMessage: err}); + emitter.emit('setState', {subscription, configEntry: subConfig, state: 'error', statusMessage: err}); }); } diff --git a/test/WatchmanProcessor-test.ts b/test/WatchmanProcessor-test.ts index a00111c..4cc9e98 100644 --- a/test/WatchmanProcessor-test.ts +++ b/test/WatchmanProcessor-test.ts @@ -74,7 +74,7 @@ describe('Watchman', () => { chai.assert.deepEqual(emitter.emit.getCall(1).args, ['render']); chai.assert.deepEqual(emitter.emit.getCall(2).args, ['debug', { msg: 'subscribe: example1' }]); chai.assert.deepEqual(emitter.emit.getCall(3).args, ['setState', - { configEntry: config.subscriptions.example1, state: 'running' }]); + { subscription: 'example1', configEntry: config.subscriptions.example1, state: 'running' }]); chai.assert.isObject(watchman, 'watchman is an object'); }); From 30b63a4677adbb88797965fb036b7bc9a50fc8ba Mon Sep 17 00:00:00 2001 From: em Date: Sun, 26 Aug 2018 11:07:02 -0700 Subject: [PATCH 3/5] Expose configuration. --- src/index.ts | 1 + test/WatchmanProcessor-test.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index aeaf458..e1edac7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ if (args.init) { } export default { + config: configManager.getConfig(), processor, terminal, }; diff --git a/test/WatchmanProcessor-test.ts b/test/WatchmanProcessor-test.ts index 4cc9e98..d949e79 100644 --- a/test/WatchmanProcessor-test.ts +++ b/test/WatchmanProcessor-test.ts @@ -74,7 +74,7 @@ describe('Watchman', () => { chai.assert.deepEqual(emitter.emit.getCall(1).args, ['render']); chai.assert.deepEqual(emitter.emit.getCall(2).args, ['debug', { msg: 'subscribe: example1' }]); chai.assert.deepEqual(emitter.emit.getCall(3).args, ['setState', - { subscription: 'example1', configEntry: config.subscriptions.example1, state: 'running' }]); + { configEntry: config.subscriptions.example1, state: 'running', subscription: 'example1' }]); chai.assert.isObject(watchman, 'watchman is an object'); }); From f7d93260e63a2f9bda57240d9f4fab08e498b2eb Mon Sep 17 00:00:00 2001 From: em Date: Sun, 26 Aug 2018 11:46:37 -0700 Subject: [PATCH 4/5] Include subscription name in failure. --- src/WatchmanProcessor.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WatchmanProcessor.ts b/src/WatchmanProcessor.ts index 04c8fe5..4028a0b 100644 --- a/src/WatchmanProcessor.ts +++ b/src/WatchmanProcessor.ts @@ -69,7 +69,7 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { promises.push(this.subscribe(resolvePath(sub.source), name, expression)); } - const render = emitter.emit.bind(emitter, 'rednder'); + const render = emitter.emit.bind(emitter, 'render'); const errHandler = emitter.emit.bind(emitter, 'error'); Promise.all(promises).then(render).catch(errHandler); @@ -113,7 +113,7 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { return new Promise((resolve, reject) => { client.command(['subscribe', folder, name, sub], (error: string) => { - error ? reject('failed to start: ' + error) : resolve(); + error ? reject({err: 'failed to start: ' + error, subscription: name}) : resolve(); }); }); } From 874258e6483b82eadadd4477b4e0e7f94d388c68 Mon Sep 17 00:00:00 2001 From: em Date: Mon, 27 Aug 2018 23:11:45 -0700 Subject: [PATCH 5/5] Use enum instead of plain strings for event names. --- bin/watchman-processor | 9 +++++---- src/WatchmanProcessor.ts | 25 ++++++++++++++----------- src/WatchmanProcessorEvent.ts | 8 ++++++++ src/index.ts | 2 ++ test/WatchmanProcessor-test.ts | 23 +++++++++++++++-------- 5 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 src/WatchmanProcessorEvent.ts diff --git a/bin/watchman-processor b/bin/watchman-processor index cd6e4f1..d346317 100755 --- a/bin/watchman-processor +++ b/bin/watchman-processor @@ -5,18 +5,19 @@ process.title = 'watchman-processor'; var index = require('../index'); var watchman = index.processor; var terminal = index.terminal; +var WatchmanProcessorEvent = index.WatchmanProcessorEvent; if (watchman && typeof watchman.start === 'function') { - watchman.emitter.on('error', function (params) { + watchman.emitter.on(WatchmanProcessorEvent.Error, function (params) { terminal.error(params.err); }); - watchman.emitter.on('debug', function (params) { + watchman.emitter.on(WatchmanProcessorEvent.Debug, function (params) { terminal.debug(params.msg); }); - watchman.emitter.on('setState', function (params) { + watchman.emitter.on(WatchmanProcessorEvent.SetState, function (params) { terminal.setState(params.configEntry, params.state, params.statusMessage); }); - watchman.emitter.on('render', terminal.render.bind(terminal)); + watchman.emitter.on(WatchmanProcessorEvent.Render, terminal.render.bind(terminal)); watchman.start() } process.on('SIGINT', function() { diff --git a/src/WatchmanProcessor.ts b/src/WatchmanProcessor.ts index 4028a0b..ffff4f9 100644 --- a/src/WatchmanProcessor.ts +++ b/src/WatchmanProcessor.ts @@ -4,6 +4,7 @@ import { inject, injectable } from 'inversify'; import { resolve as resolvePath } from 'path'; import { Config, SubConfig, Sync, WatchmanExpression, WatchmanProcessor } from '../interfaces'; import { Bindings } from './ioc.bindings'; +import { WatchmanProcessorEvent } from './WatchmanProcessorEvent'; @injectable() export class WatchmanProcessorImpl implements WatchmanProcessor { @@ -21,7 +22,7 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { public start(): void { const { client, emitter } = this; - emitter.emit('debug', {msg: 'watchman: initialize'}); + emitter.emit(WatchmanProcessorEvent.Debug, {msg: 'watchman: initialize'}); const onCapabilityCheck = this.onCapabilityCheck.bind(this); client.capabilityCheck({}, onCapabilityCheck); } @@ -49,10 +50,10 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { private onCapabilityCheck(error?: string | Error): void { const emitter = this.emitter; if (error) { - emitter.emit('error', {err: error }); + emitter.emit(WatchmanProcessorEvent.Error, {err: error }); return; } - emitter.emit('render'); + emitter.emit(WatchmanProcessorEvent.Render); const client = this.client; const onSubscription = this.onSubscription.bind(this); @@ -69,8 +70,8 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { promises.push(this.subscribe(resolvePath(sub.source), name, expression)); } - const render = emitter.emit.bind(emitter, 'render'); - const errHandler = emitter.emit.bind(emitter, 'error'); + const render = emitter.emit.bind(emitter, WatchmanProcessorEvent.Render); + const errHandler = emitter.emit.bind(emitter, WatchmanProcessorEvent.Error); Promise.all(promises).then(render).catch(errHandler); // subscription is fired regardless of which subscriber fired it @@ -88,15 +89,17 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { private syncFiles(subConfig: SubConfig, files: SubscriptionResponseFile[], subscription: string): void { const {sync, emitter } = this; - emitter.emit('setState', {subscription, configEntry: subConfig, state: 'running' }); + emitter.emit(WatchmanProcessorEvent.SetState, {subscription, configEntry: subConfig, state: 'running' }); const fileNames = (files || []).map(file => file.name); sync.syncFiles(subConfig, fileNames) .then(() => { - emitter.emit('setState', {subscription, configEntry: subConfig, state: 'good' }); + emitter.emit(WatchmanProcessorEvent.SetState, {subscription, configEntry: subConfig, state: 'good' }); }) .catch(err => { - emitter.emit('setState', {subscription, configEntry: subConfig, state: 'error', statusMessage: err}); + emitter.emit( + WatchmanProcessorEvent.SetState, + {subscription, configEntry: subConfig, state: WatchmanProcessorEvent.Error, statusMessage: err}); }); } @@ -109,7 +112,7 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { relative_root: '', }; - emitter.emit('debug', {msg: `subscribe: ${name}` }); + emitter.emit(WatchmanProcessorEvent.Debug, {msg: `subscribe: ${name}` }); return new Promise((resolve, reject) => { client.command(['subscribe', folder, name, sub], (error: string) => { @@ -122,7 +125,7 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { const emitter = this.emitter; const client = this.client; - emitter.emit('debug', {msg: `unsubscribe: ${name}` }); + emitter.emit(WatchmanProcessorEvent.Debug, {msg: `unsubscribe: ${name}` }); return new Promise(resolve => { client.command(['unsubscribe', folder, name], resolve); }); @@ -132,7 +135,7 @@ export class WatchmanProcessorImpl implements WatchmanProcessor { const emitter = this.emitter; const client = this.client; - emitter.emit('debug', {msg: `watchman: shutdown` }); + emitter.emit(WatchmanProcessorEvent.Debug, {msg: `watchman: shutdown` }); return new Promise(resolve => { client.command(['shutdown-server'], resolve); }); diff --git a/src/WatchmanProcessorEvent.ts b/src/WatchmanProcessorEvent.ts new file mode 100644 index 0000000..122433b --- /dev/null +++ b/src/WatchmanProcessorEvent.ts @@ -0,0 +1,8 @@ +enum WatchmanProcessorEvent { + Debug = 'debug', + Error = 'error', + Render = 'render', + SetState = 'setState', +} + +export {WatchmanProcessorEvent}; diff --git a/src/index.ts b/src/index.ts index e1edac7..87778bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import 'reflect-metadata'; import { Cli, ConfigManager, Terminal, WatchmanProcessor } from '../interfaces'; import { Bindings } from './ioc.bindings'; import { container } from './ioc.config'; +import { WatchmanProcessorEvent } from './WatchmanProcessorEvent'; const cli = container.get(Bindings.Cli); const configManager = container.get(Bindings.ConfigManager); @@ -30,6 +31,7 @@ if (args.init) { } export default { + WatchmanProcessorEvent, config: configManager.getConfig(), processor, terminal, diff --git a/test/WatchmanProcessor-test.ts b/test/WatchmanProcessor-test.ts index d949e79..5bfb81e 100644 --- a/test/WatchmanProcessor-test.ts +++ b/test/WatchmanProcessor-test.ts @@ -7,6 +7,7 @@ import 'ts-helpers'; import { Config } from '../interfaces'; import { SyncImpl as Sync } from '../src/Sync'; import { WatchmanProcessorImpl as Watchman } from '../src/WatchmanProcessor'; +import { WatchmanProcessorEvent } from '../src/WatchmanProcessorEvent'; const mockEventEmitter = sinon.mock(EventEmitter); const emitter = mockEventEmitter as any; @@ -54,13 +55,15 @@ describe('Watchman', () => { }); it('should log errors from watchman.capabilityCheck', () => { - mockWatchmanClient.capabilityCheck.callsArgWith(1, 'error'); + mockWatchmanClient.capabilityCheck.callsArgWith(1, WatchmanProcessorEvent.Error); const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.start(); - chai.assert.deepEqual(emitter.emit.getCall(0).args, ['debug', { msg: 'watchman: initialize' }]); - chai.assert.deepEqual(emitter.emit.getCall(1).args, ['error', { err: 'error' }]); + chai.assert.deepEqual( + emitter.emit.getCall(0).args, + [WatchmanProcessorEvent.Debug, { msg: 'watchman: initialize' }]); + chai.assert.deepEqual(emitter.emit.getCall(1).args, [WatchmanProcessorEvent.Error, { err: 'error' }]); chai.assert.isObject(watchman, 'watchman is an object'); }); @@ -70,10 +73,12 @@ describe('Watchman', () => { const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.start(); - chai.assert.deepEqual(emitter.emit.getCall(0).args, ['debug', { msg: 'watchman: initialize' }]); - chai.assert.deepEqual(emitter.emit.getCall(1).args, ['render']); - chai.assert.deepEqual(emitter.emit.getCall(2).args, ['debug', { msg: 'subscribe: example1' }]); - chai.assert.deepEqual(emitter.emit.getCall(3).args, ['setState', + chai.assert.deepEqual( + emitter.emit.getCall(0).args, + [WatchmanProcessorEvent.Debug, { msg: 'watchman: initialize' }]); + chai.assert.deepEqual(emitter.emit.getCall(1).args, [WatchmanProcessorEvent.Render]); + chai.assert.deepEqual(emitter.emit.getCall(2).args, [WatchmanProcessorEvent.Debug, { msg: 'subscribe: example1' }]); + chai.assert.deepEqual(emitter.emit.getCall(3).args, [WatchmanProcessorEvent.SetState, { configEntry: config.subscriptions.example1, state: 'running', subscription: 'example1' }]); chai.assert.isObject(watchman, 'watchman is an object'); }); @@ -100,7 +105,9 @@ describe('Watchman', () => { const watchman = new Watchman(config, watchmanClient, emitter, sync); watchman.end(); - chai.assert.deepEqual(emitter.emit.getCall(0).args, ['debug', { msg: 'unsubscribe: example1' }]); + chai.assert.deepEqual( + emitter.emit.getCall(0).args, + [WatchmanProcessorEvent.Debug, { msg: 'unsubscribe: example1' }]); }); it('should end and shutdown', () => {