lib/grunt/ngDpTask/ngDpTask.js
function buildTree(scripts) {
  var chains = [];
  var tree = {
    children: []
  };
  var currentTreeNode = tree;

  // Get the longest dependency chain for each script with the highest dependency
  // as the first element of the chain
  scripts.forEach(function(script) {
    chains = chains.concat(findLongestDependencyChains(scripts, script));
  });

  // Sort chains by length with longest chains first
  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;
  });

  // Add each chain to the tree
  chains.forEach(function(chain) {

    // Add each element of the chain as a child of its parent
    chain.forEach(function(scriptPath) {
      var currentScript = findScript(scripts, 'path', scriptPath);
      var alreadyExists = false;

      if (!currentTreeNode.children)
        currentTreeNode.children = [];

      // Check if current script does not exist in node children
      for (var i = 0; i < currentTreeNode.children.length; i++) {
        if (currentTreeNode.children[i].path === currentScript.path) {
          alreadyExists = true;
          break;
        }
      }

      // Add script to the tree
      if (!alreadyExists)
        currentTreeNode.children.push(currentScript);

      currentTreeNode = currentScript;
    });

    currentTreeNode = tree;
  });

  return tree;
}
function buildTree(scripts) {
  var chains = [];
  var tree = {
    children: []
  };
  var currentTreeNode = tree;

  // Get the longest dependency chain for each script with the highest dependency
  // as the first element of the chain
  scripts.forEach(function(script) {
    chains = chains.concat(findLongestDependencyChains(scripts, script));
  });

  // Sort chains by length with longest chains first
  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;
  });

  // Add each chain to the tree
  chains.forEach(function(chain) {

    // Add each element of the chain as a child of its parent
    chain.forEach(function(scriptPath) {
      var currentScript = findScript(scripts, 'path', scriptPath);
      var alreadyExists = false;

      if (!currentTreeNode.children)
        currentTreeNode.children = [];

      // Check if current script does not exist in node children
      for (var i = 0; i < currentTreeNode.children.length; i++) {
        if (currentTreeNode.children[i].path === currentScript.path) {
          alreadyExists = true;
          break;
        }
      }

      // Add script to the tree
      if (!alreadyExists)
        currentTreeNode.children.push(currentScript);

      currentTreeNode = currentScript;
    });

    currentTreeNode = tree;
  });

  return tree;
}
function buildTree ( scripts ) { var chains = [ ] ; var tree = { children : [ ] } ; var currentTreeNode = tree ; // Get the longest dependency chain for each script with the highest dependency // as the first element of the chain scripts . forEach ( function ( script ) { chains = chains . concat ( findLongestDependencyChains ( scripts , script ) ) ; } ) ; // Sort chains by length with longest chains first 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 ; } ) ; // Add each chain to the tree chains . forEach ( function ( chain ) { // Add each element of the chain as a child of its parent chain . forEach ( function ( scriptPath ) { var currentScript = findScript ( scripts , 'path' , scriptPath ) ; var alreadyExists = false ; if ( ! currentTreeNode . children ) currentTreeNode . children = [ ] ; // Check if current script does not exist in node children for ( var i = 0 ; i < currentTreeNode . children . length ; i ++ ) { if ( currentTreeNode . children [ i ] . path === currentScript . path ) { alreadyExists = true ; break ; } } // Add script to the tree if ( ! alreadyExists ) currentTreeNode . children . push ( currentScript ) ; currentTreeNode = currentScript ; } ) ; currentTreeNode = tree ; } ) ; return tree ; }
Builds the dependencies tree.

@method buildTree
@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
@return {Array} The list of scripts with their dependencies
@private
Builds the dependencies tree .