Angular Schematics Prompt for array type
Asked Answered
K

4

5

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"]
}
Kendyl answered 17/4, 2019 at 19:56 Comment(0)
P
4

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"
        ]
      }
    }
People answered 17/4, 2019 at 21:2 Comment(0)
T
3
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                                             ]" }
    ]
  },
Tollhouse answered 7/5, 2019 at 10:27 Comment(0)
C
0

I did not need a dynamic list, but needed to repeat the same question set over and over. So I dicided to use the library, which is used by angular schematics, directly. You can find the library here: https://www.npmjs.com/package/inquirer. I called the library directly after the schematic was called.

Cosmo answered 12/8, 2022 at 11:11 Comment(1)
Such additional information should be posted as Post edit, or in comment - not as an AnswerAnsilma
V
-1

Unfortunately, none of these answers address the issue entirely. Yes, you can show a list of options using enum and multiple items in the x-prompt property like shown in the above answers but it a JSON file, it has to be a hardcoded list of values.

This means that you won't be able to show the currently existing modules in the list. At least not as of right now (v9)

Venessavenetia answered 3/5, 2020 at 15:8 Comment(1)
it'd be great if people who disagree with an answer would at least comment with their suggested answer instead of just downvoting and leaveVenessavenetia

© 2022 - 2024 — McMap. All rights reserved.