Created
January 16, 2021 16:22
-
-
Save iksena/b05f11eef1ba87e770fe42513e171b2f to your computer and use it in GitHub Desktop.
yo microservice:route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Generator = require('yeoman-generator'); | |
class RouteGenerator extends Generator { | |
async prompting() { | |
this.log('--- Generate a route ---'); | |
this.answers = await this.prompt([ | |
{ | |
name: 'routeName', | |
type: 'input', | |
default: 'healthcheck', | |
message: 'What is the name of the route?', | |
}, | |
{ | |
name: 'routeMethod', | |
type: 'list', | |
choices: ['get', 'post', 'delete', 'patch', 'put'], | |
default: 'get', | |
message: 'What is the method of the route?', | |
}, | |
]); | |
} | |
configuring() { | |
const { routeMethod, routeName } = this.answers; | |
const routeTitle = `${routeMethod.toUpperCase()} /${routeName}`; | |
this.answers = { | |
...this.answers, | |
routeTitle, | |
}; | |
this.config.set(this.answers); | |
} | |
_writeCode() { | |
const { routeMethod, routeName } = this.answers; | |
this.fs.copyTpl( | |
this.templatePath('src/routes/name/index.ts'), | |
this.destinationPath(`src/routes/${routeName}/index.ts`), | |
this.answers, | |
); | |
this.fs.copyTpl( | |
this.templatePath('src/routes/name/method/index.ts'), | |
this.destinationPath(`src/routes/${routeName}/${routeMethod}/index.ts`), | |
this.answers, | |
); | |
this.fs.copyTpl( | |
this.templatePath('src/routes/name/method/handler.ts'), | |
this.destinationPath(`src/routes/${routeName}/${routeMethod}/handler.ts`), | |
this.answers, | |
); | |
} | |
_writeTest() { | |
const { routeMethod, routeName } = this.answers; | |
this.fs.copyTpl( | |
this.templatePath('test/routes/name/method/handler.test.ts'), | |
this.destinationPath(`test/routes/${routeName}/${routeMethod}/handler.test.ts`), | |
this.answers, | |
); | |
} | |
writing() { | |
this.log(`Generating route for ${this.answers.routeTitle}`); | |
this._writeCode(); | |
this._writeTest(); | |
} | |
end() { | |
this.log(`Route ${this.answers.routeTitle} has been generated`); | |
} | |
} | |
module.exports = RouteGenerator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment