lib/grunt/ngDpTask/ngDpTask.js
function findLongestDependencyChains(scripts, script, modulesToIgnore) {
  var chains = [];

  if (!script) script = scripts[0];

  // Avoid circular dependencies
  if (modulesToIgnore && script.module && modulesToIgnore.indexOf(script.module) !== -1) return chains;

  // Get script dependencies
  if (script.dependencies && script.dependencies.length) {
    var longestChainLength;

    // Find dependency chains of the script
    script.dependencies.forEach(function(dependency) {
      var definitionScript = findScript(scripts, 'definitions', dependency);

      if (definitionScript)
        chains = chains.concat(findLongestDependencyChains(scripts, definitionScript, script.definitions));
    });

    if (chains.length > 0) {

      // Keep the longest chain(s)
      chains.sort(function(chain1, chain2) {
        // -1 : chain1 before chain2
        // 0 : nothing change
        // 1 : chain1 after chain2

        if (chain1.length > chain2.length)
          return -1;
        else if (chain1.length < chain2.length)
          return 1;
        else return 0;
      });

      longestChainLength = chains[0].length;

      chains = chains.filter(function(chain) {
        if (chain.length === longestChainLength) {
          chain.push(script.path);
          return true;
        }

        return false;
      });

      return chains;
    }
  }

  chains.push([script.path]);
  return chains;
}
function findLongestDependencyChains(scripts, script, modulesToIgnore) {
  var chains = [];

  if (!script) script = scripts[0];

  // Avoid circular dependencies
  if (modulesToIgnore && script.module && modulesToIgnore.indexOf(script.module) !== -1) return chains;

  // Get script dependencies
  if (script.dependencies && script.dependencies.length) {
    var longestChainLength;

    // Find dependency chains of the script
    script.dependencies.forEach(function(dependency) {
      var definitionScript = findScript(scripts, 'definitions', dependency);

      if (definitionScript)
        chains = chains.concat(findLongestDependencyChains(scripts, definitionScript, script.definitions));
    });

    if (chains.length > 0) {

      // Keep the longest chain(s)
      chains.sort(function(chain1, chain2) {
        // -1 : chain1 before chain2
        // 0 : nothing change
        // 1 : chain1 after chain2

        if (chain1.length > chain2.length)
          return -1;
        else if (chain1.length < chain2.length)
          return 1;
        else return 0;
      });

      longestChainLength = chains[0].length;

      chains = chains.filter(function(chain) {
        if (chain.length === longestChainLength) {
          chain.push(script.path);
          return true;
        }

        return false;
      });

      return chains;
    }
  }

  chains.push([script.path]);
  return chains;
}
function findLongestDependencyChains ( scripts , script , modulesToIgnore ) { var chains = [ ] ; if ( ! script ) script = scripts [ 0 ] ; // Avoid circular dependencies if ( modulesToIgnore && script . module && modulesToIgnore . indexOf ( script . module ) !== - 1 ) return chains ; // Get script dependencies if ( script . dependencies && script . dependencies . length ) { var longestChainLength ; // Find dependency chains of the script script . dependencies . forEach ( function ( dependency ) { var definitionScript = findScript ( scripts , 'definitions' , dependency ) ; if ( definitionScript ) chains = chains . concat ( findLongestDependencyChains ( scripts , definitionScript , script . definitions ) ) ; } ) ; if ( chains . length > 0 ) { // Keep the longest chain(s) chains . sort ( function ( chain1 , chain2 ) { // -1 : chain1 before chain2 // 0 : nothing change // 1 : chain1 after chain2 if ( chain1 . length > chain2 . length ) return - 1 ; else if ( chain1 . length < chain2 . length ) return 1 ; else return 0 ; } ) ; longestChainLength = chains [ 0 ] . length ; chains = chains . filter ( function ( chain ) { if ( chain . length === longestChainLength ) { chain . push ( script . path ) ; return true ; } return false ; } ) ; return chains ; } } chains . push ( [ script . path ] ) ; return chains ; }
Browses a flat list of scripts to find a script longest dependency chains.

Each script may have several dependencies, each dependency can also have several dependencies.
findLongestDependencyChains helps find the longest dependency chain of one of the script.
As the script may have several longest dependency chain, a list of chains is returned.

A chain is an ordered list of script paths.

This is recursive.

@method findLongestDependencyChains
@param {Array} scripts The flat list of scripts with for each script:
- **dependencies** The list of dependency names of the script
- **definitions** The list of definition names of the script
- **path** The script path
@param {Object} [script] The script to analyze (default to the first one of the list of scripts)
@param {Array} [modulesToIgnore] The list of module names to ignore to avoid circular dependencies
@return {Array} The longest dependency chains
@private
Browses a flat list of scripts to find a script longest dependency chains .