Skip to content

Commit

Permalink
Merge pull request #44 from shannonlal/v-0-4-Templating
Browse files Browse the repository at this point in the history
V 0 4 templating
  • Loading branch information
shannonlal authored Oct 7, 2017
2 parents e9dfdd6 + 5d48414 commit 6d2c26c
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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.4.0] - 2017-10-07
### Added
- Added lodash templating @shannonlal

## [0.3.0] - 2017-09-28
### Added
- Added unit testing @shannonlal
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,21 @@ Example List Selection:

```

Example Templating:
This was a fix to the issue #8 (https://github.com/Freyskeyd/gulp-prompt/issues/8)
[Note: see sample file]( examples/template-replacement-gulpfile.js)
```javascript

return gulp.src( './package.json' )
.pipe( prompt.confirm({
type:'input',
name:'env',
message:'Hello <%= user %>, please enter selection?',
templateOptions:{ 'user': 'fred' }
}, (res) => {
console.log('Result', res);
}) );


```

25 changes: 25 additions & 0 deletions examples/template-replacement-gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

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

/**
* The following is a sample gulp file for using replacement from the command prompt
* Note: This replacement function is only available on the confirm operation.
* This is uses lodash template module and does a basic replacement of one or more
* variables
*
* https://www.npmjs.com/package/lodash.template
*/
gulp.task( 'getConfirmWithReplacement', () => {
return gulp.src( './package.json' )
.pipe( prompt.confirm({
type:'input',
name:'env',
message:'Hello <%= user %>, please enter selection?',
templateOptions:{ 'user': 'fred' }
}, (res) => {
console.log('Result', res);
}) );
});

8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var inq = require('inquirer'),
es = require('event-stream');
es = require('event-stream'),
template = require("lodash.template");

module.exports = {

Expand Down Expand Up @@ -53,6 +54,11 @@ module.exports = {
options = {};
}

if( typeof options.templateOptions !== 'undefined'){
var compiled = template( options.message );
options.message = compiled( options.templateOptions);
}

opts.message = options.message || opts.message;
opts.default = options.default || opts.default;

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-prompt",
"version": "0.3.2",
"version": "0.4.0",
"description": "Add interactive console prompts to gulp",
"main": "index.js",
"scripts": {
Expand All @@ -16,7 +16,8 @@
"license": "MIT",
"dependencies": {
"event-stream": "~3.0.20",
"inquirer": "^3.3.0"
"inquirer": "^3.3.0",
"lodash.template":"^4.4.0"
},
"devDependencies": {
"assert": "^1.4.1",
Expand Down
82 changes: 82 additions & 0 deletions test/confirm-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,88 @@ describe('gulp confirm function unit tests', function () {
let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
resp.write('../test.txt');
});

it('verify that lodash templates replacement works', function ( done ){
var prompt = function ( listOptions ){
return new Promise( (resolve,reject) => {
resolve('Test Completed');
if( Array.isArray( listOptions) && typeof listOptions[0] !== 'string' ){
assert.equal( listOptions[0].message, 'hello fred! what is your choice?' );
done();
}else{
console.log( 'Failed test');
done('options defaults not set');
}
});
};

//Mock inquirer to capture response
let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
let srcStream = source('../README.md');
let options = {
type: 'confirm',
name: 'val',
message: 'hello <%= user %>! what is your choice?',
templateOptions:{ 'user': 'fred' }
};
let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
console.log('Executing test');
resp.write('../test.txt');
});

it('verify that lodash templates is only called when template options are defined', function ( done ){
var prompt = function ( listOptions ){
return new Promise( (resolve,reject) => {
resolve('Test Completed');
if( Array.isArray( listOptions) && typeof listOptions[0] !== 'string' ){
assert.equal( listOptions[0].message, 'hello <%= user %>! what is your choice?' );
done();
}else{
console.log( 'Failed test');
done('options defaults not set');
}
});
};

//Mock inquirer to capture response
let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
let srcStream = source('../README.md');
let options = {
type: 'confirm',
name: 'val',
message: 'hello <%= user %>! what is your choice?'
};
let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
resp.write('../test.txt');
});

it('verify that lodash templates replacement works for two fields', function ( done ){
var prompt = function ( listOptions ){
return new Promise( (resolve,reject) => {
resolve('Test Completed');
if( Array.isArray( listOptions) && typeof listOptions[0] !== 'string' ){
assert.equal( listOptions[0].message, 'hello fred! what is today your choice?' );
done();
}else{
console.log( 'Failed test');
done('options defaults not set');
}
});
};

//Mock inquirer to capture response
let gulpPrompt = proxyrequire('../index.js', {'inquirer':{ prompt: prompt}});
let srcStream = source('../README.md');
let options = {
type: 'confirm',
name: 'val',
message: 'hello <%= user %>! what is <%= date %> your choice?',
templateOptions:{ 'user': 'fred', 'date':'today' }
};
let resp = srcStream.pipe( gulpPrompt.confirm( options ) );
console.log('Executing test');
resp.write('../test.txt');
});
});
});

Expand Down

0 comments on commit 6d2c26c

Please sign in to comment.