Skip to content

Commit e839568

Browse files
brandonrobertswesleygrimes
authored andcommitted
feat(effects): add support for minimal setup option for ng-add
1 parent c09ba19 commit e839568

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

modules/effects/schematics/ng-add/files/__name@dasherize@if-flat__/__name@dasherize__.effects.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { Actions, Effect } from '@ngrx/effects';
2+
import { Actions, createEffect } from '@ngrx/effects';
33

44
@Injectable()
55
export class <%= classify(name) %>Effects {

modules/effects/schematics/ng-add/index.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
createAppModuleWithEffects,
1212
} from '../../../schematics-core/testing';
1313

14-
describe('Effect ng-add Schematic', () => {
14+
describe('Effects ng-add Schematic', () => {
1515
const schematicRunner = new SchematicTestRunner(
1616
'@ngrx/effects',
1717
path.join(__dirname, '../collection.json')
@@ -66,6 +66,34 @@ describe('Effect ng-add Schematic', () => {
6666
).toBeGreaterThanOrEqual(0);
6767
});
6868

69+
it('should not create an effect if the minimal flag is provided', () => {
70+
const options = { ...defaultOptions, minimal: true };
71+
72+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
73+
const files = tree.files;
74+
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
75+
76+
expect(content).toMatch(/EffectsModule\.forRoot\(\[\]\)/);
77+
expect(
78+
files.indexOf(`${projectPath}/src/app/foo/foo.effects.spec.ts`)
79+
).toBe(-1);
80+
expect(files.indexOf(`${projectPath}/src/app/foo/foo.effects.ts`)).toBe(-1);
81+
});
82+
83+
it('should not import an effect into a specified module in the minimal flag is provided', () => {
84+
const options = {
85+
...defaultOptions,
86+
minimal: true,
87+
module: 'app.module.ts',
88+
};
89+
90+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
91+
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
92+
expect(content).not.toMatch(
93+
/import { FooEffects } from '.\/foo\/foo.effects'/
94+
);
95+
});
96+
6997
it('should be provided by default', () => {
7098
const options = { ...defaultOptions };
7199

modules/effects/schematics/ng-add/index.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,25 @@ import {
1111
mergeWith,
1212
move,
1313
noop,
14-
template,
1514
url,
1615
} from '@angular-devkit/schematics';
17-
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
18-
import * as ts from 'typescript';
1916
import {
20-
stringUtils,
21-
insertImport,
22-
buildRelativePath,
23-
addImportToModule,
2417
InsertChange,
25-
getProjectPath,
18+
addImportToModule,
19+
buildRelativePath,
2620
findModuleFromOptions,
21+
getProjectPath,
22+
insertImport,
23+
parseName,
24+
stringUtils,
2725
addPackageToPackageJson,
2826
platformVersion,
29-
parseName,
3027
} from '@ngrx/effects/schematics-core';
31-
import { Schema as RootEffectOptions } from './schema';
28+
import * as ts from 'typescript';
29+
import { Schema as EffectOptions } from './schema';
30+
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
3231

33-
function addImportToNgModule(options: RootEffectOptions): Rule {
32+
function addImportToNgModule(options: EffectOptions): Rule {
3433
return (host: Tree) => {
3534
const modulePath = options.module;
3635

@@ -39,7 +38,7 @@ function addImportToNgModule(options: RootEffectOptions): Rule {
3938
}
4039

4140
if (!host.exists(modulePath)) {
42-
throw new Error('Specified module does not exist');
41+
throw new Error(`Specified module path ${modulePath} does not exist`);
4342
}
4443

4544
const text = host.read(modulePath);
@@ -77,13 +76,21 @@ function addImportToNgModule(options: RootEffectOptions): Rule {
7776
effectsName,
7877
relativePath
7978
);
79+
80+
const effectsSetup = options.minimal ? `[]` : `[${effectsName}]`;
8081
const [effectsNgModuleImport] = addImportToModule(
8182
source,
8283
modulePath,
83-
`EffectsModule.forRoot([${effectsName}])`,
84+
`EffectsModule.forRoot(${effectsSetup})`,
8485
relativePath
8586
);
86-
const changes = [effectsModuleImport, effectsImport, effectsNgModuleImport];
87+
88+
let changes = [effectsModuleImport, effectsNgModuleImport];
89+
90+
if (!options.minimal) {
91+
changes = changes.concat([effectsImport]);
92+
}
93+
8794
const recorder = host.beginUpdate(modulePath);
8895
for (const change of changes) {
8996
if (change instanceof InsertChange) {
@@ -109,22 +116,23 @@ function addNgRxEffectsToPackageJson() {
109116
};
110117
}
111118

112-
export default function(options: RootEffectOptions): Rule {
119+
export default function(options: EffectOptions): Rule {
113120
return (host: Tree, context: SchematicContext) => {
114121
options.path = getProjectPath(host, options);
115122

116123
if (options.module) {
117124
options.module = findModuleFromOptions(host, options);
118125
}
119126

120-
const parsedPath = parseName(options.path, options.name);
127+
const parsedPath = parseName(options.path, options.name || '');
121128
options.name = parsedPath.name;
122129
options.path = parsedPath.path;
123130

124131
const templateSource = apply(url('./files'), [
125132
options.spec
126133
? noop()
127134
: filter(path => !path.endsWith('.spec.ts.template')),
135+
options.minimal ? filter(_ => false) : noop(),
128136
applyTemplates({
129137
...stringUtils,
130138
'if-flat': (s: string) =>

modules/effects/schematics/ng-add/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ export interface Schema {
77
project?: string;
88
module?: string;
99
group?: boolean;
10+
/**
11+
* Setup root effects module without registering initial effects.
12+
*/
13+
minimal?: boolean;
1014
}

0 commit comments

Comments
 (0)