Skip to content

Commit a1e9530

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
fix(Schematics): Update parsed path logic to split path and name
Closes #1064
1 parent d58ad9c commit a1e9530

File tree

33 files changed

+186
-1
lines changed

33 files changed

+186
-1
lines changed

modules/effects/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ export const stringUtils = {
7070
};
7171

7272
export { updatePackage } from './utility/update';
73+
export { parseName } from './utility/parse-name';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Path, basename, dirname, normalize } from '@angular-devkit/core';
2+
3+
export interface Location {
4+
name: string;
5+
path: Path;
6+
}
7+
8+
export function parseName(path: string, name: string): Location {
9+
const nameWithoutPath = basename(name as Path);
10+
const namePath = dirname((path + '/' + name) as Path);
11+
12+
return {
13+
name: nameWithoutPath,
14+
path: normalize('/' + namePath),
15+
};
16+
}

modules/entity/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ export const stringUtils = {
7070
};
7171

7272
export { updatePackage } from './utility/update';
73+
export { parseName } from './utility/parse-name';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Path, basename, dirname, normalize } from '@angular-devkit/core';
2+
3+
export interface Location {
4+
name: string;
5+
path: Path;
6+
}
7+
8+
export function parseName(path: string, name: string): Location {
9+
const nameWithoutPath = basename(name as Path);
10+
const namePath = dirname((path + '/' + name) as Path);
11+
12+
return {
13+
name: nameWithoutPath,
14+
path: normalize('/' + namePath),
15+
};
16+
}

modules/router-store/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ export const stringUtils = {
7070
};
7171

7272
export { updatePackage } from './utility/update';
73+
export { parseName } from './utility/parse-name';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Path, basename, dirname, normalize } from '@angular-devkit/core';
2+
3+
export interface Location {
4+
name: string;
5+
path: Path;
6+
}
7+
8+
export function parseName(path: string, name: string): Location {
9+
const nameWithoutPath = basename(name as Path);
10+
const namePath = dirname((path + '/' + name) as Path);
11+
12+
return {
13+
name: nameWithoutPath,
14+
path: normalize('/' + namePath),
15+
};
16+
}

modules/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ export const stringUtils = {
7070
};
7171

7272
export { updatePackage } from './utility/update';
73+
export { parseName } from './utility/parse-name';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Path, basename, dirname, normalize } from '@angular-devkit/core';
2+
3+
export interface Location {
4+
name: string;
5+
path: Path;
6+
}
7+
8+
export function parseName(path: string, name: string): Location {
9+
const nameWithoutPath = basename(name as Path);
10+
const namePath = dirname((path + '/' + name) as Path);
11+
12+
return {
13+
name: nameWithoutPath,
14+
path: normalize('/' + namePath),
15+
};
16+
}

modules/schematics/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ export const stringUtils = {
7070
};
7171

7272
export { updatePackage } from './utility/update';
73+
export { parseName } from './utility/parse-name';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Path, basename, dirname, normalize } from '@angular-devkit/core';
2+
3+
export interface Location {
4+
name: string;
5+
path: Path;
6+
}
7+
8+
export function parseName(path: string, name: string): Location {
9+
const nameWithoutPath = basename(name as Path);
10+
const namePath = dirname((path + '/' + name) as Path);
11+
12+
return {
13+
name: nameWithoutPath,
14+
path: normalize('/' + namePath),
15+
};
16+
}

modules/schematics/src/action/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ import {
1414
SchematicContext,
1515
} from '@angular-devkit/schematics';
1616
import { Schema as ActionOptions } from './schema';
17-
import { getProjectPath, stringUtils } from '@ngrx/schematics/schematics-core';
17+
import {
18+
getProjectPath,
19+
stringUtils,
20+
parseName,
21+
} from '@ngrx/schematics/schematics-core';
1822

