1
1
import * as fs from 'fs' ;
2
2
import * as path from 'path' ;
3
3
import * as ts from 'typescript' ;
4
+ import { stripIndent } from 'common-tags' ;
4
5
5
6
import { SchemaClass , SchemaClassFactory } from '@ngtools/json-schema' ;
6
7
@@ -73,15 +74,13 @@ export class CliConfig<JsonType> {
73
74
}
74
75
75
76
static fromConfigPath < T > ( configPath : string , otherPath : string [ ] = [ ] ) : CliConfig < T > {
76
- const configContent = fs . existsSync ( configPath )
77
- ? ts . sys . readFile ( configPath )
78
- : '{}' ;
77
+ const configContent = ts . sys . readFile ( configPath ) || '{}' ;
79
78
const schemaContent = fs . readFileSync ( DEFAULT_CONFIG_SCHEMA_PATH , 'utf-8' ) ;
80
79
81
80
let otherContents = new Array < string > ( ) ;
82
81
if ( configPath !== otherPath [ 0 ] ) {
83
82
otherContents = otherPath
84
- . map ( path => fs . existsSync ( path ) && ts . sys . readFile ( path ) )
83
+ . map ( path => ts . sys . readFile ( path ) )
85
84
. filter ( content => ! ! content ) ;
86
85
}
87
86
@@ -92,18 +91,28 @@ export class CliConfig<JsonType> {
92
91
try {
93
92
content = JSON . parse ( configContent ) ;
94
93
} catch ( err ) {
95
- throw new InvalidConfigError (
96
- ' Parsing .angular-cli.json failed. Please make sure your .angular-cli.json'
97
- + ' is valid JSON. Error:\n' + err
98
- ) ;
94
+ throw new InvalidConfigError ( stripIndent `
95
+ Parsing ' ${ configPath } ' failed. Ensure the file is valid JSON.
96
+ Error: ${ err . message }
97
+ ` ) ;
99
98
}
100
99
100
+ others = otherContents . map ( otherContent => {
101
+ try {
102
+ return JSON . parse ( otherContent ) ;
103
+ } catch ( err ) {
104
+ throw new InvalidConfigError ( stripIndent `
105
+ Parsing '${ configPath } ' failed. Ensure the file is valid JSON.
106
+ Error: ${ err . message }
107
+ ` ) ;
108
+ }
109
+ } ) ;
110
+
101
111
try {
102
112
schema = JSON . parse ( schemaContent ) ;
103
- others = otherContents . map ( otherContent => JSON . parse ( otherContent ) ) ;
104
113
} catch ( err ) {
105
114
throw new InvalidConfigError (
106
- `Parsing Angular CLI schema or other configuration files failed. Error:\n${ err } `
115
+ `Parsing Angular CLI schema failed. Error:\n${ err . message } `
107
116
) ;
108
117
}
109
118
0 commit comments