-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
102 lines (84 loc) · 2.39 KB
/
index.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
var grunt = require('grunt');
var spawn = require('child_process').spawn;
var gruntCmd = (process.platform === 'win32') ? 'grunt.cmd' : 'grunt';
var makeOptions = function (options) {
var baseOptions = {
base: null,
prefix: 'grunt-',
verbose: false,
force: true
};
if (options) {
for (var key in options) {
baseOptions[key] = options[key];
if(key != 'base' && key != 'prefix'){
grunt.option(key, options[key]);
}
}
}
return baseOptions;
};
module.exports = function (gulp, options) {
var tasks = getTasks(options);
for (var name in tasks) {
if (tasks.hasOwnProperty(name)) {
var fn = tasks[name];
gulp.task(name, fn);
}
}
};
var getTasks = module.exports.tasks = function (options) {
var opt = makeOptions(options);
var oldCwd = process.cwd();
var cwd = opt.base != null ? opt.base : oldCwd;
grunt.file.setBase(cwd);
var gruntCliDir = opt.base ? (opt.base + "/") : "";
grunt.task.init([]);
process.chdir(oldCwd);
var gruntTasks = grunt.task._tasks,
finalTasks = {};
var registerGruntTask = function (name) {
finalTasks[opt.prefix + name] = function (cb) {
if (opt.verbose) {
console.log('[grunt-gulp] Running Grunt "' + name + '" task...');
}
var args = opt.force ? [name, '--force', '--verbose=' + opt.verbose] : [name, '--verbose=' + opt.verbose];
for (var key in opt) {
if (key != 'base' && key != 'prefix') {
args = args.concat('--' + key + '=' + opt[key]);
}
}
var child = spawn(
gruntCliDir + gruntCmd,
args,
{cwd: cwd}
);
child.stdout.on('data', function (d) {
grunt.log.write(d);
});
child.stderr.on('data', function (d) {
grunt.log.error(d);
});
child.on('close', function (code) {
if (opt.verbose) {
grunt.log.ok('[grunt-gulp] Done running Grunt "' + name + '" task.');
}
if (code != 0) {
grunt.fail.warn('[grunt-gulp] Failed running Grunt "' + name + '" task.')
}
cb();
});
};
}
for (var name in gruntTasks) {
if (gruntTasks.hasOwnProperty(name)) {
// add tasks
registerGruntTask(name);
// also add target-specific tasks
for (var target in grunt.config.get(name)) {
registerGruntTask(name + ':' + target);
}
}
}
return finalTasks;
};