Skip to content

Commit cdf2ed3

Browse files
committed
chore(): replace exec with spawn and ensure all tests pass.
1 parent 57e4ad4 commit cdf2ed3

File tree

8 files changed

+154
-94
lines changed

8 files changed

+154
-94
lines changed

lib/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ Cli.runNpmHook = function runNpmHook(hook) {
332332
process.env['FORCE_COLOR'] = true;
333333

334334
var q = Q.defer();
335-
var spawn = require('cross-spawn-async');
335+
var crossSpawn = require('cross-spawn');
336336

337-
var spawned = spawn('npm', args, { stdio: ['pipe', 'pipe', process.stderr] });
337+
var spawned = crossSpawn.spawn('npm', args, { stdio: ['pipe', 'pipe', process.stderr] });
338338
spawned.on('error', function(err) {
339339
log.error('Unable to run spawn command ' + err);
340340
});

lib/utils/cordova.js

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,38 @@ var Serve = IonicAppLib.serve;
1010
var Project = IonicAppLib.project;
1111
var log = IonicAppLib.logging.logger;
1212
var ConfigXml = IonicAppLib.configXml;
13-
var childProcess = require('child_process');
14-
var childExec = childProcess.exec;
15-
var promiseExec = Q.denodeify(childExec);
13+
var crossSpawn = require('cross-spawn');
1614
var npmScripts = require('../utils/npmScripts');
1715

16+
17+
function promiseSpawn(cmd, args) {
18+
var deferred = Q.defer();
19+
var info = '';
20+
21+
try {
22+
var proc = crossSpawn.spawn(cmd, args);
23+
24+
proc.stdout.on('data', function(data) {
25+
info += data.toString('utf8');
26+
});
27+
28+
proc.on('error', function(error) {
29+
deferred.reject(error);
30+
});
31+
32+
proc.on('close', function(code) {
33+
if (code !== 0) {
34+
return deferred.reject(code);
35+
}
36+
deferred.resolve(info.replace('\n', ' '));
37+
});
38+
} catch (e) {
39+
return deferred.reject(e);
40+
}
41+
42+
return deferred.promise;
43+
}
44+
1845
/**
1946
* Returns true or false after checking if the platform exists
2047
* Synchronous
@@ -24,14 +51,21 @@ var npmScripts = require('../utils/npmScripts');
2451
* @return {Boolean} True if platform is installed
2552
*/
2653
function isPlatformInstalled(platform, baseDir) {
54+
var deferred = Q.defer();
2755
var platformPath = path.join(baseDir, 'platforms', platform);
2856

2957
try {
30-
fs.statSync(platformPath);
31-
return true;
58+
fs.stat(platformPath, function(err) {
59+
if (err) {
60+
return deferred.resolve(false);
61+
}
62+
return deferred.resolve(true);
63+
});
3264
} catch (ex) {
33-
return false;
65+
deferred.resolve(false);
3466
}
67+
68+
return deferred.promise;
3569
}
3670

3771
/**
@@ -42,14 +76,42 @@ function isPlatformInstalled(platform, baseDir) {
4276
* @return {Boolean} True if any plugin is installed
4377
*/
4478
function arePluginsInstalled(baseDir) {
79+
var deferred = Q.defer();
4580
var pluginPath = path.join(baseDir, 'plugins');
4681

4782
try {
48-
fs.statSync(pluginPath);
49-
return true;
83+
fs.stat(pluginPath, function(err) {
84+
if (err) {
85+
return deferred.resolve(false);
86+
}
87+
return deferred.resolve(true);
88+
});
5089
} catch (ex) {
51-
return false;
90+
deferred.resolve(false);
5291
}
92+
93+
return deferred.promise;
94+
}
95+
96+
/**
97+
* Install ionic required plugins
98+
*
99+
* @return {Promise} Promise upon completion
100+
*/
101+
function installPlugins() {
102+
var plugins = [
103+
'cordova-plugin-device',
104+
'cordova-plugin-console',
105+
'cordova-plugin-whitelist',
106+
'cordova-plugin-splashscreen',
107+
'cordova-plugin-statusbar',
108+
'ionic-plugin-keyboard'
109+
];
110+
111+
return Q.all(plugins.map(function(plugin) {
112+
log.info(['Installing ', plugin].join(''));
113+
return promiseSpawn('cordova', ['plugin', 'add', '--save', plugin]);
114+
}));
53115
}
54116

55117
/**
@@ -62,7 +124,7 @@ function installPlatform(platform) {
62124
log.info(chalk.yellow('• You\'re trying to build for ' + platform + ' but don\'t have the platform installed yet.'));
63125
log.info('∆ Installing ' + platform + ' for you.');
64126

65-
return promiseExec('cordova platform add ' + platform).then(function() {
127+
return promiseSpawn('cordova', ['platform', 'add', platform]).then(function() {
66128
log.info('√ Installed platform ' + platform);
67129
});
68130
}
@@ -72,9 +134,7 @@ function execCordovaCommand(optionList, isLiveReload, serveOptions) {
72134
isLiveReload = !!isLiveReload;
73135

74136
log.debug('Executing cordova cli: ' + optionList.join(' '));
75-
var cordovaProcess = childProcess.exec('cordova ' + optionList.join(' '), {
76-
maxBuffer: 1024 * 1024 * 1024
77-
});
137+
var cordovaProcess = crossSpawn.spawn('cordova', optionList);
78138

79139
cordovaProcess.stdout.on('data', function(data) {
80140
log.info(data.toString());
@@ -137,26 +197,6 @@ function execCordovaCommand(optionList, isLiveReload, serveOptions) {
137197
return deferred.promise;
138198
}
139199

140-
/**
141-
* Install ionic required plugins
142-
*
143-
* @return {Promise} Promise upon completion
144-
*/
145-
function installPlugins() {
146-
var plugins = [
147-
'cordova-plugin-device',
148-
'cordova-plugin-console',
149-
'cordova-plugin-whitelist',
150-
'cordova-plugin-splashscreen',
151-
'cordova-plugin-statusbar',
152-
'ionic-plugin-keyboard'
153-
];
154-
155-
return Q.all(plugins.map(function(plugin) {
156-
log.info(['Installing ', plugin].join(''));
157-
return promiseExec('cordova plugin add --save ' + plugin);
158-
}));
159-
}
160200

161201
/**
162202
* Filter and gather arguments from command line to be passed to Cordova

lib/utils/npmScripts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ function hasIonicScript(name) {
2020
}
2121

2222
function runIonicScript(name, argv) {
23-
var spawn = require('cross-spawn-async');
23+
var crossSpawn = require('cross-spawn');
2424
var scriptName = getScriptName(name);
2525
var q = Q.defer();
2626

2727
process.env['FORCE_COLOR'] = true;
28-
var scriptSpawn = spawn('npm', ['run', scriptName, '--'].concat(argv || []), {
28+
var scriptSpawn = crossSpawn.spawn('npm', ['run', scriptName, '--'].concat(argv || []), {
2929
stdio: [process.stdin, 'pipe', process.stderr]
3030
})
3131
.on('error', function(err) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"@ionic/app-generators": "0.0.3",
6464
"chalk": "^1.1.3",
6565
"cli-table": "0.3.1",
66-
"cross-spawn-async": "2.1.9",
66+
"cross-spawn": "^5.0.1",
6767
"expand-tilde": "1.2.0",
6868
"form-data": "0.1.4",
6969
"gulp": "3.8.8",
@@ -98,7 +98,7 @@
9898
"bundledDependencies": [
9999
"cli-table",
100100
"chalk",
101-
"cross-spawn-async",
101+
"cross-spawn",
102102
"expand-tilde",
103103
"form-data",
104104
"gulp",

spec/tasks/emulate.spec.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('emulate command', function() {
186186
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q(true));
187187

188188
emulate.run(null, argv, rawCliArguments).then(function() {
189-
expect(cordovaUtils.execCordovaCommand).toHaveBeenCalledWith(['emulate', '-n', 'ios'], false, true);
189+
expect(cordovaUtils.execCordovaCommand).toHaveBeenCalledWith(['emulate', 'ios', '-n'], false, true);
190190
done();
191191
});
192192
});
@@ -319,8 +319,13 @@ describe('emulate command', function() {
319319

320320
emulate.run(null, argv, rawCliArguments).then(function() {
321321
expect(npmScripts.hasIonicScript).toHaveBeenCalledWith('build');
322-
expect(npmScripts.runIonicScript).toHaveBeenCalledWith('serve', ['-p', jasmine.any(Number), '--address', jasmine.any(String)]);
323-
expect(cordovaUtils.execCordovaCommand).toHaveBeenCalledWith(['emulate', 'android'], false, true);
322+
expect(npmScripts.runIonicScript).toHaveBeenCalledWith('serve', [
323+
'--port', jasmine.any(Number),
324+
'--address', jasmine.any(String),
325+
'--liveReloadPort', jasmine.any(Number),
326+
'--nobrowser'
327+
]);
328+
expect(cordovaUtils.execCordovaCommand).toHaveBeenCalledWith(['emulate', 'android'], true, true);
324329
done();
325330
}).catch(function(e) {
326331
console.log(e);

spec/tasks/run.spec.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('run command', function() {
157157
spyOn(cordovaUtils, 'execCordovaCommand').andReturn(Q(true));
158158

159159
run.run(null, argv, rawCliArguments).then(function() {
160-
expect(cordovaUtils.execCordovaCommand).toHaveBeenCalledWith(['run', '-n', 'ios'], false, true);
160+
expect(cordovaUtils.execCordovaCommand).toHaveBeenCalledWith(['run', 'ios', '-n'], false, true);
161161
done();
162162
});
163163
});
@@ -314,8 +314,13 @@ describe('run command', function() {
314314

315315
run.run(null, argv, rawCliArguments).then(function() {
316316
expect(npmScripts.hasIonicScript).toHaveBeenCalledWith('build');
317-
expect(npmScripts.runIonicScript).toHaveBeenCalledWith('serve', ['-p', jasmine.any(Number), '--address', jasmine.any(String)]);
318-
expect(cordovaUtils.execCordovaCommand).toHaveBeenCalledWith(['run', 'android'], false, true);
317+
expect(npmScripts.runIonicScript).toHaveBeenCalledWith('serve', [
318+
'--port', jasmine.any(Number),
319+
'--address', jasmine.any(String),
320+
'--liveReloadPort', jasmine.any(Number),
321+
'--nobrowser'
322+
]);
323+
expect(cordovaUtils.execCordovaCommand).toHaveBeenCalledWith(['run', 'android'], true, true);
319324
done();
320325
}).catch(function(e) {
321326
console.log(e);

spec/tasks/serve.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ describe('Serve', function() {
283283
spyOn(npmScripts, 'hasIonicScript').andReturn(Q(true));
284284
spyOn(npmScripts, 'runIonicScript').andReturn(Q(true));
285285

286-
serveTask.run({}, argv).then(function() {
286+
serveTask.run({}, argv, rawCliArguments).then(function() {
287287
expect(npmScripts.hasIonicScript).toHaveBeenCalledWith('serve');
288-
expect(npmScripts.runIonicScript).toHaveBeenCalledWith('serve', argv);
288+
expect(npmScripts.runIonicScript).toHaveBeenCalledWith('serve', rawCliArguments.slice(1));
289289
done();
290290
});
291291
});

0 commit comments

Comments
 (0)