Skip to content

Commit 6bd2658

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

13 files changed

Lines changed: 380 additions & 364 deletions

File tree

‎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=functionrun(processArgv){
45+
46+
/*
47+
* Before taking any more steps, lets check their
48+
* environment and display any upgrade warnings
49+
*/
50+
returnQ.all([
51+
Cli.doRuntimeCheck(),
52+
Cli.checkLatestVersion()
53+
]).then(function(){
54+
Cli.runr(processArgv);
55+
}).catch(function(ex){
56+
returnappLibUtils.fail(ex);
57+
});
58+
};
59+
Cli.runr=functionrunr(processArgv){
4560
try{
4661

4762
/*
@@ -54,18 +69,11 @@ Cli.run = function run(processArgv) {
5469
vartaskList;
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
returnCli.runWithNpmScripts(argv,task,rawCliArguments);
185193
}elseif(gulpLoaded){
186194
returnCli.runWithGulp(argv,task,rawCliArguments);
@@ -621,13 +629,6 @@ Cli.printNewsUpdates = function printNewsUpdates(skipNewsCheck) {
621629
returnq.promise;
622630
};
623631

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

638639
Cli.attachErrorHandling=functionattachErrorHandling(){
639640
appLibUtils.errorHandler=functionerrorHandler(msg){
640-
try{
641-
log.debug('Cli.appLibUtils.errorHandler msg',msg,typeofmsg);
642-
varstack=typeofmsg=='string' ? '' : msg.stack;
643-
varerrorMessage=typeofmsg=='string' ? msg : msg.message;
644-
if(msg){
645-
varinfo=Cli.gatherInfo();
641+
log.debug('Cli.appLibUtils.errorHandler msg',msg,typeofmsg);
642+
varstack=typeofmsg=='string' ? '' : msg.stack;
643+
varerrorMessage=typeofmsg=='string' ? msg : msg.message;
644+
varpromise=Q();
645+
if(msg){
646+
promise=Info.gatherInfo().then(function(info){
646647
varionicCliVersion=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+
returnInfo.gatherInfo().then(function(info){
693+
Info.checkRuntime(info);
694+
IonicConfig.set('lastVersionChecked',version);
695+
IonicConfig.save();
696+
});
692697
}
698+
699+
returnQ();
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
varchalk=require('chalk');
4+
varserveUtil=require('../utils/serve');
45
varextend=require('../utils/extend');
56
varnpmScripts=require('../utils/npmScripts');
67
varos=require('os');
@@ -46,6 +47,10 @@ function run(ionic, argv, rawCliArguments) {
4647
varappDirectory=process.cwd();
4748
varrawArgs=rawCliArguments.slice(0);
4849
varcmdName=argv._[0].toLowerCase();
50+
varhasBuildCommand=false;
51+
varhasServeCommand=false;
52+
varaddress=argv.address||serveUtil.DEFAULT_ADDRESS;
53+
varport=argv.port||serveUtil.DEFAULT_HTTP_PORT;
4954

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

@@ -64,30 +69,45 @@ function run(ionic, argv, rawCliArguments) {
6469
varpromiseList=[]
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-
returnQ.all(promiseList).then(function(){
73-
returnnpmScripts.hasIonicScript('build');
74-
})
75-
.then(function(hasBuildCommand){
76-
if(hasBuildCommand){
79+
returnQ.all(promiseList).then(function(results){
80+
hasBuildCommand=results[2];
81+
hasServeCommand=results[3];
82+
83+
if(hasBuildCommand&&!(isLiveReload&&hasServeCommand)){
7784
returnnpmScripts.runIonicScript('build');
7885
}
79-
returnQ.resolve();
86+
returnQ();
8087
})
8188
.then(function(){
82-
if(isLiveReload){
89+
90+
if(isLiveReload&&hasServeCommand){
91+
92+
// using app-scripts and livereload is requested
93+
returnConfigXml.setConfigXml(process.cwd(),{
94+
devServer: serveUtil.getUrl(address,port)
95+
}).then(function(){
96+
isLiveReload=false;
97+
returnnpmScripts.runIonicScript('serve',['-p',port,'--address',address]);
98+
});
99+
}elseif(isLiveReload){
100+
101+
// not an app-scripts project but the user wants livereload
83102
returncordovaUtils.setupLiveReload(argv,appDirectory);
84-
}
103+
}else{
85104

86-
// ensure the content node was set back to its original
87-
returnConfigXml.setConfigXml(appDirectory,{
88-
resetContent: true,
89-
errorWhenNotFound: false
90-
});
105+
// ensure the content node was set back to its original
106+
returnConfigXml.setConfigXml(appDirectory,{
107+
resetContent: true,
108+
errorWhenNotFound: false
109+
});
110+
}
91111
})
92112
.then(function(serveOptions){
93113
varoptionList=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
varextend=require('../utils/extend');
4-
varpath=require('path');
54
varIonicAppLib=require('ionic-app-lib');
65
varInfo=IonicAppLib.info;
76
varappLibUtils=IonicAppLib.utils;
@@ -14,20 +13,13 @@ var settings = {
1413
};
1514

1615
functionrun(){
17-
try{
18-
19-
varinfo=Info.gatherInfo();
20-
21-
Info.getIonicVersion(info,process.cwd());
22-
Info.getIonicCliVersion(info,path.join(__dirname,'../../'));
23-
Info.getIonicAppScriptsVersion(info,process.cwd());
24-
16+
returnInfo.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
varchalk=require('chalk');
4+
varserveUtil=require('../utils/serve');
45
varextend=require('../utils/extend');
56
varnpmScripts=require('../utils/npmScripts');
67
varos=require('os');
@@ -46,6 +47,10 @@ function run(ionic, argv, rawCliArguments) {
4647
varappDirectory=process.cwd();
4748
varrawArgs=rawCliArguments.slice(0);
4849
varcmdName=argv._[0].toLowerCase();
50+
varhasBuildCommand=false;
51+
varhasServeCommand=false;
52+
varaddress=argv.address||serveUtil.DEFAULT_ADDRESS;
53+
varport=argv.port||serveUtil.DEFAULT_HTTP_PORT;
4954

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

@@ -64,30 +69,44 @@ function run(ionic, argv, rawCliArguments) {
6469
varpromiseList=[]
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-
returnQ.all(promiseList).then(function(){
73-
returnnpmScripts.hasIonicScript('build');
74-
})
75-
.then(function(hasBuildCommand){
76-
if(hasBuildCommand){
79+
returnQ.all(promiseList).then(function(results){
80+
hasBuildCommand=results[2];
81+
hasServeCommand=results[3];
82+
83+
if(hasBuildCommand&&!(isLiveReload&&hasServeCommand)){
7784
returnnpmScripts.runIonicScript('build');
7885
}
79-
returnQ.resolve();
80-
})
81-
.then(function(){
82-
if(isLiveReload){
86+
returnQ();
87+
}).then(function(){
88+
89+
if(isLiveReload&&hasServeCommand){
90+
91+
// using app-scripts and livereload is requested
92+
returnConfigXml.setConfigXml(process.cwd(),{
93+
devServer: serveUtil.getUrl(address,port)
94+
}).then(function(){
95+
isLiveReload=false;
96+
returnnpmScripts.runIonicScript('serve',['-p',port,'--address',address]);
97+
});
98+
}elseif(isLiveReload){
99+
100+
// not an app-scripts project but the user wants livereload
83101
returncordovaUtils.setupLiveReload(argv,appDirectory);
84-
}
102+
}else{
85103

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

0 commit comments

Comments
 (0)