generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 21
/
source.ts
506 lines (451 loc) · 19.6 KB
/
source.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* 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 fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { Interfaces } from '@oclif/core';
import { Lifecycle, Messages, Org, SfError } from '@salesforce/core';
import {
ComponentSet,
ComponentSetBuilder,
ComponentStatus,
DeployResult,
DestructiveChangesType,
FileResponse,
FileResponseSuccess,
MetadataComponent,
RequestStatus,
SourceComponent,
} from '@salesforce/source-deploy-retrieve';
import { Duration } from '@salesforce/kit';
import { ChangeResult, ConflictResponse, deleteCustomLabels, SourceTracking } from '@salesforce/source-tracking';
import {
arrayWithDeprecation,
Flags,
loglevel,
orgApiVersionFlagWithDeprecations,
requiredOrgFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
import { DeployStages } from '../../../utils/deployStages.js';
import { writeConflictTable } from '../../../utils/conflicts.js';
import { isNonDecomposedCustomLabel, isNonDecomposedCustomLabelsOrCustomLabel } from '../../../utils/metadataTypes.js';
import { getFileResponseSuccessProps, tableHeader } from '../../../utils/output.js';
import { API, DeleteSourceJson, isFileResponseDeleted, isSdrSuccess, isSourceComponent } from '../../../utils/types.js';
import { getPackageDirs, getSourceApiVersion } from '../../../utils/project.js';
import { resolveApi, validateTests } from '../../../utils/deploy.js';
import { DeployResultFormatter } from '../../../formatters/deployResultFormatter.js';
import { DeleteResultFormatter } from '../../../formatters/deleteResultFormatter.js';
import { DeployCache } from '../../../utils/deployCache.js';
import { testLevelFlag, testsFlag } from '../../../utils/flags.js';
const testFlags = 'Test';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-deploy-retrieve', 'delete.source');
const xorFlags = ['metadata', 'source-dir'];
type MixedDeployDelete = { deploy: string[]; delete: FileResponseSuccess[] };
export class Source extends SfCommand<DeleteSourceJson> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly aliases = ['force:source:delete'];
public static readonly deprecateAliases = true;
public static readonly requiresProject = true;
public static readonly flags = {
'api-version': orgApiVersionFlagWithDeprecations,
loglevel,
'target-org': requiredOrgFlagWithDeprecations,
'check-only': Flags.boolean({
aliases: ['checkonly'],
deprecateAliases: true,
char: 'c',
description: messages.getMessage('flags.check-only.description'),
summary: messages.getMessage('flags.check-only.summary'),
}),
wait: Flags.duration({
unit: 'minutes',
char: 'w',
default: Duration.minutes(33),
min: 1,
description: messages.getMessage('flags.wait.description'),
summary: messages.getMessage('flags.wait.summary'),
}),
tests: testsFlag({
helpGroup: testFlags,
char: undefined,
}),
'test-level': testLevelFlag({
aliases: ['testlevel'],
deprecateAliases: true,
helpGroup: testFlags,
description: messages.getMessage('flags.test-Level.description'),
summary: messages.getMessage('flags.test-Level.summary'),
}),
'no-prompt': Flags.boolean({
char: 'r',
aliases: ['noprompt'],
deprecateAliases: true,
summary: messages.getMessage('flags.no-prompt.summary'),
}),
metadata: arrayWithDeprecation({
char: 'm',
description: messages.getMessage('flags.metadata.description'),
summary: messages.getMessage('flags.metadata.summary'),
exactlyOne: xorFlags,
}),
'source-dir': arrayWithDeprecation({
char: 'p',
aliases: ['sourcepath'],
deprecateAliases: true,
description: messages.getMessage('flags.source-dir.description'),
summary: messages.getMessage('flags.source-dir.summary'),
exactlyOne: xorFlags,
}),
'track-source': Flags.boolean({
char: 't',
aliases: ['tracksource'],
deprecateAliases: true,
summary: messages.getMessage('flags.track-source.summary'),
exclusive: ['check-only'],
}),
'force-overwrite': Flags.boolean({
char: 'f',
aliases: ['forceoverwrite'],
deprecateAliases: true,
summary: messages.getMessage('flags.force-overwrite.summary'),
dependsOn: ['track-source'],
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
}),
};
protected fileResponses: FileResponse[] | undefined;
protected tracking: SourceTracking | undefined;
private components: MetadataComponent[] | undefined;
// create the delete FileResponse as we're parsing the comp. set to use in the output
private mixedDeployDelete: MixedDeployDelete = { delete: [], deploy: [] };
// map of component in project, to where it is stashed
private stashPath = new Map<string, string>();
private flags!: Interfaces.InferredFlags<typeof Source.flags>;
private org!: Org;
private componentSet!: ComponentSet;
private deployResult!: DeployResult;
public async run(): Promise<DeleteSourceJson> {
this.flags = (await this.parse(Source)).flags;
this.org = this.flags['target-org'];
await this.preChecks();
await this.delete();
await this.resolveSuccess();
const result = this.formatResult();
// The DeleteResultFormatter will use SDR and scan the directory, if the files have been deleted, it will throw an error
// so we'll delete the files locally now
await this.deleteFilesLocally();
// makes sure files are deleted before updating tracking files
await this.maybeUpdateTracking();
return result;
}
protected async preChecks(): Promise<void> {
if (this.flags['track-source']) {
this.tracking = await SourceTracking.create({ org: this.org, project: this.project! });
}
if (!validateTests(this.flags['test-level'], this.flags.tests)) {
throw messages.createError('error.NoTestsSpecified');
}
}
protected async delete(): Promise<void> {
const sourcepaths = this.flags['source-dir'];
this.componentSet = await ComponentSetBuilder.build({
apiversion: this.flags['api-version'],
sourceapiversion: await getSourceApiVersion(),
sourcepath: sourcepaths,
metadata: this.flags.metadata
? {
metadataEntries: this.flags.metadata,
directoryPaths: await getPackageDirs(),
}
: undefined,
projectDir: this.project?.getPath(),
});
if (this.flags['track-source'] && !this.flags['force-overwrite']) {
await this.filterConflictsByComponentSet();
}
this.components = this.componentSet.toArray();
if (!this.components.length) {
// if we didn't find any components to delete, let the user know and exit
this.styledHeader(tableHeader('Deleted Source'));
this.log('No results found');
return;
}
// create a new ComponentSet and mark everything for deletion
const cs = new ComponentSet([]);
cs.apiVersion =
this.componentSet.apiVersion ?? this.flags['api-version'] ?? (await this.org.retrieveMaxApiVersion());
cs.sourceApiVersion =
this.componentSet.sourceApiVersion ?? this.flags['api-version'] ?? (await getSourceApiVersion());
this.components.map((component) => {
if (component instanceof SourceComponent) {
cs.add(component, DestructiveChangesType.POST);
} else {
// a remote-only delete
cs.add(new SourceComponent({ name: component.fullName, type: component.type }), DestructiveChangesType.POST);
}
});
this.componentSet = cs;
if (sourcepaths) {
await Promise.all([
// determine if user is trying to delete a single file from a bundle, which is actually just an fs delete operation
// and then a constructive deploy on the "new" bundle
...this.components
.filter((comp) => comp.type.strategies?.adapter === 'bundle')
.filter(isSourceComponent)
.flatMap((bundle) =>
sourcepaths
.filter(someContentsEndWithPath(bundle))
.map((sourcepath) =>
this.moveToManifest(bundle, sourcepath, path.join(bundle.name, path.basename(sourcepath)))
)
),
// same for decomposed components with non-addressable children (ex: decomposedPermissionSet. Deleting a file means "redploy without that")
...this.components
.filter(allChildrenAreNotAddressable)
.filter(isSourceComponent)
.flatMap((decomposed) =>
sourcepaths
.filter(someContentsEndWithPath(decomposed))
.map((sourcepath) => this.moveToManifest(decomposed, sourcepath, decomposed.fullName))
),
]);
}
if (!(await this.handlePrompt())) {
await Promise.all(
this.mixedDeployDelete.delete.map(async (file) => {
await restoreFileFromStash(this.stashPath, file.filePath);
})
);
throw messages.createError('prompt.delete.cancel');
}
// fire predeploy event for the delete
await Lifecycle.getInstance().emit('predeploy', this.components);
const stages = new DeployStages({
title: 'Deleting Metadata',
jsonEnabled: this.jsonEnabled(),
});
const isRest = (await resolveApi()) === API['REST'];
stages.update({ message: `Deleting with ${isRest ? 'REST' : 'SOAP'} API` });
const deploy = await this.componentSet.deploy({
usernameOrConnection: this.org.getUsername() as string,
apiOptions: {
rest: isRest,
checkOnly: this.flags['check-only'] ?? false,
...(this.flags.tests ? { runTests: this.flags.tests } : {}),
...(this.flags['test-level'] ? { testLevel: this.flags['test-level'] } : {}),
},
});
stages.start({ deploy, username: this.org.getUsername() });
this.deployResult = await deploy.pollStatus({ timeout: this.flags.wait });
if (!deploy.id) {
stages.error();
throw new SfError('The deploy id is not available.');
}
await DeployCache.update(deploy.id, { status: this.deployResult.response.status });
await Lifecycle.getInstance().emit('postdeploy', this.deployResult);
// result.getFileResponses() will crawl the tree, but that would throw after the delete occurs.
// Extract them here for updateTracking to use later
this.fileResponses = this.mixedDeployDelete.delete.length
? this.mixedDeployDelete.delete
: this.deployResult.getFileResponses();
}
/**
* Checks the response status to determine whether the delete was successful.
*/
protected async resolveSuccess(): Promise<void> {
// if deploy failed restore the stashed files if they exist
if (this.deployResult?.response?.status !== RequestStatus.Succeeded) {
process.exitCode = 1;
await Promise.all(
this.mixedDeployDelete.delete.map(async (file) => {
await restoreFileFromStash(this.stashPath, file.filePath);
})
);
} else if (this.mixedDeployDelete.delete.length) {
// successful delete -> delete the stashed file
return deleteStash();
}
}
protected async formatResult(): Promise<DeleteSourceJson> {
const formatterOptions = {
verbose: this.flags.verbose ?? false,
testLevel: this.flags['test-level'],
};
const deleteResultFormatter = this.mixedDeployDelete.deploy.length
? new DeployResultFormatter(this.deployResult, formatterOptions, this.mixedDeployDelete.delete)
: new DeleteResultFormatter(this.deployResult, formatterOptions);
// Only display results to console when JSON flag is unset.
if (!this.jsonEnabled()) {
deleteResultFormatter.display();
}
if (this.mixedDeployDelete.deploy.length) {
// override JSON output when we actually deployed
const json = (await deleteResultFormatter.getJson()) as DeleteSourceJson;
json.deletedSource = this.mixedDeployDelete.delete; // to match toolbelt json output
json.outboundFiles = []; // to match toolbelt version
json.deletes = json.deploys; // to match toolbelt version
delete json.deploys;
return json;
}
return (await deleteResultFormatter.getJson()) as DeleteSourceJson;
}
private async maybeUpdateTracking(): Promise<void> {
if (this.flags['track-source'] ?? false) {
// might not exist if we exited from the operation early
if (!this.deployResult) {
return;
}
this.spinner.start('Updating source tracking');
const successes = (this.fileResponses ?? this.deployResult.getFileResponses()).filter(isSdrSuccess);
if (!successes.length) {
this.spinner.stop();
return;
}
await Promise.all([
this.tracking?.updateLocalTracking({
files: successes
.filter((fileResponse) => fileResponse.state !== ComponentStatus.Deleted)
.map((fileResponse) => fileResponse.filePath),
deletedFiles: successes.filter(isFileResponseDeleted).map((fileResponse) => fileResponse.filePath),
}),
this.tracking?.updateRemoteTracking(successes.map(getFileResponseSuccessProps)),
]);
this.spinner.stop();
}
}
private async deleteFilesLocally(): Promise<void> {
if (!this.flags['check-only'] && this.deployResult?.response?.status === RequestStatus.Succeeded) {
const customLabels = this.componentSet.getSourceComponents().toArray().filter(isNonDecomposedCustomLabel);
const promisesFromLabels = customLabels[0]?.xml ? [deleteCustomLabels(customLabels[0].xml, customLabels)] : [];
// mixed delete/deploy operations have already been deleted and stashed
const otherPromises = !this.mixedDeployDelete.delete.length
? (this.components ?? [])
.filter(isSourceComponent)
.flatMap((component: SourceComponent) => [
...(component.content ? [fs.promises.rm(component.content, { recursive: true, force: true })] : []),
...(component.xml && !isNonDecomposedCustomLabel(component) ? [fs.promises.rm(component.xml)] : []),
])
: [];
await Promise.all([...promisesFromLabels, ...otherPromises]);
}
}
private async moveToManifest(cmp: SourceComponent, sourcepath: string, fullName: string): Promise<void> {
this.mixedDeployDelete.delete.push({
state: ComponentStatus.Deleted,
fullName,
type: cmp.type.name,
filePath: sourcepath,
});
// stash the file in case we need to restore it due to failed deploy/aborted command
this.stashPath.set(sourcepath, path.join(os.tmpdir(), 'source_delete', fullName));
await moveFileToStash(this.stashPath, sourcepath);
// re-walk the directory to avoid picking up the deleted file
this.mixedDeployDelete.deploy.push(...cmp.walkContent());
// now from destructive changes and add to manifest
// set NOT marked for delete
this.componentSet.destructiveChangesPost.delete(`${cmp.type.id}#${cmp.fullName}`);
cmp.setMarkedForDelete(false);
this.componentSet.add(cmp);
}
private async handlePrompt(): Promise<boolean> {
if (!this.flags['no-prompt']) {
const remote = (this.components ?? [])
.filter((comp) => !(comp instanceof SourceComponent))
.map((comp) => `${comp.type.name}:${comp.fullName}`);
const local = (this.components ?? [])
.filter(isSourceComponent)
.filter(sourceComponentIsNotInMixedDeployDelete(this.mixedDeployDelete))
.flatMap((c) =>
// for custom labels, print each custom label to be deleted, not the whole file
isNonDecomposedCustomLabelsOrCustomLabel(c)
? [`${c.type.name}:${c.fullName}`]
: [c.xml, ...c.walkContent()] ?? []
)
.concat(this.mixedDeployDelete.delete.map((fr) => `${fr.fullName} (${fr.filePath})`));
const message: string[] = [
...(this.mixedDeployDelete.deploy.length
? [messages.getMessage('deployPrompt', [[...new Set(this.mixedDeployDelete.deploy)].join('\n')])]
: []),
...(remote.length ? [messages.getMessage('remotePrompt', [[...new Set(remote)].join('\n')])] : []),
// add a whitespace between remote and local
...(local.length && (this.mixedDeployDelete.deploy.length || remote.length) ? ['\n'] : []),
...(local.length ? [messages.getMessage('localPrompt', [[...new Set(local)].join('\n')])] : []),
this.flags['check-only'] ?? false
? messages.getMessage('areYouSureCheckOnly')
: messages.getMessage('areYouSure'),
];
return this.confirm({ message: message.join('\n') });
}
return true;
}
/**
* Check if any conflicts exist in a specific component set.
* If conflicts exist, this will output the table and throw
*/
private filterConflictsByComponentSet = async (): Promise<ChangeResult[]> => {
const filteredConflicts =
(await this.tracking?.getConflicts())?.filter((cr) =>
this.componentSet.has({ fullName: cr.name as string, type: cr.type as string })
) ?? [];
processConflicts(filteredConflicts, messages.getMessage('conflictMsg'));
return filteredConflicts;
};
}
const moveFileToStash = async (stashPath: Map<string, string>, file: string): Promise<void> => {
await fs.promises.mkdir(path.dirname(stashPath.get(file) as string), { recursive: true });
await fs.promises.copyFile(file, stashPath.get(file) as string);
await fs.promises.unlink(file);
};
const restoreFileFromStash = async (stashPath: Map<string, string>, file: string): Promise<void> =>
fs.promises.rename(stashPath.get(file) as string, file);
const deleteStash = async (): Promise<void> =>
fs.promises.rm(path.join(os.tmpdir(), 'source_delete'), { recursive: true, force: true });
const someContentsEndWithPath =
(cmp: SourceComponent) =>
(sourcePath: string): boolean =>
// walkContent returns absolute paths while sourcepath will usually be relative
cmp.walkContent().some((content) => content.endsWith(sourcePath));
const allChildrenAreNotAddressable = (comp: MetadataComponent): boolean => {
const types = Object.values(comp.type.children?.types ?? {});
return types.length > 0 && types.every((child) => child.isAddressable === false);
};
const sourceComponentIsNotInMixedDeployDelete =
(mixedDeployDelete: MixedDeployDelete) =>
(c: SourceComponent): boolean =>
!mixedDeployDelete.delete.some((d) => d.fullName === c.fullName && d.type === c.type.name);
/**
* Write a table (if not json) and throw an error that includes a custom message and the conflict data
*
* @param conflicts
* @param message
*/
const processConflicts = (conflicts: ChangeResult[], message: string): void => {
if (conflicts.length === 0) return;
const reformattedConflicts = Array.from(
// map do dedupe by name-type-filename
new Map(
conflicts.flatMap(changeResultToConflictResponses).map((c) => [`${c.fullName}#${c.type}#${c.filePath}`, c])
).values()
);
writeConflictTable(reformattedConflicts);
const err = new SfError(message, 'sourceConflictDetected');
err.setData(reformattedConflicts);
throw err;
};
/** each ChangeResult can have multiple filenames, each of which becomes a ConflictResponse */
const changeResultToConflictResponses = (cr: ChangeResult): ConflictResponse[] =>
(cr.filenames ?? []).map((f) => ({
state: 'Conflict',
fullName: cr.name ?? '<Name is missing>',
type: cr.type ?? '<Type is missing>',
filePath: path.resolve(f),
}));