This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathexpose.js
63 lines (49 loc) · 2.09 KB
/
expose.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
var path = require('path'),
fs = require('fs');
var crypto = require('crypto'),
shasum = crypto.createHash('sha1');
module.exports = function(grunt) {
'use strict';
var _ = grunt.util._;
function generatesha1(filename) {
var content = fs.readFileSync(filename);
shasum.update("blob " + content.length + '\0', 'utf8');
shasum.update(content);
return shasum.digest('hex');
}
grunt.registerTask(
'expose', "Expose available tasks as JSON object.", function (gruntFileName) {
var cwd = process.cwd();
gruntFileName = path.join(cwd, gruntFileName);
var cacheFileName = path.join(cwd, '.sublime-grunt.cache');
var sha1 = generatesha1(gruntFileName);
var gruntsublimecache = (grunt.file.exists(cacheFileName) && grunt.file.readJSON(cacheFileName)) || {};
if (!gruntsublimecache[gruntFileName] || gruntsublimecache[gruntFileName].sha1 !== sha1) {
var tasks = grunt.task._tasks;
_.each( tasks, function( value, key, list ) {
// We don't want to shouw or own task
if (key === 'expose') {
delete list[key];
}
else {
// Filter away reservered words that are none-targets
var targets = _.difference(Object.keys(grunt.config.getRaw( key ) || {}), ['files', 'options', 'globals']);
if ( targets.length > 0 ) {
list[ key ].targets = targets;
if ( targets.length > 1 ) {
_.each( targets, function(target) {
var name = key + ":" + target;
list[name] = { name: name, info: 'Targets ' + name + '. ' + list[key].info || '', meta: { info: list[key].meta&&list[key].meta.info} };
});
}
}
}
});
gruntsublimecache[gruntFileName] = gruntsublimecache[gruntFileName] || {};
gruntsublimecache[gruntFileName].sha1 = sha1;
gruntsublimecache[gruntFileName].tasks = tasks;
grunt.file.write(cacheFileName, JSON.stringify(gruntsublimecache));
}
}
);
};