Skip to content

Commit 3c64dd5

Browse files
committed
fix(instance): don't prompt for templates if prompts are disabled
no issue - fix usage of --no-prompt with instance#template method
1 parent 1f5b631 commit 3c64dd5

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/instance.js

+5
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ class Instance {
207207
* @public
208208
*/
209209
template(contents, descriptor, file, dir) {
210+
// If `--no-prompt` is passed to the CLI, just generate the template
211+
if (!this.ui.allowPrompt) {
212+
return this._generateTemplate(contents, file, dir);
213+
}
214+
210215
return this.ui.prompt({
211216
type: 'expand',
212217
name: 'choice',

test/unit/instance-spec.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ describe('Unit: Instance', function () {
367367
describe('template', function () {
368368
it('resolves with false if the choice is to skip', function () {
369369
let promptStub = sandbox.stub().resolves({ choice: 'skip' });
370-
let testInstance = new Instance({ prompt: promptStub }, {}, '');
370+
let testInstance = new Instance({ prompt: promptStub, allowPrompt: true }, {}, '');
371371

372372
return testInstance.template('some contents', 'a file', 'file.txt', '/some/dir').then((result) => {
373373
expect(promptStub.calledOnce).to.be.true;
@@ -377,7 +377,7 @@ describe('Unit: Instance', function () {
377377

378378
it('generates template if the choice is to proceeed', function () {
379379
let promptStub = sandbox.stub().resolves({choice: 'write'});
380-
let testInstance = new Instance({ prompt: promptStub }, {} , '');
380+
let testInstance = new Instance({ prompt: promptStub, allowPrompt: true }, {} , '');
381381
let generateStub = sandbox.stub(testInstance, '_generateTemplate').resolves(true);
382382

383383
return testInstance.template('some contents', 'a file', 'file.txt', '/some/dir').then((result) => {
@@ -393,7 +393,7 @@ describe('Unit: Instance', function () {
393393
promptStub.onCall(0).resolves({choice: 'view'});
394394
promptStub.onCall(1).resolves({choice: 'skip'});
395395
let logStub = sandbox.stub();
396-
let testInstance = new Instance({ log: logStub, prompt: promptStub }, {}, '');
396+
let testInstance = new Instance({ log: logStub, prompt: promptStub, allowPrompt: true }, {}, '');
397397

398398
return testInstance.template('some contents', 'a file', 'file.txt', '/some/dir').then((result) => {
399399
expect(result).to.be.false;
@@ -407,7 +407,7 @@ describe('Unit: Instance', function () {
407407
let promptStub = sandbox.stub();
408408
promptStub.onCall(0).resolves({choice: 'edit'});
409409
promptStub.onCall(1).resolves({contents: 'some edited contents'});
410-
let testInstance = new Instance({ prompt: promptStub }, {}, '');
410+
let testInstance = new Instance({ prompt: promptStub, allowPrompt: true }, {}, '');
411411
let generateStub = sandbox.stub(testInstance, '_generateTemplate').resolves(true);
412412

413413
return testInstance.template('some contents', 'a file', 'file.txt', '/some/dir').then((result) => {
@@ -417,6 +417,22 @@ describe('Unit: Instance', function () {
417417
expect(generateStub.args[0][0]).to.equal('some edited contents');
418418
});
419419
});
420+
421+
it('immediately calls _generateTemplate if ui.allowPrompt is false', function () {
422+
let promptStub = sandbox.stub().resolves();
423+
let testInstance = new Instance({
424+
prompt: promptStub,
425+
allowPrompt: false
426+
}, {}, '');
427+
let generateStub = sandbox.stub(testInstance, '_generateTemplate').resolves(true);
428+
429+
return testInstance.template('some contents', 'a file', 'file.txt', '/some/dir').then((result) => {
430+
expect(result).to.be.true;
431+
expect(promptStub.called).to.be.false;
432+
expect(generateStub.calledOnce).to.be.true;
433+
expect(generateStub.args[0][0]).to.equal('some contents');
434+
});
435+
});
420436
});
421437

422438
describe('_generateTemplate', function () {

0 commit comments

Comments
 (0)