I know it must be very simple, but I start using yeoman very recently and I'm not very familiar with it.
I want to get the user's input and use it to change a class name or variable
I ask the user what is the name of the class
this.controllerName = await this.prompt({
type: 'input',
name: 'controllerName',
message: `What's the name of the controller?`
})
I know how to change the file's name
this.destinationPath(`${this.controllerName.controllerName}Controller.js`)
But I don't know how to change a field inside the file. Example:
My template class
class TemplateController {
createTemplate (req, res) {
console.log('GET request')
res.json('URL WORKING!')
}
I'd like to change the class/method name for what the user answered in the prompt
class UserController {
createUser (req, res) {
console.log('GET request')
res.json('URL WORKING!')
}
Add placeholders to your base file ( yeoman uses EJS template engine ):
_controller.js
class <%= name %>Controller {
create<%= name %> (req, res) {
console.log('GET request')
res.json('URL WORKING!')
}
}
Pass user input as name
to your template:
index.js
...
writing() {
this.fs.copyTpl(
this.templatePath('_controller.js'),
this.destinationPath(`${this.controllerName.controllerName}Controller.js`),
{
name: this.controllerName.controllerName
}
)
}
...