Skip to content

Commit 9f09218

Browse files
committed
Merge branch 'release/1.3.0'
2 parents cdbf1b2 + 6a4ff7c commit 9f09218

File tree

6 files changed

+29
-4
lines changed

6 files changed

+29
-4
lines changed

cli.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ program
2020
.action((swaggerFilePath) => {
2121
swaggerFile = path.resolve(swaggerFilePath);
2222
})
23+
.option('-b, --handlebars <helperFilePath>', 'path to external handlebars helpers file (defaults to empty)', parseOutput)
2324
.option('-o, --output <outputDir>', 'directory where to put the generated files (defaults to current directory)', parseOutput, process.cwd())
2425
.option('-t, --templates <templateDir>', 'directory where templates are located (defaults to internal nodejs templates)')
2526
.parse(process.argv);
@@ -32,7 +33,8 @@ if (!swaggerFile) {
3233
codegen.generate({
3334
swagger: swaggerFile,
3435
target_dir: program.output,
35-
templates: program.templates ? path.resolve(process.cwd(), program.templates) : undefined
36+
templates: program.templates ? path.resolve(process.cwd(), program.templates) : undefined,
37+
handlebars_helper: program.handlebars ? path.resolve(process.cwd(), program.handlebars) : undefined
3638
}).then(() => {
3739
console.log(green('Done! ✨'));
3840
console.log(yellow('Check out your shiny new API at ') + magenta(program.output) + yellow('.'));

lib/codegen.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ const generateOperationFile = (config, operation, operation_name) => new Promise
7373
openbrace: '{',
7474
closebrace: '}' ,
7575
operation_name: operation_name.replace(/[}{]/g, ''),
76-
operation
76+
operation,
77+
swagger: config.data.swagger
7778
});
7879

7980
fs.writeFile(target_file, content, 'utf8', (err) => {
@@ -185,6 +186,10 @@ const generateDirectoryStructure = config => new Promise((resolve, reject) => {
185186
* @return {Promise}
186187
*/
187188
codegen.generate = config => new Promise((resolve, reject) => {
189+
if (config.handlebars_helper) {
190+
let handlebarsExt = require(config.handlebars_helper);
191+
handlebarsExt(Handlebars);
192+
}
188193
swagger2(config.swagger).then(swagger => {
189194
const random_name = randomName().dashed;
190195
config.swagger = swagger;

lib/helpers/extension.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function handlebarsExt(Handlebars) {
2+
3+
// Create a file in your project like this and put your handlebars extensions in here
4+
// include it with the -b directive
5+
//
6+
//
7+
// /**
8+
// * Function to output the word "bar"
9+
// */
10+
// Handlebars.registerHelper('foo', () => {
11+
// return "bar";
12+
// });
13+
}
14+
15+
module.exports = handlebarsExt;

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "swagger-node-codegen",
33
"main": "./lib/codegen.js",
4-
"version": "1.2.0",
4+
"version": "1.3.0",
55
"description": "An OpenAPI 3.x/Swagger 2 code generator for Node.js",
66
"bin": {
77
"snc": "./cli.js"

templates/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,6 @@ const user = require('./routes/user.route.js')
8888

8989
## Additional handlebars helpers
9090
All additional Handlebars helpers are located in [`lib/helpers/handlebars`](https://github.com/fmvilas/swagger-node-codegen/blob/master/lib/helpers/handlebars.js).
91+
92+
## Custom handlebars helpers
93+
Add your own handlebars helpers [`lib/helpers/handlebars/extension`](https://github.com/fmvilas/swagger-node-codegen/blob/master/lib/helpers/extension.js)

0 commit comments

Comments
 (0)