Skip to content

Commit db08558

Browse files
alan-agius4vikerman
authored andcommitted
fix(@schematics/angular): warn when target references a missing tsconfig
Fixes #16019
1 parent a9bee14 commit db08558

File tree

5 files changed

+44
-29
lines changed

5 files changed

+44
-29
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import { getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
2323
* - Create a prod tsconfig for which disables Ivy and enables VE compilations.
2424
*/
2525
export function updateLibraries(): Rule {
26-
return (tree: Tree) => {
26+
return (tree, context) => {
27+
const logger = context.logger;
2728
const workspacePath = getWorkspacePath(tree);
2829
const workspace = getWorkspace(tree);
2930

@@ -61,8 +62,14 @@ export function updateLibraries(): Rule {
6162
}
6263

6364
// tsConfig for production already exists.
64-
const tsConfigAst = readJsonFileAsAstObject(tree, tsConfigOption.value);
65-
const tsConfigRecorder = tree.beginUpdate(tsConfigOption.value);
65+
const tsConfigPath = tsConfigOption.value;
66+
const tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
67+
if (!tsConfigAst) {
68+
logger.warn(`Cannot find file: ${tsConfigPath}`);
69+
continue;
70+
}
71+
72+
const tsConfigRecorder = tree.beginUpdate(tsConfigPath);
6673
const ngCompilerOptions = findPropertyInAstObject(tsConfigAst, 'angularCompilerOptions');
6774
if (!ngCompilerOptions) {
6875
// Add angularCompilerOptions to the production tsConfig

packages/schematics/angular/migrations/update-9/ngsw-config.ts

+4-2
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 { Rule, Tree } from '@angular-devkit/schematics';
8+
import { Rule } from '@angular-devkit/schematics';
99
import { appendValueInAstArray, findPropertyInAstObject } from '../../utility/json-utils';
1010
import { Builders } from '../../utility/workspace-models';
1111
import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
@@ -15,13 +15,15 @@ import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from
1515
* Update ngsw-config.json to fix issue https://github.com/angular/angular-cli/pull/15277
1616
*/
1717
export function updateNGSWConfig(): Rule {
18-
return (tree: Tree) => {
18+
return (tree, context) => {
1919
const workspace = getWorkspace(tree);
20+
const logger = context.logger;
2021

2122
for (const { target } of getTargets(workspace, 'build', Builders.Browser)) {
2223
for (const options of getAllOptions(target)) {
2324
const ngswConfigPath = findPropertyInAstObject(options, 'ngswConfigPath');
2425
if (!ngswConfigPath || ngswConfigPath.kind !== 'string') {
26+
logger.warn(`Cannot find file: ${ngswConfigPath}`);
2527
continue;
2628
}
2729

packages/schematics/angular/migrations/update-9/remove-tsickle.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
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 { JsonParseMode, parseJsonAst } from '@angular-devkit/core';
9-
import { Rule, Tree } from '@angular-devkit/schematics';
8+
import { Rule } from '@angular-devkit/schematics';
109
import { removePackageJsonDependency } from '../../utility/dependencies';
1110
import { findPropertyInAstObject, removePropertyInAstObject } from '../../utility/json-utils';
1211
import { Builders } from '../../utility/workspace-models';
13-
import { getAllOptions, getTargets, getWorkspace } from './utils';
12+
import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
1413

1514
/**
1615
* Remove tsickle from libraries
1716
*/
1817
export function removeTsickle(): Rule {
19-
return (tree: Tree) => {
18+
return (tree, context) => {
2019
removePackageJsonDependency(tree, 'tsickle');
21-
20+
const logger = context.logger;
2221
const workspace = getWorkspace(tree);
2322

2423
for (const { target } of getTargets(workspace, 'build', Builders.NgPackagr)) {
@@ -28,20 +27,18 @@ export function removeTsickle(): Rule {
2827
continue;
2928
}
3029

31-
const tsConfigContent = tree.read(tsConfigOption.value);
32-
if (!tsConfigContent) {
33-
continue;
34-
}
30+
const tsConfigPath = tsConfigOption.value;
31+
const tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
32+
if (!tsConfigAst) {
33+
logger.warn(`Cannot find file: ${tsConfigPath}`);
3534

36-
const tsConfigAst = parseJsonAst(tsConfigContent.toString(), JsonParseMode.Loose);
37-
if (!tsConfigAst || tsConfigAst.kind !== 'object') {
3835
continue;
3936
}
4037

4138
const ngCompilerOptions = findPropertyInAstObject(tsConfigAst, 'angularCompilerOptions');
4239
if (ngCompilerOptions && ngCompilerOptions.kind === 'object') {
4340
// remove annotateForClosureCompiler option
44-
const recorder = tree.beginUpdate(tsConfigOption.value);
41+
const recorder = tree.beginUpdate(tsConfigPath);
4542
removePropertyInAstObject(recorder, ngCompilerOptions, 'annotateForClosureCompiler');
4643
tree.commitUpdate(recorder);
4744
}

packages/schematics/angular/migrations/update-9/update-app-tsconfigs.ts

+15-10
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 { JsonAstObject } from '@angular-devkit/core';
8+
import { JsonAstObject, logging } from '@angular-devkit/core';
99
import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics';
1010
import { posix } from 'path';
1111
import {
@@ -16,33 +16,32 @@ import {
1616
import { Builders } from '../../utility/workspace-models';
1717
import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
1818

19-
2019
/**
2120
* Update the tsconfig files for applications
2221
* - Removes enableIvy: true
2322
* - Sets stricter file inclusions
2423
*/
2524
export function updateApplicationTsConfigs(): Rule {
26-
return (tree: Tree) => {
25+
return (tree, context) => {
2726
const workspace = getWorkspace(tree);
2827

2928
for (const { target } of getTargets(workspace, 'build', Builders.Browser)) {
30-
updateTsConfig(tree, target, Builders.Browser);
29+
updateTsConfig(tree, target, Builders.Browser, context.logger);
3130
}
3231

3332
for (const { target } of getTargets(workspace, 'server', Builders.Server)) {
34-
updateTsConfig(tree, target, Builders.Server);
33+
updateTsConfig(tree, target, Builders.Server, context.logger);
3534
}
3635

3736
for (const { target } of getTargets(workspace, 'test', Builders.Karma)) {
38-
updateTsConfig(tree, target, Builders.Karma);
37+
updateTsConfig(tree, target, Builders.Karma, context.logger);
3938
}
4039

4140
return tree;
4241
};
4342
}
4443

45-
function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: Builders) {
44+
function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: Builders, logger: logging.LoggerApi) {
4645
const options = getAllOptions(builderConfig);
4746
for (const option of options) {
4847
let recorder: UpdateRecorder;
@@ -55,6 +54,7 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
5554
const tsConfigPath = tsConfigOption.value;
5655
let tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
5756
if (!tsConfigAst) {
57+
logger.warn(`Cannot find file: ${tsConfigPath}`);
5858
continue;
5959
}
6060

@@ -78,7 +78,10 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
7878
if (builderName !== Builders.Karma) {
7979
// Note: we need to re-read the tsconfig after very commit because
8080
// otherwise the updates will be out of sync since we are ammending the same node.
81-
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
81+
82+
// we are already checking that tsconfig exists above!
83+
// tslint:disable-next-line: no-non-null-assertion
84+
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath) !;
8285
const include = findPropertyInAstObject(tsConfigAst, 'include');
8386

8487
if (include && include.kind === 'array') {
@@ -109,13 +112,15 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
109112

110113
if (newFiles.length) {
111114
recorder = tree.beginUpdate(tsConfigPath);
112-
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
115+
// tslint:disable-next-line: no-non-null-assertion
116+
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath) !;
113117
insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'files', newFiles, 2);
114118
tree.commitUpdate(recorder);
115119
}
116120

117121
recorder = tree.beginUpdate(tsConfigPath);
118-
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
122+
// tslint:disable-next-line: no-non-null-assertion
123+
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath) !;
119124
removePropertyInAstObject(recorder, tsConfigAst, 'exclude');
120125
tree.commitUpdate(recorder);
121126
}

packages/schematics/angular/migrations/update-9/utils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ export function getAllOptions(builderConfig: JsonAstObject, configurationsOnly =
8585

8686
export function getWorkspace(host: Tree): JsonAstObject {
8787
const path = getWorkspacePath(host);
88+
const content = readJsonFileAsAstObject(host, path);
89+
if (!content) {
90+
throw new SchematicsException(`Could not find (${path})`);
91+
}
8892

89-
return readJsonFileAsAstObject(host, path);
93+
return content;
9094
}
9195

92-
export function readJsonFileAsAstObject(host: Tree, path: string): JsonAstObject {
96+
export function readJsonFileAsAstObject(host: Tree, path: string): JsonAstObject | undefined {
9397
const configBuffer = host.read(path);
9498
if (!configBuffer) {
9599
throw new SchematicsException(`Could not find (${path})`);

0 commit comments

Comments
 (0)