Skip to content

Commit

Permalink
fix(schematics): create schematicCollections if not exists (#3470)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Jun 28, 2022
1 parent 8ff1daa commit 011cbcc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
49 changes: 35 additions & 14 deletions modules/schematics/src/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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',
]);
});
Expand Down
14 changes: 6 additions & 8 deletions modules/schematics/src/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit 011cbcc

Please sign in to comment.