This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ConfigurationValidator.ts
132 lines (125 loc) · 6.32 KB
/
ConfigurationValidator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { IImperativeConfig } from "./doc/IImperativeConfig";
import { isNullOrUndefined } from "util";
import { TextUtils } from "../../utilities";
import { ImperativeError } from "../../error";
import { ICommandProfileProperty } from "../../cmd/src/doc/profiles/definition/ICommandProfileProperty";
/**
* Imperative-internal class to validate configuration
* You should not need to call this from your CLI.
*/
export class ConfigurationValidator {
/**
* Validate a configuration object that has already been completely loaded from
* the consumer of Imperative.
* @throws errors if the configuration is not valid
* @param {IImperativeConfig} config
*/
public static validate(config: IImperativeConfig): void {
const getMissingFieldError = (fieldName: string) => {
return new ImperativeError({
msg: "Your Imperative configuration was missing the following required field: "
+ fieldName + ". Please provide this field in order to use Imperative"
});
};
if (isNullOrUndefined(config.productDisplayName)) {
throw getMissingFieldError("productDisplayName");
}
if (isNullOrUndefined(config.commandModuleGlobs) && isNullOrUndefined(config.definitions)) {
throw new ImperativeError({
msg: "Your Imperative configuration had neither \"definitions\"" +
" nor \"commandModuleGlobs\". At least one of these fields is required so that the syntax for " +
"your CLI can be defined."
});
}
if (isNullOrUndefined(config.primaryTextColor)) {
config.primaryTextColor = "yellow";
} else {
// if the user specified a color, test to make sure it works
ConfigurationValidator.verifyChalkColor(config, "primaryTextColor", "primary text highlighting");
}
if (isNullOrUndefined(config.secondaryTextColor)) {
config.secondaryTextColor = "blue";
} else {
// if the user specified a color, test to make sure it works
ConfigurationValidator.verifyChalkColor(config, "secondaryTextColor", "secondary text highlighting");
}
if (isNullOrUndefined(config.allowConfigGroup)) {
// default allowConfigGroup to true
config.allowConfigGroup = true;
}
if (isNullOrUndefined(config.allowPlugins)) {
// default allowPlugins to true
config.allowPlugins = true;
}
// validate profile configurations
if (!isNullOrUndefined(config.profiles)) {
for (const profileConfig of config.profiles) {
if (isNullOrUndefined(profileConfig.schema)) {
throw new ImperativeError({
msg: "Your Imperative profile configuration of type \"" + profileConfig.type +
"\" has no schema. Please provide a schema for your profile so that it can be used to " +
"validate the structure and content of the user's profiles, as well as " +
"generate commands."
});
} else {
for (const propertyName of Object.keys(profileConfig.schema.properties)) {
const property: ICommandProfileProperty = profileConfig.schema.properties[propertyName];
if (!isNullOrUndefined(property.optionDefinitions) &&
property.optionDefinitions.length > 1 &&
isNullOrUndefined(profileConfig.createProfileFromArgumentsHandler)) {
throw new ImperativeError({
msg: TextUtils.formatMessage(
"Your Imperative profile configuration of type \"{{type}}\"" +
" has the schema property \"{{property}}\", which has multiple " +
"option definitions, but no handler for creating a profile from " +
"command line arguments. Imperative will not be able to determine " +
"how to map multiple command line arguments to a single profile property " +
"unless you provide a custom handler.",
{
type: profileConfig.type,
property: propertyName
})
}
);
}
}
}
}
}
}
/**
* Private utility to validate a consumer's chalk color setting
* @param config - the config object from the consumer
* @param {string} colorProperty - the property of the config object
* @param {string} colorPropertyDescriptiveName - the display name of the color you're validating
*/
private static verifyChalkColor(config: any, colorProperty: string, colorPropertyDescriptiveName: string) {
config[colorProperty] = config[colorProperty].toLowerCase();
try {
const chalk = require("chalk");
const highlighterFunction: any = chalk[config[colorProperty]];
highlighterFunction("test");
if (TextUtils.AVAILABLE_CHALK_COLORS.indexOf(config[colorProperty]) < 0) {
throw new ImperativeError(
{
msg: "The value specified was not one of the basic colors " +
"available through the chalk package."
});
}
} catch (e) {
e.message = "The value you specified for the " + colorPropertyDescriptiveName + " color (\"" + config[colorProperty] + "\") " +
"could not be successfully verified. The following error was caused while attempting to test the color: " + e.message;
throw e;
}
}
}