-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathGruntfile.js
90 lines (76 loc) · 2.22 KB
/
Gruntfile.js
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
var fs = require('fs');
var path = require('path');
var http = require('http');
module.exports = function(grunt) {
var async = grunt.util.async;
var log = grunt.log;
// Available versions of jQuery on CDN
var jQVersions = ['1.6.4', '1.7.2', '1.8.3', '1.9.1', '2.0.0'];
var cdnHost = 'code.jquery.com';
var destDir = path.join(__dirname, 'src');
function fetchjQuery(jQVersion, callback) {
var name = 'jquery-' + jQVersion + '.js';
var filePath = path.join(destDir, name);
// If file already exists, then don't download it again
if(fs.existsSync(filePath)) {
log.writeln('\u25e6', name);
setTimeout(callback, 100);
return;
}
// Stream the response in to files
var stream = fs.createWriteStream(filePath, {
'encoding': 'utf8'
});
http.request({
'host': cdnHost,
'path': '/' + name,
'headers': {
'Host': cdnHost
}
}, function(response) {
response.setEncoding('utf8');
response.on('data', stream.write.bind(stream));
response.on('end', function() {
stream.end();
log.writeln('\u2713', name);
setTimeout(callback, 100);
});
response.on('error', function(e) {
log.writeln('error', e, name);
});
}).end();
}
grunt.registerTask('download', 'Download jQuery versions from CDN', function() {
// This task is async
var done = this.async();
async.forEach(jQVersions, fetchjQuery, done);
});
// Project configuration.
grunt.initConfig({
'pkg': grunt.file.readJSON('package.json'),
'nodeunit': {
'files': 'test/index.js'
},
'watch': {
'files': ['test/**/*.js'],
'tasks': ['nodeunit'],
'options': {
'debounceDelay': 250
}
},
'jshint': {
'files': ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js'],
'options': {
'jshintrc': '.jshintrc'
}
}
});
// Load grunt plugins
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
// Register tasks.
grunt.registerTask('default', ['jshint', 'download', 'nodeunit']);
// Alias "test" to "nodeunit"
grunt.registerTask('test', ['nodeunit']);
};