2020-04-20 16:00:10 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-04-20 16:00:10 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2020-05-08 17:51:29 -04:00
|
|
|
import {assertNoErrors, getConfig, NgDevConfig} from '../utils/config';
|
|
|
|
|
|
|
|
interface Formatter {
|
|
|
|
matchers: string[];
|
|
|
|
}
|
|
|
|
|
2020-04-20 16:00:10 -04:00
|
|
|
export interface FormatConfig {
|
2020-05-08 17:51:29 -04:00
|
|
|
[keyof: string]: boolean|Formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Retrieve and validate the config as `FormatConfig`. */
|
|
|
|
export function getFormatConfig() {
|
|
|
|
// List of errors encountered validating the config.
|
|
|
|
const errors: string[] = [];
|
|
|
|
// The unvalidated config object.
|
|
|
|
const config: Partial<NgDevConfig<{format: FormatConfig}>> = getConfig();
|
|
|
|
|
|
|
|
if (config.format === undefined) {
|
|
|
|
errors.push(`No configuration defined for "format"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(config.format!)) {
|
|
|
|
switch (typeof value) {
|
|
|
|
case 'boolean':
|
|
|
|
break;
|
|
|
|
case 'object':
|
|
|
|
checkFormatterConfig(key, value, errors);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errors.push(`"format.${key}" is not a boolean or Formatter object`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertNoErrors(errors);
|
|
|
|
return config as Required<typeof config>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Validate an individual Formatter config. */
|
|
|
|
function checkFormatterConfig(key: string, config: Partial<Formatter>, errors: string[]) {
|
|
|
|
if (config.matchers === undefined) {
|
|
|
|
errors.push(`Missing "format.${key}.matchers" value`);
|
|
|
|
}
|
2020-04-20 16:00:10 -04:00
|
|
|
}
|