Skip to content

Commit

Permalink
fix(): change all exec commands within add, remove, and service to us…
Browse files Browse the repository at this point in the history
…e sync.
  • Loading branch information
jthoms1 committed Aug 3, 2016
1 parent 9308577 commit cb01e1d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 60 deletions.
21 changes: 10 additions & 11 deletions lib/ionic/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ var settings = {
function installBowerComponent(componentName) {
var bowerInstallCommand = 'bower install --save-dev ' + componentName;

var result = childProcess.exec(bowerInstallCommand);

if (result.code !== 0) {

// Error happened, report it.
var errorMessage = 'Bower error, check that "'.red.bold + componentName.verbose + '"'.red.bold +
' exists,'.red.bold + '\nor try running "'.red.bold + bowerInstallCommand.verbose + '" for more info.'.red.bold;
try {
var result = childProcess.execSync(bowerInstallCommand);
if (result.code === 0) {
return log.info('Bower component installed - ' + componentName);
}
} catch (e) {} // eslint-disable-line no-empty

appLibUtils.fail(errorMessage, 'add');
} else {
log.info('Bower component installed - ' + componentName);
}
// Error happened, report it.
var errorMessage = 'Bower error, check that "'.red.bold + componentName + '"'.red.bold +
' exists,'.red.bold + '\nor try running "'.red.bold + bowerInstallCommand + '" for more info.'.red.bold;
appLibUtils.fail(errorMessage, 'add');
}

/**
Expand Down
20 changes: 11 additions & 9 deletions lib/ionic/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ var settings = {
function uninstallBowerComponent(componentName) {
var bowerUninstallCommand = 'bower uninstall --save-dev ' + componentName;

var result = childProcess.exec(bowerUninstallCommand);
try {
var result = childProcess.execSync(bowerUninstallCommand);

if (result.code !== 0) {
var errorMessage = 'Failed to find the bower component "'.red.bold + componentName.verbose +
'"'.red.bold + '.\nAre you sure it exists?'.red.bold;
if (result.code === 0) {
var message = 'Bower component removed - ' + componentName;
return log.info(message.red);
}
} catch (e) {} // eslint-disable-line no-empty

appLibUtils.fail(errorMessage, 'remove');
} else {
var message = 'Bower component removed - ' + componentName;
log.info(message.red);
}
var errorMessage = 'Failed to find the bower component "'.red.bold + componentName +
'"'.red.bold + '.\nAre you sure it exists?'.red.bold;

appLibUtils.fail(errorMessage, 'remove');
}

/**
Expand Down
68 changes: 42 additions & 26 deletions lib/ionic/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
var execSync = require('child_process').execSync;
var _ = require('underscore');
var extend = require('../utils/extend');
var bower = require('../utils/bower');
Expand Down Expand Up @@ -59,29 +59,35 @@ function addServiceToIonicJson(serviceName) {

function installBowerComponent(serviceName) {
var bowerInstallCommand = 'bower link ionic-service-' + serviceName;
var result = exec(bowerInstallCommand);

if (result.code !== 0) {
try {
var result = execSync(bowerInstallCommand);

// Error happened, report it.
var errorMessage = 'Failed to find the service "'.bold + serviceName.verbose +
'"'.bold + '.\nAre you sure it exists?'.bold;
fail(errorMessage, 'service');
} else {
addServiceToIonicJson(serviceName);
}
if (result.code !== 0) {
return addServiceToIonicJson(serviceName);
}
} catch (e) {} // eslint-disable-line no-empty

// Error happened, report it.
var errorMessage = 'Failed to find the service "'.bold + serviceName.verbose +
'"'.bold + '.\nAre you sure it exists?'.bold;
fail(errorMessage, 'service');
}

function uninstallBowerComponent(serviceName) {
var bowerUninstallCommand = 'bower unlink ionic-service-' + serviceName;

var result = exec(bowerUninstallCommand);
try {
var result = execSync(bowerUninstallCommand);

if (result.code !== 0) {
var errorMessage = 'Failed to find the service "'.bold + serviceName.verbose +
'"'.bold + '.\nAre you sure it exists?'.bold;
fail(errorMessage, 'service');
}
if (result.code !== 0) {
return;
}
} catch (e) {} // eslint-disable-line no-empty

var errorMessage = 'Failed to find the service "'.bold + serviceName.verbose +
'"'.bold + '.\nAre you sure it exists?'.bold;
fail(errorMessage, 'service');
}

function getBowerComponentsLocation() {
Expand Down Expand Up @@ -117,12 +123,17 @@ function installBowerPlugins(directory, serviceName) {
log.info('Installing cordova plugin - ' + plugin.name + ' (' + plugin.id + ')');
var installPluginCmd = 'ionic plugin add ' + plugin.uri;
log.info(installPluginCmd);
var pluginInstallResult = exec(installPluginCmd);

if (pluginInstallResult.code !== 0) {
var errorMessage = 'Failed to find the plugin "'.bold + plugin.name.verbose + '"'.bold + '.'.bold;
fail(errorMessage, 'service');
}
try {
var pluginInstallResult = execSync(installPluginCmd);

if (pluginInstallResult.code === 0) {
return;
}
} catch (e) {} // eslint-disable-line no-empty

var errorMessage = 'Failed to find the plugin "'.bold + plugin.name.verbose + '"'.bold + '.'.bold;
fail(errorMessage, 'service');
});
}

Expand All @@ -131,12 +142,17 @@ function uninstallBowerPlugins(bowerJson) {
log.info('Uninstalling cordova plugin - ' + plugin.name);
var uninstallPluginCmd = 'ionic plugin rm ' + plugin.id;
log.info(uninstallPluginCmd);
var pluginRemoveResult = exec(uninstallPluginCmd);

if (pluginRemoveResult.code !== 0) {
var errorMessage = 'Failed to find the plugin to remove "'.bold + plugin.name.verbose + '"'.bold + '.'.bold;
fail(errorMessage, 'service');
}
try {
var pluginRemoveResult = execSync(uninstallPluginCmd);

if (pluginRemoveResult.code === 0) {
return;
}
} catch (e) {} // eslint-disable-line no-empty

var errorMessage = 'Failed to find the plugin to remove "'.bold + plugin.name.verbose + '"'.bold + '.'.bold;
fail(errorMessage, 'service');
});
}

Expand Down
14 changes: 7 additions & 7 deletions spec/tasks/add.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('add command', function() {
var argv = optimist(rawCliArguments).argv;

beforeEach(function() {
spyOn(childProcess, 'exec');
spyOn(childProcess, 'execSync');
});

it('should fail if bower is not installed', function() {
Expand Down Expand Up @@ -111,23 +111,23 @@ describe('add command', function() {

describe('installBowerComponent function', function() {

it('should call childProcess exec to install the bower component', function() {
it('should call childProcess execSync to install the bower component', function() {
spyOn(log, 'info');
spyOn(childProcess, 'exec').andReturn({ code: 0 });
spyOn(childProcess, 'execSync').andReturn({ code: 0 });

var installBowerComponent = add.__get__('installBowerComponent');
installBowerComponent('thing');
expect(childProcess.exec).toHaveBeenCalledWith('bower install --save-dev thing');
expect(childProcess.execSync).toHaveBeenCalledWith('bower install --save-dev thing');
expect(log.info).toHaveBeenCalledWith('Bower component installed - thing');
});

it('should call childProcess exec and call util fail if result.code is not equal to 0', function() {
it('should call childProcess execSync and call util fail if result.code is not equal to 0', function() {
spyOn(appLibUtils, 'fail');
spyOn(childProcess, 'exec').andReturn({ code: 1 });
spyOn(childProcess, 'execSync').andReturn({ code: 1 });

var installBowerComponent = add.__get__('installBowerComponent');
installBowerComponent('thing');
expect(childProcess.exec).toHaveBeenCalledWith('bower install --save-dev thing');
expect(childProcess.execSync).toHaveBeenCalledWith('bower install --save-dev thing');
expect(appLibUtils.fail).toHaveBeenCalledWith(jasmine.any(String), 'add');
});
});
Expand Down
14 changes: 7 additions & 7 deletions spec/tasks/remove.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('remove command', function() {
var argv = optimist(rawCliArguments).argv;

beforeEach(function() {
spyOn(childProcess, 'exec');
spyOn(childProcess, 'execSync');
});

it('should fail if bower is not installed', function() {
Expand Down Expand Up @@ -112,23 +112,23 @@ describe('remove command', function() {

describe('uninstallBowerComponent function', function() {

it('should call childProcess exec to install the bower component', function() {
it('should call childProcess execSync to install the bower component', function() {
spyOn(log, 'info');
spyOn(childProcess, 'exec').andReturn({ code: 0 });
spyOn(childProcess, 'execSync').andReturn({ code: 0 });

var uninstallBowerComponent = remove.__get__('uninstallBowerComponent');
uninstallBowerComponent('thing');
expect(childProcess.exec).toHaveBeenCalledWith('bower uninstall --save-dev thing');
expect(childProcess.execSync).toHaveBeenCalledWith('bower uninstall --save-dev thing');
expect(log.info).toHaveBeenCalledWith(jasmine.any(String));
});

it('should call childProcess exec and call util fail if result.code is not equal to 0', function() {
it('should call childProcess execSync and call util fail if result.code is not equal to 0', function() {
spyOn(appLibUtils, 'fail');
spyOn(childProcess, 'exec').andReturn({ code: 1 });
spyOn(childProcess, 'execSync').andReturn({ code: 1 });

var uninstallBowerComponent = remove.__get__('uninstallBowerComponent');
uninstallBowerComponent('thing');
expect(childProcess.exec).toHaveBeenCalledWith('bower uninstall --save-dev thing');
expect(childProcess.execSync).toHaveBeenCalledWith('bower uninstall --save-dev thing');
expect(appLibUtils.fail).toHaveBeenCalledWith(jasmine.any(String), 'remove');
});
});
Expand Down

0 comments on commit cb01e1d

Please sign in to comment.