1 |
|
2 | var connect, exec, fs, _ref;
|
3 |
|
4 | connect = require('connect');
|
5 |
|
6 | fs = require('fs');
|
7 |
|
8 | if ((_ref = fs.exists) == null) {
|
9 | fs.exists = require('path').exists;
|
10 | }
|
11 |
|
12 | exec = require('child_process').exec;
|
13 |
|
14 | module.exports = function(options) {
|
15 | var args, count, dir, finish, loading, mapping, plugin, submodules, _finish, _ref1, _ref2, _ref3;
|
16 | if (options == null) {
|
17 | options = {};
|
18 | }
|
19 | if ((_ref1 = options.method) == null) {
|
20 | options.method = 'release';
|
21 | }
|
22 | if ((_ref2 = options.version) == null) {
|
23 | options.version = '1.7.1';
|
24 | }
|
25 | if ((_ref3 = options.repository) == null) {
|
26 | options.repository = '/tmp';
|
27 | }
|
28 | loading = true;
|
29 | submodules = ['dojo', 'dijit', 'dojox', 'util'];
|
30 | mapping = {};
|
31 | args = [];
|
32 | switch (options.method) {
|
33 | case 'release':
|
34 | dir = options.repository + '/dojo-release-' + options.version;
|
35 | mapping = {
|
36 | dojo: dir + '/dojo',
|
37 | dijit: dir + '/dijit',
|
38 | dojox: dir + '/dojox',
|
39 | util: dir + '/util'
|
40 | };
|
41 | fs.exists(dir, function(exists) {
|
42 | var cmd, tgz, url;
|
43 | if (exists) {
|
44 | return finish();
|
45 | }
|
46 | url = 'http://download.dojotoolkit.org/release-' + options.version + '/dojo-release-' + options.version + '.tar.gz';
|
47 | tgz = options.repository + '/dojo-release-' + options.version + '.tar.gz';
|
48 | cmd = 'curl ' + url + ' -o ' + tgz + ' && tar -xzf ' + tgz + ' -C ' + options.repository;
|
49 | return exec(cmd, function(err, stdout, stderr) {
|
50 | return finish(err);
|
51 | });
|
52 | });
|
53 | break;
|
54 | case 'git':
|
55 | count = 0;
|
56 | _finish = function() {
|
57 | if (++count !== 4) {
|
58 | return;
|
59 | }
|
60 | return finish();
|
61 | };
|
62 | submodules.forEach(function(submodule) {
|
63 | var checkout, clone, dirname, revision;
|
64 | revision = options[submodule + '_revision'] || 'HEAD';
|
65 | dirname = 'git-' + submodule + '-' + revision;
|
66 | mapping[submodule] = options.repository + '/' + dirname;
|
67 | clone = function(next) {
|
68 | return fs.exists(options.repository + '/' + dirname, function(exists) {
|
69 | var cmds, url;
|
70 | if (exists && revision !== 'HEAD') {
|
71 | return _finish();
|
72 | }
|
73 | if (exists) {
|
74 | return next();
|
75 | }
|
76 | url = 'https://github.com/dojo/' + submodule + '.git';
|
77 | cmds = [];
|
78 | cmds.push('cd ' + options.repository);
|
79 | cmds.push('git clone ' + url + ' ' + dirname);
|
80 | cmds = cmds.join(' && ');
|
81 | return exec(cmds, function(err, stdout, stderr) {
|
82 | return next(err);
|
83 | });
|
84 | });
|
85 | };
|
86 | checkout = function(next) {
|
87 | var cmds;
|
88 | cmds = [];
|
89 | cmds.push('cd ' + options.repository + '/' + dirname);
|
90 | cmds.push('git checkout ' + revision);
|
91 | cmds = cmds.join(' && ');
|
92 | return exec(cmds, function(err, stdout, stderr) {
|
93 | return next(err);
|
94 | });
|
95 | };
|
96 | return clone(function(err) {
|
97 | if (err) {
|
98 | return finish(err);
|
99 | }
|
100 | return checkout(function(err) {
|
101 | if (err) {
|
102 | return finish(err);
|
103 | }
|
104 | return _finish();
|
105 | });
|
106 | });
|
107 | });
|
108 | break;
|
109 | default:
|
110 | throw new Error('Invalid method option "' + options.method + '" (expects "download")');
|
111 | }
|
112 | finish = function(err) {
|
113 | var arg, _i, _len, _results;
|
114 | if (err) {
|
115 | throw err;
|
116 | }
|
117 | loading = false;
|
118 | _results = [];
|
119 | for (_i = 0, _len = args.length; _i < _len; _i++) {
|
120 | arg = args[_i];
|
121 | _results.push(plugin.apply(null, arg));
|
122 | }
|
123 | return _results;
|
124 | };
|
125 | plugin = function(req, res, next) {
|
126 | var app, sttc;
|
127 | if (loading) {
|
128 | return args.push(arguments);
|
129 | }
|
130 | app = /^\/(\w+)\/.*/.exec(req.url);
|
131 | if (app && submodules.indexOf(app[1]) !== -1) {
|
132 | app = app[1];
|
133 | req.url = req.url.substr(app.length + 1);
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | sttc = connect["static"](mapping[app]);
|
147 | return sttc(req, res, function() {
|
148 | req.url = '/' + app + req.url;
|
149 | return next();
|
150 | });
|
151 | } else {
|
152 | return next();
|
153 | }
|
154 | };
|
155 | return plugin;
|
156 | };
|