Skip to content

Commit 2796230

Browse files
alan-agius4thePunderWoman
authored andcommitted
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
1 parent dedac8d commit 2796230

File tree

3 files changed

+4
-20
lines changed

3 files changed

+4
-20
lines changed

packages/core/schematics/ng-generate/standalone-migration/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,13 @@ function standaloneMigration(tree: Tree, tsconfigPath: string, basePath: string,
7979
let pendingChanges: ChangesByFile;
8080
let filesToRemove: Set<ts.SourceFile>|null = null;
8181

82-
if (options.mode === MigrationMode.toStandalone) {
83-
pendingChanges = toStandalone(sourceFiles, program, printer);
84-
} else if (options.mode === MigrationMode.pruneModules) {
82+
if (options.mode === MigrationMode.pruneModules) {
8583
const result = pruneNgModules(program, host, basePath, rootNames, sourceFiles, printer);
8684
pendingChanges = result.pendingChanges;
8785
filesToRemove = result.filesToRemove;
8886
} else {
89-
throw new SchematicsException(
90-
`Unknown schematic mode ${options.mode}. Cannot run the standalone migration.`);
87+
/** MigrationMode.toStandalone */
88+
pendingChanges = toStandalone(sourceFiles, program, printer);
9189
}
9290

9391
for (const [file, changes] of pendingChanges.entries()) {

packages/core/schematics/ng-generate/standalone-migration/schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"mode": {
88
"description": "Operation that should be performed by the migrator",
99
"type": "string",
10+
"enum": ["convert-to-standalone", "prune-ng-modules"],
1011
"default": "convert-to-standalone",
1112
"x-prompt": {
1213
"message": "Choose the type of migration:",

packages/core/schematics/test/standalone_migration_spec.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,6 @@ describe('standalone migration', () => {
178178
expect(error).toBe('Cannot run standalone migration outside of the current project.');
179179
});
180180

181-
it('should throw an error if an unknown mode is passed in', async () => {
182-
let error: string|null = null;
183-
184-
writeFile('dir.ts', 'console.log(123)');
185-
186-
try {
187-
await runMigration('does-not-exist', './');
188-
} catch (e: any) {
189-
error = e.message;
190-
}
191-
192-
expect(error).toBe(
193-
'Unknown schematic mode does-not-exist. Cannot run the standalone migration.');
194-
});
195-
196181
it('should throw an error if the passed in path is a file', async () => {
197182
let error: string|null = null;
198183

0 commit comments

Comments
 (0)