/
gulp.js
executable file
·148 lines (121 loc) · 4.01 KB
/
gulp.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env node
var path = require('path');
var fs = require('fs');
var argv = require('optimist').argv;
var resolve = require('resolve');
var findup = require('findup-sync');
var gutil = require('gulp-util');
var prettyTime = require('pretty-hrtime');
var tasks = argv._;
var cliPkg = require('../package.json');
var localBaseDir = process.cwd();
var completion = require('../lib/completion');
if (argv.completion) { return completion.print(argv.completion); }
loadRequires(argv.require, localBaseDir);
var gulpFile = getGulpFile(localBaseDir);
// find the local gulp
var localGulp = findLocalGulp(gulpFile);
var localPkg = findLocalGulpPackage(gulpFile);
// print some versions and shit
if (argv.v || argv.version) {
gutil.log('CLI version', cliPkg.version);
if (localGulp) {
gutil.log('Local version', localPkg.version);
}
process.exit(0);
}
if (!localGulp) {
gutil.log(gutil.colors.red('No local gulp install found in'), getLocalBase(gulpFile));
gutil.log(gutil.colors.red('You need to npm install it first'));
process.exit(1);
}
if (!gulpFile) {
gutil.log(gutil.colors.red('No Gulpfile found'));
process.exit(1);
}
// Wire up logging for tasks
// on local gulp singleton
logEvents(localGulp);
// Load their gulpfile and run it
gutil.log('Using file', gutil.colors.magenta(gulpFile));
loadGulpFile(localGulp, gulpFile, tasks);
function loadRequires(requires, baseDir) {
if (typeof requires === 'undefined') requires = [];
if (!Array.isArray(requires)) requires = [requires];
return requires.map(function(modName){
gutil.log('Requiring external module', gutil.colors.magenta(modName));
var mod = findLocalModule(modName, baseDir);
if (typeof mod === 'undefined') {
gutil.log('Failed to load external module', gutil.colors.magenta(modName));
}
});
}
function getLocalBase(gulpFile) {
return path.resolve(path.dirname(gulpFile));
}
function findLocalGulp(gulpFile){
var baseDir = getLocalBase(gulpFile);
return findLocalModule('gulp', baseDir);
}
function findLocalModule(modName, baseDir){
try {
return require(resolve.sync(modName, {basedir: baseDir}));
} catch(e) {}
return;
}
function findLocalGulpPackage(gulpFile){
var baseDir = getLocalBase(gulpFile);
var packageBase;
try {
packageBase = path.dirname(resolve.sync('gulp', {basedir: baseDir}));
return require(path.join(packageBase, 'package.json'));
} catch(e) {}
return;
}
function loadGulpFile(localGulp, gulpFile, tasks){
var gulpFileCwd = path.dirname(gulpFile);
process.chdir(gulpFileCwd);
gutil.log('Working directory changed to', gutil.colors.magenta(gulpFileCwd));
var theGulpfile = require(gulpFile);
// just for good measure
process.nextTick(function(){
localGulp.run.apply(localGulp, tasks);
});
return theGulpfile;
}
function getGulpFile(baseDir) {
var extensions;
if (require.extensions) {
extensions = Object.keys(require.extensions).join(',');
} else {
extensions = ['.js','.coffee'];
}
var gulpFile = findup('Gulpfile{'+extensions+'}', {nocase: true});
return gulpFile;
}
// format orchestrator errors
function formatError (e) {
if (!e.err) return e.message;
if (e.err.message) return e.err.message;
return JSON.stringify(e.err);
}
// wire up logging events
function logEvents(gulp) {
gulp.on('task_start', function(e){
gutil.log('Running', "'"+gutil.colors.cyan(e.task)+"'...");
});
gulp.on('task_stop', function(e){
var time = prettyTime(e.hrDuration);
gutil.log('Finished', "'"+gutil.colors.cyan(e.task)+"'", 'in', gutil.colors.magenta(time));
});
gulp.on('task_err', function(e){
var msg = formatError(e);
var time = prettyTime(e.hrDuration);
gutil.log('Errored', "'"+gutil.colors.cyan(e.task)+"'", 'in', gutil.colors.magenta(time), gutil.colors.red(msg));
});
gulp.on('task_not_found', function(err){
gutil.log(gutil.colors.red("Task '"+err.task+"' was not defined in your gulpfile but you tried to run it."));
gutil.log('Please check the documentation for proper gulpfile formatting.');
process.exit(1);
});
}