Skip to content

Commit

Permalink
feat(hooks): don't run gulp if node_modules doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
tlancina committed Mar 28, 2016
1 parent c17d268 commit 52f1a8f
Showing 1 changed file with 71 additions and 83 deletions.
154 changes: 71 additions & 83 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Cli = module.exports,
IonicStore = require('./ionic/store').IonicStore,
IonicConfig = new IonicStore('ionic.config'),
Info = IonicAppLib.info,
IonicProject = IonicAppLib.project,
Ionitron = require('./ionic/ionitron'),
optimist = require('optimist'),
path = require('path'),
Expand Down Expand Up @@ -94,105 +95,92 @@ Cli.run = function run(processArgv) {
if(!taskSetting) {
return Cli.printAvailableTasks();
}

logging.logger.debug('Task setting:', taskSetting);
var booleanOptions = Cli.getBooleanOptionsForTask(taskSetting);
argv = optimist(processArgv.slice(2)).boolean(booleanOptions).argv;
var taskModule = Cli.lookupTask(taskSetting.module);
var taskInstance = new taskModule();

// assume false for tasks that can be run outside of Ionic projects
// start, help, docs
var isIonicV2 = false;

// Addressing issue #471
// Changes the cwd to the project root
// (whereever it finds ionic.project, errors if it doesn't find it) for tasks that require it.
// The cliTasks are in lib/tasks/cliTasks.js - with an attribute to disable changing pwd
// for commands like start, help, docs, and v2
//this should be tracked by each task
if (!taskSetting.disableChangePwd) {
try {
logging.logger.debug('Looking up ionic root, cwd:', process.env.PWD);
var root = Utils.cdIonicRoot();
isIonicV2 = Utils.isIonicV2(root);
isIonicV2 && logging.logger.debug('Ionic 2 project.')
logging.logger.debug('ionic root now set pwd:', process.env.PWD);
} catch (error) {
//Hard fail here breaks plugin installation for v1 start task
logging.logger.debug(error);
var root = Utils.cdIonicRoot();
var project = IonicProject.load(root);
argv.v2 = project && project.get('v2');
argv.v2 && logging.logger.debug('Ionic 2 project.');

if (fs.existsSync('node_modules')) {
//run with gulp hooks
return runWithGulp(argv, taskInstance);
} else if (argv.v2) {
console.warn('WARN: No node_modules directory found, do you need to run npm install?');
}
}
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);

var booleanOptions = Cli.getBooleanOptionsForTask(taskSetting);
} catch (ex) {
logging.logger.debug('Cli.Run - Error', ex);
return Utils.fail(ex);
}
};

argv = optimist(processArgv.slice(2)).boolean(booleanOptions).argv;
function runWithGulp(argv, taskInstance){
var cmdName = argv._[0];
var beforeHook = cmdName + ':before';
var afterHook = cmdName + ':after';

var taskModule = Cli.lookupTask(taskSetting.module);
var taskInstance = new taskModule();
if (loadGulpfile()) {
logging.logger.debug('Gulpfile found');

if (isIonicV2) {
// probably a better way to do this, but to avoid changing run() for every
// task for now
argv.v2 = true;
}

console.log();
var oldConfig = fs.existsSync('ionic.config.js');
if (oldConfig) {
console.warn('WARN: ionic.config.js has been deprecated. Please use ionic.json instead.\n');
try {
var gulp = require(path.resolve(process.cwd() + '/node_modules/gulp'));
} catch(e) {
// Empty gulpfile (or one that doesn't require gulp?), and no gulp
console.error('\nGulpfile detected, but gulp is not installed');
console.error('Do you need to run `npm install`?\n');
process.exit(1);
}

var cmdName = argv._[0];
var gulpFound = loadGulp();
if (gulpFound) {
logging.logger.debug('Gulpfile found');
var beforeHook = cmdName + ':before';
var afterHook = cmdName + ':after';
try {
var gulp = require(path.resolve(process.cwd() + '/node_modules/gulp'));
} catch(e) {
// Empty gulpfile (or one that doesn't require gulp?), and no gulp
console.error('\nGulpfile detected, but gulp is not installed');
console.error('Do you need to run `npm install`?\n');
process.exit(1);
}
logEvents(gulp);

return Q.nfcall(gulp.start.bind(gulp), beforeHook).then(
function(){
// Only some commands return promises
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
},
function(e){ //task error, let gulp handle it
if (/serve|build|run|emulate|upload/.test(cmdName) && argv.v2) {
var taskName = (cmdName === 'serve') ? 'watch' : 'build';
console.warn('WARN: No \'' + beforeHook + '\' gulp task found!');
if (oldConfig && gulp.tasks[taskName]) {
console.info('Your gulpfile contains a \'' + taskName + '\' task already! Add:\n')
console.log(' gulp.task(\'' + cmdName + ':before\', [\'' + taskName + '\']);\n');
console.info('to your gulpfile to have Ionic CLI run \'' + taskName + '\' before ' + cmdName + '.');
} else {
console.warn('If your app requires a build step, you may want to ensure it runs before ' + cmdName + '.\n');
}
console.log();
logEvents(gulp);

return Q.nfcall(gulp.start.bind(gulp), beforeHook).then(
function(){
// Only some commands return promises, so wrap it
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
},
function(e){ //task error, let gulp handle it

// Warn if it's a v2 project and there isn't a build hook
if (/serve|build|run|emulate|upload/.test(cmdName) && argv.v2) {
var taskName = (cmdName === 'serve') ? 'watch' : 'build';
console.warn('WARN: No \'' + beforeHook + '\' gulp task found!');

//The task exists, but there are no hooks for it
//They have the old config file, so we're safe to yell at them
//With new config, may intentionally not have hooks if running their own build
if (gulp.tasks[taskName] && fs.existsSync('ionic.config.js')) {
console.info('Your gulpfile contains a \'' + taskName + '\' task already! Add:\n')
console.log(' gulp.task(\'' + cmdName + ':before\', [\'' + taskName + '\']);\n');
console.info('to your gulpfile to have Ionic CLI run \'' + taskName + '\' before ' + cmdName + '.\n');
} else {
console.warn('If your app requires a build step, you may want to ensure it runs before ' + cmdName + '.\n');
}
// Only some commands return promises
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
}
).then(function(){
return Q.nfcall(gulp.start.bind(gulp), argv._[0] + ':after');
});
} else {
if (/serve|build|run|emulate|upload/.test(cmdName) && argv.v2) {
console.warn('WARN: No gulpfile found!');
console.warn('If your app requires a build step, you may want to ensure it runs before ' + cmdName + '.\n');
// Only some commands return promises, so wrap it
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
}
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
}
).then(function(){
return Q.nfcall(gulp.start.bind(gulp), argv._[0] + ':after');
});
}

} catch (ex) {
logging.logger.debug('Cli.Run - Error', ex);
return Utils.fail(ex);
if (/serve|build|run|emulate|upload/.test(cmdName) && argv.v2) {
console.warn('WARN: No gulpfile found!');
console.warn('If your app requires a build step, you may want to ensure it runs before ' + cmdName + '.\n');
}
};
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
}

function loadGulp(){
function loadGulpfile(){
//TODO add babel support?
var names = ['gulpfile.js', 'Gulpfile.js'];
for (var i = 0, ii = names.length; i < ii; i++) {
try {
Expand Down

0 comments on commit 52f1a8f

Please sign in to comment.