Skip to content

Commit

Permalink
Implement git method; Convert to coffee-script; Add samples and impro…
Browse files Browse the repository at this point in the history
…ve the readme
  • Loading branch information
wdavidw committed Aug 3, 2011
1 parent a887f7c commit cf445d2
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 70 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,4 +1,6 @@
.*
!.gitignore
/node_modules
/releases/*
/repository/*
/sample/git_head/*
/sample/git_revision/*
58 changes: 3 additions & 55 deletions index.js
@@ -1,56 +1,4 @@
var connect = require('connect'),
path = require('path'),
exec = require('child_process').exec;

module.exports = function(options){
// Merge user options with default options
if(!options){ options = {}; }
options.method = options.method || 'download';
options.version = options.version || '1.6.1rc1';
options.repo_dir = options.repo_dir || __dirname+'/releases';
options.dojo_dir = options.repo_dir+'/dojo-release-'+options.version;
// Store HTTP request in case we need to download Dojo
var loading, args = [];
switch(options.method){
case 'download':
if(!path.existsSync(options.dojo_dir)){
loading = true;
var url = 'http://download.dojotoolkit.org/release-'+options.version+'/dojo-release-'+options.version+'.tar.gz';
var destinationTgz = options.repo_dir+'/dojo-release-'+options.version+'.tar.gz';
var cmd = 'curl '+url+' -o '+destinationTgz+' && tar -xzf '+destinationTgz+' -C '+options.repo_dir;
exec(cmd, function(err, stdout, stderr){
if(err){ throw err };
loading = false;
args.forEach(function(arg){
plugin.apply(null,arg);
});
});
}
break;
default:
throw new Error('Invalid method option "'+options.method+'" (expects "download")');
}
var plugin = function(req,res,next){
if(loading){
args.push(arguments);
return;
}
var app = /^\/(\w+)\/.*/.exec(req.url);
if(app && ['dojo','dijit','dojox','util'].indexOf(app[1]) !== -1){
var app = app[1];
req.url = req.url.substr(app.length+1);
// Less
connect.compiler({ src: options.dojo_dir+'/'+app, enable: ['less'] })(req,res,function(err){
if(err){ console.log(err); }
// Static
connect.static(options.dojo_dir+'/'+app)(req,res,function(){
req.url = '/'+app+req.url;
next();
});
});
}else{
next();
}
};
return plugin;
};
require('coffee-script');

module.exports = require( './lib/dojo' );
88 changes: 88 additions & 0 deletions lib/dojo.coffee
@@ -0,0 +1,88 @@
connect = require('connect')
path = require('path')
exec = require('child_process').exec

