-
Notifications
You must be signed in to change notification settings - Fork 69
/
scratchOrgSettingsGenerator.ts
399 lines (364 loc) · 13.3 KB
/
scratchOrgSettingsGenerator.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
/*
* 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 * as path from 'node:path';
import { isEmpty, env, upperFirst, Duration } from '@salesforce/kit';
import { ensureObject, JsonMap } from '@salesforce/ts-types';
import * as js2xmlparser from 'js2xmlparser';
import { Logger } from '../logger/logger';
import { SfError } from '../sfError';
import { StructuredWriter } from '../util/structuredWriter';
import { StatusResult } from '../status/types';
import { PollingClient } from '../status/pollingClient';
import { ZipWriter } from '../util/zipWriter';
import { DirectoryWriter } from '../util/directoryWriter';
import { Lifecycle } from '../lifecycleEvents';
import { Messages } from '../messages';
import { ScratchOrgInfo, ObjectSetting } from './scratchOrgTypes';
import { Org } from './org';
Messages.importMessagesDirectory(__dirname);
export enum RequestStatus {
Pending = 'Pending',
InProgress = 'InProgress',
Succeeded = 'Succeeded',
SucceededPartial = 'SucceededPartial',
Failed = 'Failed',
Canceling = 'Canceling',
Canceled = 'Canceled',
}
const breakPolling = ['Succeeded', 'SucceededPartial', 'Failed', 'Canceled'];
export type SettingType = {
members: string[];
name: 'CustomObject' | 'RecordType' | 'BusinessProcess' | 'Settings';
};
export type PackageFile = {
'@': {
xmlns: string;
};
types: SettingType[];
version: string;
};
export const createObjectFileContent = ({
allRecordTypes = [],
allBusinessProcesses = [],
apiVersion,
settingData,
objectSettingsData,
}: {
allRecordTypes?: string[];
allBusinessProcesses?: string[];
apiVersion: string;
settingData?: Record<string, unknown>;
objectSettingsData?: { [objectName: string]: ObjectSetting };
}): PackageFile => {
const output = {
'@': {
xmlns: 'http://soap.sforce.com/2006/04/metadata',
},
types: [] as SettingType[],
};
if (settingData) {
const strings = Object.keys(settingData).map((item) => upperFirst(item).replace('Settings', ''));
output.types.push({ members: strings, name: 'Settings' });
}
if (objectSettingsData) {
const strings = Object.keys(objectSettingsData).map((item) => upperFirst(item));
output.types.push({ members: strings, name: 'CustomObject' });
if (allRecordTypes.length > 0) {
output.types.push({ members: allRecordTypes, name: 'RecordType' });
}
if (allBusinessProcesses.length > 0) {
output.types.push({ members: allBusinessProcesses, name: 'BusinessProcess' });
}
}
return { ...output, ...{ version: apiVersion } };
};
const calculateBusinessProcess = (
objectName: string,
defaultRecordType: string,
capitalizeBusinessProcess: boolean
): Array<string | null> => {
let businessProcessName = null;
let businessProcessPicklistVal = null;
// These four objects require any record type to specify a "business process"--
// a restricted set of items from a standard picklist on the object.
if (['Case', 'Lead', 'Opportunity', 'Solution'].includes(objectName)) {
businessProcessName = capitalizeBusinessProcess
? `${upperFirst(defaultRecordType)}Process`
: `${defaultRecordType}Process`;
switch (objectName) {
case 'Case':
businessProcessPicklistVal = 'New';
break;
case 'Lead':
businessProcessPicklistVal = 'New - Not Contacted';
break;
case 'Opportunity':
businessProcessPicklistVal = 'Prospecting';
break;
case 'Solution':
businessProcessPicklistVal = 'Draft';
}
}
return [businessProcessName, businessProcessPicklistVal];
};
export const createRecordTypeAndBusinessProcessFileContent = (
objectName: string,
json: Record<string, unknown>,
allRecordTypes: string[],
allBusinessProcesses: string[],
capitalizeRecordTypes: boolean
): JsonMap => {
let output = {
'@': {
xmlns: 'http://soap.sforce.com/2006/04/metadata',
},
} as JsonMap;
const name = upperFirst(objectName);
const sharingModel = json.sharingModel;
if (sharingModel) {
output = {
...output,
sharingModel: upperFirst(sharingModel as string),
};
}
const defaultRecordType = capitalizeRecordTypes
? upperFirst(json.defaultRecordType as string)
: json.defaultRecordType;
if (typeof defaultRecordType === 'string') {
// We need to keep track of these globally for when we generate the package XML.
allRecordTypes.push(`${name}.${defaultRecordType}`);
const [businessProcessName, businessProcessPicklistVal] = calculateBusinessProcess(
name,
defaultRecordType,
capitalizeRecordTypes
);
// Create the record type
const recordTypes = {
fullName: defaultRecordType,
label: defaultRecordType,
active: true,
};
output = {
...output,
recordTypes: {
...recordTypes,
},
};
if (businessProcessName) {
// We need to keep track of these globally for the package.xml
const values: { fullName: string | null; default?: boolean } = {
fullName: businessProcessPicklistVal,
};
if (name !== 'Opportunity') {
values.default = true;
}
allBusinessProcesses.push(`${name}.${businessProcessName}`);
output = {
...output,
recordTypes: {
...recordTypes,
businessProcess: businessProcessName,
},
businessProcesses: {
fullName: businessProcessName,
isActive: true,
values,
},
};
}
}
return output;
};
/**
* Helper class for dealing with the settings that are defined in a scratch definition file. This class knows how to extract the
* settings from the definition, how to expand them into a MD directory and how to generate a package.xml.
*/
export default class SettingsGenerator {
private settingData?: Record<string, unknown>;
private objectSettingsData?: { [objectName: string]: ObjectSetting };
private logger: Logger;
private writer: StructuredWriter;
private allRecordTypes: string[] = [];
private allBusinessProcesses: string[] = [];
private readonly shapeDirName: string;
private readonly packageFilePath: string;
private readonly capitalizeRecordTypes: boolean;
public constructor(options?: {
mdApiTmpDir?: string;
shapeDirName?: string;
asDirectory?: boolean;
capitalizeRecordTypes?: boolean;
}) {
this.logger = Logger.childFromRoot('SettingsGenerator');
if (options?.capitalizeRecordTypes === undefined) {
const messages = Messages.loadMessages('@salesforce/core', 'scratchOrgSettingsGenerator');
void Lifecycle.getInstance().emitWarning(messages.getMessage('noCapitalizeRecordTypeConfigVar'));
this.capitalizeRecordTypes = true;
} else {
this.capitalizeRecordTypes = options.capitalizeRecordTypes;
}
// If SFDX_MDAPI_TEMP_DIR is set, copy settings to that dir for people to inspect.
const mdApiTmpDir = options?.mdApiTmpDir ?? env.getString('SFDX_MDAPI_TEMP_DIR');
this.shapeDirName = options?.shapeDirName ?? `shape_${Date.now()}`;
this.packageFilePath = path.join(this.shapeDirName, 'package.xml');
let storePath;
if (!options?.asDirectory) {
storePath = mdApiTmpDir ? path.join(mdApiTmpDir, `${this.shapeDirName}.zip`) : undefined;
this.writer = new ZipWriter(storePath);
} else {
storePath = mdApiTmpDir ? path.join(mdApiTmpDir, this.shapeDirName) : undefined;
this.writer = new DirectoryWriter(storePath);
}
}
/** extract the settings from the scratch def file, if they are present. */
// eslint-disable-next-line @typescript-eslint/require-await
public async extract(scratchDef: ScratchOrgInfo): Promise<{
settings: Record<string, unknown> | undefined;
objectSettings: { [objectName: string]: ObjectSetting } | undefined;
}> {
this.logger.debug('extracting settings from scratch definition file');
this.settingData = scratchDef.settings;
this.objectSettingsData = scratchDef.objectSettings;
this.logger.debug('settings are', this.settingData);
return { settings: this.settingData, objectSettings: this.objectSettingsData };
}
/** True if we are currently tracking setting or object setting data. */
public hasSettings(): boolean {
return !isEmpty(this.settingData) || !isEmpty(this.objectSettingsData);
}
/** Create temporary deploy directory used to upload the scratch org shape.
* This will create the dir, all of the .setting files and minimal object files needed for objectSettings
*/
public async createDeploy(): Promise<void> {
const settingsDir = path.join(this.shapeDirName, 'settings');
const objectsDir = path.join(this.shapeDirName, 'objects');
await Promise.all([
this.writeSettingsIfNeeded(settingsDir),
this.writeObjectSettingsIfNeeded(objectsDir, this.allRecordTypes, this.allBusinessProcesses),
]);
}
/**
* Deploys the settings to the org.
*/
public async deploySettingsViaFolder(
scratchOrg: Org,
apiVersion: string,
timeout: Duration = Duration.minutes(10)
): Promise<void> {
const username = scratchOrg.getUsername();
const logger = await Logger.child('deploySettingsViaFolder');
await this.createDeployPackageContents(apiVersion);
const connection = scratchOrg.getConnection();
logger.debug(`deploying to apiVersion: ${apiVersion}`);
connection.setApiVersion(apiVersion);
const { id } = await connection.deploy(this.writer.buffer, {});
logger.debug(`deploying settings id ${id}`);
let result = await connection.metadata.checkDeployStatus(id);
const pollingOptions: PollingClient.Options = {
async poll(): Promise<StatusResult> {
try {
result = await connection.metadata.checkDeployStatus(id, true);
logger.debug(`Deploy id: ${id} status: ${result.status}`);
if (breakPolling.includes(result.status)) {
return {
completed: true,
payload: result.status,
};
}
return {
completed: false,
};
} catch (error) {
logger.debug(`An error occurred trying to check deploy id: ${id}`);
logger.debug(`Error: ${(error as Error).message}`);
logger.debug('Re-trying deploy check again....');
return {
completed: false,
};
}
},
timeout,
frequency: Duration.seconds(1),
timeoutErrorName: 'DeployingSettingsTimeoutError',
};
const client = await PollingClient.create(pollingOptions);
const status = await client.subscribe<string>();
type FailureMessage = {
problemType: string;
fullName: string;
problem: string;
};
if (status !== RequestStatus.Succeeded.toString()) {
const componentFailures = ensureObject<{
componentFailures: FailureMessage | FailureMessage[];
}>(result.details).componentFailures;
const failures = (Array.isArray(componentFailures) ? componentFailures : [componentFailures])
.map((failure) => `[${failure.problemType}] ${failure.fullName} : ${failure.problem} `)
.join('\n');
throw SfError.create({
message: `A scratch org was created with username ${username}, but the settings failed to deploy due to: \n${failures}`,
name: 'ProblemDeployingSettings',
data: { ...result, username },
});
}
}
public async createDeployPackageContents(apiVersion: string): Promise<void> {
const packageObjectProps = createObjectFileContent({
allRecordTypes: this.allRecordTypes,
allBusinessProcesses: this.allBusinessProcesses,
apiVersion,
settingData: this.settingData,
objectSettingsData: this.objectSettingsData,
});
const xml = js2xmlparser.parse('Package', packageObjectProps);
await this.writer.addToStore(xml, this.packageFilePath);
await this.writer.finalize();
}
public getShapeDirName(): string {
return this.shapeDirName;
}
/**
* Returns the destination where the writer placed the settings content.
*
*/
public getDestinationPath(): string | undefined {
return this.writer.getDestinationPath();
}
private async writeObjectSettingsIfNeeded(
objectsDir: string,
allRecordTypes: string[],
allbusinessProcesses: string[]
): Promise<void> {
if (this.objectSettingsData) {
await Promise.all(
Object.entries(this.objectSettingsData).map(([item, value]) => {
const fileContent = createRecordTypeAndBusinessProcessFileContent(
item,
value,
allRecordTypes,
allbusinessProcesses,
this.capitalizeRecordTypes
);
const xml = js2xmlparser.parse('CustomObject', fileContent);
return this.writer.addToStore(xml, path.join(objectsDir, upperFirst(item) + '.object'));
})
);
}
}
private async writeSettingsIfNeeded(settingsDir: string): Promise<void> {
if (this.settingData) {
await Promise.all(
Object.entries(this.settingData).map(([item, value]) => {
const typeName = upperFirst(item);
const fname = typeName.replace('Settings', '');
const fileContent = js2xmlparser.parse(typeName, value);
return this.writer.addToStore(fileContent, path.join(settingsDir, fname + '.settings'));
})
);
}
}
}