@ -83,17 +83,6 @@ var pathFn = process.compile("(function (exports) {" + natives.path + "\n})",
var pathModule = createInternalModule ( 'path' , pathFn ) ;
var pathModule = createInternalModule ( 'path' , pathFn ) ;
var path = pathModule . exports ;
var path = pathModule . exports ;
function existsSync ( path ) {
try {
process . binding ( 'fs' ) . stat ( path ) ;
return true ;
} catch ( e ) {
return false ;
}
}
var modulePaths = [ path . join ( process . execPath , ".." , ".." , "lib" , "node" ) ] ;
var modulePaths = [ path . join ( process . execPath , ".." , ".." , "lib" , "node" ) ] ;
if ( process . env [ "HOME" ] ) {
if ( process . env [ "HOME" ] ) {
@ -104,7 +93,6 @@ if (process.env["NODE_PATH"]) {
modulePaths = process . env [ "NODE_PATH" ] . split ( ":" ) . concat ( modulePaths ) ;
modulePaths = process . env [ "NODE_PATH" ] . split ( ":" ) . concat ( modulePaths ) ;
}
}
/* Sync unless callback given */
/* Sync unless callback given */
function findModulePath ( id , dirs , callback ) {
function findModulePath ( id , dirs , callback ) {
process . assert ( dirs . constructor == Array ) ;
process . assert ( dirs . constructor == Array ) ;
@ -154,32 +142,47 @@ function findModulePath (id, dirs, callback) {
locations . push ( path . join ( dir , id , 'index' + ext ) ) ;
locations . push ( path . join ( dir , id , 'index' + ext ) ) ;
}
}
var fs = requireNative ( 'fs' ) ;
function searchLocations ( ) {
function searchLocations ( ) {
var location = locations . shift ( ) ;
var location = locations . shift ( ) ;
if ( ! location ) {
return findModulePath ( id , rest , callback ) ;
if ( ! location && rest . length > 0 ) {
return findModulePath ( id , rest ) ;
} else if ( location ) {
try {
var stats = fs . statSync ( location ) ;
if ( stats && ! stats . isDirectory ( ) ) return location ;
} catch ( e ) { }
return searchLocations ( ) ;
} else {
return false ;
}
}
}
// if async
function searchLocationsAsync ( cb ) {
if ( callback ) {
var location = locations . shift ( ) ;
path . exists ( location , function ( found ) {
if ( found ) {
if ( ! location && rest . length > 0 ) {
callback ( location ) ;
findModulePath ( id , rest , cb ) ;
} else if ( location ) {
fs . stat ( location , function ( err , stats ) {
if ( stats && ! stats . isDirectory ( ) ) {
cb ( location ) ;
} else {
} else {
return searchLocations ( ) ;
searchLocationsAsync ( cb ) ;
}
}
} ) ;
} ) ;
// if sync
} else {
} else {
if ( existsSync ( location ) ) {
cb ( false ) ;
return location ;
} else {
return searchLocations ( ) ;
}
}
}
}
}
return searchLocations ( ) ;
if ( callback ) {
return searchLocationsAsync ( callback ) ;
} else {
return searchLocations ( ) ;
}
}
}