module.exports = (options) ->
# Merge user options with default options
options ?= {}
options.method ?= 'release'
options.version ?= '1.6.1rc1'
options.repository ?= '/tmp'
# Store HTTP request in case we need to download Dojo
loading = true
submodules = ['dojo', 'dijit', 'dojox', 'util']
mapping = {}
args = []
switch options.method
when 'release'
dir = options.repository + '/dojo-release-' + options.version
mapping =
dojo: dir + '/dojo'
dijit: dir + '/dijit'
dojox: dir + '/dojox'
util: dir + '/util'
path.exists dir, (exists) ->
return finish() if exists
url = 'http://download.dojotoolkit.org/release-' + options.version + '/dojo-release-' + options.version + '.tar.gz'
tgz = options.repository + '/dojo-release-' + options.version + '.tar.gz'
cmd = 'curl ' + url + ' -o ' + tgz + ' && tar -xzf ' + tgz + ' -C ' + options.repository
exec cmd, (err, stdout, stderr) ->
finish(err)
when 'git'
count = 0
_finish = ->
return if ++count isnt 4
finish()
submodules.forEach (submodule) ->
revision = options[ submodule + '_revision' ] or 'HEAD'
dirname = 'git-' + submodule + '-' + revision
clone = (next) ->
path.exists options.repository + '/' + dirname, (exists) ->
return next() if exists
url = 'https://github.com/dojo/' + submodule + '.git'
cmds = []
cmds.push 'cd ' + options.repository
cmds.push 'git clone ' + url + ' ' + dirname
cmds = cmds.join ' && '
console.log cmds
exec cmds, (err, stdout, stderr) ->
next(err)
checkout = (next) ->
cmds = []
cmds.push 'cd ' + options.repository + '/' + dirname
cmds.push 'git checkout ' + revision
cmds = cmds.join ' && '
console.log cmds
exec cmds, (err, stdout, stderr) ->
next(err)
clone (err) ->
return finish err if err
checkout (err) ->
return finish err if err
mapping[submodule] = options.repository + '/' + dirname
_finish()
else
throw new Error 'Invalid method option "' + options.method + '" (expects "download")'
finish = (err) ->
throw err if err
loading = false
for arg in args
plugin.apply null, arg
plugin = (req, res, next) ->
return args.push arguments if loading
app = /^\/(\w+)\/.*/.exec req.url
if app and submodules.indexOf( app[1] ) isnt -1
app = app[1];
req.url = req.url.substr app.length + 1
# Less
connect.compiler({ src: mapping[app], enable: ['less'] })(req, res, (err) ->
console.log err if err
# Static
static = connect.static mapping[app]
static req, res, ->
req.url = '/' + app + req.url
next()
)
else
next()
plugin
4 changes: 2 additions & 2 deletions package.json
@@ -1,11 +1,11 @@
{ "name": "connect-dojo"
, "version": "0.0.2"
, "version": "0.0.1"
, "description": "Connect middleware exposing the Dojo Toolkit"
, "author": "David Worms <david@adaltas.com>"
, "contributors":
[ { "name": "David Worms", "email": "david@adaltas.com" } ]
, "engines": { "node": ">= 0.1.90" }
, "dependencies" : { "connect" : "1" }
, "dependencies" : { "coffee-script" : "latest" }
, "keywords": ["connect", "express", "dojo"]
, "repository":
{ "type" : "git"
Expand Down
70 changes: 65 additions & 5 deletions readme.md
@@ -1,8 +1,68 @@
Connect middleware exposing the Dojo Toolkit
============================================

Options are
- method, only support `'download'` for now, the idea is to add `'git'`
- version, Dojo version, currently `'1.6.1rc1'`
- repo_dir, Directory where Dojo source code are downloaded
- dojo_dir, Directory where to download the Dojo source for the current version
This Connect middleware transparently download and display Dojo files.

In its simplest form, it takes no argument and the latest stable release is used. Here is a quick example:

var express = require('express');
var dojo = require('connect-dojo');
var app = express.createServer();
app.configure(
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(dojo());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
);
app.listen(3000);

Options
-------

- *method*, support 'release' (default) and 'git'
- *repository*, Default parent directory where source codes are downloaded
- *version*, Dojo version, currently `'1.6.1rc1'`, apply only to the 'download' method

Using a released version of Dojo
--------------------------------

Source code is downloaded from the official Dojo website and all the versions
present on the website are available. For example to download the version '1.5.0', setup
the middleware as:

dojo({ version: '1.5.0' })

Using the git HEAD
------------------

If the option 'method' is set to 'git' and the revision option of a dojo project
is not defined, the middleware will checkout the HEAD revision. Each Dojo project
will be store inside a directory defined by the option 'repository' and named as "git-#{project}-HEAD"

Using a specific git revision
-----------------------------

If the option 'method' is set to 'git' and one of 'dojo_revision', 'dijit_revision',
'dojox_revision' or 'util_revision' is defined, then the middleware to checkout
the revision in a specific directory inside the one defined by the option 'repository'
and named as "git-${project}-#{revision}".

General note
------------

Note, to prevent multiple application from conflicting, a new directory is created
for each released version and git revision. This can lead to a large number of directories
as new revisions are setup over time. By default, source code is stored inside the
connect-dojo module and it is recommended to define your own directory through
the 'repository' option.

Testing
-------

Running the tests can take a long time because of the size of dojo. Using
expresso, run the following command by adjusting the '-t' (timeout) argument:

expresso -s -t 10000 test
2 changes: 2 additions & 0 deletions repository/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
18 changes: 18 additions & 0 deletions sample/git_head.js
@@ -0,0 +1,18 @@

var express = require('express');
var dojo = require('connect-dojo');
var app = express.createServer();
app.configure( function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(dojo({
method: 'git',
repository: __dirname + '/git_head'
}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
} );
app.listen(7000);
console.log('http://localhost:3000/dojo/dojo.js');
22 changes: 22 additions & 0 deletions sample/git_revision.js
@@ -0,0 +1,22 @@

var express = require('express');
var dojo = require('connect-dojo');
var app = express.createServer();
app.configure( function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(dojo({
method: 'git',
repository: __dirname + '/git_revision',
dojo_revision: '852b5161559f3eda16dc',
dijit_revision: '37b5298bb8b4f24134d5',
dojox_revision: '145d3bec095382c2f4ac',
util_revision: 'f9cbb550e2959024df57'
}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
} );
app.listen(7000);
console.log('http://localhost:3000/dojo/dojo.js');
15 changes: 15 additions & 0 deletions sample/simple.js
@@ -0,0 +1,15 @@

var express = require('express');
var dojo = require('connect-dojo');
var app = express.createServer();
app.configure( function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(dojo());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
} );
app.listen(7000);
console.log('http://localhost:3000/dojo/dojo.js');
7 changes: 0 additions & 7 deletions test/download.js

This file was deleted.

19 changes: 19 additions & 0 deletions test/git.coffee
@@ -0,0 +1,19 @@

dojo = require('connect-dojo');

module.exports =
'Test git # HEAD': (next) ->
middleware = dojo { method: 'git' }
req = { url: 'http://localhost' }
res = {}
middleware req, res, next
'Test git # revision': (next) ->
middleware = dojo
method: 'git',
dojo_revision: '852b5161559f3eda16dc'
dijit_revision: '37b5298bb8b4f24134d5'
dojox_revision: '145d3bec095382c2f4ac'
util_revision: 'f9cbb550e2959024df57'
req = { url: 'http://localhost' }
res = {}
middleware req, res, next
9 changes: 9 additions & 0 deletions test/release.coffee
@@ -0,0 +1,9 @@

dojo = require('connect-dojo');

module.exports =
'Test release # version': (next) ->
middleware = dojo { version: '1.5.0' }
req = { url: 'http://localhost' }
res = {}
middleware req, res, next

0 comments on commit cf445d2

Please sign in to comment.