Skip to content

Commit

Permalink
No longer performs template substition on binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoblomj authored and SBoudrias committed Jan 15, 2018
1 parent a75c5e7 commit 4429bab
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
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

0 comments on commit 4429bab

Please sign in to comment.