-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathconfig.ts
More file actions
109 lines (100 loc) · 3.48 KB
/
config.ts
File metadata and controls
109 lines (100 loc) · 3.48 KB
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
import { isAppStudio } from '@sap-ux/btp-utils';
import type { AbapTarget, AbapDeployConfig } from '../types';
import { isUrlTarget } from '@sap-ux/system-access';
import type { BspConfig } from '@sap-ux/axios-extension';
import type { Logger } from '@sap-ux/logger';
/**
* Clones the given config and removes secrets so that it can be printed to a log file.
*
* @param config - config object
* @returns config object that can be logged
*/
export function getConfigForLogging(
config: AbapDeployConfig
): AbapDeployConfig | (Omit<AbapDeployConfig, 'credentials'> & { credentials: 'hidden' }) {
if (config.credentials?.password) {
return {
...config,
credentials: 'hidden'
};
} else {
return config;
}
}
/**
* Helper function for throwing a missing property error.
*
* @param property Invalid missing property
*/
export function throwConfigMissingError(property: string): void {
throw new Error(`Invalid deployment configuration. Property ${property} is missing.`);
}
/**
* Validate the given target config. If anything mandatory is missing throw an error.
*
* @param target - target configuration to be validated
* @returns reference to the given target config
*/
function validateTarget(target: AbapTarget): AbapTarget {
if (isUrlTarget(target)) {
if (target.client) {
target.client = (target.client + '').padStart(3, '0');
}
} else if (!target.destination) {
throwConfigMissingError(isAppStudio() ? 'target-destination' : 'target-url');
}
return target;
}
/**
* Validates that credentials are provided as environment variable references, not as plain text.
*
* @param credentials - credentials to validate
*/
function validateCredentials(credentials: NonNullable<AbapDeployConfig['credentials']>): void {
const isEnvRef = (value: string | undefined): boolean => !value || value.startsWith('env:');
if (!isEnvRef(credentials.username) || !isEnvRef(credentials.password)) {
throw new Error(
'Credentials must be provided as environment variable references (e.g. env:MY_VAR), not as plain text.'
);
}
}
/**
* Type checking the config object.
*
* @param config - config to be checked
* @returns true if it is of type BSP config
*/
export function isBspConfig(config: Partial<BspConfig>): config is BspConfig {
return (config as BspConfig).name !== undefined;
}
/**
* Validate the given config. If anything mandatory is missing throw an error.
*
* @param config - the config to be validated
* @param logger Logger used by deploy tooling
* @returns reference to the given config
*/
export function validateConfig(config: AbapDeployConfig | undefined, logger?: Logger): AbapDeployConfig {
if (!config) {
throw new Error('The deployment configuration is missing.');
}
if (config.app.package && config.app.package !== config.app.package.toUpperCase()) {
const normalized = config.app.package.toUpperCase();
logger?.warn(
`Package name '${config.app.package}' was normalized to '${normalized}'. Lowercase package names may cause deployment failures.`
);
config.app.package = normalized;
}
if (config.target) {
config.target = validateTarget(config.target);
} else {
throwConfigMissingError('target');
}
if (!config.app) {
throwConfigMissingError('app');
}
if (config.credentials) {
validateCredentials(config.credentials);
}
return config;
}