@@ -357,6 +357,8 @@ function executablePath() {
357357 * @returns {string } A true concatentated path where found, else undefined
358358 */
359359function maximisePath ( ) {
360+
361+ // rank a vs b based on any numeric component in their string
360362 function compare ( a , b ) {
361363 var numA = parseFloat ( / [ . \d ] + / . exec ( a ) [ 0 ] ) ;
362364 var numB = parseFloat ( / [ . \d ] + / . exec ( b ) [ 0 ] ) ;
@@ -368,27 +370,31 @@ function maximisePath() {
368370 return 0 ;
369371 }
370372 }
371- function eachDirectoryItem ( base , pattern ) {
372- return function eachDirectoryItem ( item ) {
373- var resolved = path . resolve ( path . join ( base , item ) ) ;
374- var isDirectory = fs . statSync ( resolved ) . isDirectory ( ) ;
375- var isMatch = ( typeof pattern === 'string' ) ? ( item === pattern ) :
376- ( 'test' in pattern ) ? pattern . test ( item ) : false ;
377- return isDirectory && isMatch ;
378- } ;
379- }
373+
374+ // ensure each element in the path exists or match where there is a pattern
380375 var elements = Array . prototype . slice . call ( arguments ) ;
381376 for ( var i = 1 ; i < elements . length ; i ++ ) {
382377 var directory = path . resolve ( path . join . apply ( path , elements . slice ( 0 , i ) ) ) ;
383- var matches = fs . readdirSync ( directory )
384- . filter ( eachDirectoryItem ( directory , elements [ i ] ) )
385- . sort ( compare ) ;
386- if ( matches . length === 0 ) {
378+ if ( ! fs . existsSync ( directory ) ) {
387379 return null ;
380+ } else if ( ( typeof elements [ i ] !== 'string' ) && ( 'test' in elements [ i ] ) ) {
381+ var matches = fs . readdirSync ( directory )
382+ . filter ( function eachDirectoryItem ( item ) {
383+ var resolved = path . resolve ( path . join ( directory , item ) ) ;
384+ return elements [ i ] . test ( item ) && fs . statSync ( resolved ) . isDirectory ( ) ;
385+ } )
386+ . sort ( compare ) ;
387+ if ( matches . length === 0 ) {
388+ return null ;
389+ } else {
390+ elements [ i ] = matches [ 0 ] ;
391+ }
388392 } else {
389- elements [ i ] = matches [ 0 ] ;
393+ elements [ i ] = String ( elements [ i ] ) ;
390394 }
391395 }
396+
397+ // now join them all together
392398 return path . resolve ( elements . join ( path . sep ) ) ;
393399}
394400
0 commit comments