generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.ts
130 lines (115 loc) · 4.33 KB
/
list.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
/*
* Copyright (c) 2021, 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 { Interfaces } from '@oclif/core';
import { Messages } from '@salesforce/core';
import { Flags, SfCommand, JsonObject, SfHook, EnvList as Env } from '@salesforce/sf-plugins-core';
import { toKey, toValue } from '../../utils.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
const messages = Messages.loadMessages('@salesforce/plugin-env', 'list');
const envTypeValues = Object.keys(Env.EnvType);
const envOrderBy = (a: Env.Table<JsonObject>, b: Env.Table<JsonObject>): number => {
// both a && b are user defined - use natural sort
if (!envTypeValues.includes(a.type) && !envTypeValues.includes(a.type)) {
return a.type.localeCompare(b.type);
}
// well known always come before user defined
if (envTypeValues.includes(a.type)) return -1;
return 1;
};
const buildColumns = (table: Env.Table<JsonObject>): Record<string, { header?: string }> =>
table.data.flatMap(Object.keys).reduce<Record<string, { header?: string }>>((x, y) => {
if (x[y]) return x;
const columnEntry = {
header: toKey(y, table.keys),
get: (v: JsonObject): string | number | boolean => toValue(v[y]),
};
return { ...x, [y]: columnEntry };
}, {});
export type Environments = {
[type: string]: JsonObject[];
};
export default class EnvList extends SfCommand<Environments> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
all: Flags.boolean({
summary: messages.getMessage('flags.all.summary'),
char: 'a',
}),
columns: Flags.string({
summary: messages.getMessage('flags.columns.summary'),
multiple: true,
}),
csv: Flags.boolean({
summary: messages.getMessage('flags.csv.summary'),
}),
filter: Flags.string({
summary: messages.getMessage('flags.filter.summary'),
}),
'no-header': Flags.boolean({
summary: messages.getMessage('flags.no-header.summary'),
}),
'no-truncate': Flags.boolean({
summary: messages.getMessage('flags.no-truncate.summary'),
}),
output: Flags.string({
summary: messages.getMessage('flags.output.summary'),
options: ['csv', 'json', 'yaml'],
}),
sort: Flags.string({
summary: messages.getMessage('flags.sort.summary'),
}),
};
private flags!: Interfaces.InferredFlags<typeof EnvList.flags>;
public async run(): Promise<Environments> {
this.warn(messages.getMessage('warning.orgsNoLongerSupported', [this.config.bin]));
this.flags = (await this.parse(EnvList)).flags;
const tableOpts = {
columns: this.flags.columns?.join(','),
csv: this.flags.csv,
filter: this.flags.filter,
'no-header': this.flags['no-header'],
'no-truncate': this.flags['no-truncate'],
output: this.flags.output,
sort: this.flags.sort,
};
let final: Environments = {};
const results = await SfHook.run(this.config, 'sf:env:list', { all: this.flags.all });
const tables = results.successes
.map((r) => r.result)
.reduce((x, y) => x.concat(y), [])
.filter((t) => t.data.length > 0)
.sort(envOrderBy);
if (tables.length === 0) {
this.log(messages.getMessage('error.NoResultsFound'));
return {};
} else {
for (const table of tables) {
final = { ...final, ...{ [table.type]: table.data } };
const columns = buildColumns(table);
if (this.checkTableForNamedColumns(columns)) {
this.table(table.data, columns, { ...tableOpts, title: table.title });
this.log();
} else {
this.warn(
messages.getMessage('warning.RequestedColumnsNotPresentInEnvironment', [tableOpts.columns, table.title])
);
}
}
}
return final;
}
private checkTableForNamedColumns(columns: Record<string, { header?: string }>): boolean {
return Object.entries(columns).some(([, value]) => {
if (this.flags?.columns) {
return value.header && this.flags?.columns.includes(value.header);
}
return true;
});
}
}