Skip to content

Instantly share code, notes, and snippets.

@iksena
Created January 16, 2021 16:22
Show Gist options
  • Save iksena/b05f11eef1ba87e770fe42513e171b2f to your computer and use it in GitHub Desktop.
Save iksena/b05f11eef1ba87e770fe42513e171b2f to your computer and use it in GitHub Desktop.
yo microservice:route
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