Skip to content

Commit

Permalink
Merge pull request #99 from doctorrustynelson/0.8.0
Browse files Browse the repository at this point in the history
0.8.0
  • Loading branch information
doctorrustynelson committed Feb 6, 2016
2 parents 39dc48c + 70bab99 commit 1e43c4f
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 11 deletions.
22 changes: 22 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

'use strict';
var path = require( 'path' );

module.exports = function( grunt ){

Expand Down Expand Up @@ -50,6 +51,9 @@ module.exports = function( grunt ){
config: [
'tests/utils/config-tests.js'
],
shrinkwrap: [
'tests/ynpm-shrinkwrap-tests.js'
],
list: [
'tests/utils/ynpm-utils-find-tests.js',
'tests/ynpm-list-tests.js'
Expand All @@ -64,6 +68,23 @@ module.exports = function( grunt ){
reporter: 'verbose'
}
},

'ynpm-shrinkwrap': {
test: {
src: './tests/test-orgs/alphabet/D/0.1.0',
dest: '.',
options: {
config: {
orgs: {
'': './node_modules',
'*': path.resolve( __dirname, './tests/test-orgs/*' ),
'other': path.resolve( __dirname, './tests/test-other-org' )
},
loose_semver: true
}
}
}
},

coveralls: {
options: {
Expand All @@ -78,6 +99,7 @@ module.exports = function( grunt ){
grunt.loadNpmTasks( 'grunt-contrib-jshint' );
grunt.loadNpmTasks( 'grunt-contrib-nodeunit' );
grunt.loadNpmTasks( 'grunt-coveralls' );
grunt.loadTasks( './tasks' );

grunt.registerTask( 'test', [ 'jshint', 'nodeunit:unit', 'nodeunit:cli', 'nodeunit:yearn' ] );
grunt.registerTask( 'default', [ 'test' ] );
Expand Down
21 changes: 19 additions & 2 deletions bin/ynpm-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
var commander = require( 'commander' );
var fs = require( 'fs' );
var JSON5 = require( 'json5' );
var merge = require( 'merge' ).recursive;
var _ = require( 'lodash' );
var path = require( 'path' );

var version = require( '../package.json' ).version;
var config = require( '../lib/utils/config' ).initialize( );
Expand Down Expand Up @@ -43,7 +44,8 @@ commander

var contents = JSON5.parse( fs.readFileSync( package_json_location, 'utf8' ) );

var dependencies = merge(
var dependencies = _.merge(
{},
contents.dependencies,
contents.devDependencies,
contents.optionalDependencies
Expand Down Expand Up @@ -88,6 +90,21 @@ commander
} );
} );


commander
.command( 'shrinkwrap [root_dir]' )
.description( 'Create a ynpm shrinkwrap.' )
.action( function( root_dir ){

if( root_dir === undefined )
root_dir = process.cwd( );

ynpm.commands.shrinkwrap( root_dir, {}, function( err, shrinkwrap ){
fs.writeFileSync( path.join( root_dir, 'ynpm-shrinkwrap.json' ), JSON.stringify( shrinkwrap, null, '\t' ) );
} );
} );


commander
.command( 'check [orgs_or_specific_modules...]' )
.description( 'Print the modules that need to be updated and to what version.' )
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

var JSON5 = require( 'json5' );
var fs = require( 'fs' );
var merge = require( 'merge' ).recursive;
var _ = require( 'lodash' );

var DEFAULT_INIT_TYPE = 'LAZY';
var DEFAULT_LOGGER = 'default';
Expand Down Expand Up @@ -74,14 +74,14 @@ function initialize( user_config ){

if( YEARN_CONFIG === undefined || YEARN_CONFIG === '' || YEARN_CONFIG === 'false' ){
console.warn( 'YEARN_CONFIG is not defined. Using default config.' );
return defaultConfig( merge( orgs_override, user_config ) );
return defaultConfig( _.merge( {}, orgs_override, user_config ) );
} else if( !fs.existsSync( YEARN_CONFIG ) ){
console.warn( 'YEARN_CONFIG was not found at ' + YEARN_CONFIG + '. Using default config.' );
return defaultConfig( merge( orgs_override, user_config ) );
return defaultConfig( _.merge( {}, orgs_override, user_config ) );
} else {
var yearn_config = JSON5.parse( fs.readFileSync( YEARN_CONFIG ) );

return defaultConfig( merge( yearn_config, orgs_override, user_config ) );
return defaultConfig( _.merge( {}, yearn_config, orgs_override, user_config ) );
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/utils/dependency-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

var path = require( 'path' );
var JSON5 = require( 'json5' );
var merge = require( 'merge' ).recursive;
var _ = require( 'lodash' );
var fs = require( 'fs' );

var dependency_tree = {
Expand Down Expand Up @@ -187,7 +187,8 @@ manager.populate = function( parent_path, parent_id, package_json_location ){
var parent_link = parent_path[ parent_path.length - 1 ];

// Determine contents being used in package.json
var dependencies = merge(
var dependencies = _.merge(
{},
/* Ignore devDependencies if we're not in the root module */
( parent_link === undefined ? pkg_contents.devDependencies : {} ),
pkg_contents.dependencies,
Expand Down
36 changes: 36 additions & 0 deletions lib/utils/yearn-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,42 @@ module.exports = function( config ){
}
};

yutils.findModuleLocation = function( package_location, desired ){
var module_location;

// Check if allowing legacy yearning
if( yutils.isLegacyYearning( desired ) || config.legacy ){
yutils.LOGGER.warn( 'Assuming legacy yearning.' );

module_location = yutils.findLegacyModuleLocation( path.dirname( package_location ), desired.module );
if( module_location !== null )
return module_location;
}

// Hunt for satisfying module.
module_location = yutils.findModernModuleLocation(
path.dirname( package_location ),
( config.orgs[ desired.org ] !== undefined ? config.orgs[ desired.org ] : config.orgs[ '*' ].replace( /\*/g, desired.org ) ),
desired.module
);

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( function( a, b ){ return semver.rcompare( a, b, config.loose_semver ); } );

for( var index = 0; index < available_versions.length; ++index ){
if( semver.satisfies( available_versions[ index ], desired.version, config.loose_semver ) ){
return path.resolve( path.join( module_location, available_versions[ index ] ));
}
}
}

return null;
};

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 );
Expand Down
81 changes: 81 additions & 0 deletions lib/ynpm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var path = require( 'path' );
var fs = require( 'fs' );
var semver = require( 'semver' );
var npm = require( 'npm' );
var JSON5 = require( 'json5' );
var _ = require( 'lodash' );

module.exports = function( config, callback ){

Expand Down Expand Up @@ -90,6 +92,85 @@ module.exports = function( config, callback ){

callback( null, list );
};

ynpm.commands.shrinkwrap = function( _cwd, _dependencies_seed, _callback ){

function shrinkwrap_fn( cwd, dependencies_seed, callback ){
var package_location = path.join( cwd, 'package.json' );
var shrinkwrap_location = path.join( cwd, 'ynpm-shrinkwrap.json' );
var pkg, local_shrinkwrap;

function execute( ){
var merged_dependencies = _.merge(
{},
pkg.dependencies,
local_shrinkwrap.dependencies,
( dependencies_seed === undefined ? {} : dependencies_seed )
);

var dependencies = Object.keys( merged_dependencies );
if( dependencies.length === 0 ){
return callback( null, pkg.version );
}

var shrinkwrap = {
version: pkg.version,
dependencies: {}
};

var count = dependencies.length;
function done( ){
if( --count <= 0 )
return callback( null, shrinkwrap );
}

dependencies.forEach( function( dependency ){
var object_semver = ( typeof merged_dependencies[ dependency ] === 'object' );
var desired = yutils.extractYearningParts( dependency );
desired.org = ( desired.org === undefined ? '' : desired.org );
desired.version = ( object_semver ? merged_dependencies[ dependency ].version : merged_dependencies[ dependency ] );

desired.version = nutils.findMatchingVersions( desired.org, desired.module, desired.version, cwd )[ 0 ];

var dependency_root = yutils.findModuleLocation( package_location, desired );

shrinkwrap_fn( dependency_root, merged_dependencies[ dependency ].dependencies, function( error, dep_shrinkwrap ){
shrinkwrap.dependencies[ dependency ] = dep_shrinkwrap;
return done();
} );
} );
}

function fileRead( ){
if( local_shrinkwrap !== undefined && pkg !== undefined )
execute( );
}

fs.readFile( package_location, function( error, contents ){
pkg = JSON5.parse( contents );
fileRead( );
} );

fs.exists( shrinkwrap_location, function( exists ){
if( exists ){
fs.readFile( shrinkwrap_location, function( error, contents ){
local_shrinkwrap = JSON5.parse( contents );
fileRead( );
} );
} else {
local_shrinkwrap = { dependencies: {} };
fileRead( );
}
} );
}

shrinkwrap_fn( _cwd, _dependencies_seed, function( err, shrinkwrap ){
if( typeof shrinkwrap === 'object' )
shrinkwrap.name = JSON5.parse( fs.readFileSync( path.join( _cwd, 'package.json' ) ) ).name;

_callback( err, shrinkwrap );
} );
};

ynpm.commands.install = function( desired, callback ){

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yearn",
"version": "0.7.1",
"version": "0.8.0",
"description": "Override node's require mechanism.",
"keywords": [
"require",
Expand Down Expand Up @@ -28,7 +28,7 @@
"ynode": "./bin/ynode.js"
},
"scripts": {
"ci": "istanbul cover --preload-sources -x **/bin/** -x **/tests/** -x Gruntfile.js -v --print both grunt && grunt coveralls",
"ci": "istanbul cover --preload-sources -x **/bin/** -x **/tests/** -x **/tasks/** -x Gruntfile.js -v --print both grunt && grunt coveralls",
"test": "grunt test",
"ynode": "node ./bin/ynode.js",
"ynpm": "./bin/ynpm-cli.js"
Expand All @@ -37,7 +37,7 @@
"commander": "2.9.x",
"fs-extra": "0.26.x",
"json5": "0.4.x",
"merge": "1.2.x",
"lodash": "4.2.x",
"npm": "2.14.x",
"semver": "5.1.x",
"temp": "0.8.x"
Expand Down
35 changes: 35 additions & 0 deletions tasks/shrinkwrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var path = require( 'path' );

module.exports = function( grunt ){
grunt.registerMultiTask( 'ynpm-shrinkwrap', 'Generate a ynpm shrinkwrap.', function( ){
var done = this.async();
var options = this.options( { unsafe: false, config: {} } );
var task = this;

var config = require( '../lib/utils/config' ).initialize( options.config );

require( '../lib/ynpm' )( config, function( err, ynpm ){
task.files.forEach( function( file ){
var src = file.src.filter( function( filepath ){
if ( !grunt.file.exists( filepath ) ){
grunt.log.warn( 'Source dir "' + filepath + '" not found.' );
return false;
} else {
return true;
}
} );

var dest = path.join( file.dest, "ynpm-shrinkwrap.json" );

src.forEach( function( src ){
ynpm.commands.shrinkwrap( src, {}, function( err, shrinkwrap ){
grunt.file.write( dest, JSON.stringify( shrinkwrap, null, '\t' ) );
done();
} );
} );
} );
} );
} );
};
Loading

0 comments on commit 1e43c4f

Please sign in to comment.