From d1ed9e508f6280b2358f9be9a1ab513385e4b284 Mon Sep 17 00:00:00 2001 From: Tim Deschryver Date: Tue, 15 May 2018 05:12:49 +0200 Subject: [PATCH] fix(Schematics): remove ts extension when importing reducer in container (#1061) Closes #1056 --- .../schematics-core/utility/find-module.ts | 15 ++++++++++++-- .../schematics-core/utility/find-module.ts | 15 ++++++++++++-- .../schematics-core/utility/find-module.ts | 15 ++++++++++++-- .../schematics-core/utility/find-module.ts | 15 ++++++++++++-- .../schematics-core/utility/find-module.ts | 15 ++++++++++++-- .../schematics/src/container/index.spec.ts | 20 +++++++++++++++++++ .../schematics-core/utility/find-module.ts | 15 ++++++++++++-- .../schematics-core/utility/find-module.ts | 15 ++++++++++++-- 8 files changed, 111 insertions(+), 14 deletions(-) diff --git a/modules/effects/schematics-core/utility/find-module.ts b/modules/effects/schematics-core/utility/find-module.ts index 7494583b3e..ffb09be271 100644 --- a/modules/effects/schematics-core/utility/find-module.ts +++ b/modules/effects/schematics-core/utility/find-module.ts @@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string { // Remove file names (preserving destination) fromParts.pop(); - const toFileName = toParts.pop(); + const toFileName = convertToTypeScriptFileName(toParts.pop()); const relativePath = relative( normalize(fromParts.join('/')), @@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string { pathPrefix += '/'; } - return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName; + return toFileName + ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName + : pathPrefix + relativePath; +} + +/** + * Strips the typescript extension and clears index filenames + * foo.ts -> foo + * index.ts -> empty + */ +function convertToTypeScriptFileName(filename: string | undefined) { + return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : ''; } diff --git a/modules/entity/schematics-core/utility/find-module.ts b/modules/entity/schematics-core/utility/find-module.ts index 7494583b3e..ffb09be271 100644 --- a/modules/entity/schematics-core/utility/find-module.ts +++ b/modules/entity/schematics-core/utility/find-module.ts @@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string { // Remove file names (preserving destination) fromParts.pop(); - const toFileName = toParts.pop(); + const toFileName = convertToTypeScriptFileName(toParts.pop()); const relativePath = relative( normalize(fromParts.join('/')), @@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string { pathPrefix += '/'; } - return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName; + return toFileName + ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName + : pathPrefix + relativePath; +} + +/** + * Strips the typescript extension and clears index filenames + * foo.ts -> foo + * index.ts -> empty + */ +function convertToTypeScriptFileName(filename: string | undefined) { + return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : ''; } diff --git a/modules/router-store/schematics-core/utility/find-module.ts b/modules/router-store/schematics-core/utility/find-module.ts index 7494583b3e..ffb09be271 100644 --- a/modules/router-store/schematics-core/utility/find-module.ts +++ b/modules/router-store/schematics-core/utility/find-module.ts @@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string { // Remove file names (preserving destination) fromParts.pop(); - const toFileName = toParts.pop(); + const toFileName = convertToTypeScriptFileName(toParts.pop()); const relativePath = relative( normalize(fromParts.join('/')), @@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string { pathPrefix += '/'; } - return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName; + return toFileName + ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName + : pathPrefix + relativePath; +} + +/** + * Strips the typescript extension and clears index filenames + * foo.ts -> foo + * index.ts -> empty + */ +function convertToTypeScriptFileName(filename: string | undefined) { + return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : ''; } diff --git a/modules/schematics-core/utility/find-module.ts b/modules/schematics-core/utility/find-module.ts index 7494583b3e..ffb09be271 100644 --- a/modules/schematics-core/utility/find-module.ts +++ b/modules/schematics-core/utility/find-module.ts @@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string { // Remove file names (preserving destination) fromParts.pop(); - const toFileName = toParts.pop(); + const toFileName = convertToTypeScriptFileName(toParts.pop()); const relativePath = relative( normalize(fromParts.join('/')), @@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string { pathPrefix += '/'; } - return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName; + return toFileName + ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName + : pathPrefix + relativePath; +} + +/** + * Strips the typescript extension and clears index filenames + * foo.ts -> foo + * index.ts -> empty + */ +function convertToTypeScriptFileName(filename: string | undefined) { + return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : ''; } diff --git a/modules/schematics/schematics-core/utility/find-module.ts b/modules/schematics/schematics-core/utility/find-module.ts index 7494583b3e..ffb09be271 100644 --- a/modules/schematics/schematics-core/utility/find-module.ts +++ b/modules/schematics/schematics-core/utility/find-module.ts @@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string { // Remove file names (preserving destination) fromParts.pop(); - const toFileName = toParts.pop(); + const toFileName = convertToTypeScriptFileName(toParts.pop()); const relativePath = relative( normalize(fromParts.join('/')), @@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string { pathPrefix += '/'; } - return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName; + return toFileName + ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName + : pathPrefix + relativePath; +} + +/** + * Strips the typescript extension and clears index filenames + * foo.ts -> foo + * index.ts -> empty + */ +function convertToTypeScriptFileName(filename: string | undefined) { + return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : ''; } diff --git a/modules/schematics/src/container/index.spec.ts b/modules/schematics/src/container/index.spec.ts index 712b3ce0a4..dede70314f 100644 --- a/modules/schematics/src/container/index.spec.ts +++ b/modules/schematics/src/container/index.spec.ts @@ -57,6 +57,26 @@ describe('Container Schematic', () => { expect(content).toMatch(/import \* as fromStore from '..\/reducers';/); }); + it('should remove .ts from the state path if provided', () => { + const options = { ...defaultOptions, state: 'reducers/foo.ts' }; + appTree.create(`${projectPath}/src/app/reducers/foo.ts`, ''); + const tree = schematicRunner.runSchematic('container', options, appTree); + const content = tree.readContent( + `${projectPath}/src/app/foo/foo.component.ts` + ); + expect(content).toMatch(/import \* as fromStore from '..\/reducers\/foo';/); + }); + + it('should remove index.ts from the state path if provided', () => { + const options = { ...defaultOptions, state: 'reducers/index.ts' }; + appTree.create(`${projectPath}/src/app/reducers/index.ts`, ''); + const tree = schematicRunner.runSchematic('container', options, appTree); + const content = tree.readContent( + `${projectPath}/src/app/foo/foo.component.ts` + ); + expect(content).toMatch(/import \* as fromStore from '..\/reducers';/); + }); + it('should import Store into the component', () => { const options = { ...defaultOptions, state: 'reducers' }; appTree.create(`${projectPath}/src/app/reducers`, ''); diff --git a/modules/store-devtools/schematics-core/utility/find-module.ts b/modules/store-devtools/schematics-core/utility/find-module.ts index 7494583b3e..ffb09be271 100644 --- a/modules/store-devtools/schematics-core/utility/find-module.ts +++ b/modules/store-devtools/schematics-core/utility/find-module.ts @@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string { // Remove file names (preserving destination) fromParts.pop(); - const toFileName = toParts.pop(); + const toFileName = convertToTypeScriptFileName(toParts.pop()); const relativePath = relative( normalize(fromParts.join('/')), @@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string { pathPrefix += '/'; } - return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName; + return toFileName + ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName + : pathPrefix + relativePath; +} + +/** + * Strips the typescript extension and clears index filenames + * foo.ts -> foo + * index.ts -> empty + */ +function convertToTypeScriptFileName(filename: string | undefined) { + return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : ''; } diff --git a/modules/store/schematics-core/utility/find-module.ts b/modules/store/schematics-core/utility/find-module.ts index 7494583b3e..ffb09be271 100644 --- a/modules/store/schematics-core/utility/find-module.ts +++ b/modules/store/schematics-core/utility/find-module.ts @@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string { // Remove file names (preserving destination) fromParts.pop(); - const toFileName = toParts.pop(); + const toFileName = convertToTypeScriptFileName(toParts.pop()); const relativePath = relative( normalize(fromParts.join('/')), @@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string { pathPrefix += '/'; } - return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName; + return toFileName + ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName + : pathPrefix + relativePath; +} + +/** + * Strips the typescript extension and clears index filenames + * foo.ts -> foo + * index.ts -> empty + */ +function convertToTypeScriptFileName(filename: string | undefined) { + return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : ''; }