-
Notifications
You must be signed in to change notification settings - Fork 0
/
fromorg.ts
251 lines (230 loc) · 9.85 KB
/
fromorg.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
/*
* 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 {
Flags,
loglevel,
orgApiVersionFlagWithDeprecations,
requiredOrgFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
import { SfError, Messages } from '@salesforce/core';
import type { CustomField, CustomObject } from '@jsforce/jsforce-node/lib/api/metadata.js';
import { createRecord, getFileData } from '../../../shared/helpers/createUtil.js';
import { writeTypeFile, writeFieldFile } from '../../../shared/helpers/fileWriter.js';
import { describeObjFields, cleanQueryResponse, validCustomSettingType } from '../../../shared/helpers/metadataUtil.js';
import {
validateAPIName,
validateMetadataTypeName,
isValidMetadataRecordName,
} from '../../../shared/helpers/validationUtil.js';
import { canConvert, createObjectXML, createFieldXML } from '../../../shared/templates/templates.js';
import {
ensureFullName,
CustomFieldWithFullNameTypeAndLabel,
CustomObjectWithFullName,
} from '../../../shared/types.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-custom-metadata', 'fromorg');
export type CmdtGenerateResponse = {
outputDir: string;
recordsOutputDir: string;
};
export default class Generate extends SfCommand<CmdtGenerateResponse> {
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 aliases = ['force:cmdt:generate'];
public static readonly deprecateAliases = true;
public static readonly flags = {
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
loglevel,
// flag with a value (-n, --name=VALUE)
'dev-name': Flags.string({
char: 'n',
required: true,
summary: messages.getMessage('flags.dev-name.summary'),
parse: async (input: string) => Promise.resolve(validateMetadataTypeName(input)),
aliases: ['devname'],
}),
label: Flags.string({
char: 'l',
summary: messages.getMessage('flags.label.summary'),
}),
'plural-label': Flags.string({
char: 'p',
summary: messages.getMessage('flags.plural-label.summary'),
aliases: ['plurallabel'],
}),
visibility: Flags.string({
char: 'v',
summary: messages.getMessage('flags.visibility.summary'),
description: messages.getMessage('flags.visibility.description'),
options: ['PackageProtected', 'Protected', 'Public'],
default: 'Public',
}),
sobject: Flags.string({
char: 's',
required: true,
summary: messages.getMessage('flags.sobject.summary'),
parse: async (sobjectname: string) => Promise.resolve(validateAPIName(sobjectname)),
aliases: ['sobjectname'],
}),
'ignore-unsupported': Flags.boolean({
char: 'i',
summary: messages.getMessage('flags.ignore-unsupported.summary'),
description: messages.getMessage('flags.ignore-unsupported.description'),
aliases: ['ignoreunsupported'],
}),
'type-output-directory': Flags.directory({
char: 'd',
summary: messages.getMessage('flags.type-output-directory.summary'),
default: path.join('force-app', 'main', 'default', 'objects'),
aliases: ['typeoutputdir'],
}),
'records-output-dir': Flags.directory({
char: 'r',
summary: messages.getMessage('flags.records-output-dir.summary'),
default: path.join('force-app', 'main', 'default', 'customMetadata'),
aliases: ['recordsoutputdir'],
}),
};
public async run(): Promise<CmdtGenerateResponse> {
const { flags } = await this.parse(Generate);
const conn = flags['target-org'].getConnection(flags['api-version']);
// use default target org connection to get object describe if no source is provided.
const describeObj = validateCustomObjectDescribe(
await conn.metadata.read('CustomObject', flags.sobject),
flags.sobject
);
const label = flags.label ?? flags['dev-name'];
const pluralLabel = flags['plural-label'] ?? label;
const { 'type-output-directory': outputDir, 'records-output-dir': recordsOutputDir } = flags;
try {
this.spinner.start('creating the CMDT object');
// create custom metadata type
const objectXML = createObjectXML({ label, pluralLabel }, flags.visibility);
await writeTypeFile(fs, outputDir, flags['dev-name'], objectXML);
this.spinner.status = 'creating the CMDT fields';
// get all the field details before creating field metadata
const fields = describeObjFields(describeObj)
// added type check here to skip the creation of un supported fields
.filter(fieldHasFullnameTypeLabel)
.filter((f) => !flags['ignore-unsupported'] || canConvert(f['type']))
.flatMap((f) =>
// check for Geo Location fields before hand and create two different fields for longitude and latitude.
f.type !== 'Location' ? [f] : convertLocationFieldToText(f)
);
/* if there's no fullName, we won't be able to write the file.
* in the wsdl, metadata types inherit fullName (optional) from the Metadata base type,
* but CustomObject does always have one */
// create custom metdata fields
await Promise.all(
fields.map((f) =>
writeFieldFile(
fs,
path.join(outputDir, `${flags['dev-name']}__mdt`),
f.fullName,
createFieldXML(f, !flags['ignore-unsupported'])
)
)
);
this.spinner.status = 'creating the CMDT records';
// if customMetadata folder does not exist, create it
await fs.promises.mkdir(recordsOutputDir, { recursive: true });
const fieldDirPath = path.join(outputDir, `${flags['dev-name']}__mdt`, 'fields');
const fileNames = await fs.promises.readdir(fieldDirPath);
const fileData = await getFileData(fieldDirPath, fileNames);
// query records from source
const sObjectRecords = await conn.query(getSoqlQuery(describeObj));
await Promise.all(
sObjectRecords.records.map((rec) => {
const record = cleanQueryResponse(rec, describeObj);
const lblName = rec['Name'] as string;
const recordName = isValidMetadataRecordName(lblName) ? lblName : lblName.replace(/ +/g, '_');
return createRecord({
typename: flags['dev-name'],
recordname: recordName,
label: lblName,
inputdir: outputDir,
outputdir: recordsOutputDir,
protected: flags.visibility !== 'Public',
varargs: record,
fileData,
ignorefields: flags['ignore-unsupported'],
});
})
);
this.spinner.stop('custom metadata type and records creation in completed');
this.log(
`Congrats! Created a ${flags['dev-name']} custom metadata type with ${sObjectRecords.records.length} records!`
);
} catch (e) {
const targetDir = `${outputDir}${flags['dev-name']}__mdt`;
// dir might not exist if we never got to the creation step
if (fs.existsSync(targetDir)) {
await fs.promises.rm(targetDir, { recursive: true });
}
await Promise.all(
(await fs.promises.readdir(recordsOutputDir))
.filter((f) => f.startsWith(flags['dev-name']))
.map((f) => fs.promises.unlink(path.join(recordsOutputDir, f)))
);
this.spinner.stop('generate command failed to run');
const errMsg = messages.getMessage('generateError', [e instanceof Error ? e.message : 'unknown error']);
throw new SfError(errMsg, 'generateError');
}
return { outputDir, recordsOutputDir };
}
}
const fieldHasFullnameTypeLabel = (field: CustomField): field is CustomFieldWithFullNameTypeAndLabel =>
typeof field.fullName === 'string' && typeof field.label === 'string' && typeof field.type === 'string';
const getSoqlQuery = (describeResult: CustomObjectWithFullName): string => {
const fieldNames = describeResult.fields
.filter(fieldHasFullnameTypeLabel)
.map((field) => field.fullName)
.join(',');
// Added Name hardcoded as Name field is not retrieved as part of object describe.
return `SELECT Name, ${fieldNames} FROM ${describeResult.fullName}`;
};
const convertLocationFieldToText = (
field: CustomFieldWithFullNameTypeAndLabel
): CustomFieldWithFullNameTypeAndLabel[] => {
const baseTextField = {
required: field['required'],
trackHistory: field['trackHistory'],
trackTrending: field['trackTrending'],
type: 'Text',
length: 40,
summaryFilterItems: [],
};
return ['Lat_', 'Long_'].map((prefix) => ({
...baseTextField,
fullName: `${prefix}${field.fullName}`,
label: `${prefix}${field.label}`,
}));
};
/** throw errors if describe is empty (doesn't exist) or is CustomSetting or missing fullname */
const validateCustomObjectDescribe = (describeObj: CustomObject, objectName: string): CustomObjectWithFullName => {
// throw error if the object doesnot exist(empty json as response from the describe call.)
if (describeObj.fields.length === 0) {
const errMsg = messages.getMessage('sobjectnameNoResultError', [objectName]);
throw new SfError(errMsg, 'sobjectnameNoResultError');
}
// check for custom setting
if (describeObj.customSettingsType) {
// if custom setting check for type and visibility
if (!validCustomSettingType(describeObj)) {
const errMsg = messages.getMessage('customSettingTypeError', [objectName]);
throw new SfError(errMsg, 'customSettingTypeError');
}
}
return ensureFullName(describeObj);
};