This repository has been archived by the owner on Oct 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpose.js
66 lines (52 loc) · 2.04 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
64
65
66
var path = require('path')
var fs = require('fs')
var crypto = require('crypto')
var 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))
}
})
}