Skip to content

Commit

Permalink
feat(effects): add support for minimal setup option for ng-add
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and wesleygrimes committed Jul 9, 2019
1 parent c09ba19 commit e839568
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { Actions, createEffect } from '@ngrx/effects';

@Injectable()
export class <%= classify(name) %>Effects {
Expand Down
30 changes: 29 additions & 1 deletion modules/effects/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
createAppModuleWithEffects,
} from '../../../schematics-core/testing';

describe('Effect ng-add Schematic', () => {
describe('Effects ng-add Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@ngrx/effects',
path.join(__dirname, '../collection.json')
Expand Down Expand Up @@ -66,6 +66,34 @@ describe('Effect ng-add Schematic', () => {
).toBeGreaterThanOrEqual(0);
});

it('should not create an effect if the minimal flag is provided', () => {
const options = { ...defaultOptions, minimal: true };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const files = tree.files;
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);

expect(content).toMatch(/EffectsModule\.forRoot\(\[\]\)/);
expect(
files.indexOf(`${projectPath}/src/app/foo/foo.effects.spec.ts`)
).toBe(-1);
expect(files.indexOf(`${projectPath}/src/app/foo/foo.effects.ts`)).toBe(-1);
});

it('should not import an effect into a specified module in the minimal flag is provided', () => {
const options = {
...defaultOptions,
minimal: true,
module: 'app.module.ts',
};

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
expect(content).not.toMatch(
/import { FooEffects } from '.\/foo\/foo.effects'/
);
});

it('should be provided by default', () => {
const options = { ...defaultOptions };

Expand Down
40 changes: 24 additions & 16 deletions modules/effects/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,25 @@ import {
mergeWith,
move,
noop,
template,
url,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import * as ts from 'typescript';
import {
stringUtils,
insertImport,
buildRelativePath,
addImportToModule,
InsertChange,
getProjectPath,
addImportToModule,
buildRelativePath,
findModuleFromOptions,
getProjectPath,
insertImport,
parseName,
stringUtils,
addPackageToPackageJson,
platformVersion,
parseName,
} from '@ngrx/effects/schematics-core';
import { Schema as RootEffectOptions } from './schema';
import * as ts from 'typescript';
import { Schema as EffectOptions } from './schema';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';

function addImportToNgModule(options: RootEffectOptions): Rule {
function addImportToNgModule(options: EffectOptions): Rule {
return (host: Tree) => {
const modulePath = options.module;

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

if (!host.exists(modulePath)) {
throw new Error('Specified module does not exist');
throw new Error(`Specified module path ${modulePath} does not exist`);
}

const text = host.read(modulePath);
Expand Down Expand Up @@ -77,13 +76,21 @@ function addImportToNgModule(options: RootEffectOptions): Rule {
effectsName,
relativePath
);

const effectsSetup = options.minimal ? `[]` : `[${effectsName}]`;
const [effectsNgModuleImport] = addImportToModule(
source,
modulePath,
`EffectsModule.forRoot([${effectsName}])`,
`EffectsModule.forRoot(${effectsSetup})`,
relativePath
);
const changes = [effectsModuleImport, effectsImport, effectsNgModuleImport];

let changes = [effectsModuleImport, effectsNgModuleImport];

if (!options.minimal) {
changes = changes.concat([effectsImport]);
}

const recorder = host.beginUpdate(modulePath);
for (const change of changes) {
if (change instanceof InsertChange) {
Expand All @@ -109,22 +116,23 @@ function addNgRxEffectsToPackageJson() {
};
}

export default function(options: RootEffectOptions): Rule {
export default function(options: EffectOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);

if (options.module) {
options.module = findModuleFromOptions(host, options);
}

const parsedPath = parseName(options.path, options.name);
const parsedPath = parseName(options.path, options.name || '');
options.name = parsedPath.name;
options.path = parsedPath.path;

const templateSource = apply(url('./files'), [
options.spec
? noop()
: filter(path => !path.endsWith('.spec.ts.template')),
options.minimal ? filter(_ => false) : noop(),
applyTemplates({
...stringUtils,
'if-flat': (s: string) =>
Expand Down
4 changes: 4 additions & 0 deletions modules/effects/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ export interface Schema {
project?: string;
module?: string;
group?: boolean;
/**
* Setup root effects module without registering initial effects.
*/
minimal?: boolean;
}

0 comments on commit e839568

Please sign in to comment.