Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 327fd82

Browse files
committed
Templating: Do not run templating engine on json files. Use the .json.hbs extension for templating
1 parent a06446b commit 327fd82

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

src/util/localfs.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const _ = require('lodash');
22
const fs = require('fs');
33

4+
const Handlebars = require('handlebars');
45
const Logger = require('./logger.js');
56

67
const logger = new Logger('localfs');
7-
const Handlebars = require("handlebars");
8-
98

109
function LocalFS() {}
1110

@@ -29,26 +28,52 @@ LocalFS.prototype.checkExists = function(name, output, showOutput) {
2928
}
3029
return true;
3130
}
31+
32+
if (fs.existsSync(`${name}.hbs`)) {
33+
if (showOutput) {
34+
logger.showResult(`A template file exists for ${output}.`);
35+
}
36+
return true;
37+
}
38+
3239
if (showOutput) {
3340
logger.justShow(`${output} does not exists.`);
3441
}
42+
3543
return false;
3644
};
3745

3846
LocalFS.prototype.readFile = function(name, _showOnError) {
39-
let templateSrc = fs.readFileSync(name, 'utf8', function(error, data) {
47+
let useTemplating = false;
48+
49+
// if a file with a .hbs exist, use it for templating
50+
if (fs.existsSync(`${name}.hbs`)) {
51+
name = `${name}.hbs`;
52+
useTemplating = true;
53+
}
54+
55+
// Read the 'name' file (or the 'name.tml')
56+
const src = fs.readFileSync(name, 'utf8', (error, _data) => {
4057
if (!error) {
4158
logger.showResult(`Read file ${name} successfully.`);
4259
} else {
4360
logger.showError(`Error in reading file ${name}`);
4461
}
4562
});
46-
if(templateSrc === null) {
47-
return null
63+
64+
// If the file was not found (or cannot be read)
65+
if (src === null) {
66+
return null;
4867
}
4968

50-
var template = Handlebars.compile(templateSrc)
51-
return template(process.env)
69+
// if the file is not a template file
70+
if (!useTemplating) {
71+
return src;
72+
}
73+
logger.showResult(`Running templating engine on ${name}`);
74+
// If the file is a template file, let handlebars do its magic
75+
const template = Handlebars.compile(src);
76+
return template(process.env);
5277
};
5378

5479
LocalFS.prototype.writeFile = function(name, content) {
@@ -91,7 +116,7 @@ LocalFS.prototype.writeStream = function(name) {
91116
};
92117

93118
LocalFS.prototype.getFileName = function(fileNameWithExtension) {
94-
return fileNameWithExtension.replace(/\.[^/.]+$/, '');
119+
return fileNameWithExtension.replace(/\..+$/, '');
95120
};
96121

97122
module.exports = LocalFS;

0 commit comments

Comments
 (0)