Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No longer performs template substition on default, with option to override #97

Merged
merged 2 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Optionally, pass an `options.process` function (`process(contents)`) returning a

### `#copyTpl(from, to, context[, templateOptions [, copyOptions]])`

Copy the `from` file and parse its content as an [ejs](http://ejs.co/) template where `context` is the template context (the variable names available inside the template).
Copy the `from` file and, if it is not a binary file, parse its content as an [ejs](http://ejs.co/) template where `context` is the template context (the variable names available inside the template).

You can optionally pass a `templateOptions` object. `mem-fs-editor` automatically setup the filename option so you can easily use partials.

Expand Down
7 changes: 7 additions & 0 deletions __tests__/copy-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ describe('#copyTpl()', () => {
expect(fs.exists(path.join(newPath, 'file-tpl-partial.txt'))).toBeTruthy();
expect(fs.exists(path.join(newPath, 'file-tpl.txt'))).toBeFalsy();
});

it('perform no substitution on binary files', function () {
const filepath = path.join(__dirname, 'fixtures/file-binary.bin');
const newPath = '/new/path/file.bin';
fs.copyTpl(filepath, newPath);
expect(fs.read(newPath)).toBe(fs.read(filepath));
});
});
2 changes: 1 addition & 1 deletion __tests__/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('#copy()', () => {
it('copy files by globbing and process contents', () => {
const process = sinon.stub().returnsArg(0);
fs.copy(path.join(__dirname, '/fixtures/**'), '/output', {process});
sinon.assert.callCount(process, 7); // 5 total files under 'fixtures', not counting folders
sinon.assert.callCount(process, 8); // 7 total files under 'fixtures', not counting folders
expect(fs.read('/output/file-a.txt')).toBe('foo\n');
expect(fs.read('/output/nested/file.txt')).toBe('nested\n');
});
Expand Down
Binary file added __tests__/fixtures/file-binary.bin
Binary file not shown.
21 changes: 15 additions & 6 deletions lib/actions/copy-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@
var path = require('path');
var extend = require('deep-extend');
var ejs = require('ejs');
var isBinaryFile = require("isbinaryfile");

function render(contents, filename, context, tplSettings) {
if (isBinaryFile.sync(filename)) {
return contents.toString();
} else {
return ejs.render(
contents.toString(),
context,
// Setting filename by default allow including partials.
extend({filename: filename}, tplSettings)
);
}
}

module.exports = function (from, to, context, tplSettings, options) {
context = context || {};

this.copy(from, to, extend(options || {}, {
process: function (contents, filename) {
return ejs.render(
contents.toString(),
context,
// Setting filename by default allow including partials.
extend({filename: filename}, tplSettings || {})
);
return render(contents, filename, context, tplSettings || {});
}
}));
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ejs": "^2.3.1",
"glob": "^7.0.3",
"globby": "^6.1.0",
"isbinaryfile": "^3.0.2",
"mkdirp": "^0.5.0",
"multimatch": "^2.0.0",
"rimraf": "^2.2.8",
Expand Down