Skip to content

Commit 33fc34a

Browse files
committed
feat(gulp): run gulp tasks
Following the convention cmd:before and cmd:after. Example: `ionic serve` Would execute tasks 'serve:before' and 'serve:after' before and after the command, respectively.
1 parent e6b844e commit 33fc34a

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

lib/cli.js

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ var Cli = module.exports,
1010
Ionitron = require('./ionic/ionitron'),
1111
optimist = require('optimist'),
1212
path = require('path'),
13+
fs = require('fs'),
1314
settings = require('../package.json'),
1415
Tasks = require('./tasks/cliTasks'),
1516
Utils = IonicAppLib.utils,
1617
logging = IonicAppLib.logging,
1718
Q = require('q'),
18-
semver = require('semver');
19+
semver = require('semver'),
20+
gutil = require('gulp-util'),
21+
chalk = require('chalk'),
22+
prettyTime = require('pretty-hrtime');
1923

2024
Cli.Tasks = TASKS = Tasks;
2125

@@ -130,16 +134,90 @@ Cli.run = function run(processArgv) {
130134
argv.v2 = true;
131135
}
132136

133-
// Only some tasks return promises
134-
var promise = Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
137+
var gulpFound = loadGulp();
138+
if (gulpFound) {
139+
logging.logger.debug('Gulpfile found');
140+
var gulp = require(path.resolve(process.cwd() + '/node_modules/gulp'));
141+
logEvents(gulp);
142+
143+
return Q.nfcall(gulp.start.bind(gulp), argv._[0] + ':before').then(
144+
function(){
145+
// Only some tasks return promises
146+
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
147+
}
148+
).then(function(){
149+
return Q.nfcall(gulp.start.bind(gulp), argv._[0] + ':after');
150+
});
151+
} else {
152+
logging.logger.debug('No gulpfile found');
153+
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
154+
}
135155

136-
return promise;
137156
} catch (ex) {
138157
logging.logger.debug('Cli.Run - Error', ex);
139158
return Utils.fail(ex);
140159
}
141160
};
142161

162+
function loadGulp(){
163+
var names = ['gulpfile.js', 'Gulpfile.js'];
164+
var found = false;
165+
for (var i = 0, ii = names.length; i < ii; i++) {
166+
try {
167+
require(path.resolve(process.cwd() + '/' + names[i]));
168+
found = true;
169+
} catch(e){
170+
if (e instanceof SyntaxError) {
171+
console.error('\nThere is a syntax error in your gulpfile:');
172+
console.error(e.message)
173+
process.exit(1);
174+
}
175+
if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(names[i]) === -1) {
176+
console.log('\nLooks like you\'re missing a module in your gulpfile:');
177+
console.error(e.message);
178+
console.log('\nDo you need to `npm install`?\n');
179+
process.exit(1);
180+
}
181+
}
182+
}
183+
return found;
184+
}
185+
186+
function logEvents(gulpInst) {
187+
gulpInst.on('task_start', function(e) {
188+
// TODO: batch these
189+
// so when 5 tasks start at once it only logs one time with all 5
190+
gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
191+
});
192+
193+
gulpInst.on('task_stop', function(e) {
194+
var time = prettyTime(e.hrDuration);
195+
gutil.log(
196+
'Finished', '\'' + chalk.cyan(e.task) + '\'',
197+
'after', chalk.magenta(time)
198+
);
199+
});
200+
201+
gulpInst.on('task_err', function(e) {
202+
var msg = formatError(e);
203+
var time = prettyTime(e.hrDuration);
204+
gutil.log(
205+
'\'' + chalk.cyan(e.task) + '\'',
206+
chalk.red('errored after'),
207+
chalk.magenta(time)
208+
);
209+
gutil.log(msg);
210+
});
211+
212+
gulpInst.on('task_not_found', function(err) {
213+
gutil.log(
214+
chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
215+
);
216+
gutil.log('Please check the documentation for proper gulpfile formatting');
217+
process.exit(1);
218+
});
219+
}
220+
143221
Cli.getBooleanOptionsForTask = function getBooleanOptionsForTask(task) {
144222
var availableTaskOptions = task.options;
145223
var booleanOptions = [];

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"license": "MIT",
6060
"dependencies": {
6161
"async": "0.9.2",
62+
"chalk": "^1.1.1",
6263
"cheerio": "0.19.0",
6364
"cli-table": "0.3.1",
6465
"colors": "0.6.2",
@@ -71,12 +72,14 @@
7172
"finalhandler": "0.2.0",
7273
"form-data": "0.1.4",
7374
"gulp": "3.8.8",
75+
"gulp-util": "^3.0.7",
7476
"inquirer": "0.11.2",
7577
"ionic-app-lib": "2.0.0-beta.9",
7678
"moment": "2.11.1",
7779
"ncp": "0.4.2",
7880
"open": "0.0.5",
7981
"optimist": "0.6.0",
82+
"pretty-hrtime": "^1.0.2",
8083
"progress": "1.1.7",
8184
"prompt": "0.2.12",
8285
"proxy-middleware": "0.7.0",

0 commit comments

Comments
 (0)