Skip to content

Commit 6bd2658

Browse files
authored
feat(): allow emulate and run to use serve for livereload. (#1614)
1 parent dc4ff0a commit 6bd2658

File tree

13 files changed

+380
-364
lines changed

13 files changed

+380
-364
lines changed

lib/cli.js

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ log.level = 'info';
4242
* @return {Promise}
4343
*/
4444
Cli.run = function run(processArgv) {
45+
46+
/*
47+
* Before taking any more steps, lets check their
48+
* environment and display any upgrade warnings
49+
*/
50+
return Q.all([
51+
Cli.doRuntimeCheck(),
52+
Cli.checkLatestVersion()
53+
]).then(function() {
54+
Cli.runr(processArgv);
55+
}).catch(function(ex) {
56+
return appLibUtils.fail(ex);
57+
});
58+
};
59+
Cli.runr = function runr(processArgv) {
4560
try {
4661

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

5671
Cli.attachErrorHandling();
57-
Cli.latestVersion = Cli.checkLatestVersion();
5872

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

63-
/*
64-
* Before taking any more steps, lets check their
65-
* environment and display any upgrade warnings
66-
*/
67-
Cli.doRuntimeCheck(settings.version);
68-
6977
/*
7078
* Print version if '--version' or '--v' is an option
7179
* TODO: version should probably also be a command
@@ -180,7 +188,7 @@ Cli.run = function run(processArgv) {
180188
log.debug('\nNpm scripts:', npmScripts);
181189
log.debug('Gulpfile found:', gulpLoaded, '\n');
182190

183-
if (npmScripts) {
191+
if (npmScripts.hasOwnProperty(taskName + ':before') || npmScripts.hasOwnProperty(taskName + ':after')) {
184192
return Cli.runWithNpmScripts(argv, task, rawCliArguments);
185193
} else if (gulpLoaded) {
186194
return Cli.runWithGulp(argv, task, rawCliArguments);
@@ -621,13 +629,6 @@ Cli.printNewsUpdates = function printNewsUpdates(skipNewsCheck) {
621629
return q.promise;
622630
};
623631

624-
Cli.gatherInfo = function gatherInfo() {
625-
var info = Info.gatherInfo();
626-
Info.getIonicVersion(info, process.cwd());
627-
Info.getIonicCliVersion(info, path.join(__dirname, '../'));
628-
return info;
629-
};
630-
631632
Cli.handleUncaughtExceptions = function handleUncaughtExceptions(err) {
632633
log.error(chalk.red.bold('An uncaught exception occurred and has been reported to Ionic'));
633634
var errorMessage = typeof err === 'string' ? err : err.message;
@@ -637,12 +638,12 @@ Cli.handleUncaughtExceptions = function handleUncaughtExceptions(err) {
637638

638639
Cli.attachErrorHandling = function attachErrorHandling() {
639640
appLibUtils.errorHandler = function errorHandler(msg) {
640-
try {
641-
log.debug('Cli.appLibUtils.errorHandler msg', msg, typeof msg);
642-
var stack = typeof msg == 'string' ? '' : msg.stack;
643-
var errorMessage = typeof msg == 'string' ? msg : msg.message;
644-
if (msg) {
645-
var info = Cli.gatherInfo();
641+
log.debug('Cli.appLibUtils.errorHandler msg', msg, typeof msg);
642+
var stack = typeof msg == 'string' ? '' : msg.stack;
643+
var errorMessage = typeof msg == 'string' ? msg : msg.message;
644+
var promise = Q();
645+
if (msg) {
646+
promise = Info.gatherInfo().then(function(info) {
646647
var ionicCliVersion = info.ionic_cli;
647648
if (stack && stack.length > 0) {
648649
process.stderr.write('\n' + chalk.bold(stack) + '\n\n');
@@ -651,14 +652,16 @@ Cli.attachErrorHandling = function attachErrorHandling() {
651652
process.stderr.write(chalk.bold(' (CLI v' + ionicCliVersion + ')') + '\n');
652653

653654
Info.printInfo(info);
654-
}
655+
});
656+
}
657+
promise.then(function() {
655658
process.stderr.write('\n');
656659
process.exit(1);
657660
return '';
658-
} catch (ex) {
661+
}).catch(function(ex) {
659662
console.log('errorHandler had an error', ex);
660663
console.log(ex.stack);
661-
}
664+
});
662665
};
663666

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

688691
if (!lastVersionChecked || !versionHasBeenChecked) {
689-
Info.checkRuntime();
690-
IonicConfig.set('lastVersionChecked', version);
691-
IonicConfig.save();
692+
return Info.gatherInfo().then(function(info) {
693+
Info.checkRuntime(info);
694+
IonicConfig.set('lastVersionChecked', version);
695+
IonicConfig.save();
696+
});
692697
}
698+
699+
return Q();
693700
};
694701

695702
module.exports = Cli;

lib/ionic/emulate.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var chalk = require('chalk');
4+
var serveUtil = require('../utils/serve');
45
var extend = require('../utils/extend');
56
var npmScripts = require('../utils/npmScripts');
67
var os = require('os');
@@ -46,6 +47,10 @@ function run(ionic, argv, rawCliArguments) {
4647
var appDirectory = process.cwd();
4748
var rawArgs = rawCliArguments.slice(0);
4849
var cmdName = argv._[0].toLowerCase();
50+
var hasBuildCommand = false;
51+
var hasServeCommand = false;
52+
var address = argv.address || serveUtil.DEFAULT_ADDRESS;
53+
var port = argv.port || serveUtil.DEFAULT_HTTP_PORT;
4954

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

@@ -64,30 +69,45 @@ function run(ionic, argv, rawCliArguments) {
6469
var promiseList = []
6570
.concat(!cordovaUtils.isPlatformInstalled(runPlatform, appDirectory) ?
6671
cordovaUtils.installPlatform(runPlatform) :
67-
[])
72+
Q())
6873
.concat(!cordovaUtils.arePluginsInstalled(appDirectory) ?
6974
cordovaUtils.installPlugins() :
70-
[]);
75+
Q())
76+
.concat(npmScripts.hasIonicScript('build'))
77+
.concat(npmScripts.hasIonicScript('serve'));
7178

72-
return Q.all(promiseList).then(function() {
73-
return npmScripts.hasIonicScript('build');
74-
})
75-
.then(function(hasBuildCommand) {
76-
if (hasBuildCommand) {
79+
return Q.all(promiseList).then(function(results) {
80+
hasBuildCommand = results[2];
81+
hasServeCommand = results[3];
82+
83+
if (hasBuildCommand && !(isLiveReload && hasServeCommand)) {
7784
return npmScripts.runIonicScript('build');
7885
}
79-
return Q.resolve();
86+
return Q();
8087
})
8188
.then(function() {
82-
if (isLiveReload) {
89+
90+
if (isLiveReload && hasServeCommand) {
91+
92+
// using app-scripts and livereload is requested
93+
return ConfigXml.setConfigXml(process.cwd(), {
94+
devServer: serveUtil.getUrl(address, port)
95+
}).then(function() {
96+
isLiveReload = false;
97+
return npmScripts.runIonicScript('serve', ['-p', port, '--address', address]);
98+
});
99+
} else if (isLiveReload) {
100+
101+
// not an app-scripts project but the user wants livereload
83102
return cordovaUtils.setupLiveReload(argv, appDirectory);
84-
}
103+
} else {
85104

86-
// ensure the content node was set back to its original
87-
return ConfigXml.setConfigXml(appDirectory, {
88-
resetContent: true,
89-
errorWhenNotFound: false
90-
});
105+
// ensure the content node was set back to its original
106+
return ConfigXml.setConfigXml(appDirectory, {
107+
resetContent: true,
108+
errorWhenNotFound: false
109+
});
110+
}
91111
})
92112
.then(function(serveOptions) {
93113
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);

lib/ionic/info.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
var extend = require('../utils/extend');
4-
var path = require('path');
54
var IonicAppLib = require('ionic-app-lib');
65
var Info = IonicAppLib.info;
76
var appLibUtils = IonicAppLib.utils;
@@ -14,20 +13,13 @@ var settings = {
1413
};
1514

1615
function run() {
17-
try {
18-
19-
var info = Info.gatherInfo();
20-
21-
Info.getIonicVersion(info, process.cwd());
22-
Info.getIonicCliVersion(info, path.join(__dirname, '../../'));
23-
Info.getIonicAppScriptsVersion(info, process.cwd());
24-
16+
return Info.gatherInfo().then(function(info) {
2517
Info.printInfo(info);
2618

27-
Info.checkRuntime();
28-
} catch (ex) {
19+
Info.checkRuntime(info);
20+
}).catch(function(ex) {
2921
appLibUtils.fail('There was an error retrieving your environment information:', ex);
30-
}
22+
});
3123
}
3224

3325
module.exports = extend(settings, {

lib/ionic/run.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var chalk = require('chalk');
4+
var serveUtil = require('../utils/serve');
45
var extend = require('../utils/extend');
56
var npmScripts = require('../utils/npmScripts');
67
var os = require('os');
@@ -46,6 +47,10 @@ function run(ionic, argv, rawCliArguments) {
4647
var appDirectory = process.cwd();
4748
var rawArgs = rawCliArguments.slice(0);
4849
var cmdName = argv._[0].toLowerCase();
50+
var hasBuildCommand = false;
51+
var hasServeCommand = false;
52+
var address = argv.address || serveUtil.DEFAULT_ADDRESS;
53+
var port = argv.port || serveUtil.DEFAULT_HTTP_PORT;
4954

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

@@ -64,30 +69,44 @@ function run(ionic, argv, rawCliArguments) {
6469
var promiseList = []
6570
.concat(!cordovaUtils.isPlatformInstalled(runPlatform, appDirectory) ?
6671
cordovaUtils.installPlatform(runPlatform) :
67-
[])
72+
Q())
6873
.concat(!cordovaUtils.arePluginsInstalled(appDirectory) ?
6974
cordovaUtils.installPlugins() :
70-
[]);
75+
Q())
76+
.concat(npmScripts.hasIonicScript('build'))
77+
.concat(npmScripts.hasIonicScript('serve'));
7178

72-
return Q.all(promiseList).then(function() {
73-
return npmScripts.hasIonicScript('build');
74-
})
75-
.then(function(hasBuildCommand) {
76-
if (hasBuildCommand) {
79+
return Q.all(promiseList).then(function(results) {
80+
hasBuildCommand = results[2];
81+
hasServeCommand = results[3];
82+
83+
if (hasBuildCommand && !(isLiveReload && hasServeCommand)) {
7784
return npmScripts.runIonicScript('build');
7885
}
79-
return Q.resolve();
80-
})
81-
.then(function() {
82-
if (isLiveReload) {
86+
return Q();
87+
}).then(function() {
88+
89+
if (isLiveReload && hasServeCommand) {
90+
91+
// using app-scripts and livereload is requested
92+
return ConfigXml.setConfigXml(process.cwd(), {
93+
devServer: serveUtil.getUrl(address, port)
94+
}).then(function() {
95+
isLiveReload = false;
96+
return npmScripts.runIonicScript('serve', ['-p', port, '--address', address]);
97+
});
98+
} else if (isLiveReload) {
99+
100+
// not an app-scripts project but the user wants livereload
83101
return cordovaUtils.setupLiveReload(argv, appDirectory);
84-
}
102+
} else {
85103

86-
// ensure the content node was set back to its original
87-
return ConfigXml.setConfigXml(appDirectory, {
88-
resetContent: true,
89-
errorWhenNotFound: false
90-
});
104+
// ensure the content node was set back to its original
105+
return ConfigXml.setConfigXml(appDirectory, {
106+
resetContent: true,
107+
errorWhenNotFound: false
108+
});
109+
}
91110
})
92111
.then(function(serveOptions) {
93112
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);

0 commit comments

Comments
 (0)