Skip to content

Commit 3c41087

Browse files
authored
Merge pull request #21 from peterlenagh/feat/dynamic-template-paths
feat(templates): dynamic template paths
2 parents d3392ac + 54705a8 commit 3c41087

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,14 @@ And some template files like this:
8686
|- 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/
8888
|- $$path$$.route.js.hbs // This file will be generated for each operation and contains skeleton code for each method for an operation.
89+
|+ $$path$$/ // This folder will also be generated for each operation.
90+
|- route.js.hbs // This is another example of an operation file.
8991
```
9092
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.
9193

94+
This example also shows `$$path$$` used in a folder name - the generated folder names here will replace $$path$$ with
95+
the name of the path (in kebab-case).
96+
9297
In this example the generated directory structure will be like this:
9398
```
9499
|- index.js // This file still contains static code like before.
@@ -98,7 +103,11 @@ In this example the generated directory structure will be like this:
98103
|- pet.route.js // This file contains the code for methods on pets.
99104
| // (e.g. getPet, postPet, getPetByPetId).
100105
|- user.route.js // This file will contain the code for methods on users.
101-
// (e.g. postUserLogin, getUserByUsername, putUserByUsername, deleteUserByUsername).
106+
| // (e.g. postUserLogin, getUserByUsername, putUserByUsername, deleteUserByUsername).
107+
|+ pet/
108+
| - route.js // this file also contains the code for methods on pets.
109+
|+ user/
110+
| - route.js // this file also contains the code for methods on users.
102111
```
103112

104113
### Template file extensions

lib/generator.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ const generateFile = options => new Promise((resolve, reject) => {
9696
const generateOperationFile = (config, operation, operation_name) => new Promise((resolve, reject) => {
9797
fs.readFile(path.join(config.root, config.file_name), 'utf8', (err, data) => {
9898
if (err) return reject(err);
99-
const subdir = config.root.replace(new RegExp(`${config.templates_dir}[/]?`),'');
99+
const subdir = config.root
100+
.replace(new RegExp(`${config.templates_dir}[/]?`),'')
101+
.replace("$$path$$", _.kebabCase(operation_name));
102+
100103
const new_filename = config.file_name.replace('$$path$$', operation_name).replace(/.hbs$/, '');
101104
const target_file = path.resolve(config.target_dir, subdir, new_filename);
102105
const template = Handlebars.compile(data.toString());
@@ -108,6 +111,7 @@ const generateOperationFile = (config, operation, operation_name) => new Promise
108111
openapi: config.data.openapi
109112
});
110113

114+
xfs.mkdirpSync(path.dirname(target_file));
111115
fs.writeFile(target_file, content, 'utf8', (err) => {
112116
if (err) return reject(err);
113117
resolve();
@@ -166,7 +170,7 @@ const generateDirectoryStructure = config => new Promise((resolve, reject) => {
166170

167171
walker.on('file', async (root, stats, next) => {
168172
try {
169-
if (stats.name.includes('$$path$$')) {
173+
if (stats.name.includes('$$path$$') || root.includes("$$path$$")) {
170174
// this file should be handled for each in openapi.paths
171175
await generateOperationFiles({
172176
root,
@@ -199,7 +203,13 @@ const generateDirectoryStructure = config => new Promise((resolve, reject) => {
199203
walker.on('directory', async (root, stats, next) => {
200204
try {
201205
const dir_path = path.resolve(target_dir, path.relative(templates_dir, path.resolve(root, stats.name)));
202-
if (stats.name !== PARTIALS_DIRNAME && stats.name !== HELPERS_DIRNAME) xfs.mkdirpSync(dir_path);
206+
if (
207+
stats.name !== PARTIALS_DIRNAME &&
208+
stats.name !== HELPERS_DIRNAME &&
209+
!stats.name.includes("$$path$$")
210+
) {
211+
xfs.mkdirpSync(dir_path);
212+
}
203213
next();
204214
} catch (e) {
205215
reject(e);

0 commit comments

Comments
 (0)