-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.ts
113 lines (106 loc) · 3.91 KB
/
field.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
/*
* 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 { arrayWithDeprecation, Flags, loglevel, SfCommand } from '@salesforce/sf-plugins-core';
import { Messages, SfError } from '@salesforce/core';
import { writeFieldFile } from '../../../shared/helpers/fileWriter.js';
import { validateAPIName } from '../../../shared/helpers/validationUtil.js';
import { createDefaultTypeStructure, createFieldXML } from '../../../shared/templates/templates.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-custom-metadata', 'field');
export type CmdtFieldCreateResponse = {
fieldName: string;
label: string;
fieldtype: string;
}
export default class Create extends SfCommand<CmdtFieldCreateResponse> {
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:field:create', 'cmdt:field:create'];
public static readonly deprecateAliases = true;
public static readonly flags = {
loglevel,
name: Flags.string({
char: 'n',
required: true,
summary: messages.getMessage('flags.name.summary'),
parse: async (input: string) =>
Promise.resolve(validateAPIName(input, messages.getMessage('invalidCustomFieldError', [input]))),
aliases: ['fieldname'],
}),
type: Flags.string({
char: 'f',
required: true,
summary: messages.getMessage('flags.type.summary'),
description: messages.getMessage('flags.type.description'),
options: [
'Checkbox',
'Date',
'DateTime',
'Email',
'Number',
'Percent',
'Phone',
'Picklist',
'Text',
'TextArea',
'LongTextArea',
'Url',
],
aliases: ['fieldtype'],
}),
'picklist-values': arrayWithDeprecation({
char: 'p',
summary: messages.getMessage('flags.picklist-values.summary'),
aliases: ['picklistvalues'],
}),
'decimal-places': Flags.integer({
char: 's',
summary: messages.getMessage('flags.decimal-places.summary'),
description: messages.getMessage('flags.decimal-places.description'),
default: 0,
min: 0,
aliases: ['decimalplaces'],
}),
label: Flags.string({
char: 'l',
summary: messages.getMessage('flags.label.summary'),
}),
'output-directory': Flags.directory({
char: 'd',
summary: messages.getMessage('flags.output-directory.summary'),
description: messages.getMessage('flags.output-directory.description'),
default: '',
aliases: ['outputdir', 'outputdirectory'],
}),
};
public async run(): Promise<CmdtFieldCreateResponse> {
const { flags } = await this.parse(Create);
const picklistvalues = flags['picklist-values'] ?? [];
if (flags.type === 'Picklist' && picklistvalues?.length === 0) {
throw new SfError(messages.getMessage('picklistValuesNotSuppliedError'));
}
const data = createDefaultTypeStructure(
flags.name,
flags.type,
flags.label ?? flags.name,
picklistvalues,
flags['decimal-places']
);
const fieldXML = createFieldXML(data, false);
const saveResults = await writeFieldFile(fs, flags['output-directory'], flags.name, fieldXML);
this.log(messages.getMessage('targetDirectory', [saveResults.dir]));
this.log(messages.getMessage(saveResults.updated ? 'fileUpdate' : 'fileCreated', [saveResults.fileName]));
return {
fieldName: flags.name,
label: flags.label ?? flags.name,
fieldtype: flags.type,
};
}
}