-
Notifications
You must be signed in to change notification settings - Fork 41
/
create.ts
261 lines (229 loc) · 11 KB
/
create.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
import { flags, SfdxCommand } from '@salesforce/command';
import chalk from 'chalk';
import cli from 'cli-ux';
import { ObjectConfig } from '../../../shared/typeDefs';
import { writeJSONasXML } from '@mshanemc/plugin-helpers/dist/JSONXMLtools';
import { removeTrailingSlash } from '../../../shared/flagParsing';
import fs = require('fs-extra');
const typeDefinitions = [
{
type: 'custom',
forbidden: ['highvolume'],
specificOptions: ['activities', 'search', 'feeds'],
specificRequired: ['nametype', 'namefieldlabel'],
ending: '__c'
},
{
type: 'cmdt',
forbidden: ['activities', 'autonumberformat', 'enterprise', 'feeds', 'highvolume', 'history', 'nametype', 'reports', 'search', 'sharing'],
specificRequired: ['visibility'],
ending: '__mdt'
},
{
type: 'big',
forbidden: ['highvolume', 'activities', 'sharing', 'search', 'feeds'],
specificOptions: [],
ending: '__b'
},
{
type: 'event',
forbidden: ['activities', 'sharing', 'search', 'feeds'],
specificOptions: [],
ending: '__e'
}
];
export default class ObjectCreate extends SfdxCommand {
public static description =
'create an object in local source. Only __c (limited support), __b (big objects) and events __e are currently supported';
public static examples = [
`sfdx shane:object:create
// without any params, the cli is going to ask you questions to generate your object interactively
`,
`sfdx shane:object:create --label "Platypus" --plural "Platypi" --api Platypus__b --directory /my/project/path
// label, plural, api name specified so the tool doesn't have to ask you about them. Creates in a non-default path
`,
`sfdx shane:object:create --label "Platypus" --plural "Platypi" --api Platypus__b --directory /my/project/path
// label, plural, api name specified so the tool doesn't have to ask you about them. Creates in a non-default path
`,
`sfdx shane:object:create --label "Signal" --plural "Signals" --api Signal__e
// create a platform event
`
];
protected static flagsConfig = {
type: flags.string({ char: 't', description: 'type of object', options: typeDefinitions.map(td => td.type) }),
// attributes on all types
label: flags.string({ char: 'l', description: 'label for the UI' }),
api: flags.string({ char: 'a', description: 'api name. Ends with one of the supported types: [__b, __e]' }),
plural: flags.string({ char: 'p', description: 'plural label for the UI' }),
description: flags.string({
default: 'added from sfdx plugin',
description: "optional description so you can remember why you added this and what it's for"
}),
// type specific attributes
enterprise: flags.boolean({ description: 'enable bulk/sharing/streaming' }),
sharingmodel: flags.string({ description: 'sharing model', options: ['Read', 'ReadWrite', 'Private'], default: 'ReadWrite' }),
activities: flags.boolean({ description: 'the enableActivities flag on an object (invalid for __b, __e)' }),
search: flags.boolean({ description: 'the enableSearch flag on an object (invalid for __b, __e)' }),
reports: flags.boolean({ description: 'the enableReports flag on an object (invalid for __b, __e)' }),
history: flags.boolean({ description: 'the enableHistory flag on an object (invalid for __b, __e)' }),
feeds: flags.boolean({ description: 'the enableFeeds flag on an object (invalid for __b, __e)' }),
nametype: flags.string({ description: 'name field type', options: ['Text', 'AutoNumber'] }),
namefieldlabel: flags.string({ description: 'the label for the name field', default: 'Name' }),
autonumberformat: flags.string({ description: 'the display format for the autonumbering' }),
visibility: flags.string({
description: 'visibility for custom metadata types',
options: ['Public', 'Protected', 'PackageProtected'],
default: 'Public'
}),
highvolume: flags.boolean({ description: 'high volume, valid only for platform events (__e)' }),
// general command params
interactive: flags.boolean({ char: 'i', description: 'fully interactive--ask me every possible question.' }),
directory: flags.directory({
char: 'd',
default: 'force-app/main/default',
description: "where to create the folder (if it doesn't exist already) and file...defaults to force-app/main/default",
parse: input => removeTrailingSlash(input)
})
};
protected static requiresProject = true;
public async run(): Promise<any> {
if (!this.flags.type) {
this.flags.type = await cli.prompt(`Object type [${typeDefinitions.map(td => td.type)}]`, { default: 'custom' });
}
if (!this.flags.label) {
this.flags.label = await cli.prompt('Label for the object?');
}
if (!this.flags.plural) {
this.flags.plural = await cli.prompt('Plural label for the object?', { default: `${this.flags.label}s` });
}
if (!this.flags.api) {
const suffix = typeDefinitions.find(def => def.type === this.flags.type).ending;
this.flags.api = await cli.prompt(`API name (ends with ${suffix}) ?`, {
default: `${this.flags.label.replace(/ /g, '_')}${suffix}`
});
}
// checks and throws an error if types and params don't mix
this.validate(this.flags.type);
const outputJSON = {
'@': {
xmlns: 'http://soap.sforce.com/2006/04/metadata'
},
deploymentStatus: 'Deployed',
label: this.flags.label,
pluralLabel: this.flags.plural
} as ObjectConfig;
if (this.flags.type === 'big') {
outputJSON.indexes = {
fullName: `${this.flags.api.replace('__b', '')}Index`,
label: `${this.flags.label} Index`,
fields: []
};
}
if (this.flags.description) {
// optional attributes for all types
outputJSON.description = this.flags.description;
} else if (this.flags.interactive) {
outputJSON.description = await cli.prompt('description? Be nice to your future self!', { required: false });
}
// type specific attributes
if (this.flags.type === 'cmdt') {
delete outputJSON.deploymentStatus;
outputJSON.visibility = this.flags.visibility;
}
if (this.flags.type === 'event') {
if (this.flags.interactive && !this.flags.highvolume) {
this.flags.highvolume = await cli.confirm('High Volume (y/n)');
}
outputJSON.eventType = this.flags.highvolume ? 'HighVolume' : 'StandardVolume';
}
// type specific attributes
if (this.flags.type === 'custom') {
if (this.flags.interactive && !this.flags.nametype) {
this.flags.nametype = await cli.prompt('AutoNumber or Text?', { default: 'Text' });
}
if (this.flags.interactive && !this.flags.search) {
this.flags.search = await cli.confirm('enable Search? (y/n)');
}
if (this.flags.interactive && !this.flags.feeds) {
this.flags.feeds = await cli.confirm('enable feeds? (y/n)');
}
if (this.flags.interactive && !this.flags.reports) {
this.flags.reports = await cli.confirm('enable reports? (y/n)');
}
if (this.flags.interactive && !this.flags.history) {
this.flags.history = await cli.confirm('enable history? (y/n)');
}
if (this.flags.interactive && !this.flags.activities) {
this.flags.activities = await cli.confirm('enable activities? (y/n)');
}
if (this.flags.interactive && !this.flags.namefieldlabel) {
this.flags.namefieldlabel = await cli.prompt('What do you want to call the name field?', {
default: `${this.flags.label} Name`
});
}
if (this.flags.interactive && !this.flags.sharing) {
this.flags.sharing = await cli.prompt('Sharing model? [ReadWrite, Read, Private]', { default: 'ReadWrite' });
}
if (this.flags.interactive && !this.flags.enterprise) {
this.flags.enterprise = await cli.confirm('Enable bulk API, sharing, and streaming API? (y/n)');
}
if (this.flags.nametype === 'AutoNumber' && !this.flags.autonumberformat) {
this.flags.autonumberformat = await cli.prompt('Display format', { default: `${this.flags.label}-{0}` });
}
outputJSON.nameField = {
type: this.flags.nametype || 'Text',
label: this.flags.namefieldlabel
};
outputJSON.sharingModel = this.flags.sharingmodel;
if (this.flags.nametype === 'AutoNumber' && this.flags.autonumberformat) {
outputJSON.nameField.displayFormat = this.flags.autonumberformat;
}
if (this.flags.enterprise) {
outputJSON.enableSharing = true;
outputJSON.enableBulkApi = true;
outputJSON.enableStreamingApi = true;
}
if (this.flags.reports) {
outputJSON.enableReports = true;
}
if (this.flags.history) {
outputJSON.enableHistory = true;
}
if (this.flags.activities) {
outputJSON.enableActivities = true;
}
if (this.flags.feeds) {
outputJSON.enableFeeds = true;
}
if (this.flags.search) {
outputJSON.enableSearch = true;
}
}
const objectsPath = `${this.flags.directory}/objects`;
const thisObjectFolder = `${objectsPath}/${this.flags.api}`;
const metaFileLocation = `${thisObjectFolder}/${this.flags.api}.object-meta.xml`;
if (fs.existsSync(thisObjectFolder)) {
throw new Error(`Object already exists: ${thisObjectFolder}`);
}
await fs.ensureDir(thisObjectFolder);
fs.ensureDirSync(`${objectsPath}/${this.flags.api}/fields`);
await writeJSONasXML({
type: 'CustomObject',
json: outputJSON,
path: metaFileLocation
});
this.ux.log(`Created ${chalk.green(thisObjectFolder)}. Add fields with ${chalk.cyan(`sfdx shane:object:field -o ${this.flags.api}`)}.`);
}
private validate = typename => {
const found = typeDefinitions.find(t => t.type === typename);
if (!found) {
throw new Error(`invalid type specified (${typename})`);
}
found.forbidden.forEach(att => {
if (this.flags[att]) {
throw new Error(`${att} is not valid for ${typename}`);
}
});
return true;
};
}