refactor: update addNgRxToModule schematic to use utilities provided by angular schematics
This commit is contained in:
parent
085c250eb5
commit
2897cd2ba5
@ -8,7 +8,7 @@ describe('addNgRxToModule', () => {
|
|||||||
|
|
||||||
it('should add root configuration', () => {
|
it('should add root configuration', () => {
|
||||||
newApp('new proj --skipInstall');
|
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(
|
checkFilesExists(
|
||||||
`proj/src/app/+state/app.actions.ts`,
|
`proj/src/app/+state/app.actions.ts`,
|
||||||
@ -26,14 +26,14 @@ describe('addNgRxToModule', () => {
|
|||||||
|
|
||||||
addNgRx('proj');
|
addNgRx('proj');
|
||||||
|
|
||||||
runCLI('build', {cwd: 'proj'});
|
runCLI('build', {projectName: 'proj'});
|
||||||
runCLI('test --single-run', {cwd: 'proj'});
|
runCLI('test --single-run', {projectName: 'proj'});
|
||||||
|
|
||||||
}, 50000);
|
}, 50000);
|
||||||
|
|
||||||
it('should add empty root configuration', () => {
|
it('should add empty root configuration', () => {
|
||||||
newApp('new proj2 --skipInstall');
|
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');
|
const contents = readFile('proj2/src/app/app.module.ts');
|
||||||
expect(contents).toContain('StoreModule.forRoot');
|
expect(contents).toContain('StoreModule.forRoot');
|
||||||
@ -41,12 +41,12 @@ describe('addNgRxToModule', () => {
|
|||||||
|
|
||||||
addNgRx('proj2');
|
addNgRx('proj2');
|
||||||
|
|
||||||
runCLI('build', {cwd: 'proj2'});
|
runCLI('build', {projectName: 'proj2'});
|
||||||
}, 50000);
|
}, 50000);
|
||||||
|
|
||||||
it('should add feature configuration', () => {
|
it('should add feature configuration', () => {
|
||||||
newApp('new proj3 --skipInstall');
|
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(
|
checkFilesExists(
|
||||||
`proj3/src/app/+state/app.actions.ts`,
|
`proj3/src/app/+state/app.actions.ts`,
|
||||||
@ -65,7 +65,7 @@ describe('addNgRxToModule', () => {
|
|||||||
|
|
||||||
it('should generate files without importing them', () => {
|
it('should generate files without importing them', () => {
|
||||||
newApp('new proj4 --skipInstall');
|
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(
|
checkFilesExists(
|
||||||
`proj4/src/app/+state/app.actions.ts`,
|
`proj4/src/app/+state/app.actions.ts`,
|
||||||
|
|||||||
21
e2e/utils.ts
21
e2e/utils.ts
@ -5,17 +5,18 @@ import {readFileSync, statSync, writeFileSync} from 'fs';
|
|||||||
export function newApp(command: string): string {
|
export function newApp(command: string): string {
|
||||||
return execSync(`../node_modules/.bin/ng ${command}`, {cwd: `./tmp`}).toString();
|
return execSync(`../node_modules/.bin/ng ${command}`, {cwd: `./tmp`}).toString();
|
||||||
}
|
}
|
||||||
export function runCLI(command: string, {cwd}: {cwd: string}): string {
|
export function runCLI(command: string, {projectName: projectName}: {projectName: string}): string {
|
||||||
cwd = cwd === undefined ? '' : cwd;
|
projectName = projectName === undefined ? '' : projectName;
|
||||||
return execSync(`../../node_modules/.bin/ng ${command}`, {cwd: `./tmp/${cwd}`}).toString();
|
return execSync(`../../node_modules/.bin/ng ${command}`, {cwd: `./tmp/${projectName}`}).toString();
|
||||||
}
|
}
|
||||||
export function runSchematic(command: string, {cwd}: {cwd: string}): string {
|
export function runSchematic(command: string, {projectName}: {projectName?: string} = {}): string {
|
||||||
cwd = cwd === undefined ? '' : cwd;
|
const up = projectName ? '../' : '';
|
||||||
return execSync(`../../node_modules/.bin/schematics ${command}`, {cwd: `./tmp/${cwd}`}).toString();
|
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 {
|
export function runCommand(command: string, {projectName}: {projectName: string}): string {
|
||||||
cwd = cwd === undefined ? '' : cwd;
|
projectName = projectName === undefined ? '' : projectName;
|
||||||
return execSync(command, {cwd: `./tmp/${cwd}`}).toString();
|
return execSync(command, {cwd: `./tmp/${projectName}`}).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateFile(f: string, content: string): void {
|
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']['@ngrx/effects'] = '4.0.2';
|
||||||
p['dependencies']['jasmine-marbles'] = '0.1.0';
|
p['dependencies']['jasmine-marbles'] = '0.1.0';
|
||||||
updateFile(`${path}/package.json`, JSON.stringify(p, null, 2));
|
updateFile(`${path}/package.json`, JSON.stringify(p, null, 2));
|
||||||
return runCommand('npm install', {cwd: path});
|
return runCommand('npm install', {projectName: path});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,9 +3,8 @@ import {apply, branchAndMerge, chain, mergeWith, move, Rule, template, Tree, url
|
|||||||
import {names, toClassName, toFileName, toPropertyName} from "../name-utils";
|
import {names, toClassName, toFileName, toPropertyName} from "../name-utils";
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
import {addImportToModule, addProviderToModule} from '../utility/ast-utils';
|
import {addImportToModule, addProviderToModule, insert} from '../utility/ast-utils';
|
||||||
import {InsertChange} from '../utility/change';
|
import {insertImport} from '@schematics/angular/utility/route-utils';
|
||||||
import {insertImport} from '../utility/route-utils';
|
|
||||||
|
|
||||||
function addImportsToModule(name: string, options: any): Rule {
|
function addImportsToModule(name: string, options: any): Rule {
|
||||||
return (host: Tree) => {
|
return (host: Tree) => {
|
||||||
@ -24,17 +23,10 @@ function addImportsToModule(name: string, options: any): Rule {
|
|||||||
|
|
||||||
if (options.emptyRoot) {
|
if (options.emptyRoot) {
|
||||||
const reducer = `StoreModule.forRoot({})`;
|
const reducer = `StoreModule.forRoot({})`;
|
||||||
const changes = [
|
insert(host, modulePath, [
|
||||||
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
|
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
|
||||||
...addImportToModule(source, modulePath, reducer)
|
...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;
|
return host;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -49,7 +41,7 @@ function addImportsToModule(name: string, options: any): Rule {
|
|||||||
const effects = options.root ? `EffectsModule.forRoot([${effectsName}])` : `EffectsModule.forFeature([${effectsName}])`;
|
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 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, 'StoreModule', '@ngrx/store'),
|
||||||
insertImport(source, modulePath, 'EffectsModule', '@ngrx/effects'),
|
insertImport(source, modulePath, 'EffectsModule', '@ngrx/effects'),
|
||||||
insertImport(source, modulePath, reducerName, reducerPath),
|
insertImport(source, modulePath, reducerName, reducerPath),
|
||||||
@ -58,15 +50,7 @@ function addImportsToModule(name: string, options: any): Rule {
|
|||||||
...addImportToModule(source, modulePath, reducer),
|
...addImportToModule(source, modulePath, reducer),
|
||||||
...addImportToModule(source, modulePath, effects),
|
...addImportToModule(source, modulePath, effects),
|
||||||
...addProviderToModule(source, modulePath, effectsName)
|
...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;
|
return host;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user