Skip to content

Commit 21202f4

Browse files
ukrukargmgechev
authored andcommitted
fix(@schematics/angular): buildRelativePath handles files in root
Before, if one of the arguments was a file in root (eg "/module") code would fail with: "" must be an absolute path.
1 parent 80e3d46 commit 21202f4

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

packages/angular_devkit/core/src/virtual-fs/path.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ export function relative(from: Path, to: Path): Path {
144144
if (from == to) {
145145
p = '';
146146
} else {
147-
const splitFrom = from.split(NormalizedSep);
148-
const splitTo = to.split(NormalizedSep);
147+
const splitFrom = split(from);
148+
const splitTo = split(to);
149149

150150
while (splitFrom.length > 0 && splitTo.length > 0 && splitFrom[0] == splitTo[0]) {
151151
splitFrom.shift();

packages/angular_devkit/core/src/virtual-fs/path_spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ describe('path', () => {
133133
'/src/app/sub1/test1', '/src/app/sub2/test2',
134134
'../../sub2/test2',
135135
],
136+
['/', '/a/b/c', 'a/b/c'],
137+
['/a/b/c', '/d', '../../../d'],
136138
];
137139

138140
for (const [from, to, result] of tests) {
@@ -141,6 +143,7 @@ describe('path', () => {
141143
const t = normalize(to);
142144

143145
expect(relative(f, t)).toBe(result);
146+
expect(join(f, relative(f, t))).toBe(t);
144147
});
145148
}
146149
});

packages/schematics/angular/utility/find-module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ export function buildRelativePath(from: string, to: string): string {
130130
fromParts.pop();
131131
const toFileName = toParts.pop();
132132

133-
const relativePath = relative(normalize(fromParts.join('/')), normalize(toParts.join('/')));
133+
const relativePath = relative(normalize(fromParts.join('/') || '/'),
134+
normalize(toParts.join('/') || '/'));
134135
let pathPrefix = '';
135136

136137
// Set the path prefix for same dir or child dir, parent dir starts with `..`

packages/schematics/angular/utility/find-module_spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import { Path } from '@angular-devkit/core';
99
import { EmptyTree, Tree } from '@angular-devkit/schematics';
10-
import { ModuleOptions, findModule, findModuleFromOptions } from './find-module';
10+
import { ModuleOptions, buildRelativePath, findModule, findModuleFromOptions } from './find-module';
1111

1212

1313
describe('find-module', () => {
@@ -189,4 +189,17 @@ describe('find-module', () => {
189189
expect(modPath).toBe('/projects/my-proj/src/app.module.ts' as Path);
190190
});
191191
});
192+
193+
describe('buildRelativePath', () => {
194+
it('works', () => {
195+
expect(buildRelativePath('/test/module', '/test/service'))
196+
.toEqual('./service');
197+
expect(buildRelativePath('/test/module', '/other/service'))
198+
.toEqual('../other/service');
199+
expect(buildRelativePath('/module', '/test/service'))
200+
.toEqual('./test/service');
201+
expect(buildRelativePath('/test/service', '/module'))
202+
.toEqual('../module');
203+
});
204+
});
192205
});

0 commit comments

Comments
 (0)