Skip to content

Commit

Permalink
feat(schematics): use schematicCollections instead of defaultCollecti…
Browse files Browse the repository at this point in the history
…on (#3441)

Closes #3383
  • Loading branch information
suke committed Jun 6, 2022
1 parent baaeca8 commit 5abf828
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
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(
'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 {}

0 comments on commit 5abf828

Please sign in to comment.