-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.ts
212 lines (186 loc) · 7.77 KB
/
generate.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2022, jayree
* 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 {
SfCommand,
Flags,
arrayWithDeprecation,
orgApiVersionFlagWithDeprecations,
requiredOrgFlagWithDeprecations,
Ux,
} from '@salesforce/sf-plugins-core';
import { FileProperties, ListMetadataQuery } from '@salesforce/source-deploy-retrieve/lib/src/client/types.js';
import { Messages, Logger, Connection } from '@salesforce/core';
import { RegistryAccess, ComponentSet, PackageManifestObject } from '@salesforce/source-deploy-retrieve';
import { ensureArray } from '@salesforce/kit';
import fs from 'fs-extra';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const registryAccess = new RegistryAccess();
const messages = Messages.loadMessages('@jayree/sfdx-plugin-manifest', 'manifestgenerate');
export interface QueryResult {
size: number;
totalSize: number;
done: boolean;
queryLocator: string;
entityTypeName: string;
records: StdValueSetRecord[] | FlowDefinitionRecord[];
}
export interface StdValueSetRecord {
Id: string;
MasterLabel: string;
Metadata: { standardValue: Array<Record<string, unknown>> };
}
export interface FlowDefinitionRecord {
DeveloperName: string;
ActiveVersion: { VersionNumber: string };
LatestVersion: { VersionNumber: string };
}
export default class GeneratePackageXML extends SfCommand<PackageManifestObject> {
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 = {
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
'quick-filter': arrayWithDeprecation({
char: 'q',
description: messages.getMessage('flags.quick-filter.summary'),
deprecateAliases: true,
aliases: ['quickfilter'],
}),
'match-case': Flags.boolean({
char: 'c',
summary: messages.getMessage('flags.match-case.summary'),
deprecateAliases: true,
aliases: ['matchcase'],
}),
'match-whole-word': Flags.boolean({
char: 'w',
summary: messages.getMessage('flags.match-whole-word.summary'),
deprecateAliases: true,
aliases: ['matchwholeword'],
}),
'include-flow-versions': Flags.boolean({
summary: messages.getMessage('flags.include-flow-versions.summary'),
deprecateAliases: true,
aliases: ['includeflowversions'],
}),
file: Flags.string({
char: 'f',
summary: messages.getMessage('flags.file.summary'),
}),
'exclude-managed': Flags.boolean({
summary: messages.getMessage('flags.exclude-managed.summary'),
exclusive: ['exclude-all'],
deprecateAliases: true,
aliases: ['excludemanaged', 'x'],
}),
'exclude-all': Flags.boolean({
summary: messages.getMessage('flags.exclude-all.summary'),
exclusive: ['exclude-managed'],
deprecateAliases: true,
aliases: ['excludeall', 'a'],
}),
};
private logger!: Logger;
private conn!: Connection;
public async run(): Promise<PackageManifestObject> {
this.logger = await Logger.child('jayree:manifest:generate');
const { flags } = await this.parse(GeneratePackageXML);
this.conn = flags['target-org'].getConnection(flags['api-version']);
const ux = new Ux({ jsonEnabled: this.jsonEnabled() });
const file = flags['file'];
ux.spinner.start(`Generating ${file ?? 'package.xml'}`);
const managed = ['beta', 'deleted', 'deprecated', 'installed', 'released'];
const all = ['beta', 'deleted', 'deprecated', 'installed', 'released', 'installedEditable', 'deprecatedEditable'];
const componentFilter = (component: Partial<FileProperties>): boolean => {
const isNamespaceUndefined = component.namespacePrefix && component.manageableState === undefined;
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
const isTranslationsBeta = component.type === 'Translations' && component.manageableState === 'beta';
const isExcludedManaged =
flags['exclude-managed'] &&
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(isNamespaceUndefined ||
(component.manageableState && managed.includes(component.manageableState) && !isTranslationsBeta));
const isExcludedAll =
flags['exclude-all'] &&
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
(isNamespaceUndefined ||
(component.manageableState && all.includes(component.manageableState) && !isTranslationsBeta));
return !(isExcludedManaged ?? isExcludedAll);
};
let componentSet = await ComponentSet.fromConnection({
usernameOrConnection: this.conn,
componentFilter,
});
if (flags['include-flow-versions']) {
const flowResult = await this.listMembers({ type: 'Flow' }, '43.0');
for (const component of flowResult.filter(componentFilter)) {
componentSet.add({ fullName: component.fullName, type: registryAccess.getTypeByName(component.type) });
}
}
const quickFilter = flags['quick-filter'];
if (quickFilter) {
componentSet = componentSet.filter((component) => {
let filter = quickFilter;
const comp: { fullName: string; type: string } = { fullName: component.fullName, type: component.type.name };
if (!flags['match-case']) {
filter = quickFilter.join('~').toLowerCase().split('~');
comp.fullName = component.fullName.toLocaleLowerCase();
comp.type = component.type.name.toLowerCase();
}
if (flags['match-whole-word']) {
return filter.includes(comp.fullName) || filter.includes(comp.type);
} else {
return filter.some((str) => comp.fullName.includes(str));
}
});
}
const hasFlows = componentSet.toArray().filter((component) => component.type.name === 'Flow');
if (hasFlows.length) {
try {
const flowDefinitionQuery = (await this.conn.tooling.query(
`SELECT DeveloperName, ActiveVersion.VersionNumber, LatestVersion.VersionNumber FROM FlowDefinition where DeveloperName in (${hasFlows
.map((component) => `'${component.fullName}'`)
.toString()})`,
)) as QueryResult;
const flowDefinitionRecods = flowDefinitionQuery.records as FlowDefinitionRecord[];
for (const record of flowDefinitionRecods) {
if (record.LatestVersion?.VersionNumber !== record.ActiveVersion?.VersionNumber) {
this.log(
`DeveloperName: ${record.DeveloperName}, ActiveVersion: ${record.ActiveVersion?.VersionNumber}, LatestVersion: ${record.LatestVersion?.VersionNumber}`,
);
}
}
} catch (error) {
this.logger.debug((error as Error).message);
}
}
componentSet = componentSet.filter((component) => component.type.name !== 'FlowDefinition');
componentSet.apiVersion = this.conn.getApiVersion();
if (file) {
await fs.ensureFile(file);
await fs.writeFile(file, await componentSet.getPackageXml());
} else {
this.log(await componentSet.getPackageXml());
}
ux.spinner.stop();
return componentSet.getObject();
}
protected async listMembers(query: ListMetadataQuery, apiVersion?: string): Promise<FileProperties[]> {
let members: FileProperties[];
try {
if (!apiVersion) {
apiVersion = this.conn.getApiVersion();
}
members = ensureArray((await this.conn.metadata.list(query, apiVersion)) as FileProperties[]);
} catch (error) {
members = [];
this.logger.debug((error as Error).message);
}
return members;
}
}