var fs = require('fs');

var components = require('../modules/components');
var handlebars = require('../modules/handlebars');
var renderPage = require('../modules/renderPage');

/**
 * Helper method to organise components by their categories
 */
function sortComponents(components) {

  // Sort the components by category
  var sortedComponents = {};
  components.forEach(function(component) {

    // If the component doesn't define any demos, don't bother rendering it
    if(Object.keys(component.variants).length === 0) {
      return;
    }

    if(!component.categories) {
      component.categories = {
        primary: 'Unsorted',
        secondary: 'Unsorted'
      };
    }

    if(!sortedComponents[component.categories.primary]) {
      sortedComponents[component.categories.primary] = {};
    }

    if(!sortedComponents[component.categories.primary][component.categories.secondary]) {
      sortedComponents[component.categories.primary][component.categories.secondary] = [];
    }

    sortedComponents[component.categories.primary][component.categories.secondary].push(component);
  });

  return sortedComponents;
}

module.exports = function(app){

  /**
   * Main index page route
   */
  app.get('/', function(req, res) {

    Promise
      .all([
        handlebars(),
        components.getComponents()
      ])
      .spread(function(hbs, components) {

        res.send(renderPage(hbs, {
          title: 'Index',
          content: hbs.compile(hbs.partials['layout/index'])({
            components: sortComponents(components),
            readme: fs.readFileSync('README.md')
          })
        }));

      })
      .catch(function(err) {
        require('trace');
        require('clarify');
        console.trace(err);

        renderPage(hbs, {
          title: 'Index',
          content: 'Error'
        })
        .then(function(html) {
          res.send(html);
        });;
      });
  });

}

@@ -1379,25 +1379,16 @@
         
-res.send(
 renderPa
@@ -1389,32 +1389,32 @@
 nderPage(hbs, %7B%0A
+
           title:
@@ -1610,16 +1610,83 @@
       %7D)
+%0A        .then(function(html) %7B%0A          res.send(html);%0A        %7D
 );%0A%0A    
@@ -1941,32 +1941,32 @@
 res.send(html);%0A
+
         %7D);;%0A   
@@ -1960,17 +1960,16 @@
      %7D);
-;
 %0A      %7D

Fix index page route which was broken by transition to Promise based page rendering function