@@ -49,25 +49,24 @@ function Spawnify(command, options) {
4949}
5050
5151Spawnify . prototype . _onMessage = function onMessage ( command , options ) {
52- var firstChar ,
53- dir = options . cwd ,
54- isVolume = win . isChangeVolume ( command ) ,
55-
56- regExpCD = / ^ c d ? / ,
57- regExpCDWin = / ^ c d ? / i,
58-
59- isCD = regExpCD . test ( command ) ,
60- isCDWin = regExpCDWin . test ( command ) ,
61-
62- symbolsExec = [
63- '~' , '>' , '<' , '#' ,
64- '*' , '&' , '{' , '}' ,
65- '|' , '\'' , '"' , ';' ,
66- '`' , '$'
67- ] ,
68-
69- isSymbol = isContain ( command , symbolsExec ) ;
70-
52+ const dir = options . cwd ;
53+ const isVolume = win . isChangeVolume ( command ) ;
54+
55+ const regExpCD = / ^ c d ? / ;
56+ const regExpCDWin = / ^ c d ? / i;
57+
58+ const isCD = regExpCD . test ( command ) ;
59+ const isCDWin = regExpCDWin . test ( command ) ;
60+
61+ const symbolsExec = [
62+ '~' , '>' , '<' , '#' ,
63+ '*' , '&' , '{' , '}' ,
64+ '|' , '\'' , '"' , ';' ,
65+ '`' , '$'
66+ ] ;
67+
68+ const isSymbol = isContain ( command , symbolsExec ) ;
69+
7170 assert ( command , 'command could not be empty!' ) ;
7271 assert ( command , 'options could not be empty!' ) ;
7372
@@ -78,9 +77,9 @@ Spawnify.prototype._onMessage = function onMessage(command, options) {
7877 this . _onCD ( command || '~' , dir ) ;
7978 } else {
8079 if ( WIN )
81- command = 'cmd /C ' + command ;
80+ command = 'cmd /C ' + command ;
8281
83- firstChar = command [ 0 ] ;
82+ const firstChar = command [ 0 ] ;
8483
8584 if ( firstChar === ' ' || isSymbol )
8685 this . _set ( 'exec' , command , options ) ;
@@ -90,7 +89,7 @@ Spawnify.prototype._onMessage = function onMessage(command, options) {
9089} ;
9190
9291Spawnify . prototype . _emit = function emit ( event , data ) {
93- const count = ( event ) => {
92+ const count = ( event ) => {
9493 const listeners = this . listeners ( event ) ;
9594
9695 return listeners . length ;
@@ -139,7 +138,7 @@ Spawnify.prototype._set = function set(type, command, options) {
139138 args = command . split ( ' ' ) ;
140139 command = args . shift ( ) ;
141140
142- error = tryCatch ( function ( ) {
141+ error = tryCatch ( ( ) => {
143142 child = spawn ( command , args , options ) ;
144143 } ) ;
145144 break ;
@@ -232,40 +231,35 @@ Spawnify.prototype._setListeners = function setListeners(child) {
232231} ;
233232
234233Spawnify . prototype . _onCD = function onCD ( command , currDir ) {
235- let wasError , strs ,
236- CD = 'cd ' ,
237-
238- isChangeVolume = win . isChangeVolume ( command ) ,
239- isVolume = win . isVolume ( command ) ,
240- paramDir ,
241-
242- regExpRoot = RegExp ( '^[/\\\\]' ) ,
243-
244- isWildCard ,
245- isRoot ;
234+ let wasError ;
235+
236+ const CD = 'cd ' ;
237+ const isChangeVolume = win . isChangeVolume ( command ) ;
238+ const isVolume = win . isVolume ( command ) ;
239+ const regExpRoot = RegExp ( '^[/\\\\]' ) ;
246240
247- this . on ( 'error' , function ( ) {
241+ this . on ( 'error' , ( ) => {
248242 wasError = true ;
249243 } ) ;
250244
251- this . on ( 'close' , function ( ) {
245+ this . on ( 'close' , ( ) => {
252246 if ( ! wasError )
253247 this . _emit ( 'path' , tildify ( paramDir ) ) ;
254248 } ) ;
255249
256- paramDir = untildify ( command ) ;
257- isWildCard = isContain ( paramDir , [ '*' , '?' ] ) ;
250+ let paramDir = untildify ( command ) ;
251+ const isWildCard = isContain ( paramDir , [ '*' , '?' ] ) ;
258252
259253 if ( ! paramDir && ! WIN )
260254 paramDir = '.' ;
261255
262256 if ( ! isChangeVolume || isVolume ) {
263- paramDir = this . _getFirstWord ( paramDir ) ;
264- paramDir = path . normalize ( paramDir ) ;
257+ paramDir = this . _getFirstWord ( paramDir ) ;
258+ paramDir = path . normalize ( paramDir ) ;
265259
266- isRoot = regExpRoot . test ( paramDir ) ;
260+ const isRoot = regExpRoot . test ( paramDir ) ;
267261
268- strs = [
262+ const strs = [
269263 CD ,
270264 paramDir ,
271265 '\'' + paramDir + '\'' ,
@@ -286,43 +280,38 @@ Spawnify.prototype._onCD = function onCD(command, currDir) {
286280 }
287281
288282 if ( ! isWildCard )
289- this . _set ( 'exec' , command , { cwd : paramDir } ) ;
290- else
291- find ( paramDir , ( error , dirs ) => {
292- let dir ;
293-
294- if ( ! error )
295- dir = dirs [ 0 ] ;
296-
297- paramDir = dir ;
298- this . _set ( 'exec' , command , { cwd : dir } ) ;
283+ return this . _set ( 'exec' , command , { cwd : paramDir } ) ;
284+
285+ find ( paramDir , ( error , dirs ) => {
286+ let cwd ;
287+
288+ if ( ! error )
289+ cwd = dirs [ 0 ] ;
290+
291+ this . _set ( 'exec' , command , {
292+ cwd
299293 } ) ;
294+ } ) ;
300295} ;
301296
302297Spawnify . prototype . _getFirstWord = function getFirstWord ( str ) {
303- var word , result ,
304- regStrEnd = getRegStrEnd ( ) ,
305- regStr = '^(.*?)' ,
306- regStrQuotes = '^"(.*)"' ,
307- regExp = RegExp ( regStr + regStrEnd ) ,
308- regExpQuotes = RegExp ( regStrQuotes + regStrEnd + '?' ) ,
309- is = typeof str === 'string' ;
310-
311- if ( is ) {
312- result = str . match ( regExpQuotes ) ;
313-
314- if ( result ) {
315- word = result [ 1 ] ;
316- } else {
317- result = str . match ( regExp ) ;
318- word = result && result [ 1 ] ;
319- }
320-
321- if ( ! word )
322- word = str ;
323- }
298+ const regStrEnd = getRegStrEnd ( ) ;
299+ const regStr = '^(.*?)' ;
300+ const regStrQuotes = '^"(.*)"' ;
301+ const regExp = RegExp ( regStr + regStrEnd ) ;
302+ const regExpQuotes = RegExp ( regStrQuotes + regStrEnd + '?' ) ;
303+ const is = typeof str === 'string' ;
304+
305+ if ( ! is )
306+ return str ;
307+
308+ let result = str . match ( regExpQuotes ) ;
324309
325- return word ;
310+ if ( result )
311+ return result [ 1 ] || str ;
312+
313+ result = str . match ( regExp ) ;
314+ return result && result [ 1 ] || str ;
326315} ;
327316
328317function getRegStrEnd ( ) {
@@ -331,7 +320,7 @@ function getRegStrEnd() {
331320 . map ( ( char ) => {
332321 return '\\' + char ;
333322 } ) . join ( '|' ) ;
334-
323+
335324 const regStr = '(' + escaped + ')' ;
336325
337326 return regStr ;
0 commit comments