Skip to content

Commit

Permalink
Merge pull request #48 from shannonlal/v-0-5-ChainPrompts
Browse files Browse the repository at this point in the history
V 0 5 chain prompts
  • Loading branch information
shannonlal authored Nov 12, 2017
2 parents 4ff3e5b + f65a02d commit a856e14
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
test.js
gulpfile.js
npm-debug.log
package-lock.json
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Changelog
All notable changes to this project will be documented in this file.


The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.5.0] - 2017-11-12
### Added
- Added chaining support to confirm function @shannonlal

## [0.4.1] - 2017-10-10
### Added
- Added sample test gulp file for issue #27 @shannonlal
Expand Down
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Gulp prompt

# THIS REPOSITORY IS LOOKING FOR ADDITIONAL MAINTAINER(S)

If you are interested in getting involved please send us an e-mail or open an issue.
There are a couple of open issues and small clean up projects that we could use some help with.

Expand Down Expand Up @@ -167,7 +165,37 @@ This was a fix to the issue #8 (https://github.com/Freyskeyd/gulp-prompt/issues/
}, (res) => {
console.log('Result', res);
}) );
```

Example Chaining Prompts:
This was a fix to the issue #35 (https://github.com/Freyskeyd/gulp-prompt/issues/35)
This was a fix to the issue #34 (https://github.com/Freyskeyd/gulp-prompt/issues/34)
[Note: see sample file]( examples/chain-confirm-gulpfile.js)
```javascript

var index =0;

var chainFunction = function ( options, resp ){
console.log( 'Here is the selection ', resp);
if( index <= 3){
options.message = `Hello this is iteration ${index}`;
index++;
return options;
}else{
return;
}
};

