Skip to content

Commit f03ff80

Browse files
Alanmgechev
authored andcommitted
refactor(@schematics/angular): create relativePathToWorkspaceRoot helper and replace usages
1 parent 201856a commit f03ff80

File tree

7 files changed

+81
-32
lines changed

7 files changed

+81
-32
lines changed

packages/schematics/angular/application/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { NodeDependencyType, addPackageJsonDependency } from '../utility/depende
3737
import { findPropertyInAstObject, insertPropertyInAstObjectInOrder } from '../utility/json-utils';
3838
import { latestVersions } from '../utility/latest-versions';
3939
import { applyLintFix } from '../utility/lint-fix';
40+
import { relativePathToWorkspaceRoot } from '../utility/paths';
4041
import { validateProjectName } from '../utility/validation';
4142
import { getWorkspace, updateWorkspace } from '../utility/workspace';
4243
import { Builders, ProjectType } from '../utility/workspace-models';
@@ -147,7 +148,8 @@ function addAppToWorkspaceFile(options: ApplicationOptions, newProjectRoot: stri
147148
? options.projectRoot
148149
: `${newProjectRoot}/${options.name}`;
149150

150-
if (projectRoot !== '' && !projectRoot.endsWith('/')) {
151+
projectRoot = normalize(projectRoot);
152+
if (projectRoot) {
151153
projectRoot += '/';
152154
}
153155

@@ -324,9 +326,6 @@ export default function (options: ApplicationOptions): Rule {
324326
const appDir = isRootApp
325327
? options.projectRoot as string
326328
: `${newProjectRoot}/${options.name}`;
327-
const relativePathToWorkspaceRoot = appDir
328-
? appDir.split('/').map(() => '..').join('/')
329-
: '.';
330329
const sourceDir = `${appDir}/src/app`;
331330

332331
const e2eOptions: E2eOptions = {
@@ -342,7 +341,7 @@ export default function (options: ApplicationOptions): Rule {
342341
applyTemplates({
343342
utils: strings,
344343
...options,
345-
relativePathToWorkspaceRoot,
344+
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(appDir),
346345
appName: options.name,
347346
isRootApp,
348347
}),

packages/schematics/angular/e2e/index.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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 { normalize, strings } from '@angular-devkit/core';
8+
import { join, normalize, strings } from '@angular-devkit/core';
99
import {
1010
Rule,
1111
SchematicsException,
@@ -17,16 +17,11 @@ import {
1717
move,
1818
url,
1919
} from '@angular-devkit/schematics';
20+
import { relativePathToWorkspaceRoot } from '../utility/paths';
2021
import { getWorkspace, updateWorkspace } from '../utility/workspace';
2122
import { Builders } from '../utility/workspace-models';
2223
import { Schema as E2eOptions } from './schema';
2324

24-
function getE2eRoot(projectRoot: string): string {
25-
const root = normalize(projectRoot);
26-
27-
return root ? root + '/e2e' : 'e2e';
28-
}
29-
3025
export default function (options: E2eOptions): Rule {
3126
return async (host: Tree) => {
3227
const appProject = options.relatedAppName;
@@ -36,8 +31,7 @@ export default function (options: E2eOptions): Rule {
3631
throw new SchematicsException(`Project name "${appProject}" doesn't not exist.`);
3732
}
3833

39-
const root = getE2eRoot(project.root);
40-
const relativePathToWorkspaceRoot = root.split('/').map(() => '..').join('/');
34+
const root = join(normalize(project.root), 'e2e');
4135

4236
project.targets.add({
4337
name: 'e2e',
@@ -66,7 +60,7 @@ export default function (options: E2eOptions): Rule {
6660
applyTemplates({
6761
utils: strings,
6862
...options,
69-
relativePathToWorkspaceRoot,
63+
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(root),
7064
}),
7165
move(root),
7266
])),

packages/schematics/angular/library/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
2424
import { NodeDependencyType, addPackageJsonDependency } from '../utility/dependencies';
2525
import { latestVersions } from '../utility/latest-versions';
2626
import { applyLintFix } from '../utility/lint-fix';
27+
import { relativePathToWorkspaceRoot } from '../utility/paths';
2728
import { validateProjectName } from '../utility/validation';
2829
import { getWorkspace, updateWorkspace } from '../utility/workspace';
2930
import { Builders, ProjectType } from '../utility/workspace-models';
@@ -195,7 +196,6 @@ export default function (options: LibraryOptions): Rule {
195196
const distRoot = `dist/${folderName}`;
196197

197198
const sourceDir = `${projectRoot}/src/lib`;
198-
const relativePathToWorkspaceRoot = projectRoot.split('/').map(x => '..').join('/');
199199

200200
const templateSource = apply(url('./files'), [
201201
applyTemplates({
@@ -204,7 +204,7 @@ export default function (options: LibraryOptions): Rule {
204204
packageName,
205205
projectRoot,
206206
distRoot,
207-
relativePathToWorkspaceRoot,
207+
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(projectRoot),
208208
prefix,
209209
angularLatestVersion: latestVersions.Angular.replace('~', '').replace('^', ''),
210210
folderName,

packages/schematics/angular/service-worker/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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 { join, normalize } from '@angular-devkit/core';
89
import {
910
Rule,
1011
SchematicContext,
@@ -23,6 +24,7 @@ import { addSymbolToNgModuleMetadata, insertImport, isImported } from '../utilit
2324
import { InsertChange } from '../utility/change';
2425
import { addPackageJsonDependency, getPackageJsonDependency } from '../utility/dependencies';
2526
import { getAppModulePath } from '../utility/ng-ast-utils';
27+
import { relativePathToWorkspaceRoot } from '../utility/paths';
2628
import { targetBuildNotFoundError } from '../utility/project-targets';
2729
import { getWorkspace, updateWorkspace } from '../utility/workspace';
2830
import { BrowserBuilderOptions } from '../utility/workspace-models';
@@ -136,18 +138,19 @@ export default function (options: ServiceWorkerOptions): Rule {
136138
const root = project.root;
137139

138140
config.serviceWorker = true;
139-
config.ngswConfigPath = `${root && !root.endsWith('/') ? root + '/' : root}ngsw-config.json`;
140-
141-
const relativePathToWorkspaceRoot = project.root ?
142-
project.root.split('/').filter(x => x !== '').map(x => '..').join('/') : '.';
141+
config.ngswConfigPath = join(normalize(root), 'ngsw-config.json');
143142

144143
let { resourcesOutputPath = '' } = config;
145144
if (resourcesOutputPath) {
146-
resourcesOutputPath = '/' + resourcesOutputPath.split('/').filter(x => !!x).join('/');
145+
resourcesOutputPath = normalize(`/${resourcesOutputPath}`);
147146
}
148147

149148
const templateSource = apply(url('./files'), [
150-
applyTemplates({ ...options, resourcesOutputPath, relativePathToWorkspaceRoot }),
149+
applyTemplates({
150+
...options,
151+
resourcesOutputPath,
152+
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(project.root),
153+
}),
151154
move(project.root),
152155
]);
153156

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { normalize, split } from '@angular-devkit/core';
10+
11+
export function relativePathToWorkspaceRoot(projectRoot: string | undefined): string {
12+
const normalizedPath = split(normalize(projectRoot || ''));
13+
14+
if (normalizedPath.length === 0 || !normalizedPath[0]) {
15+
return '.';
16+
} else {
17+
return normalizedPath.map(() => '..').join('/');
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { relativePathToWorkspaceRoot } from './paths';
10+
11+
describe('paths', () => {
12+
describe('relativePathToWorkspaceRoot', () => {
13+
it(`should handle root '/'`, () => {
14+
const result = relativePathToWorkspaceRoot('/');
15+
expect(result).toBe('.');
16+
});
17+
18+
it(`should handle an empty path`, () => {
19+
const result = relativePathToWorkspaceRoot('');
20+
expect(result).toBe('.');
21+
});
22+
23+
it(`should handle an undefined path`, () => {
24+
const result = relativePathToWorkspaceRoot(undefined);
25+
expect(result).toBe('.');
26+
});
27+
28+
it(`should handle path with trailing '/'`, () => {
29+
const result = relativePathToWorkspaceRoot('foo/bar/');
30+
expect(result).toBe('../..');
31+
});
32+
33+
it(`should handle path without trailing '/'`, () => {
34+
const result = relativePathToWorkspaceRoot('foo/bar');
35+
expect(result).toBe('../..');
36+
});
37+
});
38+
});

packages/schematics/angular/web-worker/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@angular-devkit/schematics';
1313
import { appendValueInAstArray, findPropertyInAstObject } from '../utility/json-utils';
1414
import { parseName } from '../utility/parse-name';
15+
import { relativePathToWorkspaceRoot } from '../utility/paths';
1516
import { buildDefaultPath, getWorkspace, updateWorkspace } from '../utility/workspace';
1617
import { BrowserBuilderOptions, LintBuilderOptions } from '../utility/workspace-models';
1718
import { Schema as WebWorkerOptions } from './schema';
@@ -21,14 +22,6 @@ function addConfig(options: WebWorkerOptions, root: string, tsConfigPath: string
2122
return (host: Tree, context: SchematicContext) => {
2223
context.logger.debug('updating project configuration.');
2324

24-
// todo: replace with the new helper method in a seperate PR
25-
// https://github.com/angular/angular-cli/pull/14207
26-
const rootNormalized = root.endsWith('/') ? root.slice(0, -1) : root;
27-
const relativePathToWorkspaceRoot =
28-
rootNormalized
29-
? rootNormalized.split('/').map(x => '..').join('/')
30-
: '.';
31-
3225
// Add worker glob exclusion to tsconfig.app.json.
3326
const workerGlob = 'src/**/*.worker.ts';
3427
const buffer = host.read(tsConfigPath);
@@ -51,7 +44,10 @@ function addConfig(options: WebWorkerOptions, root: string, tsConfigPath: string
5144

5245
return mergeWith(
5346
apply(url('./files/worker-tsconfig'), [
54-
applyTemplates({ ...options, relativePathToWorkspaceRoot }),
47+
applyTemplates({
48+
...options,
49+
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(root),
50+
}),
5551
move(root),
5652
]),
5753
);

0 commit comments

Comments
 (0)