Skip to content

Commit

Permalink
Implement custom isBinary (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Apr 30, 2021
1 parent fec287c commit ad6ec47
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 47 deletions.
29 changes: 7 additions & 22 deletions lib/actions/copy-tpl-async.js
Expand Up @@ -2,20 +2,7 @@

var ejs = require('ejs');
var fs = require('fs').promises;
var {isBinaryFile} = require('isbinaryfile');
const {render} = require('../util');

async function renderFile(filename, context, tplSettings) {
let result;

if (await isBinaryFile(filename)) {
result = await fs.readFile(filename);
} else {
result = await ejs.renderFile(filename, context, tplSettings);
}

return result;
}
const {isBinary} = require('../util');

module.exports = async function (from, to, context, tplSettings, options) {
context = context || {};
Expand All @@ -28,15 +15,13 @@ module.exports = async function (from, to, context, tplSettings, options) {
processDestinationPath: path => path.replace(/.ejs$/, ''),
...options,
processFile: async function (filename) {
return renderFile(filename, context, tplSettings);
if (isBinary(filename, null)) {
return fs.readFile(filename);
}

return ejs.renderFile(filename, context, tplSettings);
},
process: function (contents, filename) {
return render(contents, context, {
// Setting filename by default allow including partials.
filename: filename,
...tplSettings
});
}
process: (contents, filename, destination) => this._processTpl({contents, filename, destination, context, tplSettings})
},
context,
tplSettings
Expand Down
29 changes: 20 additions & 9 deletions lib/actions/copy-tpl.js
@@ -1,8 +1,25 @@
'use strict';

const {render} = require('../util');
var ejs = require('ejs');
const {isBinary} = require('../util');

module.exports = function (from, to, context, tplSettings, options) {
module.exports._processTpl = function ({contents, filename, context, tplSettings}) {
if (isBinary(filename, contents)) {
return contents;
}

return ejs.render(
contents.toString(),
context,
{
// Setting filename by default allow including partials.
filename: filename,
...tplSettings
}
);
};

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

Expand All @@ -12,13 +29,7 @@ module.exports = function (from, to, context, tplSettings, options) {
{
processDestinationPath: path => path.replace(/.ejs$/, ''),
...options,
process: function (contents, filename) {
return render(contents, context, {
// Setting filename by default allow including partials.
filename: filename,
...tplSettings
});
}
process: (contents, filename) => this._processTpl({contents, filename, context, tplSettings})
},
context,
tplSettings
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -15,7 +15,8 @@ EditionInterface.prototype.appendTpl = require('./actions/append-tpl.js');
EditionInterface.prototype.delete = require('./actions/delete.js');
EditionInterface.prototype.copy = require('./actions/copy.js').copy;
EditionInterface.prototype._copySingle = require('./actions/copy.js')._copySingle;
EditionInterface.prototype.copyTpl = require('./actions/copy-tpl.js');
EditionInterface.prototype.copyTpl = require('./actions/copy-tpl.js').copyTpl;
EditionInterface.prototype._processTpl = require('./actions/copy-tpl.js')._processTpl;
EditionInterface.prototype.copyAsync = require('./actions/copy-async.js').copyAsync;
EditionInterface.prototype._copySingleAsync = require('./actions/copy-async.js')._copySingleAsync;
EditionInterface.prototype.copyTplAsync = require('./actions/copy-tpl-async.js');
Expand Down
29 changes: 15 additions & 14 deletions lib/util.js
Expand Up @@ -6,9 +6,11 @@ var commondir = require('commondir');
var globby = require('globby');
var normalize = require('normalize-path');
const {Transform} = require('stream');
var ejs = require('ejs');
var isBinaryFileSync = require('isbinaryfile').isBinaryFileSync;

const {default: textextensions} = require('textextensions');
const {default: binaryextensions} = require('binaryextensions');

function notNullOrExclusion(file) {
return file != null && file.charAt(0) !== '!';
}
Expand Down Expand Up @@ -74,19 +76,18 @@ exports.createTransform = function (transform) {
return stream;
};

exports.render = function (contents, context, tplSettings) {
let result;

const contentsBuffer = Buffer.from(contents, 'binary');
if (isBinaryFileSync(contentsBuffer, contentsBuffer.length)) {
result = contentsBuffer;
} else {
result = ejs.render(
contents.toString(),
context,
tplSettings
);
exports.isBinary = (filePath, newFileContents) => {
const extension = path.extname(filePath).replace(/^\./, '') || path.basename(filePath);
if (binaryextensions.includes(extension)) {
return true;
}

if (textextensions.includes(extension)) {
return false;
}

return result;
return (
(fs.existsSync(filePath) && isBinaryFileSync(filePath)) ||
(newFileContents && isBinaryFileSync(Buffer.isBuffer(newFileContents) ? newFileContents : Buffer.from(newFileContents)))
);
};
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -15,13 +15,15 @@
"lib"
],
"dependencies": {
"binaryextensions": "^4.15.0",
"commondir": "^1.0.1",
"deep-extend": "^0.6.0",
"ejs": "^3.1.6",
"globby": "^11.0.2",
"isbinaryfile": "^4.0.0",
"multimatch": "^5.0.0",
"normalize-path": "^3.0.0"
"normalize-path": "^3.0.0",
"textextensions": "^5.12.0"
},
"peerDependencies": {
"mem-fs": "^1.2.0 || ^2.0.0"
Expand Down

0 comments on commit ad6ec47

Please sign in to comment.