Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Schematics Prompt for array type

When setting up my schematics I see that I can prompt for any of the following types String | Boolean | Array | Number | Integer | Null | Object.

I am trying to set up a schematic that prompts the user to select a module from a list of available modules. So for example the user would see something like this:

What module are you adding this store to?
 > Foo
   Bar
   Baz

While there are a ton of examples of both the String and Boolean prompts nobody has provided an example that I've found for the Array prompts. For the life of me I can not figure out how to provide the options in the array to prompt the user to select from and this doesn't appear in their documentation at all.

{
  "$schema": "http://json-schema.org/schema",
  "id": "SchematicsIDXStoreGen",
  "title": "IDX Store Gen Schema",
  "type": "object",
  "properties": {
    "featureName": {
      "type": "string",
      "description": "The name of the store",
      "x-prompt": "What is the name of the store you'd like to create"
    },
    "module": {
      "type": "array",
      "description": "Select the appropriate module",
      "x-prompt": "What module are you adding this store to?" // I want to provide a list of available modules here.
    }
  },
  "required": ["featureName", "module"]
}
like image 218
efarley Avatar asked Apr 17 '19 19:04

efarley


2 Answers

You can try the following method also you wanna check the option what ever suits you.

"modules": {
      "type": "array",
      "description": "description",
      "uniqueItems": true,
      "items": {
        "type": "string"
      },
      "x-prompt": {
        "message": "Which module would you like to select?",
        "type": "list",
        "multiselect": true,
        "items": [
          "firstOption",
          "secondOption",
          "thirdOption"
        ]
      }
    }
like image 111
fastAsTortoise Avatar answered Sep 20 '22 23:09

fastAsTortoise


ng new myProject

will prompt you for styling, which is a list you can select from.

And if you look inside @angular/cli project (\packages\schematics\angular\ng-new), you can see how they did it:

"style": {
  "description": "The file extension or preprocessor to use for style files.",
  "type": "string",
  "default": "css",
  "enum": [
    "css",
    "scss",
    "sass",
    "less",
    "styl"
  ],
  "x-prompt": {
    "message": "Which stylesheet format would you like to use?",
    "type": "list",
    "items": [
      { "value": "css",  "label": "CSS" },
      { "value": "scss", "label": "SCSS   [ http://sass-lang.com/documentation/file.SASS_REFERENCE.html#syntax ]" },
      { "value": "sass", "label": "Sass   [ http://sass-lang.com/documentation/file.INDENTED_SYNTAX.html       ]" },
      { "value": "less", "label": "Less   [ http://lesscss.org                                                 ]" },
      { "value": "styl", "label": "Stylus [ http://stylus-lang.com                                             ]" }
    ]
  },
like image 21
yusuf tezel Avatar answered Sep 19 '22 23:09

yusuf tezel