Skip to content

fix(schematics): create schematicCollections if not exists #3470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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