generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 19
/
retrieve.ts
348 lines (319 loc) · 11.9 KB
/
retrieve.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (c) 2020, 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 { dirname, join, resolve } from 'node:path';
import fs from 'node:fs';
import { Lifecycle, Messages, SfError, SfProject } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import {
ComponentSet,
ComponentSetBuilder,
RequestStatus,
RetrieveVersionData,
RetrieveResult,
RegistryAccess,
} from '@salesforce/source-deploy-retrieve';
import { SourceTracking } from '@salesforce/source-tracking';
import { Interfaces } from '@oclif/core';
import {
Flags,
loglevel,
orgApiVersionFlagWithDeprecations,
requiredOrgFlagWithDeprecations,
Ux,
} from '@salesforce/sf-plugins-core';
import { SourceCommand } from '../../../sourceCommand.js';
import {
PackageRetrieval,
RetrieveCommandResult,
RetrieveResultFormatter,
} from '../../../formatters/retrieveResultFormatter.js';
import { filterConflictsByComponentSet, trackingSetup, updateTracking } from '../../../trackingFunctions.js';
import { promisesQueue } from '../../../promiseQueue.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-source', 'retrieve');
const spinnerMessages = Messages.loadMessages('@salesforce/plugin-source', 'spinner');
const retrieveMessages = Messages.loadMessages('@salesforce/plugin-source', 'retrieve');
const replacement = 'project retrieve start';
export class Retrieve extends SourceCommand {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly requiresProject = true;
public static readonly state = 'deprecated';
public static readonly hidden = true;
public static readonly deprecationOptions = {
to: replacement,
message: messages.getMessage('deprecation', [replacement]),
};
public static readonly flags = {
'api-version': { ...orgApiVersionFlagWithDeprecations, char: 'a' as const },
loglevel,
'target-org': requiredOrgFlagWithDeprecations,
retrievetargetdir: Flags.directory({
char: 'r',
description: messages.getMessage('flags.retrievetargetdir.description'),
summary: messages.getMessage('flags.retrievetargetdir.summary'),
exclusive: ['packagenames', 'sourcepath'],
}),
sourcepath: Flags.string({
multiple: true,
delimiter: ',',
char: 'p',
description: messages.getMessage('flags.sourcePath.description'),
summary: messages.getMessage('flags.sourcePath.summary'),
exclusive: ['manifest', 'metadata'],
}),
wait: Flags.duration({
unit: 'minutes',
char: 'w',
default: Duration.minutes(SourceCommand.DEFAULT_WAIT_MINUTES),
min: 1,
description: messages.getMessage('flags.wait.description'),
summary: messages.getMessage('flags.wait.summary'),
}),
manifest: Flags.file({
char: 'x',
description: messages.getMessage('flags.manifest.description'),
summary: messages.getMessage('flags.manifest.summary'),
exclusive: ['metadata', 'sourcepath'],
}),
metadata: Flags.string({
multiple: true,
delimiter: ',',
char: 'm',
description: messages.getMessage('flags.metadata.description'),
summary: messages.getMessage('flags.metadata.summary'),
exclusive: ['manifest', 'sourcepath'],
}),
packagenames: Flags.string({
multiple: true,
delimiter: ',',
char: 'n',
summary: messages.getMessage('flags.packagename.summary'),
}),
tracksource: Flags.boolean({
char: 't',
summary: messages.getMessage('flags.tracksource.summary'),
}),
forceoverwrite: Flags.boolean({
char: 'f',
summary: messages.getMessage('flags.forceoverwrite.summary'),
dependsOn: ['tracksource'],
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
}),
};
protected readonly lifecycleEventNames = ['preretrieve', 'postretrieve'];
protected retrieveResult!: RetrieveResult;
protected tracking!: SourceTracking;
private resolvedTargetDir!: string;
private flags!: Interfaces.InferredFlags<typeof Retrieve.flags>;
private registry = new RegistryAccess();
public async run(): Promise<RetrieveCommandResult> {
this.flags = (await this.parse(Retrieve)).flags;
await this.preChecks();
await this.retrieve();
this.resolveSuccess();
await this.maybeUpdateTracking();
await this.moveResultsForRetrieveTargetDir();
return this.formatResult();
}
protected async preChecks(): Promise<void> {
if (this.flags.retrievetargetdir) {
this.resolvedTargetDir = resolve(this.flags.retrievetargetdir);
if (this.overlapsPackage()) {
throw messages.createError('retrieveTargetDirOverlapsPackage', [this.flags.retrievetargetdir]);
}
}
// we need something to retrieve
const retrieveInputs = [this.flags.manifest, this.flags.metadata, this.flags.sourcepath, this.flags.packagenames];
if (!retrieveInputs.some((x) => x)) {
throw new SfError(messages.getMessage('nothingToRetrieve'));
}
if (this.flags.tracksource) {
this.tracking = await trackingSetup({
ux: new Ux({ jsonEnabled: this.jsonEnabled() }),
org: this.flags['target-org'],
project: this.project!,
ignoreConflicts: true,
});
}
}
protected async retrieve(): Promise<void> {
const username = this.flags['target-org'].getUsername() as string;
// eslint-disable-next-line @typescript-eslint/require-await
Lifecycle.getInstance().on('apiVersionRetrieve', async (apiData: RetrieveVersionData) => {
this.log(
retrieveMessages.getMessage('apiVersionMsgDetailed', [
'Retrieving',
apiData.manifestVersion,
username,
apiData.apiVersion,
])
);
});
this.spinner.start(spinnerMessages.getMessage('retrieve.componentSetBuild'));
this.componentSet = await ComponentSetBuilder.build({
apiversion: this.flags['api-version'],
sourceapiversion: await this.getSourceApiVersion(),
packagenames: this.flags.packagenames,
sourcepath: this.flags.sourcepath,
manifest: this.flags.manifest
? {
manifestPath: this.flags.manifest,
directoryPaths: this.flags.retrievetargetdir ? [] : this.getPackageDirs(),
}
: undefined,
metadata: this.flags.metadata && {
metadataEntries: this.flags.metadata,
directoryPaths: this.flags.retrievetargetdir ? [] : this.getPackageDirs(),
},
});
if (this.flags.manifest ?? this.flags.metadata) {
if (this.wantsToRetrieveCustomFields()) {
this.warn(messages.getMessage('wantsToRetrieveCustomFields'));
this.componentSet.add({
fullName: ComponentSet.WILDCARD,
type: this.registry.getTypeByName('CustomObject'),
});
}
}
if (this.flags.tracksource) {
// will throw if conflicts exist
if (!this.flags.forceoverwrite) {
await filterConflictsByComponentSet({
tracking: this.tracking,
components: this.componentSet,
ux: new Ux({ jsonEnabled: this.jsonEnabled() }),
});
}
const remoteDeletes = await this.tracking.getChanges<string>({
origin: 'remote',
state: 'delete',
format: 'string',
});
if (remoteDeletes.length) {
this.warn(messages.getMessage('retrieveWontDelete'));
}
}
await Lifecycle.getInstance().emit('preretrieve', this.componentSet.toArray());
this.spinner.status = spinnerMessages.getMessage('retrieve.sendingRequest');
const mdapiRetrieve = await this.componentSet.retrieve({
usernameOrConnection: username,
merge: true,
output: this.resolvedTargetDir || this.project!.getDefaultPackage().fullPath,
packageOptions: this.flags.packagenames,
});
this.spinner.status = spinnerMessages.getMessage('retrieve.polling');
this.retrieveResult = await mdapiRetrieve.pollStatus({ timeout: this.flags.wait });
await Lifecycle.getInstance().emit('postretrieve', this.retrieveResult.getFileResponses());
this.spinner.stop();
}
protected resolveSuccess(): void {
const StatusCodeMap = new Map<RequestStatus, number>([
[RequestStatus.Succeeded, 0],
[RequestStatus.Canceled, 1],
[RequestStatus.Failed, 1],
[RequestStatus.InProgress, 69],
[RequestStatus.Pending, 69],
[RequestStatus.Canceling, 69],
]);
this.setExitCode(StatusCodeMap.get(this.retrieveResult.response.status) ?? 1);
}
protected async formatResult(): Promise<RetrieveCommandResult> {
const packages: PackageRetrieval[] = [];
const projectPath = await SfProject.resolveProjectPath();
(this.flags.packagenames ?? []).forEach((name) => {
packages.push({ name, path: join(projectPath, name) });
});
const formatterOptions = {
waitTime: this.flags.wait.quantity,
verbose: this.flags.verbose ?? false,
packages,
};
const formatter = new RetrieveResultFormatter(
new Ux({ jsonEnabled: this.jsonEnabled() }),
formatterOptions,
this.retrieveResult
);
// Only display results to console when JSON flag is unset.
if (!this.jsonEnabled()) {
formatter.display();
}
return formatter.getJson();
}
private async maybeUpdateTracking(): Promise<void> {
if (this.flags.tracksource ?? false) {
return updateTracking({
tracking: this.tracking,
result: this.retrieveResult,
ux: new Ux({ jsonEnabled: this.jsonEnabled() }),
});
}
}
private wantsToRetrieveCustomFields(): boolean {
const hasCustomField = this.componentSet?.has({
type: this.registry.getTypeByName('CustomField'),
fullName: ComponentSet.WILDCARD,
});
const hasCustomObject = this.componentSet?.has({
type: this.registry.getTypeByName('CustomObject'),
fullName: ComponentSet.WILDCARD,
});
return (hasCustomField && !hasCustomObject) as boolean;
}
private async moveResultsForRetrieveTargetDir(): Promise<void> {
async function mv(src: string): Promise<string[]> {
let directories: string[] = [];
let files: string[] = [];
const srcStat = await fs.promises.stat(src);
if (srcStat.isDirectory()) {
const contents = await fs.promises.readdir(src, { withFileTypes: true });
[directories, files] = contents.reduce<[string[], string[]]>(
(acc, dirent) => {
if (dirent.isDirectory()) {
acc[0].push(dirent.name);
} else {
acc[1].push(dirent.name);
}
return acc;
},
[[], []]
);
directories = directories.map((dir) => join(src, dir));
} else {
files.push(src);
}
await promisesQueue(
files,
async (file: string): Promise<string> => {
const dest = join(src.replace(join('main', 'default'), ''), file);
const destDir = dirname(dest);
await fs.promises.mkdir(destDir, { recursive: true });
await fs.promises.rename(join(src, file), dest);
return dest;
},
50
);
return directories;
}
if (!this.flags.retrievetargetdir) {
return;
}
// move contents of 'main/default' to 'retrievetargetdir'
await promisesQueue([join(this.resolvedTargetDir, 'main', 'default')], mv, 5, true);
// remove 'main/default'
await fs.promises.rm(join(this.flags.retrievetargetdir, 'main'), { recursive: true });
this.retrieveResult.getFileResponses().forEach((fileResponse) => {
fileResponse.filePath = fileResponse.filePath?.replace(join('main', 'default'), '');
});
}
private overlapsPackage(): boolean {
return !!this.project!.getPackageNameFromPath(this.resolvedTargetDir);
}
}