Skip to content

Commit 5549b82

Browse files
committed
fix(commands): ensure that rawCliArguments are passed to all tasks that are ran from cli.js.
1 parent 56a9bec commit 5549b82

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

lib/cli.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Cli.run = function run(processArgv) {
141141

142142
// No need to do project specific actions, cd to root, etc.
143143
if (!task.isProjectTask) {
144-
return Q.fcall(task.run.bind(task), Cli, argv);
144+
return Q.fcall(task.run.bind(task), Cli, argv, rawCliArguments);
145145
}
146146

147147
var root = Utils.cdIonicRoot();
@@ -157,7 +157,7 @@ Cli.run = function run(processArgv) {
157157
}
158158
log.debug('node_modules directory not found, not running gulp hooks');
159159

160-
return Q.fcall(task.run.bind(task), Cli, argv);
160+
return Q.fcall(task.run.bind(task), Cli, argv, rawCliArguments);
161161
}
162162

163163
try {
@@ -184,17 +184,17 @@ Cli.run = function run(processArgv) {
184184
}
185185
log.debug('No gulpfile found, not running gulp hooks');
186186

187-
return Q.fcall(task.run.bind(task), Cli, argv);
187+
return Q.fcall(task.run.bind(task), Cli, argv, rawCliArguments);
188188
}
189189

190-
return Cli.runWithGulp(argv, task);
190+
return Cli.runWithGulp(argv, task, rawCliArguments);
191191

192192
} catch (ex) {
193193
return Utils.fail(ex);
194194
}
195195
};
196196

