diff --git a/packages/@angular/cli/commands/config.ts b/packages/@angular/cli/commands/config.ts index e83740e590..70eb8b24af 100644 --- a/packages/@angular/cli/commands/config.ts +++ b/packages/@angular/cli/commands/config.ts @@ -20,6 +20,13 @@ export interface ConfigOptions { global?: boolean; } +const validCliPaths = new Map([ + ['cli.warnings.versionMismatch', 'boolean'], + ['cli.warnings.typescriptMismatch', 'boolean'], + ['cli.defaultCollection', 'string'], + ['cli.packageManager', 'string'], +]); + /** * Splits a JSON path string into fragments. Fragments can be used to get the value referenced * 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 { } private set(options: ConfigOptions) { + if (!options.jsonPath || !options.jsonPath.trim()) { + throw new Error('Invalid Path.'); + } + if (options.global + && !options.jsonPath.startsWith('schematics.') + && !validCliPaths.has(options.jsonPath)) { + throw new Error('Invalid Path.'); + } + const [config, configPath] = getWorkspaceRaw(options.global ? 'global' : 'local'); // TODO: Modify & save without destroying comments const configValue = config.value; const value = parseJson(options.value, JsonParseMode.Loose); + const pathType = validCliPaths.get(options.jsonPath); + if (pathType) { + if (typeof value != pathType) { + throw new Error(`Invalid value type; expected a ${pathType}.`); + } + } + const result = setValueFromPath(configValue, options.jsonPath, value); if (result === undefined) { @@ -176,7 +199,7 @@ export default class ConfigCommand extends Command { throw new SilentError(); } - const output = JSON.stringify(configValue); + const output = JSON.stringify(configValue, null, 2); writeFileSync(configPath, output); } diff --git a/tests/e2e/tests/commands/config/config-global.ts b/tests/e2e/tests/commands/config/config-global.ts index d4443cb964..d351404441 100644 --- a/tests/e2e/tests/commands/config/config-global.ts +++ b/tests/e2e/tests/commands/config/config-global.ts @@ -25,6 +25,7 @@ export default function() { throw new Error(`Expected "true", received "${JSON.stringify(stdout)}".`); } }) - .then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'false')) + .then(() => expectToFail(() => ng('config', '--global', 'cli.warnings.notreal', 'true'))) + .then(() => ng('config', '--global', 'cli.warnings.versionMismatch', 'false')) .then(() => expectFileToExist(path.join(homedir(), '.angular.json'))); }