Skip to content

Commit 68eb3e5

Browse files
authored
fix(run): update run and emulate to pass additional options on to app-scripts. (#1735)
1 parent f834cca commit 68eb3e5

File tree

4 files changed

+146
-9
lines changed

4 files changed

+146
-9
lines changed

lib/ionic/emulate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function run(ionic, argv, rawCliArguments) {
7878
hasServeCommand = results[3];
7979

8080
if (hasBuildCommand && !(isLiveReload && hasServeCommand)) {
81-
return npmScripts.runIonicScript('build');
81+
return npmScripts.runIonicScript('build', rawArgs.slice(2));
8282
}
8383
return Q();
8484
})

lib/ionic/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function run(ionic, argv, rawCliArguments) {
7878
hasServeCommand = results[3];
7979

8080
if (hasBuildCommand && !(isLiveReload && hasServeCommand)) {
81-
return npmScripts.runIonicScript('build');
81+
return npmScripts.runIonicScript('build', rawArgs.slice(2));
8282
}
8383
return Q();
8484
})

lib/utils/cordova.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ function startAppScriptsServer(argv) {
321321
runLivereload: true,
322322
isPlatformServe: true
323323
});
324+
options = consolidateOptions(['port', 'p'], options);
325+
options = consolidateOptions(['liveReloadPort', 'r'], options);
326+
options = consolidateOptions(['consolelogs', 'c'], options);
327+
options = consolidateOptions(['serverlogs', 's'], options);
328+
options = consolidateOptions(['livereload', 'l'], options);
324329

325330
return Serve.getAddress(options)
326331
.then(function() {
@@ -338,15 +343,13 @@ function startAppScriptsServer(argv) {
338343
// Execute the serve command from app-scripts
339344
// Also remove platform from the raw args passed
340345
return npmScripts.runIonicScript('serve',
341-
[
342-
'--port', options.port,
343-
'--address', options.address,
344-
'--liveReloadPort', options.liveReloadPort,
346+
optionsToArray(options)
347+
348+
// Serve specific options not related to the actual run or emulate code
349+
.concat([
345350
'--iscordovaserve',
346351
'--nobrowser'
347-
]
348-
.concat(options.consolelogs ? '--consolelogs' : [])
349-
.concat(options.serverlogs ? '--serverlogs' : [])
352+
])
350353
);
351354
})
352355
.then(function() {
@@ -356,6 +359,49 @@ function startAppScriptsServer(argv) {
356359
});
357360
}
358361

362+
/**
363+
* Consolidate a list of options based on those passed to it.
364+
*
365+
* @param {Array} list of option names. Consolidate will be to the first array element name
366+
* @param {Object} key/value object that is to be used as the source of consolidation
367+
* @return {Object} key/value object with consolidated options
368+
*/
369+
function consolidateOptions(list, options) {
370+
var newOptions = _.extend({}, options);
371+
372+
list.forEach(function(optionName) {
373+
delete newOptions[optionName];
374+
});
375+
376+
var value = list.reduce(function(final, optionName) {
377+
if (options.hasOwnProperty(optionName) && !!options[optionName]) {
378+
final = options[optionName];
379+
}
380+
return final;
381+
}, null);
382+
383+
if (value !== null) {
384+
newOptions[list[0]] = value;
385+
}
386+
387+
return newOptions;
388+
}
389+
390+
function optionsToArray(options) {
391+
return Object.keys(options)
392+
.filter(function(itemName) {
393+
return itemName !== '_' && itemName.indexOf('$') !== 0;
394+
})
395+
.reduce(function(array, optionName) {
396+
if (typeof options[optionName] === 'boolean' && options[optionName]) {
397+
array.push('--' + optionName);
398+
} else if (typeof options[optionName] !== 'boolean') {
399+
array.push('--' + optionName, options[optionName]);
400+
}
401+
return array;
402+
}, []);
403+
}
404+
359405
module.exports = {
360406
isPlatformInstalled: isPlatformInstalled,
361407
arePluginsInstalled: arePluginsInstalled,

spec/utils/cordova.spec.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,94 @@ describe('filterArgumentsForCordova', function() {
423423
});
424424
});
425425

426+
describe('consolidate options', function() {
427+
var consolidateOptions = cordovaUtils.__get__('consolidateOptions');
428+
429+
it('should consolidate options to the first array element', function() {
430+
var options = {
431+
_: ['run', 'ios'],
432+
consolelogs: false,
433+
c: true,
434+
serverlogs: false,
435+
s: true,
436+
debug: false,
437+
release: false,
438+
l: true,
439+
p: 8100,
440+
r: 6000,
441+
'$0': 'ionic',
442+
v2: true,
443+
runLivereload: true,
444+
isPlatformServe: true
445+
};
446+
var results = consolidateOptions(['port', 'p'], options);
447+
expect(results).toEqual({
448+
_: ['run', 'ios'],
449+
consolelogs: false,
450+
c: true,
451+
serverlogs: false,
452+
s: true,
453+
debug: false,
454+
release: false,
455+
l: true,
456+
port: 8100,
457+
r: 6000,
458+
'$0': 'ionic',
459+
v2: true,
460+
runLivereload: true,
461+
isPlatformServe: true
462+
});
463+
});
464+
465+
it('should consolidate duplicates', function() {
466+
var options = {
467+
_: ['run', 'ios'],
468+
consolelogs: false,
469+
c: true,
470+
serverlogs: false,
471+
s: true,
472+
debug: false,
473+
release: false,
474+
l: true,
475+
p: 8100,
476+
port: 8100,
477+
r: 6000,
478+
'$0': 'ionic',
479+
v2: true,
480+
runLivereload: true,
481+
isPlatformServe: true
482+
};
483+
var results = consolidateOptions(['port', 'p'], options);
484+
expect(results).toEqual({
485+
_: ['run', 'ios'],
486+
consolelogs: false,
487+
c: true,
488+
serverlogs: false,
489+
s: true,
490+
debug: false,
491+
release: false,
492+
l: true,
493+
port: 8100,
494+
r: 6000,
495+
'$0': 'ionic',
496+
v2: true,
497+
runLivereload: true,
498+
isPlatformServe: true
499+
});
500+
});
501+
});
502+
503+
describe('optionsToArray options', function() {
504+
var optionsToArray = cordovaUtils.__get__('optionsToArray');
505+
506+
it('should convert argv options into an array', function() {
507+
var processArguments = ['node', 'ionic', 'run', 'ios', '--port', '8100', '--livereload'];
508+
var rawCliArguments = processArguments.slice(2);
509+
var argv = optimist(rawCliArguments).argv;
510+
var results = optionsToArray(argv);
511+
expect(results).toEqual([
512+
'--port', 8100,
513+
'--livereload',
514+
]);
515+
});
516+
});

0 commit comments

Comments
 (0)