197-
Cli.runWithGulp = function runWithGulp(argv, taskInstance) {
197+
Cli.runWithGulp = function runWithGulp(argv, taskInstance, rawCliArguments) {
198198
var cmdName = argv._[0];
199199
var beforeHook = cmdName + ':before';
200200
var afterHook = cmdName + ':after';
@@ -229,7 +229,7 @@ Cli.runWithGulp = function runWithGulp(argv, taskInstance) {
229229

230230
// run cmd
231231
.then(function() {
232-
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv);
232+
return Q.fcall(taskInstance.run.bind(taskInstance), Cli, argv, rawCliArguments);
233233
})
234234

235235
// run afterHook

spec/cli.spec.js

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

33
var IonicAppLib = require('ionic-app-lib');
44
var semver = require('semver');
5+
var optimist = require('optimist');
56
var Ionitron = require('../lib/utils/ionitron');
67
var Q = require('q');
78
var helpUtils = require('../lib/utils/help');
@@ -162,6 +163,10 @@ describe('Cli', function() {
162163
});
163164

164165
it('should change cwd to project root for project tasks', function(done) {
166+
var processArguments = ['node', 'bin/ionic', 'fake'];
167+
var rawCliArguments = processArguments.slice(2);
168+
var argv = optimist(rawCliArguments).argv;
169+
165170
var FakeTask = {
166171
name: 'fake',
167172
title: 'fake',
@@ -171,15 +176,77 @@ describe('Cli', function() {
171176
isProjectTask: true
172177
};
173178
spyOn(IonicCli, 'getTaskSettingsByName').andReturn(FakeTask);
179+
spyOn(FakeTask, 'run').andReturn(Q(true));
174180

175-
IonicCli.run(['node', 'bin/ionic', 'fake'])
181+
IonicCli.run(processArguments)
176182
.then(function() {
177183
expect(Utils.cdIonicRoot).toHaveBeenCalled();
184+
expect(FakeTask.run).toHaveBeenCalledWith(IonicCli, argv, rawCliArguments);
185+
done();
186+
});
187+
});
188+
189+
it('should skip loading gulpfile if node_modules does not exist', function(done) {
190+
var processArguments = ['node', 'bin/ionic', 'fake'];
191+
var rawCliArguments = processArguments.slice(2);
192+
var argv = optimist(rawCliArguments).argv;
193+
194+
var FakeTask = {
195+
name: 'fake',
196+
title: 'fake',
197+
run: function() {
198+
return Q(true);
199+
},
200+
isProjectTask: true
201+
};
202+
spyOn(IonicCli, 'getTaskSettingsByName').andReturn(FakeTask);
203+
spyOn(fs, 'existsSync').andReturn(false);
204+
spyOn(FakeTask, 'run').andReturn(Q(true));
205+
spyOn(IonicCli, 'loadGulpfile');
206+
spyOn(IonicCli, 'runWithGulp');
207+
208+
IonicCli.run(processArguments)
209+
.then(function() {
210+
expect(IonicCli.loadGulpfile).not.toHaveBeenCalled();
211+
expect(IonicCli.runWithGulp).not.toHaveBeenCalled();
212+
expect(FakeTask.run).toHaveBeenCalledWith(IonicCli, argv, rawCliArguments);
213+
done();
214+
});
215+
});
216+
217+
it('should skip runWithGulp if a gulpfile does not exist', function(done) {
218+
var processArguments = ['node', 'bin/ionic', 'fake'];
219+
var rawCliArguments = processArguments.slice(2);
220+
var argv = optimist(rawCliArguments).argv;
221+
222+
var FakeTask = {
223+
name: 'fake',
224+
title: 'fake',
225+
run: function() {
226+
return Q(true);
227+
},
228+
isProjectTask: true
229+
};
230+
spyOn(IonicCli, 'getTaskSettingsByName').andReturn(FakeTask);
231+
spyOn(fs, 'existsSync').andReturn(true);
232+
spyOn(IonicCli, 'loadGulpfile').andReturn(false);
233+
spyOn(FakeTask, 'run').andReturn(Q(true));
234+
spyOn(IonicCli, 'runWithGulp');
235+
236+
IonicCli.run(processArguments)
237+
.then(function() {
238+
expect(IonicCli.loadGulpfile).toHaveBeenCalled();
239+
expect(IonicCli.runWithGulp).not.toHaveBeenCalled();
240+
expect(FakeTask.run).toHaveBeenCalledWith(IonicCli, argv, rawCliArguments);
178241
done();
179242
});
180243
});
181244

182245
it('should not change cwd to project root for non project tasks', function(done) {
246+
var processArguments = ['node', 'bin/ionic', 'fake'];
247+
var rawCliArguments = processArguments.slice(2);
248+
var argv = optimist(rawCliArguments).argv;
249+
183250
var FakeTask = {
184251
name: 'fake',
185252
title: 'fake',
@@ -189,10 +256,12 @@ describe('Cli', function() {
189256
isProjectTask: false
190257
};
191258
spyOn(IonicCli, 'getTaskSettingsByName').andReturn(FakeTask);
259+
spyOn(FakeTask, 'run').andReturn(Q(true));
192260

193-
IonicCli.run(['node', 'bin/ionic', 'fake'])
261+
IonicCli.run(processArguments)
194262
.then(function() {
195263
expect(Utils.cdIonicRoot).not.toHaveBeenCalled();
264+
expect(FakeTask.run).toHaveBeenCalledWith(IonicCli, argv, rawCliArguments);
196265
done();
197266
});
198267
});
@@ -267,7 +336,6 @@ describe('Cli', function() {
267336
IonicCli.run(['node', 'bin/ionic', 'fake'])
268337
.then(function() {
269338
expect(IonicCli.loadGulpfile).toHaveBeenCalled();
270-
expect(FakeTask.run).toHaveBeenCalled();
271339
expect(IonicCli.runWithGulp).not.toHaveBeenCalled();
272340
done();
273341
});
@@ -557,6 +625,7 @@ describe('Cli', function() {
557625
describe('runWithGulp function', function() {
558626
var fakeTask;
559627
var argv;
628+
var rawCliArguments;
560629
var qCallbacks;
561630

562631
beforeEach(function() {
@@ -572,6 +641,7 @@ describe('Cli', function() {
572641
_: ['fake'],
573642
v2: true
574643
};
644+
rawCliArguments = ['fake', '--v2'];
575645
gulp.tasks = {
576646
'fake:before': function() {},
577647
'fake:after': function() {}
@@ -598,7 +668,7 @@ describe('Cli', function() {
598668
it('should try to load gulp, exit if it fails', function() {
599669
spyOn(path, 'resolve').andReturn('./wrong_path');
600670

601-
IonicCli.runWithGulp(argv, fakeTask);
671+
IonicCli.runWithGulp(argv, fakeTask, rawCliArguments);
602672

603673
expect(log.error).toHaveBeenCalledWith('\nGulpfile detected, but gulp is not installed'.red);
604674
expect(log.error).toHaveBeenCalledWith('Do you need to run `npm install`?\n'.red);
@@ -611,12 +681,12 @@ describe('Cli', function() {
611681

612682

613683
it('should run logEvents, the command and the gulp hooks', function(done) {
614-
IonicCli.runWithGulp(argv, fakeTask).then(function() {
684+
IonicCli.runWithGulp(argv, fakeTask, rawCliArguments).then(function() {
615685

616686
expect(IonicCli.logEvents).toHaveBeenCalled();
617687
expect(gulp.start).toHaveBeenCalledWith('fake:before', qCallbacks[0]);
618688
expect(gulp.start).toHaveBeenCalledWith('fake:after', qCallbacks[1]);
619-
expect(fakeTask.run).toHaveBeenCalledWith(IonicCli, argv);
689+
expect(fakeTask.run).toHaveBeenCalledWith(IonicCli, argv, rawCliArguments);
620690

621691
expect(log.error).not.toHaveBeenCalled();
622692
expect(process.exit).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)