Skip to content

[BUG] Generate command prompts for values that are already provided via CLI flagsΒ #501

@JarekToro

Description

@JarekToro

Generate command prompts for values that are already provided via CLI flags

Information

  • Version: 6.6.3
  • Packages: @tsed/cli

The generate command incorrectly prompts for values that are already provided via command-line flags. When passing options like --route, --directory, etc., the CLI still prompts for these values instead of using what was provided.

Example

# This command provides the route via --route flag
tsed g --route="api/receipts" "Controller" "Receipts"

# But still prompts:
? Which route? (/receipts)

Even though the data object clearly contains the route:

{
  verbose: false,
  type: 'Controller',
  name: 'Receipts',
  route: 'api/receipts',  // ← Route is provided
  rawArgs: []
}
? Which route? (/receipts)  // ← But still prompts

Root Cause

The when conditions in the prompt definitions have inverted logic. They show prompts when values exist instead of when they're missing.

Current broken code:

{
    type: "input",
    name: "route",
    message: "Which route?",
    when(state) {
        // This returns true (shows prompt) when route EXISTS
        return !!(["controller", "server"].includes(state.type || initialOptions.type) || initialOptions.route);
    },
    ...
}

The || initialOptions.route causes the prompt to show when route exists, which is backwards.

Affected Prompts

All prompts with this pattern are affected:

  • route - prompts even when --route is provided
  • directory - prompts even when --directory is provided
  • templateType - prompts even when --templateType is provided
  • middlewarePosition - prompts even when --middlewarePosition is provided

Solution

The when conditions need to check for the absence of values, not their presence:

{
    type: "input",
    name: "route",
    message: "Which route?",
    when(state) {
        // Only show prompt when route is NOT provided
        return !initialOptions.route && ["controller", "server"].includes((state.type || initialOptions.type).toLowerCase());
    },
    default: (state) => {
        return state.type === "server" ? "/rest" : this.routePipe.transform(getName(state));
    }
},
{
    type: "list",
    name: "directory",
    message: "Which directory?",
    when(state) {
        // Only show prompt when directory is NOT provided
        return ["controller"].includes((state.type || initialOptions.type).toLowerCase()) && !initialOptions.directory;
    },
    choices: this.getDirectories("controllers")
},
// Similar fixes needed for templateType and middlewarePosition prompts

Note: Added .toLowerCase() to handle case-insensitive type matching ('Controller' vs 'controller').

Acceptance criteria

  • Prompts should only appear when values are NOT provided via CLI flags
  • When --route is provided, the route prompt should be skipped
  • When --directory is provided, the directory prompt should be skipped
  • When other options are provided via flags, their respective prompts should be skipped
  • Type comparisons should be case-insensitive

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions