Skip to content

Commit

Permalink
fix(Schematics): Update parsed path logic to split path and name
Browse files Browse the repository at this point in the history
Closes #1064
  • Loading branch information
brandonroberts authored and MikeRyanDev committed May 15, 2018
1 parent d58ad9c commit a1e9530
Show file tree
Hide file tree
Showing 33 changed files with 186 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/effects/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ export const stringUtils = {
};

export { updatePackage } from './utility/update';
export { parseName } from './utility/parse-name';
16 changes: 16 additions & 0 deletions modules/effects/schematics-core/utility/parse-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Path, basename, dirname, normalize } from '@angular-devkit/core';

export interface Location {
name: string;
path: Path;
}

export function parseName(path: string, name: string): Location {
const nameWithoutPath = basename(name as Path);
const namePath = dirname((path + '/' + name) as Path);

return {
name: nameWithoutPath,
path: normalize('/' + namePath),
};
}
1 change: 1 addition & 0 deletions modules/entity/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ export const stringUtils = {
};

export { updatePackage } from './utility/update';
export { parseName } from './utility/parse-name';
16 changes: 16 additions & 0 deletions modules/entity/schematics-core/utility/parse-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Path, basename, dirname, normalize } from '@angular-devkit/core';

export interface Location {
name: string;
path: Path;
}

export function parseName(path: string, name: string): Location {
const nameWithoutPath = basename(name as Path);
const namePath = dirname((path + '/' + name) as Path);

return {
name: nameWithoutPath,
path: normalize('/' + namePath),
};
}
1 change: 1 addition & 0 deletions modules/router-store/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ export const stringUtils = {
};

export { updatePackage } from './utility/update';
export { parseName } from './utility/parse-name';
16 changes: 16 additions & 0 deletions modules/router-store/schematics-core/utility/parse-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Path, basename, dirname, normalize } from '@angular-devkit/core';

export interface Location {
name: string;
path: Path;
}

export function parseName(path: string, name: string): Location {
const nameWithoutPath = basename(name as Path);
const namePath = dirname((path + '/' + name) as Path);

return {
name: nameWithoutPath,
path: normalize('/' + namePath),
};
}
1 change: 1 addition & 0 deletions modules/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ export const stringUtils = {
};

export { updatePackage } from './utility/update';
export { parseName } from './utility/parse-name';
16 changes: 16 additions & 0 deletions modules/schematics-core/utility/parse-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Path, basename, dirname, normalize } from '@angular-devkit/core';

export interface Location {
name: string;
path: Path;
}

export function parseName(path: string, name: string): Location {
const nameWithoutPath = basename(name as Path);
const namePath = dirname((path + '/' + name) as Path);

return {
name: nameWithoutPath,
path: normalize('/' + namePath),
};
}
1 change: 1 addition & 0 deletions modules/schematics/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ export const stringUtils = {
};

export { updatePackage } from './utility/update';
export { parseName } from './utility/parse-name';
16 changes: 16 additions & 0 deletions modules/schematics/schematics-core/utility/parse-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Path, basename, dirname, normalize } from '@angular-devkit/core';

export interface Location {
name: string;
path: Path;
}

export function parseName(path: string, name: string): Location {
const nameWithoutPath = basename(name as Path);
const namePath = dirname((path + '/' + name) as Path);

return {
name: nameWithoutPath,
path: normalize('/' + namePath),
};
}
11 changes: 10 additions & 1 deletion modules/schematics/src/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ import {
SchematicContext,
} from '@angular-devkit/schematics';
import { Schema as ActionOptions } from './schema';
import { getProjectPath, stringUtils } from '@ngrx/schematics/schematics-core';
import {
getProjectPath,
stringUtils,
parseName,
} from '@ngrx/schematics/schematics-core';

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

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({
Expand All @@ -32,6 +40,7 @@ export default function(options: ActionOptions): Rule {
...(options as object),
dot: () => '.',
} as any),
move(parsedPath.path),
]);

return chain([branchAndMerge(chain([mergeWith(templateSource)]))])(
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
InsertChange,
getProjectPath,
omit,
parseName,
} from '@ngrx/schematics/schematics-core';
import { Schema as ContainerOptions } from './schema';

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

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

const opts = ['state', 'stateInterface'].reduce(
(current: Partial<ContainerOptions>, key) => {
return omit(current, key as any);
Expand All @@ -138,6 +143,7 @@ export default function(options: ContainerOptions): Rule {
...(options as object),
dot: () => '.',
} as any),
move(parsedPath.path),
]);

