Skip to content

Commit af27ec5

Browse files
clydinmgechev
authored andcommitted
refactor(@schematics/angular): use new helpers in ivy libraries migration
This change refactors the ivy-libraries v9 migration to use the `updateWorkspace` and `JSONFile` helper utilities.
1 parent 20bb298 commit af27ec5

File tree

1 file changed

+48
-71
lines changed

1 file changed

+48
-71
lines changed

packages/schematics/angular/migrations/update-9/ivy-libraries.ts

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
*/
88

99
import { join, normalize } from '@angular-devkit/core';
10-
import { Rule, Tree } from '@angular-devkit/schematics';
11-
import { getWorkspacePath } from '../../utility/config';
12-
import {
13-
appendPropertyInAstObject,
14-
findPropertyInAstObject,
15-
insertPropertyInAstObjectInOrder,
16-
} from '../../utility/json-utils';
10+
import { Rule, Tree, chain } from '@angular-devkit/schematics';
11+
import { JSONFile } from '../../utility/json-file';
12+
import { updateWorkspace } from '../../utility/workspace';
1713
import { Builders } from '../../utility/workspace-models';
18-
import { getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
1914

2015
/**
2116
* Updates a pre version 9 library to version 9 Ivy library.
@@ -25,83 +20,44 @@ import { getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
2520
* - Create a prod tsconfig for which disables Ivy and enables VE compilations.
2621
*/
2722
export function updateLibraries(): Rule {
28-
return (tree, context) => {
29-
const logger = context.logger;
30-
const workspacePath = getWorkspacePath(tree);
31-
const workspace = getWorkspace(tree);
32-
33-
const recorder = tree.beginUpdate(workspacePath);
34-
for (const { target, project } of getTargets(workspace, 'build', Builders.DeprecatedNgPackagr)) {
35-
const projectRoot = findPropertyInAstObject(project, 'root');
36-
if (!projectRoot || projectRoot.kind !== 'string') {
37-
break;
38-
}
39-
40-
const configurations = findPropertyInAstObject(target, 'configurations');
41-
const tsConfig = join(normalize(projectRoot.value), 'tsconfig.lib.prod.json');
23+
return updateWorkspace(workspace => {
24+
const followupRules: Rule[] = [];
4225

43-
if (!configurations || configurations.kind !== 'object') {
44-
// Configurations doesn't exist.
45-
appendPropertyInAstObject(recorder, target, 'configurations', { production: { tsConfig } }, 10);
46-
createTsConfig(tree, tsConfig);
26+
for (const [, project] of workspace.projects) {
27+
if (typeof project.root !== 'string') {
4728
continue;
4829
}
4930

50-
const prodConfig = findPropertyInAstObject(configurations, 'production');
51-
if (!prodConfig || prodConfig.kind !== 'object') {
52-
// Production configuration doesn't exist.
53-
insertPropertyInAstObjectInOrder(recorder, configurations, 'production', { tsConfig }, 12);
54-
createTsConfig(tree, tsConfig);
55-
continue;
56-
}
57-
58-
const tsConfigOption = findPropertyInAstObject(prodConfig, 'tsConfig');
59-
if (!tsConfigOption || tsConfigOption.kind !== 'string') {
60-
// No tsconfig for production has been defined.
61-
insertPropertyInAstObjectInOrder(recorder, prodConfig, 'tsConfig', tsConfig, 14);
62-
createTsConfig(tree, tsConfig);
63-
continue;
64-
}
31+
for (const [, target] of project.targets) {
32+
if (target.builder !== Builders.DeprecatedNgPackagr) {
33+
continue;
34+
}
6535

66-
// tsConfig for production already exists.
67-
const tsConfigPath = tsConfigOption.value;
68-
const tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
69-
if (!tsConfigAst) {
70-
logger.warn(`Cannot find file: ${tsConfigPath}`);
71-
continue;
72-
}
36+
const tsConfig = join(normalize(project.root), 'tsconfig.lib.prod.json');
7337

74-
const tsConfigRecorder = tree.beginUpdate(tsConfigPath);
75-
const ngCompilerOptions = findPropertyInAstObject(tsConfigAst, 'angularCompilerOptions');
76-
if (!ngCompilerOptions) {
77-
// Add angularCompilerOptions to the production tsConfig
78-
appendPropertyInAstObject(tsConfigRecorder, tsConfigAst, 'angularCompilerOptions', { enableIvy: false }, 2);
79-
tree.commitUpdate(tsConfigRecorder);
80-
continue;
81-
}
38+
if (!target.configurations || !target.configurations.production) {
39+
// Production configuration does not exist
40+
target.configurations = { ...target.configurations, production: { tsConfig } };
8241

83-
if (ngCompilerOptions.kind === 'object') {
84-
const enableIvy = findPropertyInAstObject(ngCompilerOptions, 'enableIvy');
85-
// Add enableIvy false
86-
if (!enableIvy) {
87-
appendPropertyInAstObject(tsConfigRecorder, ngCompilerOptions, 'enableIvy', false, 4);
88-
tree.commitUpdate(tsConfigRecorder);
42+
followupRules.push((tree) => createTsConfig(tree, tsConfig));
8943
continue;
9044
}
9145

92-
if (enableIvy.kind !== 'false') {
93-
const { start, end } = enableIvy;
94-
tsConfigRecorder.remove(start.offset, end.offset - start.offset);
95-
tsConfigRecorder.insertLeft(start.offset, 'false');
96-
tree.commitUpdate(tsConfigRecorder);
46+
const existingTsconfig = target.configurations.production.tsConfig;
47+
if (!existingTsconfig || typeof existingTsconfig !== 'string') {
48+
// Production configuration TS configuration does not exist or malformed
49+
target.configurations.production.tsConfig = tsConfig;
50+
51+
followupRules.push((tree) => createTsConfig(tree, tsConfig));
52+
continue;
9753
}
54+
55+
followupRules.push(updateTsConfig(existingTsconfig));
9856
}
9957
}
10058

101-
tree.commitUpdate(recorder);
102-
103-
return tree;
104-
};
59+
return chain(followupRules);
60+
});
10561
}
10662

10763
function createTsConfig(tree: Tree, tsConfigPath: string) {
@@ -116,3 +72,24 @@ function createTsConfig(tree: Tree, tsConfigPath: string) {
11672
tree.create(tsConfigPath, JSON.stringify(tsConfigContent, undefined, 2));
11773
}
11874
}
75+
76+
function updateTsConfig(tsConfigPath: string): Rule {
77+
return (tree, { logger }) => {
78+
let json;
79+
try {
80+
json = new JSONFile(tree, tsConfigPath);
81+
} catch {
82+
logger.warn(`Cannot find file: ${tsConfigPath}`);
83+
84+
return;
85+
}
86+
87+
const enableIvyPath = ['angularCompilerOptions', 'enableIvy'];
88+
89+
if (json.get(enableIvyPath) === false) {
90+
return;
91+
}
92+
93+
json.modify(enableIvyPath, false);
94+
};
95+
}

0 commit comments

Comments
 (0)