forked from pablocubico/ufcaruf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volofile
282 lines (240 loc) · 9.86 KB
/
volofile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*jslint regexp: true */
/*global define, console, process */
var crypto = require('crypto');
var fs = require('fs');
var path = require('path');
var buildDir = 'www-built';
var pagesDir = 'www-ghpages';
try {
var ghdeploy = require('volo-ghdeploy')('www-built', 'www-ghdeploy');
}
catch(e) {
console.log("You don't have the volo-ghdeploy command installed.\n" +
'You must install this first:\n\n' +
'npm install -g volo-ghdeploy');
process.exit(1);
}
module.exports = {
// Installs twitter bootstrap
install_twbootstrap: {
run: function (d, v, namedArgs, nowrap) {
var tempName = 'tempbootstrap',
jsNameRegExp = /bootstrap-(\w*)\.js$/;
function finished_message() {
console.log('\nTwitter Bootstrap installed.\n' +
'Add this css in your <head>:\n' +
'<link rel="stylesheet"' +
' href="css/bootstrap.css">\n\n' +
'If you want the responsive styles, ' +
'add this also:\n' +
'<link rel="stylesheet"' +
' href="css/bootstrap-responsive.css">\n');
if(nowrap) {
console.log('You also need to include any ' +
'js for components you use, like so:\n' +
'<script src="js/lib/bootstrap/button.js">' +
'</script>');
}
else {
// Double-quoting the require command is
// necessary so that require.js doesn't strip
// it out
console.log('You also need to install any ' +
'js components by just requiring ' +
'them, like so:\n' +
"require(\"bootstrap/button\");");
}
}
v.command('create', tempName, 'twitter/bootstrap')
.then(function () {
//Move the JS to the right location.
var jsFiles = v.getFilteredFileList(tempName + '/js', /\.js$/, /js\/tests\//),
promises = [];
jsFiles.forEach(function (file) {
//Pull off the name part from bootstrap-name.js pattern.
var match = jsNameRegExp.exec(file),
name, destName, damd;
if (!match) {
return;
}
name = jsNameRegExp.exec(file)[1];
destName = 'www/js/lib/bootstrap/' + name + '.js';
damd = q.defer();
v.copyFile(file, destName);
if(nowrap != 'nowrap') {
//Convert the file to AMD style
amdify.run.apply(amdify, [damd, v, {
depends: 'jquery'
}, destName]);
}
promises.push(damd);
});
//Wait for all the amdify calls to finish.
return q.all(promises);
})
.then(function () {
//Copy the images over.
v.copyDir(tempName + '/img', 'www/img');
//Copy the less files.
v.copyDir(tempName + '/less', 'tools/less');
//Compile the CSS.
return v.command('bootstrap_less').then(function () {
v.rm(tempName);
finished_message();
});
})
.then(d.resolve, d.reject);
}
},
//Builds the JS and CSS into one file each. If you want to do
//dynamic loading of scripts, pass -dynamic to the build, and
//require.js will be used to load scripts.
build: {
flags: {
//Does not print the build output.
'q': 'quiet'
},
depends: ['less'],
run: 'node tools/r.js -o tools/build.js'
},
//Generates an SHA1 digest that represents the contents of the
//a directory. Call it like so: "volo digest dir=path/to/directory"
digest: {
validate: function (namedArgs) {
var dir = namedArgs.dir;
if (!dir) {
return new Error('Please specify a target directory for ' +
'the digest');
}
if (!path.existsSync(dir)) {
return new Error('Target directory for digest does ' +
'not exist: ' + dir);
}
return undefined;
},
run: function (d, v, namedArgs) {
var q = v.require('q');
var dir = namedArgs.dir,
files = v.getFilteredFileList(dir),
digests = [],
i = 0;
function getDigest(fileName) {
var shaSum = crypto.createHash('sha1'),
d = q.defer(),
stream = fs.ReadStream(fileName);
stream.on('data', function(data) {
shaSum.update(data);
});
stream.on('end', function() {
d.resolve(shaSum.digest('base64'));
});
return d.promise;
}
function digestFile(fileName) {
getDigest(fileName).then(function (digest) {
var shaSum;
digests[i] = digest;
i += 1;
if (i < files.length) {
digestFile(files[i]);
} else {
//All done, now generate the final digest,
//using the combination of the other digests
shaSum = crypto.createHash('sha1');
shaSum.update(digests.join(','));
d.resolve(shaSum.digest('base64'));
}
});
}
digestFile(files[0]);
}
},
//Compile all the less files into css
less: function(d, v, namedArgs) {
var q = v.require('q');
var files = v.getFilteredFileList('www/css');
return q.all([
files.map(function (path) {
if (/\.less$/.test(path)) {
var dest = path.replace(/\.less$/, '.css');
return v.exec(['node tools/oneless.js ' + path + ' > ' + dest]);
}
})
])
.then(function() {
d.resolve();
});
},
ghdeploy: ghdeploy,
//Runs less on the .less files in tools/less to generate the CSS files.
bootstrap_less: function (d, v, namedArgs) {
q.all([
v.exec('node tools/oneless.js tools/less/bootstrap.less > www/css/bootstrap.css'),
v.exec('node tools/oneless.js tools/less/responsive.less > www/css/bootstrap-responsive.css')
])
.then(function () {
d.resolve();
})
.fail(d.reject);
},
appcache: function (d, v, namedArgs) {
var q = v.require('q');
var hasBuilt = v.exists(buildDir);
v.command('build')
.then(function () {
var manifest = v.read('tools/manifest.appcache'),
master = v.read(buildDir + '/index.html'),
appFiles;
appFiles = v.getFilteredFileList(buildDir);
appFiles = appFiles.map(function (file) {
var start = file.indexOf('/' + buildDir + '/');
start = (start !== -1) ? (start + 11) : 0;
return file.substr(start, file.length);
});
master = master
.replace(/<html\s?/, '<html manifest="manifest.appcache" ')
.replace(/manifest\.appcache"\s>/, 'manifest.appcache">');
v.write(buildDir + '/index.html', master);
return v.command('digest', 'dir=' + buildDir)
.then(function (stamp) {
manifest = v.template(manifest, {
files : appFiles.join('\n'),
stamp : stamp
});
v.write(buildDir + '/manifest.appcache', manifest);
});
})
.then(function () {
//Inform the user of the right mime type, but only do it if
//there was not a previous build done.
d.resolve(hasBuilt ? '': 'Be sure to set the mime type for ' +
'.appcache files to be: text/cache-manifest');
})
.fail(d.reject);
},
serve: function(d, v, namedArgs) {
try {
var connect = require('connect');
}
catch(e) {
console.log('To use the `serve` command, you must ' +
'install the connect module:\n\n' +
'npm install connect');
return;
}
var lessMiddleware = require('less-middleware');
var port = 8008;
var base = path.join(process.cwd(), namedArgs.base || 'www');
var middleware = [
lessMiddleware({ src: base }),
connect.static(base),
connect.directory(base),
];
connect.logger.format("OpenWebApp",
"[D] server :method :url :status " +
":res[content-length] - :response-time ms");
middleware.unshift(connect.logger("OpenWebApp"));
console.log("starting web server on port " + port);
connect.apply(null, middleware).listen(port);
}
};