Skip to content

Commit 03a574b

Browse files
authored
Merge pull request #14 from peterlenagh/feat/optional-hbs-extensions-in-templates
feat(templates): support optional .hbs extensions in templates.
2 parents beb2998 + 10dd722 commit 03a574b

29 files changed

+21
-7
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ And some template files like this:
8383
```
8484
|- index.js // This file contains static code, e.g. starting a webserver and including ./api/index.js
8585
|+ api/
86-
|- index.js // This is a static template, it contains placeholders that will be filled in, e.g. includes for each file in routes
86+
|- index.js.hbs // This is a static template, it contains placeholders that will be filled in, e.g. includes for each file in routes
8787
|+ routes/
88-
|- $$path$$.route.js // This file will be generated for each operation and contains skeleton code for each method for an operation.
88+
|- $$path$$.route.js.hbs // This file will be generated for each operation and contains skeleton code for each method for an operation.
8989
```
90-
The first important thing to notice here is the variable notation in `$$path$$.route.js`. It will be replaced by the name of the path.
90+
The first important thing to notice here is the variable notation in `$$path$$.route.js.hbs`. It will be replaced by the name of the path.
9191

9292
In this example the generated directory structure will be like this:
9393
```
@@ -101,6 +101,15 @@ In this example the generated directory structure will be like this:
101101
// (e.g. postUserLogin, getUserByUsername, putUserByUsername, deleteUserByUsername).
102102
```
103103

104+
### Template file extensions
105+
You can (optionally) name your template files with `.hbs` extensions, which will be removed when writing the generated
106+
file. e.g. `index.js.hbs` writes `index.js`. `index.js` would also write to `index.js`, if you prefer to omit the hbs
107+
extension.
108+
109+
The only case where the `.hbs` extension isn't optional would be if you are writing handlebars templates with the
110+
templates. In that case the the template would need the extension `.hbs.hbs`. `usertpl.hbs.hbs` writes `usertpl.hbs`
111+
(but `usertpl.hbs` as a source would write `usertpl` with no extension).
112+
104113
### Template file content
105114
The generator passes the OpenAPI spec to template files, so all information should be available there.
106115
In addition to that, the code generator adds a bit [more data](#data-passed-to-handlebars-templates) that can be helpful.

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)