Skip to content

Commit

Permalink
Change copyTpl to have the same function signature as copy so it …
Browse files Browse the repository at this point in the history
…can accept more options

Fixes #25
  • Loading branch information
blai committed Apr 10, 2015
1 parent cee7ef7 commit fb1bc87
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])`

Expand Down
17 changes: 11 additions & 6 deletions actions/copy-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
17 changes: 14 additions & 3 deletions test/copy-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
1 change: 1 addition & 0 deletions test/fixtures/.file-tpl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= name %>

0 comments on commit fb1bc87

Please # to comment.