Skip to content

Commit 70531bc

Browse files
committed
feat(emulate): ensure emulate and run are choosing an IP and port that are available.
1 parent bfadd19 commit 70531bc

4 files changed

Lines changed: 64 additions & 23 deletions

File tree

‎lib/ionic/emulate.js‎

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

33
varchalk=require('chalk');
4-
varserveUtil=require('../utils/serve');
54
varextend=require('../utils/extend');
65
varnpmScripts=require('../utils/npmScripts');
76
varos=require('os');
@@ -49,16 +48,14 @@ function run(ionic, argv, rawCliArguments) {
4948
varcmdName=argv._[0].toLowerCase();
5049
varhasBuildCommand=false;
5150
varhasServeCommand=false;
52-
varaddress=argv.address||serveUtil.DEFAULT_ADDRESS;
53-
varport=argv.port||serveUtil.DEFAULT_HTTP_PORT;
5451

5552
varisLiveReload=argv.livereload||argv['live-reload']||argv.l||false;
5653

5754
// If platform was not passed then add it to the rawArgs
5855
varrunPlatform=argv._[1];
5956
if(!runPlatform){
6057
runPlatform='ios';
61-
rawArgs.push(runPlatform);
58+
rawArgs.splice(1,0,runPlatform);
6259
}
6360

6461
if(runPlatform==='ios'&&os.platform()!=='darwin'){
@@ -92,12 +89,9 @@ function run(ionic, argv, rawCliArguments) {
9289
if(isLiveReload&&hasServeCommand){
9390

9491
// using app-scripts and livereload is requested
95-
returnnpmScripts.runIonicScript('serve',['-p',port,'--address',address,'--nobrowser'])
96-
.then(function(){
97-
returnConfigXml.setConfigXml(process.cwd(),{
98-
devServer: serveUtil.getUrl(address,port)
99-
});
100-
});
92+
// Also remove commandName from the rawArgs passed
93+
returncordovaUtils.startAppScriptsServer(argv);
94+
10195
}elseif(isLiveReload){
10296

10397
// not an app-scripts project but the user wants livereload

‎lib/ionic/run.js‎

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

33
varchalk=require('chalk');
4-
varserveUtil=require('../utils/serve');
54
varextend=require('../utils/extend');
65
varnpmScripts=require('../utils/npmScripts');
76
varos=require('os');
@@ -49,16 +48,14 @@ function run(ionic, argv, rawCliArguments) {
4948
varcmdName=argv._[0].toLowerCase();
5049
varhasBuildCommand=false;
5150
varhasServeCommand=false;
52-
varaddress=argv.address||serveUtil.DEFAULT_ADDRESS;
53-
varport=argv.port||serveUtil.DEFAULT_HTTP_PORT;
5451

5552
varisLiveReload=argv.livereload||argv['live-reload']||argv.l||false;
5653

5754
// If platform was not passed then add it to the rawArgs
5855
varrunPlatform=argv._[1];
5956
if(!runPlatform){
6057
runPlatform='ios';
61-
rawArgs.push(runPlatform);
58+
rawArgs.splice(1,0,runPlatform);
6259
}
6360

6461
if(runPlatform==='ios'&&os.platform()!=='darwin'){
@@ -92,12 +89,8 @@ function run(ionic, argv, rawCliArguments) {
9289
if(isLiveReload&&hasServeCommand){
9390

9491
// using app-scripts and livereload is requested
95-
returnnpmScripts.runIonicScript('serve',['-p',port,'--address',address,'--nobrowser'])
96-
.then(function(){
97-
returnConfigXml.setConfigXml(process.cwd(),{
98-
devServer: serveUtil.getUrl(address,port)
99-
});
100-
});
92+
// Also remove commandName from the rawArgs passed
93+
returncordovaUtils.startAppScriptsServer(argv);
10194
}elseif(isLiveReload){
10295

10396
// not an app-scripts project but the user wants livereload

‎lib/utils/cordova.js‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var ConfigXml = IonicAppLib.configXml;
1313
varchildProcess=require('child_process');
1414
varchildExec=childProcess.exec;
1515
varpromiseExec=Q.denodeify(childExec);
16+
varnpmScripts=require('../utils/npmScripts');
1617

1718
/**
1819
* Returns true or false after checking if the platform exists
@@ -266,12 +267,59 @@ function setupLiveReload(argv, baseDir) {
266267
});
267268
}
268269

270+
/**
271+
* Start the app scripts server for emulator or device
272+
*
273+
* @param {Number} port
274+
* @param {String} address
275+
* @return {Promise} Promise upon completion
276+
*/
277+
functionstartAppScriptsServer(argv){
278+
varoptions=_.extend(argv,{
279+
runLivereload: true,
280+
isPlatformServe: true
281+
});
282+
283+
returnServe.getAddress(options)
284+
.then(function(){
285+
286+
// Check that the server port is available
287+
returnServe.checkPorts(true,options.port,options.address,options);
288+
})
289+
.then(function(){
290+
291+
// Check that the liveReload port is available
292+
returnServe.checkPorts(false,options.liveReloadPort,options.address,options);
293+
})
294+
.then(function(){
295+
296+
// Execute the serve command from app-scripts
297+
// Also remove platform from the raw args passed
298+
returnnpmScripts.runIonicScript('serve',
299+
[
300+
'--port',options.port,
301+
'--address',options.address,
302+
'--liveReloadPort',options.liveReloadPort,
303+
'--nobrowser'
304+
]
305+
.concat(options.consolelogs ? '--consolelogs' : [])
306+
.concat(options.serverlogs ? '--serverlogs' : [])
307+
);
308+
})
309+
.then(function(){
310+
returnConfigXml.setConfigXml(process.cwd(),{
311+
devServer: Serve.host(options.address,options.port)
312+
});
313+
});
314+
}
315+
269316
module.exports={
270317
isPlatformInstalled: isPlatformInstalled,
271318
arePluginsInstalled: arePluginsInstalled,
272319
installPlatform: installPlatform,
273320
installPlugins: installPlugins,
274321
execCordovaCommand: execCordovaCommand,
275322
filterArgumentsForCordova: filterArgumentsForCordova,
276-
setupLiveReload: setupLiveReload
323+
setupLiveReload: setupLiveReload,
324+
startAppScriptsServer: startAppScriptsServer
277325
};

‎lib/utils/npmScripts.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ function hasIonicScript(name) {
2020
}
2121

2222
functionrunIonicScript(name,argv){
23-
varspawn=require('cross-spawn-async');
23+
varspawn=require('cross-spawn-async');
2424
varscriptName=getScriptName(name);
2525
varq=Q.defer();
2626

27-
varscriptSpawn=spawn('npm',['run',scriptName,'--','--colors'].concat(argv||[]),[process.stdin,'pipe',process.stderr])
27+
process.env['FORCE_COLOR']=true;
28+
varscriptSpawn=spawn('npm',['run',scriptName,'--'].concat(argv||[]),[process.stdin,'pipe',process.stderr])
2829
.on('error',function(err){
2930
log.debug('Spawn command',scriptName,'failed');
3031
q.reject('Unable to run spawn command '+err);
@@ -48,6 +49,11 @@ function runIonicScript(name, argv) {
4849
}
4950
returnq.resolve();
5051
});
52+
53+
// If this process ends ensure that we killed the spawned child
54+
process.on('exit',function(){
55+
scriptSpawn.kill();
56+
});
5157

5258
returnq.promise;
5359
}

0 commit comments

Comments
 (0)