'use strict';

// MODULES //

var debug = require( 'debug' )( 'entry-points:async:resolve' );
var resolve = require( 'resolve' );
var resolveDir = require( './resolve_dir.js' );


// MAIN //

/**
* Resolves a list of packages.
*
* @private
* @param {StringArray} pkgs - list of packages
* @param {string} dir - base directory from which to resolve packages
* @param {Callback} clbk - callback to invoke after resolving packages
*/
function getPkgs( pkgs, dir, clbk ) {
	var count;
	var opts;
	var len;
	var out;
	var i;

	len = pkgs.length;
	debug( 'Resolving %d packages...', len );

	out = new Array( len );
	count = 0;
	for ( i = 0; i < len; i++ ) {
		debug( 'Resolving package: %s (%d of %d).', pkgs[ i ], i+1, len );
		opts = {
			'basedir': dir
		};
		out[ i ] = {
			'id': null,
			'pkg': pkgs[ i ],
			'dir': null,
			'data': null
		};
		resolve( pkgs[ i ], opts, createClbk( i ) );
	}
	/**
	* Returns a callback to be invoked upon resolving a package.
	*
	* @private
	* @param {NonNegativeInteger} idx - index
	* @returns {Callback} callback
	*/
	function createClbk( idx ) {
		var name = pkgs[ idx ];
		var k = idx + 1;

		return onResolve;

		/**
		* Callback invoked upon resolving a package directory.
		*
		* @private
		* @param {(Error|null)} error - error object
		* @param {string} dir - package directory
		* @returns {void}
		*/
		function onDir( error, dir ) {
			if ( error ) {
				debug( 'Encountered an error while resolving package directory: %s (%d of %d). Error: %s', name, k, len, error.message );
				return clbk( error );
			}
			debug( 'Resolved package directory for package: %s (%d of %d). Dir: %s', name, k, len, dir );
			out[ idx ].dir = dir;

			count += 1;
			debug( 'Resolved %d of %d packages.', count, len );
			if ( count === len ) {
				debug( 'Resolved all packages.' );
				return clbk( null, out );
			}
		} // end FUNCTION onDir()

		/**
		* Callback to be invoked upon resolving a package.
		*
		* @private
		* @param {(Error|null)} error - error object
		* @param {string} main - main entry point
		* @param {Object} [pkg] - `package.json` contents
		* @returns {void}
		*/
		function onResolve( error, main, pkg ) {
			if ( error ) {
				debug( 'Encountered an error while resolving package: %s (%d of %d). Error: %s', name, k, len, error.message );
				return clbk( error );
			}
			debug( 'Resolved package: %s (%d of %d). Main: %s.', name, k, len, main );
			out[ idx ].id = main;
			if ( !pkg ) {
				pkg = {};
			}
			out[ idx ].data = pkg;

			debug( 'Resolving package directory for package: %s (%d of %d)...', name, k, len );
			resolveDir( main, onDir );
		} // end FUNCTION onResolve()
	} // end FUNCTION createClbk()
} // end FUNCTION getPkgs()


// EXPORTS //

module.exports = getPkgs;

@@ -462,16 +462,29 @@
 lbk ) %7B%0A
+%09var errFLG;%0A
 %09var cou
@@ -1383,24 +1383,60 @@
 or, dir ) %7B%0A
+%09%09%09if ( errFLG ) %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A
 %09%09%09if ( erro
@@ -1559,32 +1559,51 @@
 rror.message );%0A
+%09%09%09%09errFLG = true;%0A
 %09%09%09%09return clbk(
@@ -2222,24 +2222,60 @@
 in, pkg ) %7B%0A
+%09%09%09if ( errFLG ) %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A
 %09%09%09if ( erro
@@ -2392,24 +2392,43 @@
 .message );%0A
+%09%09%09%09errFLG = true;%0A
 %09%09%09%09return c

Fix repeated invocation of callback if multiple errors