Skip to content

Commit

Permalink
feat(gulp): run gulp tasks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tlancina committed Mar 28, 2016
1 parent e6b844e commit 33fc34a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
86 changes: 82 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ var Cli = module.exports,
Ionitron = require('./ionic/ionitron'),
optimist = require('optimist'),
path = require('path'),
fs = require('fs'),
settings = require('../package.json'),
Tasks = require('./tasks/cliTasks'),
Utils = IonicAppLib.utils,
logging = IonicAppLib.logging,
Q = require('q'),
semver = require('semver');
semver = require('semver'),
gutil = require('gulp-util'),
chalk = require('chalk'),
prettyTime = require('pretty-hrtime');

Cli.Tasks = TASKS = Tasks;

Expand Down Expand Up @@ -130,16 +134,90 @@ Cli.run = function run(processArgv) {
argv.v2 = true;
}

// Only some tasks return promises
var promise = Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
var gulpFound = loadGulp();
if (gulpFound) {
logging.logger.debug('Gulpfile found');
var gulp = require(path.resolve(process.cwd() + '/node_modules/gulp'));
logEvents(gulp);

return Q.nfcall(gulp.start.bind(gulp), argv._[0] + ':before').then(
function(){
// Only some tasks return promises
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
}
).then(function(){
return Q.nfcall(gulp.start.bind(gulp), argv._[0] + ':after');
});
} else {
logging.logger.debug('No gulpfile found');
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
}

return promise;
} catch (ex) {
logging.logger.debug('Cli.Run - Error', ex);
return Utils.fail(ex);
}
};

function loadGulp(){
var names = ['gulpfile.js', 'Gulpfile.js'];
var found = false;
for (var i = 0, ii = names.length; i < ii; i++) {
try {
require(path.resolve(process.cwd() + '/' + names[i]));
found = true;
} catch(e){
if (e instanceof SyntaxError) {
console.error('\nThere is a syntax error in your gulpfile:');
console.error(e.message)
process.exit(1);
}
if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(names[i]) === -1) {
console.log('\nLooks like you\'re missing a module in your gulpfile:');
console.error(e.message);
console.log('\nDo you need to `npm install`?\n');
process.exit(1);
}
}
}
return found;
}

function logEvents(gulpInst) {
gulpInst.on('task_start', function(e) {
// TODO: batch these
// so when 5 tasks start at once it only logs one time with all 5
gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
});

gulpInst.on('task_stop', function(e) {
var time = prettyTime(e.hrDuration);
gutil.log(
'Finished', '\'' + chalk.cyan(e.task) + '\'',
'after', chalk.magenta(time)
);
});

gulpInst.on('task_err', function(e) {
var msg = formatError(e);
var time = prettyTime(e.hrDuration);
gutil.log(
'\'' + chalk.cyan(e.task) + '\'',
chalk.red('errored after'),
chalk.magenta(time)
);
gutil.log(msg);
});

gulpInst.on('task_not_found', function(err) {
gutil.log(
chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
);
gutil.log('Please check the documentation for proper gulpfile formatting');
process.exit(1);
});
}

Cli.getBooleanOptionsForTask = function getBooleanOptionsForTask(task) {
var availableTaskOptions = task.options;
var booleanOptions = [];
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"license": "MIT",
"dependencies": {
"async": "0.9.2",
"chalk": "^1.1.1",
"cheerio": "0.19.0",
"cli-table": "0.3.1",
"colors": "0.6.2",
Expand All @@ -71,12 +72,14 @@
"finalhandler": "0.2.0",
"form-data": "0.1.4",
"gulp": "3.8.8",
"gulp-util": "^3.0.7",
"inquirer": "0.11.2",
"ionic-app-lib": "2.0.0-beta.9",
"moment": "2.11.1",
"ncp": "0.4.2",
"open": "0.0.5",
"optimist": "0.6.0",
"pretty-hrtime": "^1.0.2",
"progress": "1.1.7",
"prompt": "0.2.12",
"proxy-middleware": "0.7.0",
Expand Down

0 comments on commit 33fc34a

Please sign in to comment.