gulp.task( 'chainConfirm', () => {
return gulp.src( '../package.json' )
.pipe( prompt.confirm({
type:'input',
name:'env',
message:'Hello First interation, please enter selection?',
chainFunction:chainFunction
}, (res) => {
console.log('Result', res);
}) );
});
```

24 changes: 24 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Examples

The following is a selection of example gulp files which can be used for testing
purposes

## Execute Gulp File
`node_modules/.bin/gulp --gulpfile examples/{sample gulp file} {gulp task}`

Example:
`node_modules/.bin/gulp --gulpfile examples/list-selection-gulpfile.js getSelection`

## Gulp Examples

1. First Choice Selection
`examples/first-choice-selection-gulpfile.js`

2. List selection which expands the screen
`examples/list-selection-gulpfile.js`

3. Templating option for messages
`examples/template-replacement-gulpfile.js`

4. Chain a series of prompts together
`examples/chain-confirm-gulpfile.js`
36 changes: 36 additions & 0 deletions examples/chain-confirm-gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var gulp = require( 'gulp' );
var prompt = require('../index');
var index =0;

var chainFunction = function ( options, resp ){
console.log( 'Here is the selection ', resp);
if( index <= 3){
options.message = `Hello this is iteration ${index}`;
index++;
return options;
}else{
return;
}
};

/**
* The following is a sample gulp file for chaining requests. It
* will pass a chaining function to the confirm function and will
* allow the user to chain multiple requests together
* variables
*
*/
gulp.task( 'chainConfirm', () => {
return gulp.src( '../package.json' )
.pipe( prompt.confirm({
type:'input',
name:'env',
message:'Hello First interation, please enter selection?',
chainFunction:chainFunction
}, (res) => {
console.log('Result', res);
}) );
});

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var prompt = require('../index');
* (https://github.com/Freyskeyd/gulp-prompt/issues/27)
*/
gulp.task( 'selectFirst', () => {
return gulp.src( './package.json' )
return gulp.src( '../package.json' )
.pipe( prompt.prompt({
type:'list',
name:'env',
Expand All @@ -18,4 +18,4 @@ gulp.task( 'selectFirst', () => {
}, (res) => {
console.log('Result', res);
}) );
});
});
2 changes: 1 addition & 1 deletion examples/list-selection-gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var prompt = require('../index');
* https://github.com/SBoudrias/Inquirer.js#questions
*/
gulp.task( 'getSelection', () => {
return gulp.src( './package.json' )
return gulp.src( '../package.json' )
.pipe( prompt.prompt({
type:'list',
name:'env',
Expand Down
2 changes: 1 addition & 1 deletion examples/template-replacement-gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var prompt = require('../index');
* https://www.npmjs.com/package/lodash.template
*/
gulp.task( 'getConfirmWithReplacement', () => {
return gulp.src( './package.json' )
return gulp.src( '../package.json' )
.pipe( prompt.confirm({
type:'input',
name:'env',
Expand Down
58 changes: 49 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ var inq = require('inquirer'),

module.exports = {

/**
* The following method will act as a proxy to the inquirer
* prompt function. It will pass the questions object directly to
* inqurier's prompt function
* @param {*} questions
* @param {*} callback
*/
prompt: function(questions, callback) {
var prompted = false;
return es.map(function(file, cb) {
Expand Down Expand Up @@ -32,13 +39,36 @@ module.exports = {

confirm: function(options) {
var prompted = false;
let chainFunction;
return es.map(function(file, cb) {

if (prompted === true) {
cb(null,file);
return;
}

/**
* The following chainHandler was designed to be called recursively so as to allow
* users the ability to chain multple calls to inquirer together. It will stop
* calls to the chain handler when the chain function returns undefined.
* @param {objects} options
*/
var chainHandler = function( options ){

return new Promise( (resolve,reject)=>{
inq.prompt([options]).then( resp =>{
let opts = chainFunction( options, resp );
if( typeof opts === 'undefined'){
return resolve('response');
}else{
chainHandler( opts );
}
}).catch( err =>{
reject( 'Unexpected Error');
});
});
}

var opts = {
type: 'confirm',
name: 'val',
Expand All @@ -62,17 +92,27 @@ module.exports = {
opts.message = options.message || opts.message;
opts.default = options.default || opts.default;

inq.prompt([opts]).then(function(res) {

if (res.val) {
if( typeof options.chainFunction === 'undefined'){
inq.prompt([opts]).then(function(res) {

if (res.val) {
cb(null, file);
}
});
prompted = true;
}else{
chainFunction = options.chainFunction;
return chainHandler( opts ).then(function(res) {
if (res.val) {
cb(null, file);
}
}).catch( err => {
cb(null, file);
}

});

prompted = true;
});

prompted = true;
}
});
},

inq: inq
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-prompt",
"version": "0.4.1",
"version": "0.5.0",
"description": "Add interactive console prompts to gulp",
"main": "index.js",
"scripts": {
Expand All @@ -17,7 +17,7 @@
"dependencies": {
"event-stream": "~3.0.20",
"inquirer": "^3.3.0",
"lodash.template":"^4.4.0"
"lodash.template": "^4.4.0"
},
"devDependencies": {
"assert": "^1.4.1",
Expand Down
59 changes: 59 additions & 0 deletions test/chain-prompt-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var assert = require('assert');
//Gulp uses vinyl source streams
var source = require('vinyl-source-stream');

var proxyrequire = require('proxyquire');
/**
* The following test spec will verify the operation of the chain prompt function
*/
describe('gulp chain prompt unit tests', function () {
describe('verify that chain prompt function operates correctly', function () {


it('verify that chain prompt does not convert questions to array if already an array', function ( ){
var prompt = function ( questions ){
return new Promise( (resolve,reject) => {
resolve('completed prompt');
});
};

//Mock inquirer to capture response
gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
let srcStream = source('../README.md');
let resp = srcStream.pipe( gulpPrompt.confirm( ['options string'] ) );
resp.write('../test.txt');
});

it('verify that chain function gets called correctly', function ( ){
var prompt = function ( questions ){
return new Promise( (resolve,reject) => {
resolve('completed prompt');
});
};

var chainFunction = function ( opts, resp ){
console.log('Chain Function called');
console.log( 'Response', resp);
assert.equal( opts, options );
};
let index = 0;
let options = {
type: 'confirm',
name: 'val',
message: 'Test Message?',
default: true,
chainFunction: chainFunction
};


//Mock inquirer to capture response
gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
let srcStream = source('../README.md');

let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
resp.write('../test.txt');
});
});
});


0 comments on commit a856e14

Please sign in to comment.