return chain([
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/effect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
findModuleFromOptions,
} from '@ngrx/schematics/schematics-core';
import { Schema as EffectOptions } from './schema';
import { parseName } from '@ngrx/schematics/schematics-core';

function addImportToNgModule(options: EffectOptions): Rule {
return (host: Tree) => {
Expand Down Expand Up @@ -99,6 +100,10 @@ export default function(options: EffectOptions): Rule {
options.module = findModuleFromOptions(host, options);
}

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({
Expand All @@ -111,6 +116,7 @@ export default function(options: EffectOptions): Rule {
...(options as object),
dot: () => '.',
} as any),
move(parsedPath.path),
]);

return chain([
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/entity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ import {
addReducerImportToNgModule,
getProjectPath,
findModuleFromOptions,
parseName,
} from '@ngrx/schematics/schematics-core';
import { Schema as EntityOptions } from './schema';

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

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

if (options.module) {
options.module = findModuleFromOptions(host, options);
}
Expand All @@ -44,6 +49,7 @@ export default function(options: EntityOptions): Rule {
...(options as object),
dot: () => '.',
} as any),
move(parsedPath.path),
]);

return chain([
Expand Down
27 changes: 27 additions & 0 deletions modules/schematics/src/feature/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,31 @@ describe('Feature Schematic', () => {
files.indexOf(`${projectPath}/src/app/effects/foo.effects.spec.ts`)
).toBeGreaterThanOrEqual(0);
});

it('should respect the path provided for the feature name', () => {
const options = {
...defaultOptions,
name: 'foo/Foo',
group: true,
module: 'app',
};

const tree = schematicRunner.runSchematic('feature', options, appTree);
const effectsFileContent = tree.readContent(
`${projectPath}/src/app/foo/effects/foo.effects.ts`
);
const reducerFileContent = tree.readContent(
`${projectPath}/src/app/foo/reducers/foo.reducer.ts`
);
const moduleFileContent = tree.readContent(
`${projectPath}/src/app/app.module.ts`
);

expect(moduleFileContent).toMatch(
/import { FooEffects } from '\.\/foo\/effects\/foo.effects';/
);
expect(moduleFileContent).toMatch(
/import \* as fromFoo from '\.\/foo\/reducers\/foo.reducer';/
);
});
});
6 changes: 6 additions & 0 deletions modules/schematics/src/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
stringUtils,
addReducerToState,
addReducerImportToNgModule,
parseName,
} from '@ngrx/schematics/schematics-core';
import { Schema as ReducerOptions } from './schema';

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

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({
Expand All @@ -43,6 +48,7 @@ export default function(options: ReducerOptions): Rule {
...(options as object),
dot: () => '.',
} as any),
move(parsedPath.path),
]);

return chain([
Expand Down
6 changes: 6 additions & 0 deletions modules/schematics/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getProjectPath,
findModuleFromOptions,
addImportToModule,
parseName,
} from '@ngrx/schematics/schematics-core';
import { Schema as StoreOptions } from './schema';

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

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

const statePath = `/${options.path}/${options.statePath}/index.ts`;
const environmentsPath = buildRelativePath(
statePath,
Expand All @@ -153,6 +158,7 @@ export default function(options: StoreOptions): Rule {
...(options as object),
environmentsPath,
} as any),
move(parsedPath.path),
]);

return chain([
Expand Down
1 change: 1 addition & 0 deletions modules/store-devtools/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ export const stringUtils = {
};

export { updatePackage } from './utility/update';
export { parseName } from './utility/parse-name';
16 changes: 16 additions & 0 deletions modules/store-devtools/schematics-core/utility/parse-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Path, basename, dirname, normalize } from '@angular-devkit/core';

export interface Location {
name: string;
path: Path;
}

export function parseName(path: string, name: string): Location {
const nameWithoutPath = basename(name as Path);
const namePath = dirname((path + '/' + name) as Path);

return {
name: nameWithoutPath,
path: normalize('/' + namePath),
};
}
1 change: 1 addition & 0 deletions modules/store/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ export const stringUtils = {
};

export { updatePackage } from './utility/update';
export { parseName } from './utility/parse-name';
16 changes: 16 additions & 0 deletions modules/store/schematics-core/utility/parse-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Path, basename, dirname, normalize } from '@angular-devkit/core';

export interface Location {
name: string;
path: Path;
}

export function parseName(path: string, name: string): Location {
const nameWithoutPath = basename(name as Path);
const namePath = dirname((path + '/' + name) as Path);

return {
name: nameWithoutPath,
path: normalize('/' + namePath),
};
}

0 comments on commit a1e9530

Please sign in to comment.