-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.ts
96 lines (82 loc) · 3.19 KB
/
util.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
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {
EnvironmentVariable,
OrgConfigProperties,
ORG_CONFIG_ALLOWED_PROPERTIES,
SfdxPropertyKeys,
SFDX_ALLOWED_PROPERTIES,
SUPPORTED_ENV_VARS,
Messages,
} from '@salesforce/core';
import { HelpSection, HelpSectionKeyValueTable } from '@oclif/core';
/**
* Function to build a help section for command help.
* Takes a string to be used as section header text and an array of enums
* that identify the variable or property to be included in the help
* body.
*
* @param header
* @param vars
*/
export function toHelpSection(
header: string,
...vars: Array<OrgConfigProperties | SfdxPropertyKeys | EnvironmentVariable | string | Record<string, string>>
): HelpSection {
const body = vars
.flatMap((v) => {
if (typeof v === 'string') {
const orgConfig = ORG_CONFIG_ALLOWED_PROPERTIES.find(({ key }) => key.toString() === v);
if (orgConfig) {
return { name: orgConfig.key, description: orgConfig.description };
}
const sfdxProperty = SFDX_ALLOWED_PROPERTIES.find(({ key }) => key.toString() === v);
if (sfdxProperty) {
return { name: sfdxProperty.key.valueOf(), description: sfdxProperty.description };
}
const envVar = Object.entries(SUPPORTED_ENV_VARS).find(([k]) => k === v);
if (envVar) {
const [eKey, data] = envVar;
return { name: eKey, description: data.description };
}
return undefined;
} else {
return Object.entries(v).map(([name, description]) => ({ name, description }));
}
})
.filter(isHelpSectionBodyEntry);
return { header, body };
}
const isHelpSectionBodyEntry = (entry: unknown): entry is HelpSectionKeyValueTable[number] =>
typeof entry === 'object' && entry !== null && 'name' in entry && 'description' in entry;
export function parseVarArgs(args: Record<string, unknown>, argv: string[]): Record<string, string | undefined> {
const final: Record<string, string | undefined> = {};
const argVals = Object.values(args);
// Remove arguments from varargs
const varargs = argv.filter((val) => !argVals.includes(val));
// Support `config set key value`
if (varargs.length === 2 && !varargs[0].includes('=')) {
return { [varargs[0]]: varargs[1] };
}
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');
// Ensure that all args are in the right format (e.g. key=value key1=value1)
varargs.forEach((arg) => {
const split = arg.split('=');
if (split.length !== 2) {
throw messages.createError('error.InvalidArgumentFormat', [arg]);
}
const [name, value] = split;
if (final[name]) {
throw messages.createError('error.DuplicateArgument', [name]);
}
final[name] = value || undefined;
});
return final;
}
export const removeEmpty = (obj: Record<string, unknown>): Record<string, unknown> =>
Object.fromEntries(Object.entries(obj).filter(([, v]) => v != null));