Skip to content

Commit cb01e1d

Browse files
committed
fix(): change all exec commands within add, remove, and service to use sync.
1 parent 9308577 commit cb01e1d

File tree

5 files changed

+77
-60
lines changed

5 files changed

+77
-60
lines changed

lib/ionic/add.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ var settings = {
2323
function installBowerComponent(componentName) {
2424
var bowerInstallCommand = 'bower install --save-dev ' + componentName;
2525

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

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

4039
/**

lib/ionic/remove.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ var settings = {
2121
function uninstallBowerComponent(componentName) {
2222
var bowerUninstallCommand = 'bower uninstall --save-dev ' + componentName;
2323

24-
var result = childProcess.exec(bowerUninstallCommand);
24+
try {
25+
var result = childProcess.execSync(bowerUninstallCommand);
2526

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

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

3739
/**

lib/ionic/service.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var fs = require('fs');
44
var path = require('path');
5-
var exec = require('child_process').exec;
5+
var execSync = require('child_process').execSync;
66
var _ = require('underscore');
77
var extend = require('../utils/extend');
88
var bower = require('../utils/bower');
@@ -59,29 +59,35 @@ function addServiceToIonicJson(serviceName) {
5959

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

64-
if (result.code !== 0) {
63+
try {
64+
var result = execSync(bowerInstallCommand);
6565

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

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

78-
var result = exec(bowerUninstallCommand);
80+
try {
81+
var result = execSync(bowerUninstallCommand);
7982

80-
if (result.code !== 0) {
81-
var errorMessage = 'Failed to find the service "'.bold + serviceName.verbose +
82-
'"'.bold + '.\nAre you sure it exists?'.bold;
83-
fail(errorMessage, 'service');
84-
}
83+
if (result.code !== 0) {
84+
return;
85+
}
86+
} catch (e) {} // eslint-disable-line no-empty
87+
88+
var errorMessage = 'Failed to find the service "'.bold + serviceName.verbose +
89+
'"'.bold + '.\nAre you sure it exists?'.bold;
90+
fail(errorMessage, 'service');
8591
}
8692

8793
function getBowerComponentsLocation() {
@@ -117,12 +123,17 @@ function installBowerPlugins(directory, serviceName) {
117123
log.info('Installing cordova plugin - ' + plugin.name + ' (' + plugin.id + ')');
118124
var installPluginCmd = 'ionic plugin add ' + plugin.uri;
119125
log.info(installPluginCmd);
120-
var pluginInstallResult = exec(installPluginCmd);
121126

122-
if (pluginInstallResult.code !== 0) {
123-
var errorMessage = 'Failed to find the plugin "'.bold + plugin.name.verbose + '"'.bold + '.'.bold;
124-
fail(errorMessage, 'service');
125-
}
127+
try {
128+
var pluginInstallResult = execSync(installPluginCmd);
129+
130+
if (pluginInstallResult.code === 0) {
131+
return;
132+
}
133+
} catch (e) {} // eslint-disable-line no-empty
134+
135+
var errorMessage = 'Failed to find the plugin "'.bold + plugin.name.verbose + '"'.bold + '.'.bold;
136+
fail(errorMessage, 'service');
126137
});
127138
}
128139

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

136-
if (pluginRemoveResult.code !== 0) {
137-
var errorMessage = 'Failed to find the plugin to remove "'.bold + plugin.name.verbose + '"'.bold + '.'.bold;
138-
fail(errorMessage, 'service');
139-
}
146+
try {
147+
var pluginRemoveResult = execSync(uninstallPluginCmd);
148+
149+
if (pluginRemoveResult.code === 0) {
150+
return;
151+
}
152+
} catch (e) {} // eslint-disable-line no-empty
153+
154+
var errorMessage = 'Failed to find the plugin to remove "'.bold + plugin.name.verbose + '"'.bold + '.'.bold;
155+
fail(errorMessage, 'service');
140156
});
141157
}
142158

spec/tasks/add.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('add command', function() {
3636
var argv = optimist(rawCliArguments).argv;
3737

3838
beforeEach(function() {
39-
spyOn(childProcess, 'exec');
39+
spyOn(childProcess, 'execSync');
4040
});
4141

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

112112
describe('installBowerComponent function', function() {
113113

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

118118
var installBowerComponent = add.__get__('installBowerComponent');
119119
installBowerComponent('thing');
120-
expect(childProcess.exec).toHaveBeenCalledWith('bower install --save-dev thing');
120+
expect(childProcess.execSync).toHaveBeenCalledWith('bower install --save-dev thing');
121121
expect(log.info).toHaveBeenCalledWith('Bower component installed - thing');
122122
});
123123

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

128128
var installBowerComponent = add.__get__('installBowerComponent');
129129
installBowerComponent('thing');
130-
expect(childProcess.exec).toHaveBeenCalledWith('bower install --save-dev thing');
130+
expect(childProcess.execSync).toHaveBeenCalledWith('bower install --save-dev thing');
131131
expect(appLibUtils.fail).toHaveBeenCalledWith(jasmine.any(String), 'add');
132132
});
133133
});

spec/tasks/remove.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('remove command', function() {
3636
var argv = optimist(rawCliArguments).argv;
3737

3838
beforeEach(function() {
39-
spyOn(childProcess, 'exec');
39+
spyOn(childProcess, 'execSync');
4040
});
4141

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

113113
describe('uninstallBowerComponent function', function() {
114114

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

119119
var uninstallBowerComponent = remove.__get__('uninstallBowerComponent');
120120
uninstallBowerComponent('thing');
121-
expect(childProcess.exec).toHaveBeenCalledWith('bower uninstall --save-dev thing');
121+
expect(childProcess.execSync).toHaveBeenCalledWith('bower uninstall --save-dev thing');
122122
expect(log.info).toHaveBeenCalledWith(jasmine.any(String));
123123
});
124124

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

129129
var uninstallBowerComponent = remove.__get__('uninstallBowerComponent');
130130
uninstallBowerComponent('thing');
131-
expect(childProcess.exec).toHaveBeenCalledWith('bower uninstall --save-dev thing');
131+
expect(childProcess.execSync).toHaveBeenCalledWith('bower uninstall --save-dev thing');
132132
expect(appLibUtils.fail).toHaveBeenCalledWith(jasmine.any(String), 'remove');
133133
});
134134
});

0 commit comments

Comments
 (0)