Skip to content

Commit

Permalink
Closes #39 forced legacy now supported
Browse files Browse the repository at this point in the history
  • Loading branch information
doctorrustynelson committed Apr 20, 2015
1 parent fa1d8d5 commit 5b96079
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 94 deletions.
10 changes: 9 additions & 1 deletion bin/ynode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( );

Expand Down
2 changes: 2 additions & 0 deletions lib/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
Expand All @@ -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 );

Expand Down
24 changes: 17 additions & 7 deletions lib/utils/yearn-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
85 changes: 44 additions & 41 deletions lib/yearn-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand Down
3 changes: 3 additions & 0 deletions tests/utils/config-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports.configTests = {
logger: 'default',
init_type: 'LAZY',
load_missing: false,
legacy: false,
override: true,
prompt: 'ynode> ',
orgs: { '': './node_modules' },
Expand Down Expand Up @@ -76,6 +77,7 @@ module.exports.configTests = {
logger: 'log4js',
init_type: 'LAZY',
load_missing: false,
legacy: false,
override: true,
prompt: 'ynode> ',
orgs: { '': './node_modules' },
Expand All @@ -98,6 +100,7 @@ module.exports.configTests = {
logger: 'default',
init_type: 'LAZY',
load_missing: false,
legacy: false,
override: true,
prompt: 'ynode> ',
orgs: { '': './node_modules' },
Expand Down
69 changes: 24 additions & 45 deletions tests/utils/yearn-utils-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,101 +255,80 @@ 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();
},

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();
}
};

0 comments on commit 5b96079

Please sign in to comment.