创建链式Schematics遇阻:子命令触发父命令问题求助
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
subschematicsfield 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 rootmigrationfactory as intended. - When you run
mySchematic:migration:expressormySchematic:migration:hapi, the CLI traverses thesubschematicstree and runs the corresponding child factory instead of falling back to the parent.
Why Your Previous Configurations Failed
Flat Configuration (First Attempt)
The CLI prioritizes matching valid standalone schematics first. Sincemigrationis a valid top-level schematic, it stops there instead of looking for the fullmigration:hapipath. The parser doesn't automatically associate flat schematics with colons as parent-child relationships.Direct Nested Configuration (Second Attempt)
Nesting child schematics directly under the parent without usingsubschematicsisn't part of the Schematics collection schema. The CLI ignores those nested properties entirely, so it still falls back to running the parentmigrationschematic.
Verification Steps
To ensure everything works as expected:
- Double-check that
migration,express, andhapifactory 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

