// `server` section processor: read and populate N.server tree
//


'use strict';


/*global underscore, N*/


// stdlib
var fs    = require('fs');
var path  = require('path');


// 3rd-party
var _       = underscore;
var fstools = require('fs-tools');


// internal
var stopwatch  = require('./utils/stopwatch');
var expandTree = require('./utils/expand_tree');


////////////////////////////////////////////////////////////////////////////////


module.exports = function (tmpdir, sandbox, callback) {
  var
  config  = sandbox.config,
  timer   = stopwatch();

  try {
    _.each(config.packages, function (pkgConfig, pkgName) {
      var apiMethods = {};

      if (pkgConfig.server) {
        pkgConfig.server.files.forEach(function (pathname) {
          var
          init = pathname.require(),
          func = init(N, pathname.apiMethods);

          if (!_.isFunction(func)) {
            throw new Error(pathname + " constructor returned non-Function");
          }

          apiMethods[pathname.apiPath] = func;
        });

        // merge subtree into main tree
        _.extend(N.server, apiMethods);
      }
    });
  } catch (err) {
    callback(err);
    return;
  } finally {
    N.logger.info('Processed server section ' + timer.elapsed);
  }

  expandTree(N.server);

  callback();
};

// `server` section processor: read and populate N.server tree
//


'use strict';


/*global underscore, N*/


// stdlib
var fs    = require('fs');
var path  = require('path');


// 3rd-party
var _       = underscore;
var fstools = require('fs-tools');


// internal
var stopwatch  = require('./utils/stopwatch');
var expandTree = require('./utils/expand_tree');


////////////////////////////////////////////////////////////////////////////////


module.exports = function (tmpdir, sandbox, callback) {
  var
  config  = sandbox.config,
  timer   = stopwatch();

  try {
    _.each(config.packages, function (pkgConfig, pkgName) {
      var apiMethods = {};

      if (pkgConfig.server) {
        pkgConfig.server.files.forEach(function (pathname) {
          var
          init = pathname.require(),
          func = init(N, pathname.apiPath);

          if (!_.isFunction(func)) {
            throw new Error(pathname + " constructor returned non-Function");
          }

          apiMethods[pathname.apiPath] = func;
        });

        // merge subtree into main tree
        _.extend(N.server, apiMethods);
      }
    });
  } catch (err) {
    callback(err);
    return;
  } finally {
    N.logger.info('Processed server section ' + timer.elapsed);
  }

  expandTree(N.server);

  callback();
};

Fix server tree init