Skip to content

Commit 1a01b1e

Browse files
clydinhansl
authored andcommitted
refactor(@angular/cli): validate global cli options with config set
1 parent cbc824a commit 1a01b1e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/@angular/cli/commands/config.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export interface ConfigOptions {
2020
global?: boolean;
2121
}
2222

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+
2330
/**
2431
* Splits a JSON path string into fragments. Fragments can be used to get the value referenced
2532
* 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 {
157164
}
158165

159166
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+
160176
const [config, configPath] = getWorkspaceRaw(options.global ? 'global' : 'local');
161177

162178
// TODO: Modify & save without destroying comments
163179
const configValue = config.value;
164180

165181
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+
166189
const result = setValueFromPath(configValue, options.jsonPath, value);
167190

168191
if (result === undefined) {
@@ -176,7 +199,7 @@ export default class ConfigCommand extends Command {
176199
throw new SilentError();
177200
}
178201

179-
const output = JSON.stringify(configValue);
202+
const output = JSON.stringify(configValue, null, 2);
180203
writeFileSync(configPath, output);
181204
}
182205

tests/e2e/tests/commands/config/config-global.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function() {
2525
throw new Error(`Expected "true", received "${JSON.stringify(stdout)}".`);
2626
}
2727
})
28-
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'false'))
28+
.then(() => expectToFail(() => ng('config', '--global', 'cli.warnings.notreal', 'true')))
29+
.then(() => ng('config', '--global', 'cli.warnings.versionMismatch', 'false'))
2930
.then(() => expectFileToExist(path.join(homedir(), '.angular.json')));
3031
}

0 commit comments

Comments
 (0)