From a06446bf8b69bacf695639038fdc89bc3140dd05 Mon Sep 17 00:00:00 2001 From: Jonathan Hurter Date: Tue, 6 Aug 2019 22:33:09 +0200 Subject: [PATCH 1/3] Add support for templating --- package.json | 1 + src/util/localfs.js | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1edbc70..eb094d1 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "commander": "2.11.0", "fs": "0.0.1-security", "gifencoder": "^1.1.0", + "handlebars": "^4.1.2", "lodash": "^4.17.15", "nconf": "^0.8.5", "png-file-stream": "^1.2.1", diff --git a/src/util/localfs.js b/src/util/localfs.js index b8b9357..4166d82 100644 --- a/src/util/localfs.js +++ b/src/util/localfs.js @@ -4,6 +4,8 @@ const fs = require('fs'); const Logger = require('./logger.js'); const logger = new Logger('localfs'); +const Handlebars = require("handlebars"); + function LocalFS() {} @@ -34,13 +36,19 @@ LocalFS.prototype.checkExists = function(name, output, showOutput) { }; LocalFS.prototype.readFile = function(name, _showOnError) { - return fs.readFileSync(name, 'utf8', (error, _data) => { + let templateSrc = fs.readFileSync(name, 'utf8', function(error, data) { if (!error) { logger.showResult(`Read file ${name} successfully.`); } else { logger.showError(`Error in reading file ${name}`); } }); + if(templateSrc === null) { + return null + } + + var template = Handlebars.compile(templateSrc) + return template(process.env) }; LocalFS.prototype.writeFile = function(name, content) { From 327fd82735713d55c60bc30523ada37a6c4569e7 Mon Sep 17 00:00:00 2001 From: Jonathan Hurter Date: Wed, 9 Oct 2019 21:46:08 +0200 Subject: [PATCH 2/3] Templating: Do not run templating engine on json files. Use the .json.hbs extension for templating --- src/util/localfs.js | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/util/localfs.js b/src/util/localfs.js index 4166d82..cb5cfce 100644 --- a/src/util/localfs.js +++ b/src/util/localfs.js @@ -1,11 +1,10 @@ const _ = require('lodash'); const fs = require('fs'); +const Handlebars = require('handlebars'); const Logger = require('./logger.js'); const logger = new Logger('localfs'); -const Handlebars = require("handlebars"); - function LocalFS() {} @@ -29,26 +28,52 @@ LocalFS.prototype.checkExists = function(name, output, showOutput) { } return true; } + + if (fs.existsSync(`${name}.hbs`)) { + if (showOutput) { + logger.showResult(`A template file exists for ${output}.`); + } + return true; + } + if (showOutput) { logger.justShow(`${output} does not exists.`); } + return false; }; LocalFS.prototype.readFile = function(name, _showOnError) { - let templateSrc = fs.readFileSync(name, 'utf8', function(error, data) { + let useTemplating = false; + + // if a file with a .hbs exist, use it for templating + if (fs.existsSync(`${name}.hbs`)) { + name = `${name}.hbs`; + useTemplating = true; + } + + // Read the 'name' file (or the 'name.tml') + const src = fs.readFileSync(name, 'utf8', (error, _data) => { if (!error) { logger.showResult(`Read file ${name} successfully.`); } else { logger.showError(`Error in reading file ${name}`); } }); - if(templateSrc === null) { - return null + + // If the file was not found (or cannot be read) + if (src === null) { + return null; } - var template = Handlebars.compile(templateSrc) - return template(process.env) + // if the file is not a template file + if (!useTemplating) { + return src; + } + logger.showResult(`Running templating engine on ${name}`); + // If the file is a template file, let handlebars do its magic + const template = Handlebars.compile(src); + return template(process.env); }; LocalFS.prototype.writeFile = function(name, content) { @@ -91,7 +116,7 @@ LocalFS.prototype.writeStream = function(name) { }; LocalFS.prototype.getFileName = function(fileNameWithExtension) { - return fileNameWithExtension.replace(/\.[^/.]+$/, ''); + return fileNameWithExtension.replace(/\..+$/, ''); }; module.exports = LocalFS; From b51e103903c21913e2b90a491d768ecee726eb9c Mon Sep 17 00:00:00 2001 From: Jonathan Hurter Date: Wed, 9 Oct 2019 21:54:41 +0200 Subject: [PATCH 3/3] Templating: Warn the user when a template file exist --- src/util/localfs.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/util/localfs.js b/src/util/localfs.js index cb5cfce..03c3855 100644 --- a/src/util/localfs.js +++ b/src/util/localfs.js @@ -77,6 +77,9 @@ LocalFS.prototype.readFile = function(name, _showOnError) { }; LocalFS.prototype.writeFile = function(name, content) { + if (fs.existsSync(`${name}.hbs`)) { + logger.showError(`The ${name} file will be ignored since ${name}.hbs exists`); + } fs.writeFileSync(name, content); };