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