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

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

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
var chalk = require('chalk');
4-
var serveUtil = require('../utils/serve');
54
var extend = require('../utils/extend');
65
var npmScripts = require('../utils/npmScripts');
76
var os = require('os');
@@ -49,16 +48,14 @@ function run(ionic, argv, rawCliArguments) {
4948
var cmdName = argv._[0].toLowerCase();
5049
var hasBuildCommand = false;
5150
var hasServeCommand = false;
52-
var address = argv.address || serveUtil.DEFAULT_ADDRESS;
53-
var port = argv.port || serveUtil.DEFAULT_HTTP_PORT;
5451

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

5754
// If platform was not passed then add it to the rawArgs
5855
var runPlatform = 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-
return npmScripts.runIonicScript('serve', ['-p', port, '--address', address, '--nobrowser'])
96-
.then(function() {
97-
return ConfigXml.setConfigXml(process.cwd(), {
98-
devServer: serveUtil.getUrl(address, port)
99-
});
100-
});
92+
// Also remove commandName from the rawArgs passed
93+
return cordovaUtils.startAppScriptsServer(argv);
94+
10195
} else if (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
var chalk = require('chalk');
4-
var serveUtil = require('../utils/serve');
54
var extend = require('../utils/extend');
65
var npmScripts = require('../utils/npmScripts');
76
var os = require('os');
@@ -49,16 +48,14 @@ function run(ionic, argv, rawCliArguments) {
4948
var cmdName = argv._[0].toLowerCase();
5049
var hasBuildCommand = false;
5150
var hasServeCommand = false;
52-
var address = argv.address || serveUtil.DEFAULT_ADDRESS;
53-
var port = argv.port || serveUtil.DEFAULT_HTTP_PORT;
5451

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

5754
// If platform was not passed then add it to the rawArgs
5855
var runPlatform = 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-
return npmScripts.runIonicScript('serve', ['-p', port, '--address', address, '--nobrowser'])
96-
.then(function() {
97-
return ConfigXml.setConfigXml(process.cwd(), {
98-
devServer: serveUtil.getUrl(address, port)
99-
});
100-
});
92+
// Also remove commandName from the rawArgs passed
93+
return cordovaUtils.startAppScriptsServer(argv);
10194
} else if (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
var childProcess = require('child_process');
1414
var childExec = childProcess.exec;
1515
var promiseExec = Q.denodeify(childExec);
16+
var npmScripts = 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+
function startAppScriptsServer(argv) {
278+
var options = _.extend(argv, {
279+
runLivereload: true,
280+
isPlatformServe: true
281+
});
282+
283+
return Serve.getAddress(options)
284+
.then(function() {
285+
286+
// Check that the server port is available
287+
return Serve.checkPorts(true, options.port, options.address, options);
288+
})
289+
.then(function() {
290+
291+
// Check that the liveReload port is available
292+
return Serve.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+
return npmScripts.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+
return ConfigXml.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
function runIonicScript(name, argv) {
23-
var spawn = require('cross-spawn-async');
23+
var spawn = require('cross-spawn-async');
2424
var scriptName = getScriptName(name);
2525
var q = Q.defer();
2626

27-
var scriptSpawn = spawn('npm', ['run', scriptName, '--', '--colors'].concat(argv || []), [process.stdin, 'pipe', process.stderr])
27+
process.env['FORCE_COLOR'] = true;
28+
var scriptSpawn = 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
return q.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
return q.promise;
5359
}

0 commit comments

Comments
 (0)