Skip to content

Commit fcf4878

Browse files
committed
feat: allow destructive changes with MetadataOptions
1 parent 0f4a75c commit fcf4878

File tree

31 files changed

+378
-3
lines changed

31 files changed

+378
-3
lines changed

src/collections/componentSetBuilder.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { StateAggregator, Logger, SfError, Messages } from '@salesforce/core';
1010
import fs from 'graceful-fs';
1111
import minimatch from 'minimatch';
1212
import { MetadataComponent } from '../resolve/types';
13+
import { SourceComponent } from '../resolve/sourceComponent';
1314
import { ComponentSet } from '../collections/componentSet';
1415
import { RegistryAccess } from '../registry/registryAccess';
1516
import type { FileProperties } from '../client/types';
1617
import { MetadataType } from '../registry/types';
17-
import { FromConnectionOptions } from './types';
18+
import { DestructiveChangesType, FromConnectionOptions } from './types';
1819

1920
Messages.importMessagesDirectory(__dirname);
2021
const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sdr');
@@ -29,6 +30,14 @@ export type ManifestOption = {
2930
type MetadataOption = {
3031
metadataEntries: string[];
3132
directoryPaths: string[];
33+
/**
34+
* Array of metadata type:name pairs to delete before the deploy. Use of wildcards is not allowed.
35+
*/
36+
destructiveEntriesPre?: string[];
37+
/**
38+
* Array of metadata type:name pairs to delete after the deploy. Use of wildcards is not allowed.
39+
*/
40+
destructiveEntriesPost?: string[];
3241
};
3342

3443
type OrgOption = {
@@ -114,6 +123,26 @@ export class ComponentSetBuilder {
114123
.map(addToComponentSet(componentSetFilter));
115124

116125
logger.debug(`Searching for matching metadata in directories: ${directoryPaths.join(', ')}`);
126+
127+
// add destructive changes if defined. Because these are deletes, all entries
128+
// are resolved to SourceComponents
129+
if (metadata.destructiveEntriesPre) {
130+
metadata.destructiveEntriesPre
131+
.map(entryToTypeAndName(registryAccess))
132+
.map(assertNoWildcardInDestructiveEntries)
133+
.flatMap(typeAndNameToMetadataComponents({ directoryPaths, registry: registryAccess }))
134+
.map((mdComponent) => new SourceComponent({ type: mdComponent.type, name: mdComponent.fullName }))
135+
.map(addToComponentSet(componentSet, DestructiveChangesType.PRE));
136+
}
137+
if (metadata.destructiveEntriesPost) {
138+
metadata.destructiveEntriesPost
139+
.map(entryToTypeAndName(registryAccess))
140+
.map(assertNoWildcardInDestructiveEntries)
141+
.flatMap(typeAndNameToMetadataComponents({ directoryPaths, registry: registryAccess }))
142+
.map((mdComponent) => new SourceComponent({ type: mdComponent.type, name: mdComponent.fullName }))
143+
.map(addToComponentSet(componentSet, DestructiveChangesType.POST));
144+
}
145+
117146
const resolvedComponents = ComponentSet.fromSource({
118147
fsPaths: directoryPaths,
119148
include: componentSetFilter,
@@ -162,9 +191,9 @@ export class ComponentSetBuilder {
162191
}
163192

164193
const addToComponentSet =
165-
(cs: ComponentSet) =>
194+
(cs: ComponentSet, deletionType?: DestructiveChangesType) =>
166195
(cmp: MetadataComponent): MetadataComponent => {
167-
cs.add(cmp);
196+
cs.add(cmp, deletionType);
168197
return cmp;
169198
};
170199

@@ -195,6 +224,13 @@ const assertComponentSetIsNotUndefined = (componentSet: ComponentSet | undefined
195224
return componentSet;
196225
};
197226

227+
const assertNoWildcardInDestructiveEntries = (mdEntry: MetadataTypeAndMetadataName): MetadataTypeAndMetadataName => {
228+
if (mdEntry.metadataName.includes('*')) {
229+
throw SfError.create({ message: 'Wildcards are not supported when providing destructive metadata entries' });
230+
}
231+
return mdEntry;
232+
};
233+
198234
/** This is only for debug output of matched files based on the command flags.
199235
* It will log up to 20 file matches. */
200236
const logComponents = (logger: Logger, componentSet: ComponentSet): void => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class OneClass {
2+
public OneClass() {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<types>
4+
<members>RedClass</members>
5+
<name>ApexClass</name>
6+
</types>
7+
<version>60.0</version>
8+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<types>
4+
<members>OneClass</members>
5+
<name>ApexClass</name>
6+
</types>
7+
<version>60.0</version>
8+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class OneClass {
2+
public OneClass() {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<types>
4+
<members>RedClass</members>
5+
<name>ApexClass</name>
6+
</types>
7+
<version>60.0</version>
8+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<types>
4+
<members>OneClass</members>
5+
<name>ApexClass</name>
6+
</types>
7+
<version>60.0</version>
8+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class OneClass {
2+
public OneClass() {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class TwoClass {
2+
public TwoClass() {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<types>
4+
<members>BlueClass</members>
5+
<members>RedClass</members>
6+
<name>ApexClass</name>
7+
</types>
8+
<version>60.0</version>
9+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<types>
4+
<members>OneClass</members>
5+
<members>TwoClass</members>
6+
<name>ApexClass</name>
7+
</types>
8+
<version>60.0</version>
9+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class OneClass {
2+
public OneClass() {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class TwoClass {
2+
public TwoClass() {
3+
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<types>
4+
<members>BlueClass</members>
5+
<members>RedClass</members>
6+
<name>ApexClass</name>
7+
</types>
8+
<version>60.0</version>
9+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<types>
4+
<members>OneClass</members>
5+
<members>TwoClass</members>
6+
<name>ApexClass</name>
7+
</types>
8+
<version>60.0</version>
9+
</Package>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class BlueClass {
2+
public BlueClass() {
3+
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class OneClass {
2+
public OneClass() {
3+
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class RedClass {
2+
public RedClass() {
3+
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public with sharing class TwoClass {
2+
public TwoClass() {
3+
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>59.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "destructiveCompSetsProject",
3+
"namespace": "",
4+
"packageDirectories": [
5+
{
6+
"default": true,
7+
"path": "force-app"
8+
}
9+
],
10+
"sfdcLoginUrl": "https://login.salesforce.com",
11+
"sourceApiVersion": "52.0"
12+
}

0 commit comments

Comments
 (0)