diff --git a/modules/schematics/src/ng-add/index.spec.ts b/modules/schematics/src/ng-add/index.spec.ts index 810424a4e0..1344cbfefd 100644 --- a/modules/schematics/src/ng-add/index.spec.ts +++ b/modules/schematics/src/ng-add/index.spec.ts @@ -22,29 +22,50 @@ describe('ng-add Schematic', () => { appTree = await createWorkspace(schematicRunner, appTree); }); - it('should fail if schematicCollections is not defined', async () => { + it('should add @ngrx/schematics into schematicCollections ', async () => { appTree.overwrite( '/angular.json', - JSON.stringify(defaultWorkspace, undefined, 2) + JSON.stringify( + { + ...defaultWorkspace, + cli: { schematicCollections: ['existingCollection'] }, + }, + undefined, + 2 + ) ); - let thrownError: Error | null = null; - try { - await schematicRunner - .runSchematicAsync('ng-add', {}, appTree) - .toPromise(); - } catch (err: any) { - thrownError = err; - } + const tree = await schematicRunner + .runSchematicAsync('ng-add', {}, appTree) + .toPromise(); + const workspace = JSON.parse(tree.readContent('/angular.json')); + expect(workspace.cli.schematicCollections).toEqual([ + 'existingCollection', + '@ngrx/schematics', + ]); + }); + + it('should create schematicCollections is not defined', async () => { + appTree.overwrite( + '/angular.json', + JSON.stringify(defaultWorkspace, undefined, 2) + ); - expect(thrownError).toBeDefined(); + const tree = await schematicRunner + .runSchematicAsync('ng-add', {}, appTree) + .toPromise(); + const workspace = JSON.parse(tree.readContent('/angular.json')); + expect(workspace.cli.schematicCollections).toEqual(['@ngrx/schematics']); }); - it('should add @ngrx/schematics into schematicCollections ', async () => { + it('should create schematicCollections is not defined using the original defaultCollection ', async () => { appTree.overwrite( '/angular.json', JSON.stringify( - { ...defaultWorkspace, cli: { schematicCollections: ['foo'] } }, + { + ...defaultWorkspace, + cli: { defaultCollection: 'existingCollection' }, + }, undefined, 2 ) @@ -55,7 +76,7 @@ describe('ng-add Schematic', () => { .toPromise(); const workspace = JSON.parse(tree.readContent('/angular.json')); expect(workspace.cli.schematicCollections).toEqual([ - 'foo', + 'existingCollection', '@ngrx/schematics', ]); }); diff --git a/modules/schematics/src/ng-add/index.ts b/modules/schematics/src/ng-add/index.ts index 23c63e78ef..08f4105b56 100644 --- a/modules/schematics/src/ng-add/index.ts +++ b/modules/schematics/src/ng-add/index.ts @@ -10,16 +10,14 @@ function updateSchematicCollections(host: Tree) { const workspace = getWorkspace(host); const path = getWorkspacePath(host); - if (!(workspace['cli'] && workspace['cli']['schematicCollections'])) { - throw new Error( - 'schematicCollections is not defined in the global cli options' - ); + workspace.cli = workspace.cli || {}; + workspace.cli.schematicCollections = workspace.cli.schematicCollections || []; + if (workspace.cli.defaultCollection) { + workspace.cli.schematicCollections.push(workspace.cli.defaultCollection); + delete workspace.cli.defaultCollection; } + workspace.cli.schematicCollections.push('@ngrx/schematics'); - workspace['cli']['schematicCollections'] = [ - ...workspace['cli']['schematicCollections'], - '@ngrx/schematics', - ]; host.overwrite(path, JSON.stringify(workspace, null, 2)); }