From 2796230e953eb8c29d6227a1a3858f5f08a8f200 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 26 Jan 2023 09:55:27 +0000 Subject: [PATCH] fix(migrations): add `enum` in `mode` option in `standalone` schema (#48851) Currently the `mode` is validated during schematic execution. While this cover a case of incorrect value this caused other parts were the correct values cannot be determined. Options in schemas are used for a number of reasons during runtime. - These are used to build auto complete - Validation of inputs prior of the schematic is built with meaningful errors such as suggested inputs. - Generation of help output. Eventually these should also be used to generate DTS. This is already done in the CLI to avoid having to write Types manually. PR Close #48851 --- .../ng-generate/standalone-migration/index.ts | 8 +++----- .../ng-generate/standalone-migration/schema.json | 1 + .../schematics/test/standalone_migration_spec.ts | 15 --------------- 3 files changed, 4 insertions(+), 20 deletions(-) diff --git a/packages/core/schematics/ng-generate/standalone-migration/index.ts b/packages/core/schematics/ng-generate/standalone-migration/index.ts index 2e58cf706c7..77a2331239f 100644 --- a/packages/core/schematics/ng-generate/standalone-migration/index.ts +++ b/packages/core/schematics/ng-generate/standalone-migration/index.ts @@ -79,15 +79,13 @@ function standaloneMigration(tree: Tree, tsconfigPath: string, basePath: string, let pendingChanges: ChangesByFile; let filesToRemove: Set|null = null; - if (options.mode === MigrationMode.toStandalone) { - pendingChanges = toStandalone(sourceFiles, program, printer); - } else if (options.mode === MigrationMode.pruneModules) { + if (options.mode === MigrationMode.pruneModules) { const result = pruneNgModules(program, host, basePath, rootNames, sourceFiles, printer); pendingChanges = result.pendingChanges; filesToRemove = result.filesToRemove; } else { - throw new SchematicsException( - `Unknown schematic mode ${options.mode}. Cannot run the standalone migration.`); + /** MigrationMode.toStandalone */ + pendingChanges = toStandalone(sourceFiles, program, printer); } for (const [file, changes] of pendingChanges.entries()) { diff --git a/packages/core/schematics/ng-generate/standalone-migration/schema.json b/packages/core/schematics/ng-generate/standalone-migration/schema.json index 512d08b6e8f..94e3bfc98e7 100644 --- a/packages/core/schematics/ng-generate/standalone-migration/schema.json +++ b/packages/core/schematics/ng-generate/standalone-migration/schema.json @@ -7,6 +7,7 @@ "mode": { "description": "Operation that should be performed by the migrator", "type": "string", + "enum": ["convert-to-standalone", "prune-ng-modules"], "default": "convert-to-standalone", "x-prompt": { "message": "Choose the type of migration:", diff --git a/packages/core/schematics/test/standalone_migration_spec.ts b/packages/core/schematics/test/standalone_migration_spec.ts index c5b6987a0ad..24cdba4b141 100644 --- a/packages/core/schematics/test/standalone_migration_spec.ts +++ b/packages/core/schematics/test/standalone_migration_spec.ts @@ -178,21 +178,6 @@ describe('standalone migration', () => { expect(error).toBe('Cannot run standalone migration outside of the current project.'); }); - it('should throw an error if an unknown mode is passed in', async () => { - let error: string|null = null; - - writeFile('dir.ts', 'console.log(123)'); - - try { - await runMigration('does-not-exist', './'); - } catch (e: any) { - error = e.message; - } - - expect(error).toBe( - 'Unknown schematic mode does-not-exist. Cannot run the standalone migration.'); - }); - it('should throw an error if the passed in path is a file', async () => { let error: string|null = null;