Skip to content

Additional TS schematics schema API #22242

Description

@kroeder

🚀 Feature request

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Description

I already commented on this in #18578 (comment) but thought this is worth a separate issue since the other issue is not directly related to my proposal.

The current schema.json approach where you can define different inputs to your schematic is fine for a lot of schematics. However, there are a few that would greatly benefit from a more dynamic approach! There are other issues that requested a separate type for the JSON schema but I think this would over-complicate the current easy approach.

My proposal is to offer a TypeScript API as a target for a schematic schema.

Example collection.json

{
    "schematic-with-typescript-schema": {
        "description": "...",
        "factory": "...",
        "schema": "./some-schematic/schema#typescriptSchema"
    }
}

Example TS schema

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

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

export function TSExampleSchema() {
    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()
    })
}

This is more-or-less pseudo-code to give you an idea of what I want to achieve: an API that is more flexible but one that should not replace the simple JSON approach.

One example usage could be something like a table schematic that already knows the target data model by pre-fetching possible selections from a e.g. GraphQL Introspection before offering a list of answers

ng generate my-package:table
> Based on which model?
   [ ] Users
   [ ] Articles
   [ ] Products

Describe the solution you'd like

A TS API to offer more advanced schematics

Describe alternatives you've considered

Extending the current JSON API. However, it is good as it is right now and most likely could not offer an async API without being over-complicated

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: @angular-devkit/schematicsfeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under consideration

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions