diff --git a/bin/ynode.js b/bin/ynode.js index 500f677..5c71f96 100644 --- a/bin/ynode.js +++ b/bin/ynode.js @@ -4,7 +4,15 @@ var repl = require( 'repl' ); var path = require( 'path' ); -global.yearn = require( '../lib/yearn' )( ); + +var legacy = false; +var index; +while( ( index = process.argv.indexOf( '--legacy' ) ) !== -1 ){ + legacy = true; + process.argv.splice( index, 1 ); +} + +global.yearn = require( '../lib/yearn' )( { legacy: legacy } ); process.argv.shift( ); diff --git a/lib/utils/config.js b/lib/utils/config.js index 18f5d53..3df8f98 100644 --- a/lib/utils/config.js +++ b/lib/utils/config.js @@ -9,6 +9,7 @@ var merge = require( 'merge' ).recursive; var DEFAULT_INIT_TYPE = 'LAZY'; var DEFAULT_LOGGER = 'default'; var DEFAULT_LOAD_MISSING = false; +var DEFAULT_LEGACY = false; var DEFAULT_OVERRIDE = true; var DEFAULT_PROMPT = 'ynode> '; var DEFAULT_ORGS = { '': './node_modules' }; @@ -27,6 +28,7 @@ function defaultConfig( config ){ config.logger = config.logger || DEFAULT_LOGGER; config.init_type = config.initalize || DEFAULT_INIT_TYPE; config.load_missing = ( config.load_missing === undefined ? DEFAULT_LOAD_MISSING : config.load_missing ); + config.legacy = ( config.legacy === undefined ? DEFAULT_LEGACY : config.legacy ); config.override = ( config.override === undefined ? DEFAULT_OVERRIDE : config.override ); config.prompt = ( config.prompt === undefined ? DEFAULT_PROMPT : config.prompt ); diff --git a/lib/utils/yearn-utils.js b/lib/utils/yearn-utils.js index 7e5110f..14b7707 100644 --- a/lib/utils/yearn-utils.js +++ b/lib/utils/yearn-utils.js @@ -81,22 +81,32 @@ module.exports = function( config ){ } }; - yutils.findModuleLocation = function( root_location, module, legacy_yearning ){ + yutils.findModernModuleLocation = function( package_location, org_location, module ){ + var root_location = path.resolve( package_location, org_location ); yutils.LOGGER.debug( 'Checking for module in ' + root_location ); if( fs.existsSync( path.join( root_location, module ) ) ){ return path.join( root_location, module ); } - if( legacy_yearning !== true ){ - return null; - } + return null; + }; + + yutils.findLegacyModuleLocation = function( package_location, module ){ + var root_location = path.resolve( package_location, './node_modules' ); - if( path.dirname( root_location ).length <= path.resolve( '/' ).length ){ - return null; + var system_root_length = path.resolve( '/' ).length; + while( path.dirname( root_location ).length > system_root_length ){ + yutils.LOGGER.debug( 'Checking for module in ' + root_location ); + + if( fs.existsSync( path.join( root_location, module ) ) ){ + return path.join( root_location, module ); + } + + root_location = path.resolve( root_location, '../../node_modules' ); } - return yutils.findModuleLocation( path.resolve( root_location, '../../node_modules' ), module, legacy_yearning ); + return null; }; return yutils; diff --git a/lib/yearn-core.js b/lib/yearn-core.js index 316c5cf..5a9b8c0 100644 --- a/lib/yearn-core.js +++ b/lib/yearn-core.js @@ -132,53 +132,56 @@ core.determineYearningPath = function( desired, parent ){ throw error; } - var module_location = yutils.findModuleLocation( - path.resolve( path.dirname( package_json_location ), path.join( config.orgs[ desired.org ] ) ), - desired.module, - yutils.isLegacyYearning( desired ) + var module_location = yutils.findModernModuleLocation( + path.dirname( package_json_location ), + config.orgs[ desired.org ], + desired.module ); - // Check if the module exists at this level - if( !fs.existsSync( module_location ) ){ - error = new Error( 'Module "' + desired.module + '" was not found in org "' + desired.org + '".' ); - yutils.LOGGER.error( error.message ); - throw error; + // Check if the module exists using the modern syntax + if( fs.existsSync( module_location ) ){ + // Look for "org/module_name/version" + var available_versions = fs.readdirSync( module_location ).filter( function( version ){ + // Filter out bad versions + return yutils.isValidSemVer( version ); + }).sort( semver.rcompare ); - } - - // Look for "org/module_name/version" - var available_versions = fs.readdirSync( module_location ).filter( function( version ){ - // Filter out bad versions - return yutils.isValidSemVer( version ); - }).sort( semver.rcompare ); - - for( var index = 0; index < available_versions.length; ++index ){ - if( semver.satisfies( available_versions[ index ], desired.version ) ){ - module_path = path.resolve( path.join( module_location, available_versions[ index ] )); - if( yearn_map[ desired.org ] === undefined ){ - yearn_map[ desired.org ] = {}; - } - - if( yearn_map[ desired.org ][ desired.module ] === undefined ){ - yearn_map[ desired.org ][ desired.module ] = {}; + for( var index = 0; index < available_versions.length; ++index ){ + if( semver.satisfies( available_versions[ index ], desired.version ) ){ + module_path = path.resolve( path.join( module_location, available_versions[ index ] )); + if( yearn_map[ desired.org ] === undefined ){ + yearn_map[ desired.org ] = {}; + } + + if( yearn_map[ desired.org ][ desired.module ] === undefined ){ + yearn_map[ desired.org ][ desired.module ] = {}; + } + + yearn_map[ desired.org ][ desired.module ][ desired.version ] = module_path; + + if( desired.file !== undefined ) + module_path = path.join( module_path, desired.file ); + + return module_path; } - - yearn_map[ desired.org ][ desired.module ][ desired.version ] = module_path; - - if( desired.file !== undefined ) - module_path = path.join( module_path, desired.file ); - - return module_path; } } - - yutils.LOGGER.warn( 'Unable to find satisfactory version (assuming legacy yearning).' ); - - if( desired.file !== undefined ) - module_location = path.join( module_location, desired.file ); - - // Fallback to looking for "org/module_name". - return module_location; + + // Check if allowing legacy yearning + if( yutils.isLegacyYearning( desired ) || config.legacy ){ + yutils.LOGGER.warn( 'Unable to find satisfactory version (assuming legacy yearning).' ); + + module_location = yutils.findLegacyModuleLocation( path.dirname( package_json_location ), desired.module ); + + if( desired.file !== undefined ) + module_location = path.join( module_location, desired.file ); + + return module_location; + } else { + error = new Error( 'Module "' + desired.module + '" was not found in org "' + desired.org + '".' ); + yutils.LOGGER.error( error.message ); + throw error; + } }; core.resolve = function( desired, parent ){ diff --git a/tests/utils/config-tests.js b/tests/utils/config-tests.js index fcd4a7a..5dee7f2 100644 --- a/tests/utils/config-tests.js +++ b/tests/utils/config-tests.js @@ -48,6 +48,7 @@ module.exports.configTests = { logger: 'default', init_type: 'LAZY', load_missing: false, + legacy: false, override: true, prompt: 'ynode> ', orgs: { '': './node_modules' }, @@ -76,6 +77,7 @@ module.exports.configTests = { logger: 'log4js', init_type: 'LAZY', load_missing: false, + legacy: false, override: true, prompt: 'ynode> ', orgs: { '': './node_modules' }, @@ -98,6 +100,7 @@ module.exports.configTests = { logger: 'default', init_type: 'LAZY', load_missing: false, + legacy: false, override: true, prompt: 'ynode> ', orgs: { '': './node_modules' }, diff --git a/tests/utils/yearn-utils-tests.js b/tests/utils/yearn-utils-tests.js index 4cd752e..31c25d8 100644 --- a/tests/utils/yearn-utils-tests.js +++ b/tests/utils/yearn-utils-tests.js @@ -255,19 +255,11 @@ module.exports.findPackageJsonLocationTests = { }, }; -module.exports.findModuleLocationTests = { +module.exports.findModernModuleLocationTests = { testCorrectDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/' ), 'test-module-2' ), - path.resolve( __dirname, '../node_modules/test-module-2' ) - ); - unit.done(); - }, - - testCorrectWithLegacyDirectory: function( unit ){ - unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/' ), 'test-module-2', true ), + yearn_utils.findModernModuleLocation( __dirname, '../node_modules/', 'test-module-2' ), path.resolve( __dirname, '../node_modules/test-module-2' ) ); unit.done(); @@ -275,81 +267,68 @@ module.exports.findModuleLocationTests = { testChildDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/test-module-2' ), 'test-module-2' ), + yearn_utils.findModernModuleLocation( path.resolve( __dirname, '../node_modules/test-module-2' ), '../node_modules/' , 'test-module-2' ), null ); unit.done(); }, - testChildWithLegacyDirectory: function( unit ){ + testNoModuleRootDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/test-module-2' ), 'test-module-2', true ), - path.resolve( __dirname, '../node_modules/test-module-2' ) + yearn_utils.findModernModuleLocation( path.resolve( '/' ), '../node_modules/', 'test-module-2' ), + null ); unit.done(); }, - testGrandChildDirectory: function( unit ){ + testNoModuleChildOfRootDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/test-module-2/1.0.0' ), 'test-module-2' ), + yearn_utils.findModernModuleLocation( path.resolve( require('os').tmpdir() ), '../node_modules/', 'test-module-2' ), null ); unit.done(); - }, + } +}; + +module.exports.findLegacyModuleLocationTests = { - testGrandChildWithLegacyDirectory: function( unit ){ + testCorrectDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/test-module-2/1.0.0' ), 'test-module-2', true ), + yearn_utils.findLegacyModuleLocation( path.resolve( __dirname, '../node_modules/' ), 'test-module-2' ), path.resolve( __dirname, '../node_modules/test-module-2' ) ); unit.done(); }, - testNephewDirectory: function( unit ){ - unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/test-module-1' ), 'test-module-2' ), - null - ); - unit.done(); - }, - - testNephewWithLegacyDirectory: function( unit ){ + testChildDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/test-module-1' ), 'test-module-2', true ), + yearn_utils.findLegacyModuleLocation( path.resolve( __dirname, '../node_modules/test-module-2' ), 'test-module-2', true ), path.resolve( __dirname, '../node_modules/test-module-2' ) ); unit.done(); }, - testGrandNephewDirectory: function( unit ){ + testGrandChildDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/test-module-1/1.0.0' ), 'test-module-2' ), - null + yearn_utils.findLegacyModuleLocation( path.resolve( __dirname, '../node_modules/test-module-2/1.0.0' ), 'test-module-2', true ), + path.resolve( __dirname, '../node_modules/test-module-2' ) ); unit.done(); }, - testGrandNephewWithLegacyDirectory: function( unit ){ + testNephewDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( __dirname, '../node_modules/test-module-1/1.0.0' ), 'test-module-2', true ), + yearn_utils.findLegacyModuleLocation( path.resolve( __dirname, '../node_modules/test-module-1' ), 'test-module-2', true ), path.resolve( __dirname, '../node_modules/test-module-2' ) ); unit.done(); }, - testNoModuleRootDirectoryWithLegacy: function( unit ){ + testGrandNephewDirectory: function( unit ){ unit.equal( - yearn_utils.findModuleLocation( path.resolve( '/' ), 'test-module-2', true ), - null + yearn_utils.findLegacyModuleLocation( path.resolve( __dirname, '../node_modules/test-module-1/1.0.0' ), 'test-module-2', true ), + path.resolve( __dirname, '../node_modules/test-module-2' ) ); unit.done(); }, - - testNoModuleChildOfRootDirectoryWithLegacy: function( unit ){ - unit.equal( - yearn_utils.findModuleLocation( path.resolve( require('os').tmpdir() ), 'test-module-2', true ), - null - ); - unit.done(); - } };