Skip to content

Commit ad393e9

Browse files
committed
feat(templates): support optional .hbs extensions on templates.
1 parent beb2998 commit ad393e9

28 files changed

+9
-4
lines changed

lib/generator.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ const generateFile = options => new Promise((resolve, reject) => {
4545
const template = Handlebars.compile(content);
4646
const parsed_content = template(data);
4747
const template_path = path.relative(templates_dir, path.resolve(root, file_name));
48-
const generated_path = path.resolve(target_dir, template_path);
49-
48+
const generated_path = path.resolve(target_dir, template_path).replace('.hbs', '');
5049
fs.writeFile(generated_path, parsed_content, 'utf8', (err) => {
5150
if (err) return reject(err);
5251
resolve();
@@ -69,7 +68,7 @@ const generateOperationFile = (config, operation, operation_name) => new Promise
6968
fs.readFile(path.join(config.root, config.file_name), 'utf8', (err, data) => {
7069
if (err) return reject(err);
7170
const subdir = config.root.replace(new RegExp(`${config.templates_dir}[/]?`),'');
72-
const new_filename = config.file_name.replace('$$path$$', operation_name);
71+
const new_filename = config.file_name.replace('$$path$$', operation_name).replace('.hbs', '');
7372
const target_file = path.resolve(config.target_dir, subdir, new_filename);
7473
const template = Handlebars.compile(data.toString());
7574
const content = template({

lib/register-partial.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ const getFileContent = filePath => {
88
};
99

1010
module.exports = async filePath => {
11-
const partialName = _.camelCase(path.basename(filePath, path.extname(filePath)));
11+
let extname = path.extname(filePath);
12+
// rm 2 extensions if the last is .hbs e.g. .md.hbs
13+
if (extname === ".hbs") {
14+
const filePathSansExt = filePath.replace(extname, "");
15+
extname = path.extname(filePathSansExt) + extname;
16+
}
17+
const partialName = _.camelCase(path.basename(filePath, extname));
1218
Handlebars.registerPartial(partialName, getFileContent(filePath));
1319
};

0 commit comments

Comments
 (0)