Skip to content
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

feat(schematics): use schematicCollections instead of defaultCollection #3441

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
53 changes: 33 additions & 20 deletions modules/schematics/src/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import {
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { createWorkspace } from '@ngrx/schematics-core/testing';
import { Schema as SchematicOptions } from './schema';

describe('ng-add Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@ngrx/schematics',
path.join(__dirname, '../../collection.json')
);
const defaultOptions: SchematicOptions = {
defaultCollection: true,

const defaultWorkspace = {
version: 1,
projects: {},
};

let appTree: UnitTestTree;
Expand All @@ -21,29 +22,41 @@ describe('ng-add Schematic', () => {
appTree = await createWorkspace(schematicRunner, appTree);
});

it(`should leave the workspace's cli as default`, async () => {
const options: SchematicOptions = {
...defaultOptions,
defaultCollection: false,
};
it('should fail if schematicCollections is not defined', async () => {
appTree.overwrite(
'/angular.json',
JSON.stringify(defaultWorkspace, undefined, 2)
);

const tree = await schematicRunner
.runSchematicAsync('ng-add', options, appTree)
.toPromise();
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.cli).not.toBeDefined();
let thrownError: Error | null = null;
try {
await schematicRunner
.runSchematicAsync('ng-add', {}, appTree)
.toPromise();
} catch (err: any) {
thrownError = err;
}

expect(thrownError).toBeDefined();
});

it('should set workspace default cli to @ngrx/schematics', async () => {
const options: SchematicOptions = {
...defaultOptions,
defaultCollection: true,
};
it('should add @ngrx/schematics into schematicCollections ', async () => {
appTree.overwrite(
'/angular.json',
JSON.stringify(
{ ...defaultWorkspace, cli: { schematicCollections: ['foo'] } },
undefined,
2
)
);

const tree = await schematicRunner
.runSchematicAsync('ng-add', options, appTree)
.runSchematicAsync('ng-add', {}, appTree)
.toPromise();
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.cli.defaultCollection).toEqual('@ngrx/schematics');
expect(workspace.cli.schematicCollections).toEqual([
'foo',
'@ngrx/schematics',
]);
});
});
31 changes: 14 additions & 17 deletions modules/schematics/src/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import {
chain,
noop,
Rule,
SchematicContext,
Tree,
} from '@angular-devkit/schematics';
import { getWorkspace, getWorkspacePath } from '../../schematics-core';

import { Schema as SchematicOptions } from './schema';

function updateWorkspaceCli(host: Tree, value: any) {
function updateSchematicCollections(host: Tree) {
const workspace = getWorkspace(host);
const path = getWorkspacePath(host);

workspace['cli'] = {
...workspace['cli'],
...value,
};
if (!(workspace['cli'] && workspace['cli']['schematicCollections'])) {
throw new Error(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we throwing an error here? If the schematicCollections property doesn't exist, we should create it and add @ngrx/schematics to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be on me 😅
#3383 (comment)

Copy link
Member

@timdeschryver timdeschryver May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's 2vs1, I'm fine with creating the property.
I don't know what Angular's schematic will do with it though (in the case that NgRx is updated before Angular)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh ok. I was thinking of cases like Nx where it may not be present, but I think they account for that anyway. Throwing the error should be ok for now

'schematicCollections is not defined in the global cli options'
);
}

workspace['cli']['schematicCollections'] = [
...workspace['cli']['schematicCollections'],
'@ngrx/schematics',
];
host.overwrite(path, JSON.stringify(workspace, null, 2));
}

function setAsDefaultSchematics() {
const cli = {
defaultCollection: '@ngrx/schematics',
};
function updateWorkspaceCli() {
return (host: Tree) => {
updateWorkspaceCli(host, cli);
updateSchematicCollections(host);
return host;
};
}

export default function (options: SchematicOptions): Rule {
export default function (): Rule {
return (host: Tree, context: SchematicContext) => {
return chain([
options && options.defaultCollection ? setAsDefaultSchematics() : noop(),
])(host, context);
return chain([updateWorkspaceCli()])(host, context);
};
}
10 changes: 1 addition & 9 deletions modules/schematics/src/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
"$id": "SchematicsNgRxSchematics",
"title": "Scaffolding library for Angular applications using NgRx libraries",
"type": "object",
"properties": {
"defaultCollection": {
"type": "boolean",
"default": true,
"description": "Use @ngrx/schematics as the default collection",
"x-prompt": "Do you want to use @ngrx/schematics as the default collection?",
"alias": "d"
}
},
"properties": {},
"required": []
}
5 changes: 2 additions & 3 deletions modules/schematics/src/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export interface Schema {
defaultCollection?: boolean;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Schema {}