From 47ffda1be0907acf115c22b6da953c00fa91d2d1 Mon Sep 17 00:00:00 2001 From: suke Date: Sat, 28 May 2022 23:13:19 +0900 Subject: [PATCH] feat(schematics): use schematicCollections instead of defaultCollection Closes #3383 --- modules/schematics/src/ng-add/index.spec.ts | 53 +++++++++++++-------- modules/schematics/src/ng-add/index.ts | 31 ++++++------ modules/schematics/src/ng-add/schema.json | 10 +--- modules/schematics/src/ng-add/schema.ts | 5 +- 4 files changed, 50 insertions(+), 49 deletions(-) diff --git a/modules/schematics/src/ng-add/index.spec.ts b/modules/schematics/src/ng-add/index.spec.ts index c28d10d1eb..810424a4e0 100644 --- a/modules/schematics/src/ng-add/index.spec.ts +++ b/modules/schematics/src/ng-add/index.spec.ts @@ -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; @@ -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', + ]); }); }); diff --git a/modules/schematics/src/ng-add/index.ts b/modules/schematics/src/ng-add/index.ts index 14accef167..23c63e78ef 100644 --- a/modules/schematics/src/ng-add/index.ts +++ b/modules/schematics/src/ng-add/index.ts @@ -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); }; } diff --git a/modules/schematics/src/ng-add/schema.json b/modules/schematics/src/ng-add/schema.json index 4b9d907e36..45daf79272 100644 --- a/modules/schematics/src/ng-add/schema.json +++ b/modules/schematics/src/ng-add/schema.json @@ -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": [] } diff --git a/modules/schematics/src/ng-add/schema.ts b/modules/schematics/src/ng-add/schema.ts index 3d0dc802b8..02bea61c22 100644 --- a/modules/schematics/src/ng-add/schema.ts +++ b/modules/schematics/src/ng-add/schema.ts @@ -1,3 +1,2 @@ -export interface Schema { - defaultCollection?: boolean; -} +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface Schema {}