Skip to content

Commit

Permalink
fix(run): update run and emulate to pass additional options on to app…
Browse files Browse the repository at this point in the history
…-scripts. (#1735)
  • Loading branch information
jthoms1 authored Dec 6, 2016
1 parent f834cca commit 68eb3e5
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/ionic/emulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function run(ionic, argv, rawCliArguments) {
hasServeCommand = results[3];

if (hasBuildCommand && !(isLiveReload && hasServeCommand)) {
return npmScripts.runIonicScript('build');
return npmScripts.runIonicScript('build', rawArgs.slice(2));
}
return Q();
})
Expand Down
2 changes: 1 addition & 1 deletion lib/ionic/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function run(ionic, argv, rawCliArguments) {
hasServeCommand = results[3];

if (hasBuildCommand && !(isLiveReload && hasServeCommand)) {
return npmScripts.runIonicScript('build');
return npmScripts.runIonicScript('build', rawArgs.slice(2));
}
return Q();
})
Expand Down
60 changes: 53 additions & 7 deletions lib/utils/cordova.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ function startAppScriptsServer(argv) {
runLivereload: true,
isPlatformServe: true
});
options = consolidateOptions(['port', 'p'], options);
options = consolidateOptions(['liveReloadPort', 'r'], options);
options = consolidateOptions(['consolelogs', 'c'], options);
options = consolidateOptions(['serverlogs', 's'], options);
options = consolidateOptions(['livereload', 'l'], options);

return Serve.getAddress(options)
.then(function() {
Expand All @@ -338,15 +343,13 @@ function startAppScriptsServer(argv) {
// Execute the serve command from app-scripts
// Also remove platform from the raw args passed
return npmScripts.runIonicScript('serve',
[
'--port', options.port,
'--address', options.address,
'--liveReloadPort', options.liveReloadPort,
optionsToArray(options)

// Serve specific options not related to the actual run or emulate code
.concat([
'--iscordovaserve',
'--nobrowser'
]
.concat(options.consolelogs ? '--consolelogs' : [])
.concat(options.serverlogs ? '--serverlogs' : [])
])
);
})
.then(function() {
Expand All @@ -356,6 +359,49 @@ function startAppScriptsServer(argv) {
});
}

/**
* Consolidate a list of options based on those passed to it.
*
* @param {Array} list of option names. Consolidate will be to the first array element name
* @param {Object} key/value object that is to be used as the source of consolidation
* @return {Object} key/value object with consolidated options
*/
function consolidateOptions(list, options) {
var newOptions = _.extend({}, options);

list.forEach(function(optionName) {
delete newOptions[optionName];
});

var value = list.reduce(function(final, optionName) {
if (options.hasOwnProperty(optionName) && !!options[optionName]) {
final = options[optionName];
}
return final;
}, null);

if (value !== null) {
newOptions[list[0]] = value;
}

return newOptions;
}

function optionsToArray(options) {
return Object.keys(options)
.filter(function(itemName) {
return itemName !== '_' && itemName.indexOf('$') !== 0;
})
.reduce(function(array, optionName) {
if (typeof options[optionName] === 'boolean' && options[optionName]) {
array.push('--' + optionName);
} else if (typeof options[optionName] !== 'boolean') {
array.push('--' + optionName, options[optionName]);
}
return array;
}, []);
}

module.exports = {
isPlatformInstalled: isPlatformInstalled,
arePluginsInstalled: arePluginsInstalled,
Expand Down
91 changes: 91 additions & 0 deletions spec/utils/cordova.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,94 @@ describe('filterArgumentsForCordova', function() {
});
});

describe('consolidate options', function() {
var consolidateOptions = cordovaUtils.__get__('consolidateOptions');

it('should consolidate options to the first array element', function() {
var options = {
_: ['run', 'ios'],
consolelogs: false,
c: true,
serverlogs: false,
s: true,
debug: false,
release: false,
l: true,
p: 8100,
r: 6000,
'$0': 'ionic',
v2: true,
runLivereload: true,
isPlatformServe: true
};
var results = consolidateOptions(['port', 'p'], options);
expect(results).toEqual({
_: ['run', 'ios'],
consolelogs: false,
c: true,
serverlogs: false,
s: true,
debug: false,
release: false,
l: true,
port: 8100,
r: 6000,
'$0': 'ionic',
v2: true,
runLivereload: true,
isPlatformServe: true
});
});

it('should consolidate duplicates', function() {
var options = {
_: ['run', 'ios'],
consolelogs: false,
c: true,
serverlogs: false,
s: true,
debug: false,
release: false,
l: true,
p: 8100,
port: 8100,
r: 6000,
'$0': 'ionic',
v2: true,
runLivereload: true,
isPlatformServe: true
};
var results = consolidateOptions(['port', 'p'], options);
expect(results).toEqual({
_: ['run', 'ios'],
consolelogs: false,
c: true,
serverlogs: false,
s: true,
debug: false,
release: false,
l: true,
port: 8100,
r: 6000,
'$0': 'ionic',
v2: true,
runLivereload: true,
isPlatformServe: true
});
});
});

describe('optionsToArray options', function() {
var optionsToArray = cordovaUtils.__get__('optionsToArray');

it('should convert argv options into an array', function() {
var processArguments = ['node', 'ionic', 'run', 'ios', '--port', '8100', '--livereload'];
var rawCliArguments = processArguments.slice(2);
var argv = optimist(rawCliArguments).argv;
var results = optionsToArray(argv);
expect(results).toEqual([
'--port', 8100,
'--livereload',
]);
});
});

0 comments on commit 68eb3e5

Please sign in to comment.