Skip to content

Commit 54705a8

Browse files
committed
feat(templates): support 7873path7873 variable in template directory names for dynamic directories.
1 parent 37231cf commit 54705a8

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
@@ -67,7 +67,10 @@ const generateFile = options => new Promise((resolve, reject) => {
6767
const generateOperationFile = (config, operation, operation_name) => new Promise((resolve, reject) => {
6868
fs.readFile(path.join(config.root, config.file_name), 'utf8', (err, data) => {
6969
if (err) return reject(err);
70-
const subdir = config.root.replace(new RegExp(`${config.templates_dir}[/]?`),'');
70+
const subdir = config.root
71+
.replace(new RegExp(`${config.templates_dir}[/]?`),'')
72+
.replace("$$path$$", _.kebabCase(operation_name));
73+
7174
const new_filename = config.file_name.replace('$$path$$', operation_name).replace(/.hbs$/, '');
7275
const target_file = path.resolve(config.target_dir, subdir, new_filename);
7376
const template = Handlebars.compile(data.toString());
@@ -79,6 +82,7 @@ const generateOperationFile = (config, operation, operation_name) => new Promise
7982
openapi: config.data.openapi
8083
});
8184

85+
xfs.mkdirpSync(path.dirname(target_file));
8286
fs.writeFile(target_file, content, 'utf8', (err) => {
8387
if (err) return reject(err);
8488
resolve();
@@ -137,7 +141,7 @@ const generateDirectoryStructure = config => new Promise((resolve, reject) => {
137141

138142
walker.on('file', async (root, stats, next) => {
139143
try {
140-
if (stats.name.includes('$$path$$')) {
144+
if (stats.name.includes('$$path$$') || root.includes("$$path$$")) {
141145
// this file should be handled for each in openapi.paths
142146
await generateOperationFiles({
143147
root,
@@ -170,7 +174,13 @@ const generateDirectoryStructure = config => new Promise((resolve, reject) => {
170174
walker.on('directory', async (root, stats, next) => {
171175
try {
172176
const dir_path = path.resolve(target_dir, path.relative(templates_dir, path.resolve(root, stats.name)));
173-
if (stats.name !== PARTIALS_DIRNAME && stats.name !== HELPERS_DIRNAME) xfs.mkdirpSync(dir_path);
177+
if (
178+
stats.name !== PARTIALS_DIRNAME &&
179+
stats.name !== HELPERS_DIRNAME &&
180+
!stats.name.includes("$$path$$")
181+
) {
182+
xfs.mkdirpSync(dir_path);
183+
}
174184
next();
175185
} catch (e) {
176186
reject(e);

0 commit comments

Comments
 (0)