1923
export default function(options: ActionOptions): Rule {
2024
return (host: Tree, context: SchematicContext) => {
2125
options.path = getProjectPath(host, options);
2226

27+
const parsedPath = parseName(options.path, options.name);
28+
options.name = parsedPath.name;
29+
options.path = parsedPath.path;
30+
2331
const templateSource = apply(url('./files'), [
2432
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
2533
template({
@@ -32,6 +40,7 @@ export default function(options: ActionOptions): Rule {
3240
...(options as object),
3341
dot: () => '.',
3442
} as any),
43+
move(parsedPath.path),
3544
]);
3645

3746
return chain([branchAndMerge(chain([mergeWith(templateSource)]))])(

modules/schematics/src/container/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
InsertChange,
2424
getProjectPath,
2525
omit,
26+
parseName,
2627
} from '@ngrx/schematics/schematics-core';
2728
import { Schema as ContainerOptions } from './schema';
2829

@@ -123,6 +124,10 @@ export default function(options: ContainerOptions): Rule {
123124
return (host: Tree, context: SchematicContext) => {
124125
options.path = getProjectPath(host, options);
125126

127+
const parsedPath = parseName(options.path, options.name);
128+
options.name = parsedPath.name;
129+
options.path = parsedPath.path;
130+
126131
const opts = ['state', 'stateInterface'].reduce(
127132
(current: Partial<ContainerOptions>, key) => {
128133
return omit(current, key as any);
@@ -138,6 +143,7 @@ export default function(options: ContainerOptions): Rule {
138143
...(options as object),
139144
dot: () => '.',
140145
} as any),
146+
move(parsedPath.path),
141147
]);
142148

143149
return chain([

modules/schematics/src/effect/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
findModuleFromOptions,
2525
} from '@ngrx/schematics/schematics-core';
2626
import { Schema as EffectOptions } from './schema';
27+
import { parseName } from '@ngrx/schematics/schematics-core';
2728

2829
function addImportToNgModule(options: EffectOptions): Rule {
2930
return (host: Tree) => {
@@ -99,6 +100,10 @@ export default function(options: EffectOptions): Rule {
99100
options.module = findModuleFromOptions(host, options);
100101
}
101102

103+
const parsedPath = parseName(options.path, options.name);
104+
options.name = parsedPath.name;
105+
options.path = parsedPath.path;
106+
102107
const templateSource = apply(url('./files'), [
103108
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
104109
template({
@@ -111,6 +116,7 @@ export default function(options: EffectOptions): Rule {
111116
...(options as object),
112117
dot: () => '.',
113118
} as any),
119+
move(parsedPath.path),
114120
]);
115121

116122
return chain([

modules/schematics/src/entity/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ import {
1919
addReducerImportToNgModule,
2020
getProjectPath,
2121
findModuleFromOptions,
22+
parseName,
2223
} from '@ngrx/schematics/schematics-core';
2324
import { Schema as EntityOptions } from './schema';
2425

2526
export default function(options: EntityOptions): Rule {
2627
return (host: Tree, context: SchematicContext) => {
2728
options.path = getProjectPath(host, options);
2829

30+
const parsedPath = parseName(options.path, options.name);
31+
options.name = parsedPath.name;
32+
options.path = parsedPath.path;
33+
2934
if (options.module) {
3035
options.module = findModuleFromOptions(host, options);
3136
}
@@ -44,6 +49,7 @@ export default function(options: EntityOptions): Rule {
4449
...(options as object),
4550
dot: () => '.',
4651
} as any),
52+
move(parsedPath.path),
4753
]);
4854

4955
return chain([

modules/schematics/src/feature/index.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,31 @@ describe('Feature Schematic', () => {
7575
files.indexOf(`${projectPath}/src/app/effects/foo.effects.spec.ts`)
7676
).toBeGreaterThanOrEqual(0);
7777
});
78+
79+
it('should respect the path provided for the feature name', () => {
80+
const options = {
81+
...defaultOptions,
82+
name: 'foo/Foo',
83+
group: true,
84+
module: 'app',
85+
};
86+
87+
const tree = schematicRunner.runSchematic('feature', options, appTree);
88+
const effectsFileContent = tree.readContent(
89+
`${projectPath}/src/app/foo/effects/foo.effects.ts`
90+
);
91+
const reducerFileContent = tree.readContent(
92+
`${projectPath}/src/app/foo/reducers/foo.reducer.ts`
93+
);
94+
const moduleFileContent = tree.readContent(
95+
`${projectPath}/src/app/app.module.ts`
96+
);
97+
98+
expect(moduleFileContent).toMatch(
99+
/import { FooEffects } from '\.\/foo\/effects\/foo.effects';/
100+
);
101+
expect(moduleFileContent).toMatch(
102+
/import \* as fromFoo from '\.\/foo\/reducers\/foo.reducer';/
103+
);
104+
});
78105
});

modules/schematics/src/reducer/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
stringUtils,
2121
addReducerToState,
2222
addReducerImportToNgModule,
23+
parseName,
2324
} from '@ngrx/schematics/schematics-core';
2425
import { Schema as ReducerOptions } from './schema';
2526

@@ -31,6 +32,10 @@ export default function(options: ReducerOptions): Rule {
3132
options.module = findModuleFromOptions(host, options);
3233
}
3334

35+
const parsedPath = parseName(options.path, options.name);
36+
options.name = parsedPath.name;
37+
options.path = parsedPath.path;
38+
3439
const templateSource = apply(url('./files'), [
3540
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
3641
template({
@@ -43,6 +48,7 @@ export default function(options: ReducerOptions): Rule {
4348
...(options as object),
4449
dot: () => '.',
4550
} as any),
51+
move(parsedPath.path),
4652
]);
4753

4854
return chain([

modules/schematics/src/store/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
getProjectPath,
2323
findModuleFromOptions,
2424
addImportToModule,
25+
parseName,
2526
} from '@ngrx/schematics/schematics-core';
2627
import { Schema as StoreOptions } from './schema';
2728

@@ -129,6 +130,10 @@ export default function(options: StoreOptions): Rule {
129130
return (host: Tree, context: SchematicContext) => {
130131
options.path = getProjectPath(host, options);
131132

133+
const parsedPath = parseName(options.path, options.name);
134+
options.name = parsedPath.name;
135+
options.path = parsedPath.path;
136+
132137
const statePath = `/${options.path}/${options.statePath}/index.ts`;
133138
const environmentsPath = buildRelativePath(
134139
statePath,
@@ -153,6 +158,7 @@ export default function(options: StoreOptions): Rule {
153158
...(options as object),
154159
environmentsPath,
155160
} as any),
161+
move(parsedPath.path),
156162
]);
157163

158164
return chain([

modules/store-devtools/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ export const stringUtils = {
7070
};
7171

7272
export { updatePackage } from './utility/update';
73+
export { parseName } from './utility/parse-name';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Path, basename, dirname, normalize } from '@angular-devkit/core';
2+
3+
export interface Location {
4+
name: string;
5+
path: Path;
6+
}
7+
8+
export function parseName(path: string, name: string): Location {
9+
const nameWithoutPath = basename(name as Path);
10+
const namePath = dirname((path + '/' + name) as Path);
11+
12+
return {
13+
name: nameWithoutPath,
14+
path: normalize('/' + namePath),
15+
};
16+
}

modules/store/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ export const stringUtils = {
7070
};
7171

7272
export { updatePackage } from './utility/update';
73+
export { parseName } from './utility/parse-name';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Path, basename, dirname, normalize } from '@angular-devkit/core';
2+
3+
export interface Location {
4+
name: string;
5+
path: Path;
6+
}
7+
8+
export function parseName(path: string, name: string): Location {
9+
const nameWithoutPath = basename(name as Path);
10+
const namePath = dirname((path + '/' + name) as Path);
11+
12+
return {
13+
name: nameWithoutPath,
14+
path: normalize('/' + namePath),
15+
};
16+
}

0 commit comments

Comments
 (0)