From fb1bc872a30b4b17f5eca8fcc60a7fc5174d6481 Mon Sep 17 00:00:00 2001 From: Brian Lai Date: Fri, 10 Apr 2015 15:29:50 -0700 Subject: [PATCH] Change `copyTpl` to have the same function signature as `copy` so it can accept more options Fixes #25 --- README.md | 8 ++++++-- actions/copy-tpl.js | 17 +++++++++++------ test/copy-tpl.js | 17 ++++++++++++++--- test/fixtures/.file-tpl.txt | 1 + 4 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/.file-tpl.txt diff --git a/README.md b/README.md index de5aed7..4b9e28d 100644 --- a/README.md +++ b/README.md @@ -58,11 +58,15 @@ Optionally, pass an `options.process` function (`process(contents)`) returning a `from` can be a glob pattern that'll be match against the file system. If that's the case, then `to` must be an output directory. For a globified `from`, you can optionally pass in an `options.globOptions` object to change its pattern matching behavior. The full list of options are being described [here](https://github.com/isaacs/node-glob#options). The `nodir` flag is forced to be `true` in `globOptions` to ensure a vinyl object representing each matching directory is marked as `deleted` in the `mem-fs` store. -### `#copyTpl(from, to, context, [settings])` +### `#copyTpl(from, to, [options])` Copy the `from` file and parse its content as an underscore template where `context` is the template context. -Optionally pass a template `settings` object. +`options.tplSettings` (optional) object as the options object when calling `_.template`, [more details here](https://lodash.com/docs#template). + +`options.tplContext` (optional) can be use to pass in the context object for invoking the compiled template function. + +`options.globOptions` (optional) would be used to control globbing behavior when `from` is a glob pattern. ### `#move(from, to, [options])` diff --git a/actions/copy-tpl.js b/actions/copy-tpl.js index 4dcb1fd..61b0be5 100644 --- a/actions/copy-tpl.js +++ b/actions/copy-tpl.js @@ -3,12 +3,17 @@ var path = require('path'); var _ = require('lodash'); -module.exports = function (from, to, context, tplSettings) { - context = context || {}; - tplSettings = tplSettings || {}; - this.copy(from, to, { +module.exports = function (from, to, options) { + options = options || {}; + + var tplContext = options.tplContext || {}; + var tplSettings = options.tplSettings || {}; + + var copyOption = _(options).omit('tplContext', 'tplSettings').assign({ process: function (contents) { - return _.template(contents.toString(), tplSettings)(context); + return _.template(contents.toString(), tplSettings)(tplContext); } - }); + }).value(); + + this.copy(from, to, copyOption); }; diff --git a/test/copy-tpl.js b/test/copy-tpl.js index 8091edc..76ae6c5 100644 --- a/test/copy-tpl.js +++ b/test/copy-tpl.js @@ -14,16 +14,27 @@ describe('#copyTpl()', function () { it('copy file and process contents as underscore template', function () { var filepath = path.join(__dirname, 'fixtures/file-tpl.txt'); var newPath = '/new/path/file.txt'; - this.fs.copyTpl(filepath, newPath, { name: 'new content' }); + this.fs.copyTpl(filepath, newPath, { tplContext: { name: 'new content' } }); assert.equal(this.fs.read(newPath), 'new content\n'); }); it('copy file with template settings', function() { var filepath = path.join(__dirname, 'fixtures/file-tpl-mustache.txt'); var newPath = '/new/path/file.txt'; - this.fs.copyTpl(filepath, newPath, { name: 'mustache' }, { - interpolate: /{{([\s\S]+?)}}/g + this.fs.copyTpl(filepath, newPath, { + tplContext: { name: 'mustache' }, + tplSettings: { interpolate: /{{([\s\S]+?)}}/g } }); assert.equal(this.fs.read(newPath), 'mustache\n'); }); + + it('copy/process template with globOptions', function () { + var filepath = path.join(__dirname, 'fixtures/.file-tpl.txt'); + var newPath = '/new/path/file.txt'; + this.fs.copyTpl(filepath, newPath, { + tplContext: { name: 'new content' }, + globOptions: { dot: true } + }); + assert.equal(this.fs.read(newPath), 'new content\n'); + }); }); diff --git a/test/fixtures/.file-tpl.txt b/test/fixtures/.file-tpl.txt new file mode 100644 index 0000000..8229b16 --- /dev/null +++ b/test/fixtures/.file-tpl.txt @@ -0,0 +1 @@ +<%= name %>