copy-tpl.js 898 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var extend = require('deep-extend');
  3. var ejs = require('ejs');
  4. var isBinaryFile = require('isbinaryfile');
  5. function render(contents, filename, context, tplSettings) {
  6. let result;
  7. const contentsBuffer = Buffer.from(contents, 'binary');
  8. if (isBinaryFile.sync(contentsBuffer, contentsBuffer.length)) {
  9. result = contentsBuffer;
  10. } else {
  11. result = ejs.render(
  12. contents.toString(),
  13. context,
  14. // Setting filename by default allow including partials.
  15. extend({filename: filename}, tplSettings)
  16. );
  17. }
  18. return result;
  19. }
  20. module.exports = function (from, to, context, tplSettings, options) {
  21. context = context || {};
  22. tplSettings = tplSettings || {}
  23. this.copy(from, to, extend(options || {}, {
  24. process: function (contents, filename) {
  25. return render(contents, filename, context, tplSettings);
  26. }
  27. }),
  28. context, tplSettings);
  29. };