question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional “X-Prompt” in Angular Schematics

See original GitHub issue

It seems feature to add conditional x-prompts is missing in angular schematics.

Requirement : To display conditional prompts, based on the previous selection ( “Analytics”, in this case ).

Problem : For any selection, it is still displaying the prompt to add Google Analytics’s Tracking Id even if i select AppDynamics or none.

References : Applying Subschemas Conditionally Angular Schematics

Note: For Simplicity, i have removed “allOf”,“anyOf” etc. and code for any other analytics tool, still it was not working.

Code (Schema.json):

{
  "$schema": "http://json-schema.org/schema",
  "id": "MyDemoSchema",
  "title": "My Demo Schematics Schema",
  "type": "object",
  "properties": {
    "analytics": {
      "enum": [
        "none",
        "GoogleAnalytics",
        "AppDynamics"
      ],
      "x-prompt": "Which Analytics would you like to use?"
    }
  },
  "if": {
    "properties": {
      "analytics": {
        "const": "GoogleAnalytics"  
      }
    }
  },
  "then": {
    "properties": {
      "GoogleAnalytics": {
        "type": "string",
        "x-prompt": "Enter Tracking ID?"
      }
    }
  }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:37
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
kroedercommented, Aug 23, 2021

I have the same goal but a different proposal. Instead of increasing the basic schema.json feature-set, I’d rather use a second function-driven API. I believe adding a condition API to a json file will increase the complexity tremendously.

Here’s a proposal. It certainly contains lots of flaws and is meant as a start to discuss further possible approaches.

collection.json

{
  "$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json",
    "dynamic-generate-props": {
        "description": "Dynamic inputs",
        "factory": "./dynamic/index#dynamicSchematic",
        "schema": "./dynamic/schema#conditionalInputs"
    }
  }
}

dynamic/schema.ts#conditionalInputs

/**
 * User code
 */
interface MyInput {
    name: string;
    path: string;
    needsFooter: boolean;
    footerColor?: FooterColor;
}

type FooterColor = 'blue' | 'green' | 'red';

export function conditionalInputs() {
    return schematicsSchema<MyInput>({
        name: () => createInput({
            type: InputTypes.STRING,
            xPrompt: "Name of your component"
        }),
        path: () => executionPath(),
        needsFooter: () => createInput({
            type: InputTypes.BOOLEAN,
            xPrompt: "Do you need a footer?"
        }),
        footerColor: (prevValues) => prevValues.needsFooter 
            ? createInput({
                type: InputTypes.ENUM,
                enum: [
                    "blue",
                    "green",
                    "red"
                ],
                xPrompt: "Footer color"
            })
          : noop()
    })
}

Angular framework (pseudo code)

enum InputTypes { STRING, BOOLEAN, NUMBER, ENUM }
type InputFunction<TInput> = { [key: string]: (prevValues?: TInput) => any; }

function schematicsSchema<TInput>(userInputs: InputFunction<TInput>) {
    return {};
}

function createInput(options: any) {}

function executionPath() {}

function noop() {}

Having an interface in the user code is intentional. Right now, if I am writing a schematic, the input properties are not typed and I need to manage two lists of inputs in parallel (schema.json inputs / schematic factory code input type)

Any thoughts on this? Or maybe disagreement to a TS API? 🙂

0reactions
angular-automatic-lock-bot[bot]commented, Sep 4, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular schematics conditional property/prompt - Stack Overflow
What I've discovered - if you need conditional prompts, then you can't ... import { askConfirmation } from '@angular/cli/utilities/prompt'; ...
Read more >
Enhance developer experience: Use prompts in your schematics
Okay so first step to adding prompts to our schematics is to add x-prompt to your schema.json . Now if you aren't familiar...
Read more >
Angular CLI Prompts | Blog - Kevin Schuchard
In order to prompt the user for a selection, you can both provide suggestions and limit the possible choices with the enum type....
Read more >
Authoring schematics - Angular
Similarly, you can add a prompt to let the user decide whether the schematic uses color when executing its hello action. The schema...
Read more >
The Complete Guide to Custom Angular Schematics - Morioh
Prompts are the way to provide schematic options without the need to know about them before hand. To use them we have to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found