@@ -20,6 +20,13 @@ export interface ConfigOptions {
20
20
global ?: boolean ;
21
21
}
22
22
23
+ const validCliPaths = new Map ( [
24
+ [ 'cli.warnings.versionMismatch' , 'boolean' ] ,
25
+ [ 'cli.warnings.typescriptMismatch' , 'boolean' ] ,
26
+ [ 'cli.defaultCollection' , 'string' ] ,
27
+ [ 'cli.packageManager' , 'string' ] ,
28
+ ] ) ;
29
+
23
30
/**
24
31
* Splits a JSON path string into fragments. Fragments can be used to get the value referenced
25
32
* by the path. For example, a path of "a[3].foo.bar[2]" would give you a fragment array of
@@ -157,12 +164,28 @@ export default class ConfigCommand extends Command {
157
164
}
158
165
159
166
private set ( options : ConfigOptions ) {
167
+ if ( ! options . jsonPath || ! options . jsonPath . trim ( ) ) {
168
+ throw new Error ( 'Invalid Path.' ) ;
169
+ }
170
+ if ( options . global
171
+ && ! options . jsonPath . startsWith ( 'schematics.' )
172
+ && ! validCliPaths . has ( options . jsonPath ) ) {
173
+ throw new Error ( 'Invalid Path.' ) ;
174
+ }
175
+
160
176
const [ config , configPath ] = getWorkspaceRaw ( options . global ? 'global' : 'local' ) ;
161
177
162
178
// TODO: Modify & save without destroying comments
163
179
const configValue = config . value ;
164
180
165
181
const value = parseJson ( options . value , JsonParseMode . Loose ) ;
182
+ const pathType = validCliPaths . get ( options . jsonPath ) ;
183
+ if ( pathType ) {
184
+ if ( typeof value != pathType ) {
185
+ throw new Error ( `Invalid value type; expected a ${ pathType } .` ) ;
186
+ }
187
+ }
188
+
166
189
const result = setValueFromPath ( configValue , options . jsonPath , value ) ;
167
190
168
191
if ( result === undefined ) {
@@ -176,7 +199,7 @@ export default class ConfigCommand extends Command {
176
199
throw new SilentError ( ) ;
177
200
}
178
201
179
- const output = JSON . stringify ( configValue ) ;
202
+ const output = JSON . stringify ( configValue , null , 2 ) ;
180
203
writeFileSync ( configPath , output ) ;
181
204
}
182
205
0 commit comments