Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional TS schematics schema API #22242

Open
1 of 15 tasks
kroeder opened this issue Nov 28, 2021 · 1 comment
Open
1 of 15 tasks

Additional TS schematics schema API #22242

kroeder opened this issue Nov 28, 2021 · 1 comment

Comments

@kroeder
Copy link

@kroeder kroeder commented Nov 28, 2021

🚀 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

@alan-agius4
Copy link
Collaborator

@alan-agius4 alan-agius4 commented Nov 28, 2021

This is somewhat also related to #16705.

Loading

@ngbot ngbot bot added this to the Backlog milestone Nov 29, 2021
@ngbot ngbot bot added this to the Backlog milestone Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants