Skip to content

Commit d1ed9e5

Browse files
timdeschryverbrandonroberts
authored andcommitted
fix(Schematics): remove ts extension when importing reducer in container (#1061)
Closes #1056
1 parent a1e9530 commit d1ed9e5

File tree

8 files changed

+111
-14
lines changed

8 files changed

+111
-14
lines changed

modules/effects/schematics-core/utility/find-module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string {
9898

9999
// Remove file names (preserving destination)
100100
fromParts.pop();
101-
const toFileName = toParts.pop();
101+
const toFileName = convertToTypeScriptFileName(toParts.pop());
102102

103103
const relativePath = relative(
104104
normalize(fromParts.join('/')),
@@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string {
116116
pathPrefix += '/';
117117
}
118118

119-
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
119+
return toFileName
120+
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121+
: pathPrefix + relativePath;
122+
}
123+
124+
/**
125+
* Strips the typescript extension and clears index filenames
126+
* foo.ts -> foo
127+
* index.ts -> empty
128+
*/
129+
function convertToTypeScriptFileName(filename: string | undefined) {
130+
return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
120131
}

modules/entity/schematics-core/utility/find-module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string {
9898

9999
// Remove file names (preserving destination)
100100
fromParts.pop();
101-
const toFileName = toParts.pop();
101+
const toFileName = convertToTypeScriptFileName(toParts.pop());
102102

103103
const relativePath = relative(
104104
normalize(fromParts.join('/')),
@@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string {
116116
pathPrefix += '/';
117117
}
118118

119-
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
119+
return toFileName
120+
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121+
: pathPrefix + relativePath;
122+
}
123+
124+
/**
125+
* Strips the typescript extension and clears index filenames
126+
* foo.ts -> foo
127+
* index.ts -> empty
128+
*/
129+
function convertToTypeScriptFileName(filename: string | undefined) {
130+
return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
120131
}

modules/router-store/schematics-core/utility/find-module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string {
9898

9999
// Remove file names (preserving destination)
100100
fromParts.pop();
101-
const toFileName = toParts.pop();
101+
const toFileName = convertToTypeScriptFileName(toParts.pop());
102102

103103
const relativePath = relative(
104104
normalize(fromParts.join('/')),
@@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string {
116116
pathPrefix += '/';
117117
}
118118

119-
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
119+
return toFileName
120+
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121+
: pathPrefix + relativePath;
122+
}
123+
124+
/**
125+
* Strips the typescript extension and clears index filenames
126+
* foo.ts -> foo
127+
* index.ts -> empty
128+
*/
129+
function convertToTypeScriptFileName(filename: string | undefined) {
130+
return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
120131
}

modules/schematics-core/utility/find-module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string {
9898

9999
// Remove file names (preserving destination)
100100
fromParts.pop();
101-
const toFileName = toParts.pop();
101+
const toFileName = convertToTypeScriptFileName(toParts.pop());
102102

103103
const relativePath = relative(
104104
normalize(fromParts.join('/')),
@@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string {
116116
pathPrefix += '/';
117117
}
118118

119-
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
119+
return toFileName
120+
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121+
: pathPrefix + relativePath;
122+
}
123+
124+
/**
125+
* Strips the typescript extension and clears index filenames
126+
* foo.ts -> foo
127+
* index.ts -> empty
128+
*/
129+
function convertToTypeScriptFileName(filename: string | undefined) {
130+
return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
120131
}

modules/schematics/schematics-core/utility/find-module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string {
9898

9999
// Remove file names (preserving destination)
100100
fromParts.pop();
101-
const toFileName = toParts.pop();
101+
const toFileName = convertToTypeScriptFileName(toParts.pop());
102102

103103
const relativePath = relative(
104104
normalize(fromParts.join('/')),
@@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string {
116116
pathPrefix += '/';
117117
}
118118

119-
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
119+
return toFileName
120+
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121+
: pathPrefix + relativePath;
122+
}
123+
124+
/**
125+
* Strips the typescript extension and clears index filenames
126+
* foo.ts -> foo
127+
* index.ts -> empty
128+
*/
129+
function convertToTypeScriptFileName(filename: string | undefined) {
130+
return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
120131
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ describe('Container Schematic', () => {
5757
expect(content).toMatch(/import \* as fromStore from '..\/reducers';/);
5858
});
5959

60+
it('should remove .ts from the state path if provided', () => {
61+
const options = { ...defaultOptions, state: 'reducers/foo.ts' };
62+
appTree.create(`${projectPath}/src/app/reducers/foo.ts`, '');
63+
const tree = schematicRunner.runSchematic('container', options, appTree);
64+
const content = tree.readContent(
65+
`${projectPath}/src/app/foo/foo.component.ts`
66+
);
67+
expect(content).toMatch(/import \* as fromStore from '..\/reducers\/foo';/);
68+
});
69+
70+
it('should remove index.ts from the state path if provided', () => {
71+
const options = { ...defaultOptions, state: 'reducers/index.ts' };
72+
appTree.create(`${projectPath}/src/app/reducers/index.ts`, '');
73+
const tree = schematicRunner.runSchematic('container', options, appTree);
74+
const content = tree.readContent(
75+
`${projectPath}/src/app/foo/foo.component.ts`
76+
);
77+
expect(content).toMatch(/import \* as fromStore from '..\/reducers';/);
78+
});
79+
6080
it('should import Store into the component', () => {
6181
const options = { ...defaultOptions, state: 'reducers' };
6282
appTree.create(`${projectPath}/src/app/reducers`, '');

modules/store-devtools/schematics-core/utility/find-module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string {
9898

9999
// Remove file names (preserving destination)
100100
fromParts.pop();
101-
const toFileName = toParts.pop();
101+
const toFileName = convertToTypeScriptFileName(toParts.pop());
102102

103103
const relativePath = relative(
104104
normalize(fromParts.join('/')),
@@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string {
116116
pathPrefix += '/';
117117
}
118118

119-
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
119+
return toFileName
120+
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121+
: pathPrefix + relativePath;
122+
}
123+
124+
/**
125+
* Strips the typescript extension and clears index filenames
126+
* foo.ts -> foo
127+
* index.ts -> empty
128+
*/
129+
function convertToTypeScriptFileName(filename: string | undefined) {
130+
return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
120131
}

modules/store/schematics-core/utility/find-module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function buildRelativePath(from: string, to: string): string {
9898

9999
// Remove file names (preserving destination)
100100
fromParts.pop();
101-
const toFileName = toParts.pop();
101+
const toFileName = convertToTypeScriptFileName(toParts.pop());
102102

103103
const relativePath = relative(
104104
normalize(fromParts.join('/')),
@@ -116,5 +116,16 @@ export function buildRelativePath(from: string, to: string): string {
116116
pathPrefix += '/';
117117
}
118118

119-
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
119+
return toFileName
120+
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121+
: pathPrefix + relativePath;
122+
}
123+
124+
/**
125+
* Strips the typescript extension and clears index filenames
126+
* foo.ts -> foo
127+
* index.ts -> empty
128+
*/
129+
function convertToTypeScriptFileName(filename: string | undefined) {
130+
return filename ? filename.replace(/(\.ts)|(index\.ts)$/, '') : '';
120131
}

0 commit comments

Comments
 (0)