Skip to content

Commit

Permalink
feat(): allow emulate and run to use serve for livereload. (#1614)
Browse files Browse the repository at this point in the history
  • Loading branch information
jthoms1 authored Nov 9, 2016
1 parent dc4ff0a commit 6bd2658
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 364 deletions.
61 changes: 34 additions & 27 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ log.level = 'info';
* @return {Promise}
*/
Cli.run = function run(processArgv) {

/*
* Before taking any more steps, lets check their
* environment and display any upgrade warnings
*/
return Q.all([
Cli.doRuntimeCheck(),
Cli.checkLatestVersion()
]).then(function() {
Cli.runr(processArgv);
}).catch(function(ex) {
return appLibUtils.fail(ex);
});
};
Cli.runr = function runr(processArgv) {
try {

/*
Expand All @@ -54,18 +69,11 @@ Cli.run = function run(processArgv) {
var taskList;

Cli.attachErrorHandling();
Cli.latestVersion = Cli.checkLatestVersion();

process.on('exit', function() {
Cli.printVersionWarning(settings.version, Cli.npmVersion);
});

/*
* Before taking any more steps, lets check their
* environment and display any upgrade warnings
*/
Cli.doRuntimeCheck(settings.version);

/*
* Print version if '--version' or '--v' is an option
* TODO: version should probably also be a command
Expand Down Expand Up @@ -180,7 +188,7 @@ Cli.run = function run(processArgv) {
log.debug('\nNpm scripts:', npmScripts);
log.debug('Gulpfile found:', gulpLoaded, '\n');

if (npmScripts) {
if (npmScripts.hasOwnProperty(taskName + ':before') || npmScripts.hasOwnProperty(taskName + ':after')) {
return Cli.runWithNpmScripts(argv, task, rawCliArguments);
} else if (gulpLoaded) {
return Cli.runWithGulp(argv, task, rawCliArguments);
Expand Down Expand Up @@ -621,13 +629,6 @@ Cli.printNewsUpdates = function printNewsUpdates(skipNewsCheck) {
return q.promise;
};

Cli.gatherInfo = function gatherInfo() {
var info = Info.gatherInfo();
Info.getIonicVersion(info, process.cwd());
Info.getIonicCliVersion(info, path.join(__dirname, '../'));
return info;
};

Cli.handleUncaughtExceptions = function handleUncaughtExceptions(err) {
log.error(chalk.red.bold('An uncaught exception occurred and has been reported to Ionic'));
var errorMessage = typeof err === 'string' ? err : err.message;
Expand All @@ -637,12 +638,12 @@ Cli.handleUncaughtExceptions = function handleUncaughtExceptions(err) {

Cli.attachErrorHandling = function attachErrorHandling() {
appLibUtils.errorHandler = function errorHandler(msg) {
try {
log.debug('Cli.appLibUtils.errorHandler msg', msg, typeof msg);
var stack = typeof msg == 'string' ? '' : msg.stack;
var errorMessage = typeof msg == 'string' ? msg : msg.message;
if (msg) {
var info = Cli.gatherInfo();
log.debug('Cli.appLibUtils.errorHandler msg', msg, typeof msg);
var stack = typeof msg == 'string' ? '' : msg.stack;
var errorMessage = typeof msg == 'string' ? msg : msg.message;
var promise = Q();
if (msg) {
promise = Info.gatherInfo().then(function(info) {
var ionicCliVersion = info.ionic_cli;
if (stack && stack.length > 0) {
process.stderr.write('\n' + chalk.bold(stack) + '\n\n');
Expand All @@ -651,14 +652,16 @@ Cli.attachErrorHandling = function attachErrorHandling() {
process.stderr.write(chalk.bold(' (CLI v' + ionicCliVersion + ')') + '\n');

Info.printInfo(info);
}
});
}
promise.then(function() {
process.stderr.write('\n');
process.exit(1);
return '';
} catch (ex) {
}).catch(function(ex) {
console.log('errorHandler had an error', ex);
console.log(ex.stack);
}
});
};

// TODO Attach error reporter here
Expand Down Expand Up @@ -686,10 +689,14 @@ Cli.doRuntimeCheck = function doRuntimeCheck(version) {
}

if (!lastVersionChecked || !versionHasBeenChecked) {
Info.checkRuntime();
IonicConfig.set('lastVersionChecked', version);
IonicConfig.save();
return Info.gatherInfo().then(function(info) {
Info.checkRuntime(info);
IonicConfig.set('lastVersionChecked', version);
IonicConfig.save();
});
}

return Q();
};

module.exports = Cli;
50 changes: 35 additions & 15 deletions lib/ionic/emulate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var chalk = require('chalk');
var serveUtil = require('../utils/serve');
var extend = require('../utils/extend');
var npmScripts = require('../utils/npmScripts');
var os = require('os');
Expand Down Expand Up @@ -46,6 +47,10 @@ function run(ionic, argv, rawCliArguments) {
var appDirectory = process.cwd();
var rawArgs = rawCliArguments.slice(0);
var cmdName = argv._[0].toLowerCase();
var hasBuildCommand = false;
var hasServeCommand = false;
var address = argv.address || serveUtil.DEFAULT_ADDRESS;
var port = argv.port || serveUtil.DEFAULT_HTTP_PORT;

var isLiveReload = argv.livereload || argv['live-reload'] || argv.l || false;

Expand All @@ -64,30 +69,45 @@ function run(ionic, argv, rawCliArguments) {
var promiseList = []
.concat(!cordovaUtils.isPlatformInstalled(runPlatform, appDirectory) ?
cordovaUtils.installPlatform(runPlatform) :
[])
Q())
.concat(!cordovaUtils.arePluginsInstalled(appDirectory) ?
cordovaUtils.installPlugins() :
[]);
Q())
.concat(npmScripts.hasIonicScript('build'))
.concat(npmScripts.hasIonicScript('serve'));

return Q.all(promiseList).then(function() {
return npmScripts.hasIonicScript('build');
})
.then(function(hasBuildCommand) {
if (hasBuildCommand) {
return Q.all(promiseList).then(function(results) {
hasBuildCommand = results[2];
hasServeCommand = results[3];

if (hasBuildCommand && !(isLiveReload && hasServeCommand)) {
return npmScripts.runIonicScript('build');
}
return Q.resolve();
return Q();
})
.then(function() {
if (isLiveReload) {

if (isLiveReload && hasServeCommand) {

// using app-scripts and livereload is requested
return ConfigXml.setConfigXml(process.cwd(), {
devServer: serveUtil.getUrl(address, port)
}).then(function() {
isLiveReload = false;
return npmScripts.runIonicScript('serve', ['-p', port, '--address', address]);
});
} else if (isLiveReload) {

// not an app-scripts project but the user wants livereload
return cordovaUtils.setupLiveReload(argv, appDirectory);
}
} else {

// ensure the content node was set back to its original
return ConfigXml.setConfigXml(appDirectory, {
resetContent: true,
errorWhenNotFound: false
});
// ensure the content node was set back to its original
return ConfigXml.setConfigXml(appDirectory, {
resetContent: true,
errorWhenNotFound: false
});
}
})
.then(function(serveOptions) {
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);
Expand Down
16 changes: 4 additions & 12 deletions lib/ionic/info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var extend = require('../utils/extend');
var path = require('path');
var IonicAppLib = require('ionic-app-lib');
var Info = IonicAppLib.info;
var appLibUtils = IonicAppLib.utils;
Expand All @@ -14,20 +13,13 @@ var settings = {
};

function run() {
try {

var info = Info.gatherInfo();

Info.getIonicVersion(info, process.cwd());
Info.getIonicCliVersion(info, path.join(__dirname, '../../'));
Info.getIonicAppScriptsVersion(info, process.cwd());

return Info.gatherInfo().then(function(info) {
Info.printInfo(info);

Info.checkRuntime();
} catch (ex) {
Info.checkRuntime(info);
}).catch(function(ex) {
appLibUtils.fail('There was an error retrieving your environment information:', ex);
}
});
}

module.exports = extend(settings, {
Expand Down
53 changes: 36 additions & 17 deletions lib/ionic/run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var chalk = require('chalk');
var serveUtil = require('../utils/serve');
var extend = require('../utils/extend');
var npmScripts = require('../utils/npmScripts');
var os = require('os');
Expand Down Expand Up @@ -46,6 +47,10 @@ function run(ionic, argv, rawCliArguments) {
var appDirectory = process.cwd();
var rawArgs = rawCliArguments.slice(0);
var cmdName = argv._[0].toLowerCase();
var hasBuildCommand = false;
var hasServeCommand = false;
var address = argv.address || serveUtil.DEFAULT_ADDRESS;
var port = argv.port || serveUtil.DEFAULT_HTTP_PORT;

var isLiveReload = argv.livereload || argv['live-reload'] || argv.l || false;

Expand All @@ -64,30 +69,44 @@ function run(ionic, argv, rawCliArguments) {
var promiseList = []
.concat(!cordovaUtils.isPlatformInstalled(runPlatform, appDirectory) ?
cordovaUtils.installPlatform(runPlatform) :
[])
Q())
.concat(!cordovaUtils.arePluginsInstalled(appDirectory) ?
cordovaUtils.installPlugins() :
[]);
Q())
.concat(npmScripts.hasIonicScript('build'))
.concat(npmScripts.hasIonicScript('serve'));

return Q.all(promiseList).then(function() {
return npmScripts.hasIonicScript('build');
})
.then(function(hasBuildCommand) {
if (hasBuildCommand) {
return Q.all(promiseList).then(function(results) {
hasBuildCommand = results[2];
hasServeCommand = results[3];

if (hasBuildCommand && !(isLiveReload && hasServeCommand)) {
return npmScripts.runIonicScript('build');
}
return Q.resolve();
})
.then(function() {
if (isLiveReload) {
return Q();
}).then(function() {

if (isLiveReload && hasServeCommand) {

// using app-scripts and livereload is requested
return ConfigXml.setConfigXml(process.cwd(), {
devServer: serveUtil.getUrl(address, port)
}).then(function() {
isLiveReload = false;
return npmScripts.runIonicScript('serve', ['-p', port, '--address', address]);
});
} else if (isLiveReload) {

// not an app-scripts project but the user wants livereload
return cordovaUtils.setupLiveReload(argv, appDirectory);
}
} else {

// ensure the content node was set back to its original
return ConfigXml.setConfigXml(appDirectory, {
resetContent: true,
errorWhenNotFound: false
});
// ensure the content node was set back to its original
return ConfigXml.setConfigXml(appDirectory, {
resetContent: true,
errorWhenNotFound: false
});
}
})
.then(function(serveOptions) {
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);
Expand Down
Loading

0 comments on commit 6bd2658

Please sign in to comment.