A template simple to show how to write a custom template by yourself.
- A template repo must have a template directory that holds the template files.
- A template repo may have a metadata file for the template which can be either a meta.js or meta.json file. It can contain the following fields:
prompts
: used to collect user options datafilters
: used to conditional filter files to rendercompleteMessage
: the message to be displayed to the user when the template has been generated. You can include custom instruction here
The prompts
field in the metadata file should be an object hash containing prompts for the user. For each entry, the key is the variable name and the value is an Inquirer.js question object. Example:
{
"description": {
"type": "string",
"required": false,
"message": "Project description",
"default": "project description"
}
}
After all prompts are finished, all files inside template
will be rendered using Handlebars, with the prompt results as the data.
A prompt can be made conditional by adding a when
field, which should be a JavaScript expression evaluated with data collected from previous prompts. For example:
{
"prompts": {
"lint": {
"type": "confirm",
"message": ""Use ESLint to lint your code?"
},
"eslint": {
"when": "lint",
"type": "list",
"message": "Pick a lint config",
"choices": [
"standard",
"airbnb",
"none"
]
}
}
}
The prompt for eslint
will only be triggered when the user answered yes to the lint
prompt.
The filters
field in the metadata file should be an object hash containing file filtering rules. For each entry, the key is a minimatch glob pattern and the value is a JavaScript expression evaluated in the context of prompt answers data. Example:
{
"prompts": {
"unit": {
"type": "confirm",
"message": "Setup unit tests with Mocha?"
}
},
"filters": {
"test/*": "unit"
}
}
Files under test
will only be generated if the user answered yes to the prompt for unit
.
Note that the dot
option for minimatch is set to true
so glob patterns would also match dotfiles by default.
destDirName
- destination directory name
{
"completeMessage": "To get started:\n\n cd {{destDirName}}\n npm install\n npm run dev"
}
isCwd
- generating template into current directory
{
"completeMessage": "{{#isCwd}}To get started:\n\n npm install\n npm run dev.{{else}}To get started:\n\n cd {{destDirName}}\n npm install\n npm run dev.{{/isCwd}}"
}