Skip to content

Commit

Permalink
fix(cordova): ensure that each cordova command will properly catch wh… (
Browse files Browse the repository at this point in the history
#1154)

* fix(cordova): ensure that each cordova command will properly catch when cordova throws an error.

* fix(cordova): show error message to user when iOS is only available on an OSX machine.
  • Loading branch information
jthoms1 authored Jul 7, 2016
1 parent 51469d6 commit 142128a
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 17 deletions.
9 changes: 8 additions & 1 deletion lib/ionic/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var os = require('os');
var Q = require('q');
var IonicAppLib = require('ionic-app-lib');
var ConfigXml = IonicAppLib.configXml;
var log = IonicAppLib.logging.logger;
var cordovaUtils = require('../utils/cordova');

var settings = {
Expand Down Expand Up @@ -37,7 +38,8 @@ function run(ionic, argv, rawCliArguments) {
}

if (runPlatform === 'ios' && os.platform() !== 'darwin') {
return Q.reject('✗ You cannot run iOS unless you are on Mac OSX.');
log.error('✗ You cannot run iOS unless you are on Mac OSX.');
return Q();
}

var promiseList = []
Expand All @@ -59,6 +61,11 @@ function run(ionic, argv, rawCliArguments) {
.then(function() {
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);
return cordovaUtils.execCordovaCommand(optionList);
})
.catch(function(ex) {
if (ex instanceof Error) {
log.error(ex);
}
});
}

Expand Down
6 changes: 6 additions & 0 deletions lib/ionic/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var extend = Object.assign || require('util')._extend; // eslint-disable-line no-underscore-dangle
var IonicAppLib = require('ionic-app-lib');
var ConfigXml = IonicAppLib.configXml;
var log = IonicAppLib.logging.logger;
var cordovaUtils = require('../utils/cordova');

var settings = {
Expand All @@ -23,6 +24,11 @@ function run(ionic, argv, rawCliArguments) {
}).then(function() {
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);
return cordovaUtils.execCordovaCommand(optionList);
})
.catch(function(ex) {
if (ex instanceof Error) {
log.error(ex);
}
});
}

Expand Down
9 changes: 8 additions & 1 deletion lib/ionic/emulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var os = require('os');
var Q = require('q');
var IonicAppLib = require('ionic-app-lib');
var ConfigXml = IonicAppLib.configXml;
var log = IonicAppLib.logging.logger;
var cordovaUtils = require('../utils/cordova');

var cordovaRunEmulateOptions = {
Expand Down Expand Up @@ -54,7 +55,8 @@ function run(ionic, argv, rawCliArguments) {
}

if (runPlatform === 'ios' && os.platform() !== 'darwin') {
return Q.reject('✗ You cannot run iOS unless you are on Mac OSX.');
log.error('✗ You cannot run iOS unless you are on Mac OSX.');
return Q();
}

var promiseList = []
Expand All @@ -79,6 +81,11 @@ function run(ionic, argv, rawCliArguments) {
.then(function(serveOptions) {
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);
return cordovaUtils.execCordovaCommand(optionList, isLiveReload, serveOptions);
})
.catch(function(ex) {
if (ex instanceof Error) {
log.error(ex);
}
});
}

Expand Down
5 changes: 5 additions & 0 deletions lib/ionic/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ function run(ionic, argv, rawCliArguments) {
log.info('Removing platform from package.json file');
return State.removePlatform(appDirectory, argumentName);
}
})
.catch(function(ex) {
if (ex instanceof Error) {
log.error(ex);
}
});
}

Expand Down
5 changes: 5 additions & 0 deletions lib/ionic/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ function run(ionic, argv, rawCliArguments) {
log.info('Removing plugin from package.json file');
return State.removePlugin(appDirectory, argumentName);
}
})
.catch(function(ex) {
if (ex instanceof Error) {
log.error(ex);
}
});
}

Expand Down
6 changes: 6 additions & 0 deletions lib/ionic/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var extend = Object.assign || require('util')._extend; // eslint-disable-line no-underscore-dangle
var IonicAppLib = require('ionic-app-lib');
var ConfigXml = IonicAppLib.configXml;
var log = IonicAppLib.logging.logger;
var cordovaUtils = require('../utils/cordova');

