User interactions
Prompts
module . exports = class extends Generator {
async prompting () {
const answers = await this . prompt ([
{
type : "input" ,
name : "name" ,
message : "Your project name" ,
default : this . appname // Default to current folder name
},
{
type : "confirm" ,
name : "cool" ,
message : "Would you like to enable the Cool feature?"
}
]);
this . log ( "app name" , answers . name );
this . log ( "cool feature" , answers . cool );
}
};
Using user answers at a later stage
module . exports = class extends Generator {
async prompting () {
this . answers = await this . prompt ([
{
type : "confirm" ,
name : "cool" ,
message : "Would you like to enable the Cool feature?"
}
]);
}
writing () {
this . log ( "cool feature" , this . answers . cool ); // user answer `cool` used
}
};
Remembering user preferences
this . prompt ({
type : "input" ,
name : "username" ,
message : "What's your GitHub username" ,
store : true
});
Arguments
yo webapp my-project
desc Description for the argument required Boolean whether it is required type String, Number, Array (can also be a custom function receiving the raw string value and parsing it) default Default value for this argument
module . exports = class extends Generator {
// note: arguments and options should be defined in the constructor.
constructor ( args , opts ) {
super ( args , opts );
// This makes `appname` a required argument.
this . argument ( "appname" , { type : String , required : true });
// And you can then access it later; e.g.
this . log ( this . options . appname );
}
};
Options
yo webapp --coffee
desc Description for the option alias Short name for option type Either Boolean, String or Number (can also be a custom function receiving the raw string value and parsing it) default Default value hide Boolean whether to hide from help
module . exports = class extends Generator {
// note: arguments and options should be defined in the constructor.
constructor ( args , opts ) {
super ( args , opts );
// This method adds support for a `--coffee` flag
this . option ( "coffee" );
// And you can then access it later; e.g.
this . scriptSuffix = this . options . coffee ? ".coffee" : ".js" ;
}
};
Outputting Information
module . exports = class extends Generator {
myAction () {
this . log ( "Something has gone wrong!" );
}
};