You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建链式Schematics遇阻:子命令触发父命令问题求助

Fixing Schematics Subcommand Matching for migration:express/migration:hapi

I've run into this exact issue before with Schematics' command matching! The problem here is how you're defining your subcommands—neither of your current configurations follow the official schema for subschematics. Let me break down the fix:

Correct collection.json Configuration

You need to use the subschematics property to explicitly define child commands under your parent migration schematic. This tells the Schematics CLI to properly recognize the full command paths:

{
  "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
  "schematics": {
    "migration": {
      "description": "Create full migration files",
      "factory": "./migrations/index#migration",
      "schema": "./migrations/migration-schema.json",
      "subschematics": {
        "express": {
          "description": "Create Express-specific migration files",
          "factory": "./migrations/index#express",
          "schema": "./migrations/express-schema.json"
        },
        "hapi": {
          "description": "Create Hapi-specific migration files",
          "factory": "./migrations/index#hapi",
          "schema": "./migrations/hapi-schema.json"
        }
      }
    }
  }
}

Why This Works

  • The subschematics field is the official, supported way to define nested commands in Schematics. Your second configuration tried nesting child schematics directly under the parent, but that's not a valid structure the CLI parser understands.
  • When you run mySchematic:migration, it executes the root migration factory as intended.
  • When you run mySchematic:migration:express or mySchematic:migration:hapi, the CLI traverses the subschematics tree and runs the corresponding child factory instead of falling back to the parent.

Why Your Previous Configurations Failed

  1. Flat Configuration (First Attempt)
    The CLI prioritizes matching valid standalone schematics first. Since migration is a valid top-level schematic, it stops there instead of looking for the full migration:hapi path. The parser doesn't automatically associate flat schematics with colons as parent-child relationships.

  2. Direct Nested Configuration (Second Attempt)
    Nesting child schematics directly under the parent without using subschematics isn't part of the Schematics collection schema. The CLI ignores those nested properties entirely, so it still falls back to running the parent migration schematic.

Verification Steps

To ensure everything works as expected:

  • Double-check that migration, express, and hapi factory functions are correctly exported from ./migrations/index.
  • Test the commands directly with the Schematics CLI for debugging:
    # Test parent command
    schematics ./path-to-your-collection.json migration
    # Test Express subcommand
    schematics ./path-to-your-collection.json migration:express
    # Test Hapi subcommand
    schematics ./path-to-your-collection.json migration:hapi
    

内容的提问来源于stack exchange,提问作者Leandro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:32:50