refactor(@angular/cli): validate global cli options with config set

This commit is contained in:
Charles Lyding 2018-03-29 14:32:56 -04:00 committed by Hans
parent cbc824a20c
commit 1a01b1e876
2 changed files with 26 additions and 2 deletions

View File

@ -20,6 +20,13 @@ export interface ConfigOptions {
global?: boolean; 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 * 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 * 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) { 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'); const [config, configPath] = getWorkspaceRaw(options.global ? 'global' : 'local');
// TODO: Modify & save without destroying comments // TODO: Modify & save without destroying comments
const configValue = config.value; const configValue = config.value;
const value = parseJson(options.value, JsonParseMode.Loose); 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); const result = setValueFromPath(configValue, options.jsonPath, value);
if (result === undefined) { if (result === undefined) {
@ -176,7 +199,7 @@ export default class ConfigCommand extends Command {
throw new SilentError(); throw new SilentError();
} }
const output = JSON.stringify(configValue); const output = JSON.stringify(configValue, null, 2);
writeFileSync(configPath, output); writeFileSync(configPath, output);
} }

View File

@ -25,6 +25,7 @@ export default function() {
throw new Error(`Expected "true", received "${JSON.stringify(stdout)}".`); 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'))); .then(() => expectFileToExist(path.join(homedir(), '.angular.json')));
} }