9
9
10
10
'use strict' ;
11
11
12
- var net = require ( 'net ' ) ;
13
- var EE = require ( 'events' ) . EventEmitter ;
14
- var childProcess = require ( 'child_process ' ) ;
15
- var bser = require ( 'bser ' ) ;
12
+ const bser = require ( 'bser ' ) ;
13
+ const childProcess = require ( 'child_process' ) ;
14
+ const { EventEmitter } = require ( 'events ' ) ;
15
+ const net = require ( 'net ' ) ;
16
16
17
17
// We'll emit the responses to these when they get sent down to us
18
- var unilateralTags = [ 'subscription' , 'log' ] ;
18
+ const unilateralTags = [ 'subscription' , 'log' ] ;
19
19
20
20
/**
21
21
* @param options An object with the following optional keys:
22
22
* * 'watchmanBinaryPath' (string) Absolute path to the watchman binary.
23
23
* If not provided, the Client locates the binary using the PATH specified
24
24
* by the node child_process's default env.
25
25
*/
26
- class Client extends EE {
26
+ class Client extends EventEmitter {
27
27
constructor ( options ) {
28
28
super ( ) ;
29
- const self = this ;
30
29
31
30
this . watchmanBinaryPath = 'watchman' ;
32
31
if ( options && options . watchmanBinaryPath ) {
@@ -52,11 +51,11 @@ class Client extends EE {
52
51
}
53
52
54
53
cancelCommands ( why ) {
55
- var error = new Error ( why ) ;
54
+ const error = new Error ( why ) ;
56
55
57
56
// Steal all pending commands before we start cancellation, in
58
57
// case something decides to schedule more commands
59
- var cmds = this . commands ;
58
+ const cmds = this . commands ;
60
59
this . commands = [ ] ;
61
60
62
61
if ( this . currentCommand ) {
@@ -65,37 +64,35 @@ class Client extends EE {
65
64
}
66
65
67
66
// Synthesize an error condition for any commands that were queued
68
- cmds . forEach ( function ( cmd ) {
67
+ cmds . forEach ( cmd => {
69
68
cmd . cb ( error ) ;
70
69
} ) ;
71
70
}
72
71
73
72
connect ( ) {
74
- var self = this ;
75
-
76
- function makeSock ( sockname ) {
73
+ const makeSock = sockname => {
77
74
// bunser will decode the watchman BSER protocol for us
78
- self . bunser = new bser . BunserBuf ( ) ;
75
+ this . bunser = new bser . BunserBuf ( ) ;
79
76
// For each decoded line:
80
- self . bunser . on ( 'value' , function ( obj ) {
77
+ this . bunser . on ( 'value' , obj => {
81
78
// Figure out if this is a unliteral response or if it is the
82
79
// response portion of a request-response sequence. At the time
83
80
// of writing, there are only two possible unilateral responses.
84
- var unilateral = false ;
85
- for ( var i = 0 ; i < unilateralTags . length ; i ++ ) {
86
- var tag = unilateralTags [ i ] ;
81
+ let unilateral = false ;
82
+ for ( let i = 0 ; i < unilateralTags . length ; i ++ ) {
83
+ const tag = unilateralTags [ i ] ;
87
84
if ( tag in obj ) {
88
85
unilateral = tag ;
89
86
}
90
87
}
91
88
92
89
if ( unilateral ) {
93
- self . emit ( unilateral , obj ) ;
94
- } else if ( self . currentCommand ) {
95
- var cmd = self . currentCommand ;
96
- self . currentCommand = null ;
90
+ this . emit ( unilateral , obj ) ;
91
+ } else if ( this . currentCommand ) {
92
+ const cmd = this . currentCommand ;
93
+ this . currentCommand = null ;
97
94
if ( 'error' in obj ) {
98
- var error = new Error ( obj . error ) ;
95
+ const error = new Error ( obj . error ) ;
99
96
error . watchmanResponse = obj ;
100
97
cmd . cb ( error ) ;
101
98
} else {
@@ -104,34 +101,34 @@ class Client extends EE {
104
101
}
105
102
106
103
// See if we can dispatch the next queued command, if any
107
- self . sendNextCommand ( ) ;
104
+ this . sendNextCommand ( ) ;
108
105
} ) ;
109
- self . bunser . on ( 'error' , function ( err ) {
110
- self . emit ( 'error' , err ) ;
106
+ this . bunser . on ( 'error' , err => {
107
+ this . emit ( 'error' , err ) ;
111
108
} ) ;
112
109
113
- self . socket = net . createConnection ( sockname ) ;
114
- self . socket . on ( 'connect' , function ( ) {
115
- self . connecting = false ;
116
- self . emit ( 'connect' ) ;
117
- self . sendNextCommand ( ) ;
110
+ this . socket = net . createConnection ( sockname ) ;
111
+ this . socket . on ( 'connect' , ( ) => {
112
+ this . connecting = false ;
113
+ this . emit ( 'connect' ) ;
114
+ this . sendNextCommand ( ) ;
118
115
} ) ;
119
- self . socket . on ( 'error' , function ( err ) {
120
- self . connecting = false ;
121
- self . emit ( 'error' , err ) ;
116
+ this . socket . on ( 'error' , err => {
117
+ this . connecting = false ;
118
+ this . emit ( 'error' , err ) ;
122
119
} ) ;
123
- self . socket . on ( 'data' , function ( buf ) {
124
- if ( self . bunser ) {
125
- self . bunser . append ( buf ) ;
120
+ this . socket . on ( 'data' , buf => {
121
+ if ( this . bunser ) {
122
+ this . bunser . append ( buf ) ;
126
123
}
127
124
} ) ;
128
- self . socket . on ( 'end' , function ( ) {
129
- self . socket = null ;
130
- self . bunser = null ;
131
- self . cancelCommands ( 'The watchman connection was closed' ) ;
132
- self . emit ( 'end' ) ;
125
+ this . socket . on ( 'end' , ( ) => {
126
+ this . socket = null ;
127
+ this . bunser = null ;
128
+ this . cancelCommands ( 'The watchman connection was closed' ) ;
129
+ this . emit ( 'end' ) ;
133
130
} ) ;
134
- }
131
+ } ;
135
132
136
133
// triggers will export the sock path to the environment.
137
134
// If we're invoked in such a way, we can simply pick up the
@@ -145,16 +142,16 @@ class Client extends EE {
145
142
// We need to ask the client binary where to find it.
146
143
// This will cause the service to start for us if it isn't
147
144
// already running.
148
- var args = [ '--no-pretty' , 'get-sockname' ] ;
145
+ const args = [ '--no-pretty' , 'get-sockname' ] ;
149
146
150
147
// We use the more elaborate spawn rather than exec because there
151
148
// are some error cases on Windows where process spawning can hang.
152
149
// It is desirable to pipe stderr directly to stderr live so that
153
150
// we can discover the problem.
154
- var proc = null ;
155
- var spawnFailed = false ;
151
+ let proc = null ;
152
+ let spawnFailed = false ;
156
153
157
- function spawnError ( error ) {
154
+ const spawnError = error => {
158
155
if ( spawnFailed ) {
159
156
// For ENOENT, proc 'close' will also trigger with a negative code,
160
157
// let's suppress that second error.
@@ -172,8 +169,8 @@ class Client extends EE {
172
169
'for installation instructions' ;
173
170
}
174
171
console . error ( 'Watchman: ' , error . message ) ;
175
- self . emit ( 'error' , error ) ;
176
- }
172
+ this . emit ( 'error' , error ) ;
173
+ } ;
177
174
178
175
try {
179
176
proc = childProcess . spawn ( this . watchmanBinaryPath , args , {
@@ -185,25 +182,25 @@ class Client extends EE {
185
182
return ;
186
183
}
187
184
188
- var stdout = [ ] ;
189
- var stderr = [ ] ;
190
- proc . stdout . on ( 'data' , function ( data ) {
185
+ const stdout = [ ] ;
186
+ const stderr = [ ] ;
187
+ proc . stdout . on ( 'data' , data => {
191
188
stdout . push ( data ) ;
192
189
} ) ;
193
- proc . stderr . on ( 'data' , function ( data ) {
190
+ proc . stderr . on ( 'data' , data => {
194
191
data = data . toString ( 'utf8' ) ;
195
192
stderr . push ( data ) ;
196
193
console . error ( data ) ;
197
194
} ) ;
198
- proc . on ( 'error' , function ( error ) {
195
+ proc . on ( 'error' , error => {
199
196
spawnError ( error ) ;
200
197
} ) ;
201
198
202
- proc . on ( 'close' , function ( code , signal ) {
199
+ proc . on ( 'close' , ( code , signal ) => {
203
200
if ( code !== 0 ) {
204
201
spawnError (
205
202
new Error (
206
- self . watchmanBinaryPath +
203
+ this . watchmanBinaryPath +
207
204
' ' +
208
205
args . join ( ' ' ) +
209
206
' returned with exit code=' +
@@ -217,23 +214,21 @@ class Client extends EE {
217
214
return ;
218
215
}
219
216
try {
220
- var obj = JSON . parse ( stdout . join ( '' ) ) ;
217
+ const obj = JSON . parse ( stdout . join ( '' ) ) ;
221
218
if ( 'error' in obj ) {
222
- var error = new Error ( obj . error ) ;
219
+ const error = new Error ( obj . error ) ;
223
220
error . watchmanResponse = obj ;
224
- self . emit ( 'error' , error ) ;
221
+ this . emit ( 'error' , error ) ;
225
222
return ;
226
223
}
227
224
makeSock ( obj . sockname ) ;
228
225
} catch ( e ) {
229
- self . emit ( 'error' , e ) ;
226
+ this . emit ( 'error' , e ) ;
230
227
}
231
228
} ) ;
232
229
}
233
230
234
- command ( args , done ) {
235
- done = done || function ( ) { } ;
236
-
231
+ command ( args , done = ( ) => { } ) {
237
232
// Queue up the command
238
233
this . commands . push ( { cmd : args , cb : done } ) ;
239
234
@@ -254,12 +249,12 @@ class Client extends EE {
254
249
// This is a helper that we expose for testing purposes
255
250
_synthesizeCapabilityCheck ( resp , optional , required ) {
256
251
resp . capabilities = { } ;
257
- var version = resp . version ;
258
- optional . forEach ( function ( name ) {
252
+ const version = resp . version ;
253
+ optional . forEach ( name => {
259
254
resp . capabilities [ name ] = have_cap ( version , name ) ;
260
255
} ) ;
261
- required . forEach ( function ( name ) {
262
- var have = have_cap ( version , name ) ;
256
+ required . forEach ( name => {
257
+ const have = have_cap ( version , name ) ;
263
258
resp . capabilities [ name ] = have ;
264
259
if ( ! have ) {
265
260
resp . error =
@@ -272,9 +267,8 @@ class Client extends EE {
272
267
}
273
268
274
269
capabilityCheck ( caps , done ) {
275
- var optional = caps . optional || [ ] ;
276
- var required = caps . required || [ ] ;
277
- var self = this ;
270
+ const optional = caps . optional || [ ] ;
271
+ const required = caps . required || [ ] ;
278
272
this . command (
279
273
[
280
274
'version' ,
@@ -283,15 +277,15 @@ class Client extends EE {
283
277
required : required ,
284
278
} ,
285
279
] ,
286
- function ( error , resp ) {
280
+ ( error , resp ) => {
287
281
if ( error ) {
288
282
done ( error ) ;
289
283
return ;
290
284
}
291
285
if ( ! ( 'capabilities' in resp ) ) {
292
286
// Server doesn't support capabilities, so we need to
293
287
// synthesize the results based on the version
294
- resp = self . _synthesizeCapabilityCheck ( resp , optional , required ) ;
288
+ resp = this . _synthesizeCapabilityCheck ( resp , optional , required ) ;
295
289
if ( resp . error ) {
296
290
error = new Error ( resp . error ) ;
297
291
error . watchmanResponse = resp ;
@@ -315,7 +309,7 @@ class Client extends EE {
315
309
}
316
310
}
317
311
318
- var cap_versions = {
312
+ const cap_versions = {
319
313
'cmd-watch-del-all' : '3.1.1' ,
320
314
'cmd-watch-project' : '3.1' ,
321
315
relative_root : '3.3' ,
@@ -328,8 +322,8 @@ var cap_versions = {
328
322
function vers_compare ( a , b ) {
329
323
a = a . split ( '.' ) ;
330
324
b = b . split ( '.' ) ;
331
- for ( var i = 0 ; i < 3 ; i ++ ) {
332
- var d = parseInt ( a [ i ] || '0' ) - parseInt ( b [ i ] || '0' ) ;
325
+ for ( let i = 0 ; i < 3 ; i ++ ) {
326
+ const d = parseInt ( a [ i ] || '0' ) - parseInt ( b [ i ] || '0' ) ;
333
327
if ( d != 0 ) {
334
328
return d ;
335
329
}
0 commit comments