Skip to content

Commit c92e799

Browse files
clydinmgechev
authored andcommitted
refactor(@schematics/angular): use new workspace helpers in differential-loading migration
1 parent dd8acd9 commit c92e799

File tree

1 file changed

+26
-43
lines changed

1 file changed

+26
-43
lines changed

packages/schematics/angular/migrations/update-8/differential-loading.ts

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
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 {
9-
JsonParseMode,
10-
isJsonObject,
11-
join,
12-
normalize,
13-
parseJson,
14-
} from '@angular-devkit/core';
8+
import { join, normalize } from '@angular-devkit/core';
159
import { Rule, Tree } from '@angular-devkit/schematics';
1610
import { JSONFile } from '../../utility/json-file';
11+
import { getWorkspace } from '../../utility/workspace';
12+
import { Builders } from '../../utility/workspace-models';
1713

1814
const browserslistContent = `# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
1915
# For additional information regarding the format and rule options, please see:
@@ -29,62 +25,51 @@ not dead
2925
not IE 9-11 # For IE 9-11 support, remove 'not'.`;
3026

3127
export function updateES5Projects(): Rule {
32-
return (tree: Tree) => {
28+
return async (tree) => {
3329
// update workspace tsconfig
3430
updateTsConfig(tree, '/tsconfig.json');
3531

36-
const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json');
37-
38-
if (!angularConfigContent) {
39-
return;
40-
}
41-
42-
const angularJson = parseJson(angularConfigContent.toString(), JsonParseMode.Loose);
43-
44-
if (!isJsonObject(angularJson) || !isJsonObject(angularJson.projects)) {
45-
// If that field isn't there, no use...
32+
let workspace;
33+
try {
34+
workspace = await getWorkspace(tree);
35+
} catch {
4636
return;
4737
}
4838

49-
// For all projects
50-
for (const [name, project] of Object.entries(angularJson.projects)) {
51-
if (!isJsonObject(project)) {
52-
continue;
53-
}
54-
if (typeof project.root != 'string' || project.projectType !== 'application') {
39+
for (const [projectName, project] of workspace.projects) {
40+
if (typeof project.root !== 'string') {
5541
continue;
5642
}
57-
if (name.endsWith('-e2e')) {
43+
44+
if (projectName.endsWith('-e2e')) {
5845
// Skip existing separate E2E projects
5946
continue;
6047
}
6148

62-
// Older projects app and spec ts configs had script and module set in them.
63-
const architect = project.architect;
64-
if (!(isJsonObject(architect)
65-
&& isJsonObject(architect.build)
66-
&& architect.build.builder === '@angular-devkit/build-angular:browser')
67-
) {
68-
// Skip projects who's build builder is not build-angular:browser
49+
const buildTarget = project.targets.get('build');
50+
if (!buildTarget || buildTarget.builder !== Builders.Browser) {
6951
continue;
7052
}
7153

72-
const buildOptionsConfig = architect.build.options;
73-
if (isJsonObject(buildOptionsConfig) && typeof buildOptionsConfig.tsConfig === 'string') {
74-
updateTsConfig(tree, buildOptionsConfig.tsConfig);
54+
const buildTsConfig = buildTarget?.options?.tsConfig;
55+
if (buildTsConfig && typeof buildTsConfig === 'string') {
56+
updateTsConfig(tree, buildTsConfig);
57+
}
58+
59+
const testTarget = project.targets.get('test');
60+
if (!testTarget) {
61+
continue;
7562
}
7663

77-
const testConfig = architect.test;
78-
if (isJsonObject(testConfig)
79-
&& isJsonObject(testConfig.options)
80-
&& typeof testConfig.options.tsConfig === 'string') {
81-
updateTsConfig(tree, testConfig.options.tsConfig);
64+
const testTsConfig = testTarget?.options?.tsConfig;
65+
if (testTsConfig && typeof testTsConfig === 'string') {
66+
updateTsConfig(tree, testTsConfig);
8267
}
8368

8469
const browserslistPath = join(normalize(project.root), 'browserslist');
8570

8671
// Move the CLI 7 style browserlist to root if it's there.
87-
const sourceRoot = project.sourceRoot === 'string'
72+
const sourceRoot = typeof project.sourceRoot === 'string'
8873
? project.sourceRoot
8974
: join(normalize(project.root), 'src');
9075
const srcBrowsersList = join(normalize(sourceRoot), 'browserslist');
@@ -95,8 +80,6 @@ export function updateES5Projects(): Rule {
9580
tree.create(browserslistPath, browserslistContent);
9681
}
9782
}
98-
99-
return tree;
10083
};
10184
}
10285

0 commit comments

Comments
 (0)