diff --git a/e2e/addngrx.test.ts b/e2e/addngrx.test.ts index bd57041a8d..dfccd7d4b8 100644 --- a/e2e/addngrx.test.ts +++ b/e2e/addngrx.test.ts @@ -8,7 +8,7 @@ describe('addNgRxToModule', () => { it('should add root configuration', () => { newApp('new proj --skipInstall'); - runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --root', {cwd: 'proj'}); + runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --root', {projectName: 'proj'}); checkFilesExists( `proj/src/app/+state/app.actions.ts`, @@ -26,14 +26,14 @@ describe('addNgRxToModule', () => { addNgRx('proj'); - runCLI('build', {cwd: 'proj'}); - runCLI('test --single-run', {cwd: 'proj'}); + runCLI('build', {projectName: 'proj'}); + runCLI('test --single-run', {projectName: 'proj'}); }, 50000); it('should add empty root configuration', () => { newApp('new proj2 --skipInstall'); - runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --emptyRoot', {cwd: 'proj2'}); + runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --emptyRoot', {projectName: 'proj2'}); const contents = readFile('proj2/src/app/app.module.ts'); expect(contents).toContain('StoreModule.forRoot'); @@ -41,12 +41,12 @@ describe('addNgRxToModule', () => { addNgRx('proj2'); - runCLI('build', {cwd: 'proj2'}); + runCLI('build', {projectName: 'proj2'}); }, 50000); it('should add feature configuration', () => { newApp('new proj3 --skipInstall'); - runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts', {cwd: 'proj3'}); + runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts', {projectName: 'proj3'}); checkFilesExists( `proj3/src/app/+state/app.actions.ts`, @@ -65,7 +65,7 @@ describe('addNgRxToModule', () => { it('should generate files without importing them', () => { newApp('new proj4 --skipInstall'); - runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --skipImport', {cwd: 'proj4'}); + runSchematic('@nrwl/nx:addNgRxToModule --module=src/app/app.module.ts --skipImport', {projectName: 'proj4'}); checkFilesExists( `proj4/src/app/+state/app.actions.ts`, diff --git a/e2e/utils.ts b/e2e/utils.ts index 3f4dffad8f..1fe1f9d1fa 100644 --- a/e2e/utils.ts +++ b/e2e/utils.ts @@ -5,17 +5,18 @@ import {readFileSync, statSync, writeFileSync} from 'fs'; export function newApp(command: string): string { return execSync(`../node_modules/.bin/ng ${command}`, {cwd: `./tmp`}).toString(); } -export function runCLI(command: string, {cwd}: {cwd: string}): string { - cwd = cwd === undefined ? '' : cwd; - return execSync(`../../node_modules/.bin/ng ${command}`, {cwd: `./tmp/${cwd}`}).toString(); +export function runCLI(command: string, {projectName: projectName}: {projectName: string}): string { + projectName = projectName === undefined ? '' : projectName; + return execSync(`../../node_modules/.bin/ng ${command}`, {cwd: `./tmp/${projectName}`}).toString(); } -export function runSchematic(command: string, {cwd}: {cwd: string}): string { - cwd = cwd === undefined ? '' : cwd; - return execSync(`../../node_modules/.bin/schematics ${command}`, {cwd: `./tmp/${cwd}`}).toString(); +export function runSchematic(command: string, {projectName}: {projectName?: string} = {}): string { + const up = projectName ? '../' : ''; + projectName = projectName === undefined ? '' : projectName; + return execSync(`../${up}node_modules/.bin/schematics ${command}`, {cwd: `./tmp/${projectName}`}).toString(); } -export function runCommand(command: string, {cwd}: {cwd: string}): string { - cwd = cwd === undefined ? '' : cwd; - return execSync(command, {cwd: `./tmp/${cwd}`}).toString(); +export function runCommand(command: string, {projectName}: {projectName: string}): string { + projectName = projectName === undefined ? '' : projectName; + return execSync(command, {cwd: `./tmp/${projectName}`}).toString(); } export function updateFile(f: string, content: string): void { @@ -70,5 +71,5 @@ export function addNgRx(path: string): string { p['dependencies']['@ngrx/effects'] = '4.0.2'; p['dependencies']['jasmine-marbles'] = '0.1.0'; updateFile(`${path}/package.json`, JSON.stringify(p, null, 2)); - return runCommand('npm install', {cwd: path}); + return runCommand('npm install', {projectName: path}); } diff --git a/src/schematics/addNgRxToModule/index.ts b/src/schematics/addNgRxToModule/index.ts index 6dc7a3f74d..912acb77ce 100644 --- a/src/schematics/addNgRxToModule/index.ts +++ b/src/schematics/addNgRxToModule/index.ts @@ -3,9 +3,8 @@ import {apply, branchAndMerge, chain, mergeWith, move, Rule, template, Tree, url import {names, toClassName, toFileName, toPropertyName} from "../name-utils"; import * as path from 'path'; import * as ts from 'typescript'; -import {addImportToModule, addProviderToModule} from '../utility/ast-utils'; -import {InsertChange} from '../utility/change'; -import {insertImport} from '../utility/route-utils'; +import {addImportToModule, addProviderToModule, insert} from '../utility/ast-utils'; +import {insertImport} from '@schematics/angular/utility/route-utils'; function addImportsToModule(name: string, options: any): Rule { return (host: Tree) => { @@ -24,17 +23,10 @@ function addImportsToModule(name: string, options: any): Rule { if (options.emptyRoot) { const reducer = `StoreModule.forRoot({})`; - const changes = [ + insert(host, modulePath, [ insertImport(source, modulePath, 'StoreModule', '@ngrx/store'), ...addImportToModule(source, modulePath, reducer) - ]; - const declarationRecorder = host.beginUpdate(modulePath); - for (const change of changes) { - if (change instanceof InsertChange) { - declarationRecorder.insertLeft(change.pos, change.toAdd); - } - } - host.commitUpdate(declarationRecorder); + ]); return host; } else { @@ -49,7 +41,7 @@ function addImportsToModule(name: string, options: any): Rule { const effects = options.root ? `EffectsModule.forRoot([${effectsName}])` : `EffectsModule.forFeature([${effectsName}])`; const reducer = options.root ? `StoreModule.forRoot(${reducerName}, {initialState: ${initName}})` : `StoreModule.forFeature('${toPropertyName(name)}', ${reducerName}, {initialState: ${initName}})`; - const changes = [ + insert(host, modulePath, [ insertImport(source, modulePath, 'StoreModule', '@ngrx/store'), insertImport(source, modulePath, 'EffectsModule', '@ngrx/effects'), insertImport(source, modulePath, reducerName, reducerPath), @@ -58,15 +50,7 @@ function addImportsToModule(name: string, options: any): Rule { ...addImportToModule(source, modulePath, reducer), ...addImportToModule(source, modulePath, effects), ...addProviderToModule(source, modulePath, effectsName) - ]; - const declarationRecorder = host.beginUpdate(modulePath); - for (const change of changes) { - if (change instanceof InsertChange) { - declarationRecorder.insertLeft(change.pos, change.toAdd); - } - } - - host.commitUpdate(declarationRecorder); + ]); return host; } };