Skip to content

Commit 599cfb6

Browse files
timdeschryverbrandonroberts
authored andcommitted
fix(Store): Resolve environment path when generating a new store (#1071)
1 parent d1ed9e5 commit 599cfb6

File tree

9 files changed

+311
-219
lines changed

9 files changed

+311
-219
lines changed

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

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Path, join, normalize, relative, strings } from '@angular-devkit/core';
8+
import {
9+
Path,
10+
join,
11+
normalize,
12+
relative,
13+
strings,
14+
basename,
15+
extname,
16+
dirname,
17+
} from '@angular-devkit/core';
918
import { DirEntry, Tree } from '@angular-devkit/schematics';
1019

1120
export interface ModuleOptions {
@@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path {
8998
* Build a relative path from one file path to another file path.
9099
*/
91100
export function buildRelativePath(from: string, to: string): string {
92-
from = normalize(from);
93-
to = normalize(to);
94-
95-
// Convert to arrays.
96-
const fromParts = from.split('/');
97-
const toParts = to.split('/');
98-
99-
// Remove file names (preserving destination)
100-
fromParts.pop();
101-
const toFileName = convertToTypeScriptFileName(toParts.pop());
102-
103-
const relativePath = relative(
104-
normalize(fromParts.join('/')),
105-
normalize(toParts.join('/'))
106-
);
107-
let pathPrefix = '';
108-
109-
// Set the path prefix for same dir or child dir, parent dir starts with `..`
110-
if (!relativePath) {
111-
pathPrefix = '.';
112-
} else if (!relativePath.startsWith('.')) {
113-
pathPrefix = `./`;
114-
}
115-
if (pathPrefix && !pathPrefix.endsWith('/')) {
116-
pathPrefix += '/';
117-
}
118-
119-
return toFileName
120-
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121-
: pathPrefix + relativePath;
101+
const {
102+
path: fromPath,
103+
filename: fromFileName,
104+
directory: fromDirectory,
105+
} = parsePath(from);
106+
const {
107+
path: toPath,
108+
filename: toFileName,
109+
directory: toDirectory,
110+
} = parsePath(to);
111+
const relativePath = relative(fromDirectory, toDirectory);
112+
const fixedRelativePath = relativePath.startsWith('.')
113+
? relativePath
114+
: `./${relativePath}`;
115+
116+
return !toFileName || toFileName === 'index.ts'
117+
? fixedRelativePath
118+
: `${
119+
fixedRelativePath.endsWith('/')
120+
? fixedRelativePath
121+
: fixedRelativePath + '/'
122+
}${convertToTypeScriptFileName(toFileName)}`;
122123
}
123124

125+
function parsePath(path: string) {
126+
const pathNormalized = normalize(path) as Path;
127+
const filename = extname(pathNormalized) ? basename(pathNormalized) : '';
128+
const directory = filename ? dirname(pathNormalized) : pathNormalized;
129+
return {
130+
path: pathNormalized,
131+
filename,
132+
directory,
133+
};
134+
}
124135
/**
125136
* Strips the typescript extension and clears index filenames
126137
* foo.ts -> foo

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

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Path, join, normalize, relative, strings } from '@angular-devkit/core';
8+
import {
9+
Path,
10+
join,
11+
normalize,
12+
relative,
13+
strings,
14+
basename,
15+
extname,
16+
dirname,
17+
} from '@angular-devkit/core';
918
import { DirEntry, Tree } from '@angular-devkit/schematics';
1019

1120
export interface ModuleOptions {
@@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path {
8998
* Build a relative path from one file path to another file path.
9099
*/
91100
export function buildRelativePath(from: string, to: string): string {
92-
from = normalize(from);
93-
to = normalize(to);
94-
95-
// Convert to arrays.
96-
const fromParts = from.split('/');
97-
const toParts = to.split('/');
98-
99-
// Remove file names (preserving destination)
100-
fromParts.pop();
101-
const toFileName = convertToTypeScriptFileName(toParts.pop());
102-
103-
const relativePath = relative(
104-
normalize(fromParts.join('/')),
105-
normalize(toParts.join('/'))
106-
);
107-
let pathPrefix = '';
108-
109-
// Set the path prefix for same dir or child dir, parent dir starts with `..`
110-
if (!relativePath) {
111-
pathPrefix = '.';
112-
} else if (!relativePath.startsWith('.')) {
113-
pathPrefix = `./`;
114-
}
115-
if (pathPrefix && !pathPrefix.endsWith('/')) {
116-
pathPrefix += '/';
117-
}
118-
119-
return toFileName
120-
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121-
: pathPrefix + relativePath;
101+
const {
102+
path: fromPath,
103+
filename: fromFileName,
104+
directory: fromDirectory,
105+
} = parsePath(from);
106+
const {
107+
path: toPath,
108+
filename: toFileName,
109+
directory: toDirectory,
110+
} = parsePath(to);
111+
const relativePath = relative(fromDirectory, toDirectory);
112+
const fixedRelativePath = relativePath.startsWith('.')
113+
? relativePath
114+
: `./${relativePath}`;
115+
116+
return !toFileName || toFileName === 'index.ts'
117+
? fixedRelativePath
118+
: `${
119+
fixedRelativePath.endsWith('/')
120+
? fixedRelativePath
121+
: fixedRelativePath + '/'
122+
}${convertToTypeScriptFileName(toFileName)}`;
122123
}
123124

125+
function parsePath(path: string) {
126+
const pathNormalized = normalize(path) as Path;
127+
const filename = extname(pathNormalized) ? basename(pathNormalized) : '';
128+
const directory = filename ? dirname(pathNormalized) : pathNormalized;
129+
return {
130+
path: pathNormalized,
131+
filename,
132+
directory,
133+
};
134+
}
124135
/**
125136
* Strips the typescript extension and clears index filenames
126137
* foo.ts -> foo

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

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Path, join, normalize, relative, strings } from '@angular-devkit/core';
8+
import {
9+
Path,
10+
join,
11+
normalize,
12+
relative,
13+
strings,
14+
basename,
15+
extname,
16+
dirname,
17+
} from '@angular-devkit/core';
918
import { DirEntry, Tree } from '@angular-devkit/schematics';
1019

1120
export interface ModuleOptions {
@@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path {
8998
* Build a relative path from one file path to another file path.
9099
*/
91100
export function buildRelativePath(from: string, to: string): string {
92-
from = normalize(from);
93-
to = normalize(to);
94-
95-
// Convert to arrays.
96-
const fromParts = from.split('/');
97-
const toParts = to.split('/');
98-
99-
// Remove file names (preserving destination)
100-
fromParts.pop();
101-
const toFileName = convertToTypeScriptFileName(toParts.pop());
102-
103-
const relativePath = relative(
104-
normalize(fromParts.join('/')),
105-
normalize(toParts.join('/'))
106-
);
107-
let pathPrefix = '';
108-
109-
// Set the path prefix for same dir or child dir, parent dir starts with `..`
110-
if (!relativePath) {
111-
pathPrefix = '.';
112-
} else if (!relativePath.startsWith('.')) {
113-
pathPrefix = `./`;
114-
}
115-
if (pathPrefix && !pathPrefix.endsWith('/')) {
116-
pathPrefix += '/';
117-
}
118-
119-
return toFileName
120-
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121-
: pathPrefix + relativePath;
101+
const {
102+
path: fromPath,
103+
filename: fromFileName,
104+
directory: fromDirectory,
105+
} = parsePath(from);
106+
const {
107+
path: toPath,
108+
filename: toFileName,
109+
directory: toDirectory,
110+
} = parsePath(to);
111+
const relativePath = relative(fromDirectory, toDirectory);
112+
const fixedRelativePath = relativePath.startsWith('.')
113+
? relativePath
114+
: `./${relativePath}`;
115+
116+
return !toFileName || toFileName === 'index.ts'
117+
? fixedRelativePath
118+
: `${
119+
fixedRelativePath.endsWith('/')
120+
? fixedRelativePath
121+
: fixedRelativePath + '/'
122+
}${convertToTypeScriptFileName(toFileName)}`;
122123
}
123124

125+
function parsePath(path: string) {
126+
const pathNormalized = normalize(path) as Path;
127+
const filename = extname(pathNormalized) ? basename(pathNormalized) : '';
128+
const directory = filename ? dirname(pathNormalized) : pathNormalized;
129+
return {
130+
path: pathNormalized,
131+
filename,
132+
directory,
133+
};
134+
}
124135
/**
125136
* Strips the typescript extension and clears index filenames
126137
* foo.ts -> foo

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

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Path, join, normalize, relative, strings } from '@angular-devkit/core';
8+
import {
9+
Path,
10+
join,
11+
normalize,
12+
relative,
13+
strings,
14+
basename,
15+
extname,
16+
dirname,
17+
} from '@angular-devkit/core';
918
import { DirEntry, Tree } from '@angular-devkit/schematics';
1019

1120
export interface ModuleOptions {
@@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path {
8998
* Build a relative path from one file path to another file path.
9099
*/
91100
export function buildRelativePath(from: string, to: string): string {
92-
from = normalize(from);
93-
to = normalize(to);
94-
95-
// Convert to arrays.
96-
const fromParts = from.split('/');
97-
const toParts = to.split('/');
98-
99-
// Remove file names (preserving destination)
100-
fromParts.pop();
101-
const toFileName = convertToTypeScriptFileName(toParts.pop());
102-
103-
const relativePath = relative(
104-
normalize(fromParts.join('/')),
105-
normalize(toParts.join('/'))
106-
);
107-
let pathPrefix = '';
108-
109-
// Set the path prefix for same dir or child dir, parent dir starts with `..`
110-
if (!relativePath) {
111-
pathPrefix = '.';
112-
} else if (!relativePath.startsWith('.')) {
113-
pathPrefix = `./`;
114-
}
115-
if (pathPrefix && !pathPrefix.endsWith('/')) {
116-
pathPrefix += '/';
117-
}
118-
119-
return toFileName
120-
? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName
121-
: pathPrefix + relativePath;
101+
const {
102+
path: fromPath,
103+
filename: fromFileName,
104+
directory: fromDirectory,
105+
} = parsePath(from);
106+
const {
107+
path: toPath,
108+
filename: toFileName,
109+
directory: toDirectory,
110+
} = parsePath(to);
111+
const relativePath = relative(fromDirectory, toDirectory);
112+
const fixedRelativePath = relativePath.startsWith('.')
113+
? relativePath
114+
: `./${relativePath}`;
115+
116+
return !toFileName || toFileName === 'index.ts'
117+
? fixedRelativePath
118+
: `${
119+
fixedRelativePath.endsWith('/')
120+
? fixedRelativePath
121+
: fixedRelativePath + '/'
122+
}${convertToTypeScriptFileName(toFileName)}`;
122123
}
123124

125+
function parsePath(path: string) {
126+
const pathNormalized = normalize(path) as Path;
127+
const filename = extname(pathNormalized) ? basename(pathNormalized) : '';
128+
const directory = filename ? dirname(pathNormalized) : pathNormalized;
129+
return {
130+
path: pathNormalized,
131+
filename,
132+
directory,
133+
};
134+
}
124135
/**
125136
* Strips the typescript extension and clears index filenames
126137
* foo.ts -> foo

0 commit comments

Comments
 (0)