1 | #!/usr/bin/env node
|
2 |
|
3 | var path = require('path');
|
4 |
|
5 | var argv = require('optimist').argv;
|
6 | var completion = require('../lib/completion');
|
7 |
|
8 | if (argv.completion) {
|
9 | return completion(argv.completion);
|
10 | }
|
11 |
|
12 | var resolve = require('resolve');
|
13 | var findup = require('findup-sync');
|
14 | var gutil = require('gulp-util');
|
15 | var prettyTime = require('pretty-hrtime');
|
16 |
|
17 | var tasks = argv._;
|
18 | var cliPkg = require('../package.json');
|
19 |
|
20 | var localBaseDir = process.cwd();
|
21 |
|
22 | loadRequires(argv.require, localBaseDir);
|
23 |
|
24 | var gulpFile = getGulpFile(localBaseDir);
|
25 |
|
26 |
|
27 | var localGulp = findLocalGulp(gulpFile);
|
28 | var localPkg = findLocalGulpPackage(gulpFile);
|
29 |
|
30 |
|
31 | if (argv.v || argv.version) {
|
32 | gutil.log('CLI version', cliPkg.version);
|
33 | if (localGulp) {
|
34 | gutil.log('Local version', localPkg.version);
|
35 | }
|
36 | process.exit(0);
|
37 | }
|
38 |
|
39 | if (!localGulp) {
|
40 | gutil.log(gutil.colors.red('No local gulp install found in'), getLocalBase(gulpFile));
|
41 | gutil.log(gutil.colors.red('Perhaps do: npm install gulp'));
|
42 | process.exit(1);
|
43 | }
|
44 |
|
45 | if (!gulpFile) {
|
46 | gutil.log(gutil.colors.red('No Gulpfile found'));
|
47 | process.exit(1);
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 | logEvents(localGulp);
|
53 |
|
54 |
|
55 | gutil.log('Using file', gutil.colors.magenta(gulpFile));
|
56 | loadGulpFile(localGulp, gulpFile, tasks);
|
57 |
|
58 | function loadRequires(requires, baseDir) {
|
59 | if (typeof requires === 'undefined') requires = [];
|
60 | if (!Array.isArray(requires)) requires = [requires];
|
61 | return requires.map(function(modName){
|
62 | gutil.log('Requiring external module', gutil.colors.magenta(modName));
|
63 | var mod = findLocalModule(modName, baseDir);
|
64 | if (typeof mod === 'undefined') {
|
65 | gutil.log('Failed to load external module', gutil.colors.magenta(modName));
|
66 | }
|
67 | });
|
68 | }
|
69 |
|
70 | function getLocalBase(gulpFile) {
|
71 | return path.resolve(path.dirname(gulpFile));
|
72 | }
|
73 |
|
74 | function findLocalGulp(gulpFile){
|
75 | var baseDir = getLocalBase(gulpFile);
|
76 | return findLocalModule('gulp', baseDir);
|
77 | }
|
78 |
|
79 | function findLocalModule(modName, baseDir){
|
80 | try {
|
81 | return require(resolve.sync(modName, {basedir: baseDir}));
|
82 | } catch(e) {}
|
83 | return;
|
84 | }
|
85 |
|
86 | function findLocalGulpPackage(gulpFile){
|
87 | var baseDir = getLocalBase(gulpFile);
|
88 | var packageBase;
|
89 | try {
|
90 | packageBase = path.dirname(resolve.sync('gulp', {basedir: baseDir}));
|
91 | return require(path.join(packageBase, 'package.json'));
|
92 | } catch(e) {}
|
93 | return;
|
94 | }
|
95 |
|
96 | function loadGulpFile(localGulp, gulpFile, tasks){
|
97 | var gulpFileCwd = path.dirname(gulpFile);
|
98 | process.chdir(gulpFileCwd);
|
99 | gutil.log('Working directory changed to', gutil.colors.magenta(gulpFileCwd));
|
100 |
|
101 | var theGulpfile = require(gulpFile);
|
102 |
|
103 |
|
104 | process.nextTick(function(){
|
105 | localGulp.run.apply(localGulp, tasks);
|
106 | });
|
107 | return theGulpfile;
|
108 | }
|
109 |
|
110 | function getGulpFile(baseDir) {
|
111 | var extensions;
|
112 | if (require.extensions) {
|
113 | extensions = Object.keys(require.extensions).join(',');
|
114 | } else {
|
115 | extensions = ['.js','.coffee'];
|
116 | }
|
117 | var gulpFile = findup('Gulpfile{'+extensions+'}', {nocase: true});
|
118 | return gulpFile;
|
119 | }
|
120 |
|
121 |
|
122 | function formatError (e) {
|
123 | if (!e.err) return e.message;
|
124 | if (e.err.message) return e.err.message;
|
125 | return JSON.stringify(e.err);
|
126 | }
|
127 |
|
128 |
|
129 | function logEvents(gulp) {
|
130 | gulp.on('task_start', function(e){
|
131 | gutil.log('Running', "'"+gutil.colors.cyan(e.task)+"'...");
|
132 | });
|
133 |
|
134 | gulp.on('task_stop', function(e){
|
135 | var time = prettyTime(e.hrDuration);
|
136 | gutil.log('Finished', "'"+gutil.colors.cyan(e.task)+"'", 'in', gutil.colors.magenta(time));
|
137 | });
|
138 |
|
139 | gulp.on('task_err', function(e){
|
140 | var msg = formatError(e);
|
141 | var time = prettyTime(e.hrDuration);
|
142 | gutil.log('Errored', "'"+gutil.colors.cyan(e.task)+"'", 'in', gutil.colors.magenta(time), gutil.colors.red(msg));
|
143 | });
|
144 |
|
145 | gulp.on('task_not_found', function(err){
|
146 | gutil.log(gutil.colors.red("Task '"+err.task+"' was not defined in your gulpfile but you tried to run it."));
|
147 | gutil.log('Please check the documentation for proper gulpfile formatting.');
|
148 | process.exit(1);
|
149 | });
|
150 | }
|