amdjs/amdjs-tests — pull request #19 by rbackhouse (2013-03-31 15:09:47+00:00)
Title: Support AMD loaders that provide a Node.js based server-side component 

I have an AMD loader that provides a server-side component (Node.js based). I would like to add the loader to the set of implementations.

To do this I have modified the test framework to support manifest entries providing installers. A manifest can contain the module id of an installer module. e.g

exports.manifest.zazl = {
name: 'zazl @ 1.0.2',
impl: 'zazl/zazl.js',
config: 'zazl/config.js',
installer: '../impl/zazl/installer'
};

When the test framework server is started it will scan the manifests for installer properties. If any are found the installer module is loaded and an expected install function called.

installer = require(manifest[framework].installer);
// Call the installer with the express app object allowing it to setup any handlers
installer.install(app, installerCallback);

The express "app" object is passed to the installer along with a reference to an installerCallback function the the installer calls to indicate it is complete. The callback function starts the framework once all discovered installers have completed.

If no installers are discovered then the server is started directly.

An additional change in this pull request is to add npm as a dev dependency to the package.json. This allows installers to use npm to programmatically install what they need.

An example installer look like this :

var npm = require('npm');
var path = require('path');
var fs = require('fs');
var util = require('util');

function startZazl(zazloptimizer, app) {
var testsdir = fs.realpathSync(path.join(__dirname, "../../tests"));
var optimizer = zazloptimizer.createConnectOptimizer(testsdir, false);
app.use("/_javascript", optimizer);
}

exports.install = function(app, cb) {
npm.load([], function(err, npm) {
if (err) {
cb("zazl", false);
return;
}
var zazloptimizer;
try {
zazloptimizer = require('zazloptimizer');
util.log("zazloptimizer is already installed");
startZazl(zazloptimizer, app);
cb("zazl", true);
} catch (e) {
npm.commands.install(['zazloptimizer'], function() {
util.log("zazloptimizer has been installed");
zazloptimizer = require('zazloptimizer');
startZazl(zazloptimizer, app);
cb("zazl", true);
});
}
});
}