var settings = {
Expand All @@ -24,6 +25,11 @@ function run(ionic, argv, rawCliArguments) {
.then(function() {
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);
return cordovaUtils.execCordovaCommand(optionList);
})
.catch(function(ex) {
if (ex instanceof Error) {
log.error(ex);
}
});
}

Expand Down
9 changes: 8 additions & 1 deletion lib/ionic/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var os = require('os');
var Q = require('q');
var IonicAppLib = require('ionic-app-lib');
var ConfigXml = IonicAppLib.configXml;
var log = IonicAppLib.logging.logger;
var cordovaUtils = require('../utils/cordova');

var cordovaRunEmulateOptions = {
Expand Down Expand Up @@ -54,7 +55,8 @@ function run(ionic, argv, rawCliArguments) {
}

if (runPlatform === 'ios' && os.platform() !== 'darwin') {
return Q.reject('✗ You cannot run iOS unless you are on Mac OSX.');
log.error('✗ You cannot run iOS unless you are on Mac OSX.');
return Q();
}

var promiseList = []
Expand All @@ -79,6 +81,11 @@ function run(ionic, argv, rawCliArguments) {
.then(function(serveOptions) {
var optionList = cordovaUtils.filterArgumentsForCordova(cmdName, argv, rawArgs);
return cordovaUtils.execCordovaCommand(optionList, isLiveReload, serveOptions);
})
.catch(function(ex) {
if (ex instanceof Error) {
log.error(ex);
}
});
}

Expand Down
34 changes: 30 additions & 4 deletions spec/tasks/build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ describe('build command', function() {
it('should fail if the system is not Mac and the platform is iOS', function(done) {
spyOn(os, 'platform').andReturn('windows');

build.run(null, argv, rawCliArguments).catch(function(error) {
expect(error).toEqual('✗ You cannot run iOS unless you are on Mac OSX.');
build.run(null, argv, rawCliArguments).then(function() {
expect(log.error).toHaveBeenCalledWith('✗ You cannot run iOS unless you are on Mac OSX.');
done();
});
});
});


describe('cordova platform and plugin checks', function() {

var appDirectory = '/ionic/app/path';
Expand All @@ -91,11 +90,11 @@ describe('build command', function() {

spyOn(cordovaUtils, 'installPlatform').andReturn(Q(true));
spyOn(cordovaUtils, 'installPlugins').andReturn(Q(true));
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q(0));
});

it('should try to install the cordova platform if it is not installed', function(done) {
spyOn(cordovaUtils, 'isPlatformInstalled').andReturn(false);
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q(0));

build.run(null, argv, rawCliArguments).then(function() {
expect(cordovaUtils.installPlatform).toHaveBeenCalledWith('ios');
Expand All @@ -106,6 +105,7 @@ describe('build command', function() {

it('should not try to install the cordova platform if it is installed', function(done) {
spyOn(cordovaUtils, 'isPlatformInstalled').andReturn(true);
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q(0));

build.run(null, argv, rawCliArguments).then(function() {
expect(cordovaUtils.installPlatform).not.toHaveBeenCalledWith();
Expand All @@ -116,6 +116,7 @@ describe('build command', function() {
it('should install plugins if they are not installed', function(done) {
spyOn(cordovaUtils, 'isPlatformInstalled').andReturn(true);
spyOn(cordovaUtils, 'arePluginsInstalled').andReturn(false);
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q(0));

build.run(null, argv, rawCliArguments).then(function() {
expect(cordovaUtils.arePluginsInstalled).toHaveBeenCalledWith(appDirectory);
Expand All @@ -127,12 +128,37 @@ describe('build command', function() {
it('should not install plugins if they are installed', function(done) {
spyOn(cordovaUtils, 'isPlatformInstalled').andReturn(true);
spyOn(cordovaUtils, 'arePluginsInstalled').andReturn(true);
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q(0));

build.run(null, argv, rawCliArguments).then(function() {
expect(cordovaUtils.arePluginsInstalled).toHaveBeenCalledWith(appDirectory);
expect(cordovaUtils.installPlugins).not.toHaveBeenCalledWith();
done();
});
});

it('should fail if any functions throw', function(done) {
var error = new Error('error occurred');
spyOn(cordovaUtils, 'isPlatformInstalled').andReturn(true);
spyOn(cordovaUtils, 'arePluginsInstalled').andReturn(true);
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q.reject(error));

build.run({}, argv, rawCliArguments).then(function() {
expect(log.error).toHaveBeenCalledWith(error);
done();
});
});

it('should fail if any functions throw and not log if not an instance of an Error', function(done) {
var error = 1;
spyOn(cordovaUtils, 'isPlatformInstalled').andReturn(true);
spyOn(cordovaUtils, 'arePluginsInstalled').andReturn(true);
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q.reject(error));

build.run({}, argv, rawCliArguments).then(function() {
expect(log.error).not.toHaveBeenCalled();
done();
});
});
});
});
14 changes: 12 additions & 2 deletions spec/tasks/compile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ describe('compile command', function() {
var error = new Error('error occurred');
spyOn(ConfigXml, 'setConfigXml').andReturn(Q.reject(error));

compile.run({}, argv, rawCliArguments).catch(function(ex) {
expect(ex).toEqual(error);
compile.run({}, argv, rawCliArguments).then(function() {
expect(log.error).toHaveBeenCalledWith(error);
done();
});
});

it('should fail if any functions throw and not log if error is not instanceof Error', function(done) {
var error = 1;
spyOn(ConfigXml, 'setConfigXml').andReturn(Q.reject(error));

compile.run({}, argv, rawCliArguments).then(function() {
expect(log.error).not.toHaveBeenCalled();
done();
});
});
Expand Down
38 changes: 35 additions & 3 deletions spec/tasks/emulate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('emulate command', function() {
it('should default to iOS for the platform', function(done) {
spyOn(os, 'platform').andReturn('darwin');

emulate.run(null, argv, rawCliArguments).catch(function() {
emulate.run(null, argv, rawCliArguments).then(function() {
expect(cordovaUtils.isPlatformInstalled).toHaveBeenCalledWith('ios', appDirectory);
done();
});
Expand All @@ -66,8 +66,8 @@ describe('emulate command', function() {
it('should fail if the system is not Mac and the platform is iOS', function(done) {
spyOn(os, 'platform').andReturn('windows');

emulate.run(null, argv, rawCliArguments).catch(function(error) {
expect(error).toEqual('✗ You cannot run iOS unless you are on Mac OSX.');
emulate.run(null, argv, rawCliArguments).then(function() {
expect(log.error).toHaveBeenCalledWith('✗ You cannot run iOS unless you are on Mac OSX.');
done();
});
});
Expand Down Expand Up @@ -140,6 +140,38 @@ describe('emulate command', function() {
spyOn(os, 'platform').andReturn('darwin');
});

it('should fail if any functions throw', function(done) {
var processArguments = ['node', 'ionic', 'emulate', '-n'];
var rawCliArguments = processArguments.slice(2);
var argv = optimist(rawCliArguments).argv;

var error = new Error('error occurred');
spyOn(cordovaUtils, 'isPlatformInstalled').andReturn(true);
spyOn(cordovaUtils, 'arePluginsInstalled').andReturn(true);
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q.reject(error));

emulate.run({}, argv, rawCliArguments).then(function() {
expect(log.error).toHaveBeenCalledWith(error);
done();
});
});

it('should fail if any functions throw and not log if not an instance of an Error', function(done) {
var processArguments = ['node', 'ionic', 'emulate', '-n'];
var rawCliArguments = processArguments.slice(2);
var argv = optimist(rawCliArguments).argv;

var error = 1;
spyOn(cordovaUtils, 'isPlatformInstalled').andReturn(true);
spyOn(cordovaUtils, 'arePluginsInstalled').andReturn(true);
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q.reject(error));

emulate.run({}, argv, rawCliArguments).then(function() {
expect(log.error).not.toHaveBeenCalled();
done();
});
});

it('should execute the command against the cordova util', function(done) {
var processArguments = ['node', 'ionic', 'emulate', '-n'];
var rawCliArguments = processArguments.slice(2);
Expand Down
Loading

0 comments on commit 142128a

Please sign in to comment.