5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { JsonAstNode , JsonAstString , JsonParseMode , dirname , join , normalize , parseJsonAst , resolve } from '@angular-devkit/core' ;
8
+ import { JsonAstNode , JsonAstString , JsonParseMode , dirname , join , logging , normalize , parseJsonAst , resolve } from '@angular-devkit/core' ;
9
9
import { DirEntry , Rule , chain } from '@angular-devkit/schematics' ;
10
10
import { findPropertyInAstObject } from '../../utility/json-utils' ;
11
11
import { getWorkspace } from '../../utility/workspace' ;
@@ -18,7 +18,7 @@ const SOLUTIONS_TS_CONFIG_HEADER = `/*
18
18
*/
19
19
` ;
20
20
21
- function * visitExtendedJsonFiles ( directory : DirEntry ) : IterableIterator < [ string , JsonAstString ] > {
21
+ function * visitExtendedJsonFiles ( directory : DirEntry , logger : logging . LoggerApi ) : IterableIterator < [ string , JsonAstString ] > {
22
22
for ( const path of directory . subfiles ) {
23
23
if ( ! path . endsWith ( '.json' ) ) {
24
24
continue ;
@@ -33,8 +33,16 @@ function* visitExtendedJsonFiles(directory: DirEntry): IterableIterator<[string,
33
33
let jsonAst : JsonAstNode ;
34
34
try {
35
35
jsonAst = parseJsonAst ( content , JsonParseMode . Loose ) ;
36
- } catch {
37
- throw new Error ( `Invalid JSON AST Object (${ path } )` ) ;
36
+ } catch ( error ) {
37
+ let jsonFilePath = `${ join ( directory . path , path ) } ` ;
38
+ jsonFilePath = jsonFilePath . startsWith ( '/' ) ? jsonFilePath . substr ( 1 ) : jsonFilePath ;
39
+
40
+ const msg = error instanceof Error ? error . message : error ;
41
+ logger . warn (
42
+ `Failed to parse "${ jsonFilePath } " as JSON AST Object. ${ msg } \n` +
43
+ 'If this is a TypeScript configuration file you will need to update the "extends" value manually.' ,
44
+ ) ;
45
+ continue ;
38
46
}
39
47
40
48
if ( jsonAst . kind !== 'object' ) {
@@ -54,12 +62,12 @@ function* visitExtendedJsonFiles(directory: DirEntry): IterableIterator<[string,
54
62
continue ;
55
63
}
56
64
57
- yield * visitExtendedJsonFiles ( directory . dir ( path ) ) ;
65
+ yield * visitExtendedJsonFiles ( directory . dir ( path ) , logger ) ;
58
66
}
59
67
}
60
68
61
69
function updateTsconfigExtendsRule ( ) : Rule {
62
- return host => {
70
+ return ( host , context ) => {
63
71
if ( ! host . exists ( 'tsconfig.json' ) ) {
64
72
return ;
65
73
}
@@ -68,7 +76,7 @@ function updateTsconfigExtendsRule(): Rule {
68
76
host . rename ( 'tsconfig.json' , 'tsconfig.base.json' ) ;
69
77
70
78
// Iterate over all tsconfig files and change the extends from 'tsconfig.json' 'tsconfig.base.json'
71
- for ( const [ tsconfigPath , extendsAst ] of visitExtendedJsonFiles ( host . root ) ) {
79
+ for ( const [ tsconfigPath , extendsAst ] of visitExtendedJsonFiles ( host . root , context . logger ) ) {
72
80
const tsConfigDir = dirname ( normalize ( tsconfigPath ) ) ;
73
81
if ( '/tsconfig.json' !== resolve ( tsConfigDir , normalize ( extendsAst . value ) ) ) {
74
82
// tsconfig extends doesn't refer to the workspace tsconfig path.
0 commit comments