-
Notifications
You must be signed in to change notification settings - Fork 92
/
Gruntfile.js
103 lines (95 loc) · 2.91 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
91
92
93
94
95
96
97
98
99
100
101
102
103
module.exports = function(grunt) {
var fs = require('fs');
var async = require('async');
var request = require('request');
var semver = require('semver');
var modules = fs.readdirSync('.').filter(function(name) {
return fs.statSync(name).isDirectory() && !grunt.util._.contains(['.git', '_tasks', 'node_modules'], name);
});
grunt.registerTask('check', function() {
var done = this.async();
var q = async.queue(function(pkg, callback) {
grunt.log.verbose.writeln('processing ' + pkg.name);
checkupdate(pkg, function(err, version) {
if (err) {
grunt.log.warn(err);
} else {
if (version && version !== pkg.version) {
grunt.log.writeln(print(pkg.name, pkg.version, version));
} else {
grunt.log.writeln(print(pkg.name, pkg.version));
}
}
});
}, modules.length);
modules.forEach(function(m) {
try {
var pkg = require('./' + m + '/package.json');
q.push(pkg, function(err) {
if (err) {
grunt.log.verbose.error(err);
} else {
grunt.log.verbose.writeln('add task ' + pkg.name);
}
});
} catch (e) {
grunt.log.verbose.error(e);
}
});
q.drain = function() {
done();
};
});
grunt.registerTask('default', ['check']);
function checkupdate(pkg, callback) {
if (pkg.package) {
grunt.log.verbose.writeln(pkg.package);
request({json: true, url: pkg.package}, function(err, res, body) {
if (err) {
callback(err);
} else if (res.statusCode !== 200) {
callback('status code: ' + res.statusCode + ' ' + pkg.package);
} else {
callback(null, body.version);
}
});
} else if (pkg.repository) {
var repo = pkg.repository.url;
repo = repo.replace('https://github.com/', '');
repo = repo.replace('git://github.com/', '');
repo = repo.replace('[email protected]:', '');
repo = repo.replace('.git', '');
getVersion(repo, callback);
} else {
callback('check ' + pkg.homepage);
}
}
function getVersion(repo, callback) {
var uri = 'https://api.github.com/repos/' + repo + '/tags'
request({
json: true,
url: uri,
headers: {
'User-Agent': 'request'
}
}, function(err, res, body) {
if (err) {
callback(err);
} else if (res.statusCode !== 200) {
callback('status code: ' + res.statusCode + ' ' + uri);
} else {
var names = body.map(function(tag) {
var version = semver.valid(tag.name);
if (version) return version;
});
callback(null, names.sort().pop());
}
});
}
function print(name, version, latest) {
var s = name + new Array(25 - name.length).join(' ');
s += version + new Array(15 - version.length).join(' ');
s += latest || '';
return s;
}
};