-
Notifications
You must be signed in to change notification settings - Fork 8
Exercise 2: prompts
Kevin Schuchard edited this page Apr 30, 2019
·
14 revisions
- Create a schema.json file at src/hello-world/schema.json.
- Add a new
propertiestop-level property. - Within that object, define a
nameproperty. - Set the
typeproperty value tostring. - Provide a
descriptionproperty and value. - Provide the prompt text via the
x-promptproperty.
Here is an example of a string prompt:
{
"properties": {
"name": {
"type": "string",
"description": "The name of the person to 👋 to.",
"x-prompt": "What is the name of the person you want to say hi to?"
}
}
}Next, add an enum prompt such as the one below
{
"properties": {
"fileExtension": {
"enum": ["ts", "html", "css", "spec.ts"],
"description": "The file extension type",
"default": "ts",
"x-prompt": "Select a file type extension"
}
}
}In order utilize prompts, you need tell the CLI which schema to use.
- Open the src/collection.json file and under
factoryadd aschemaproperty that points the the schema file you just edited.
"factory": "./hello-world/index#helloWorld",
"schema": "./hello-world/schema.json"- Open the src/hello-world/index.ts file and define a new
HelloWorldOptionsinterface. - The interface should have
nameandfileExtensionmembers that are both of typestring. - Modify the
helloWorldfunction to replace theanytype for the_optionsargument with the newHelloWorldOptionsinterface.
- Read the prompt value from the
_optionsand interpolate the values in the file name and content arguments totree.create()
return tree.create(`${_options.name}.${_options.fileExtension}`, `<p>Hi, ${_options.name}!<p>`)npm run buildschematics .:hello-world --dry-run=falseYou should see a file add to the root directory with the name and fileExtension type you entered.