Skip to content

Commit

Permalink
fix(Schematics): remove ts extension when importing reducer in contai…
Browse files Browse the repository at this point in the history
…ner (#1061)

Closes #1056
  • Loading branch information
timdeschryver authored and brandonroberts committed May 15, 2018
1 parent a1e9530 commit d1ed9e5
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 14 deletions.
15 changes: 13 additions & 2 deletions modules/effects/schematics-core/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')),
Expand All @@ -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)$/, '') : '';
}
15 changes: 13 additions & 2 deletions modules/entity/schematics-core/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')),
Expand All @@ -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)$/, '') : '';
}
15 changes: 13 additions & 2 deletions modules/router-store/schematics-core/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')),
Expand All @@ -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)$/, '') : '';
}
15 changes: 13 additions & 2 deletions modules/schematics-core/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')),
Expand All @@ -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)$/, '') : '';
}
15 changes: 13 additions & 2 deletions modules/schematics/schematics-core/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')),
Expand All @@ -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)$/, '') : '';
}
20 changes: 20 additions & 0 deletions modules/schematics/src/container/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`, '');
Expand Down
15 changes: 13 additions & 2 deletions modules/store-devtools/schematics-core/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')),
Expand All @@ -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)$/, '') : '';
}
15 changes: 13 additions & 2 deletions modules/store/schematics-core/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')),
Expand All @@ -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)$/, '') : '';
}

0 comments on commit d1ed9e5

Please sign in to comment.