-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.ts
89 lines (81 loc) · 3.58 KB
/
object.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
/*
* 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 { Flags, loglevel, SfCommand } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { writeTypeFile } from '../../../shared/helpers/fileWriter.js';
import { validateMetadataTypeName, validateLessThanForty } from '../../../shared/helpers/validationUtil.js';
import { createObjectXML } from '../../../shared/templates/templates.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-custom-metadata', 'object');
export type CmdtCreateResponse = {
typename: string;
label: string;
pluralLabel: string;
visibility: string;
}
export default class Create extends SfCommand<CmdtCreateResponse> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly aliases = ['force:cmdt:create', 'cmdt:create'];
public static readonly deprecateAliases = true;
public static readonly requiresProject = true;
public static readonly flags = {
loglevel,
'type-name': Flags.string({
char: 'n',
summary: messages.getMessage('flags.type-name.summary'),
description: messages.getMessage('flags.type-name.description'),
required: true,
parse: async (input: string) => Promise.resolve(validateMetadataTypeName(input)),
aliases: ['typename'],
}),
label: Flags.string({
char: 'l',
summary: messages.getMessage('flags.label.summary'),
parse: async (input) =>
Promise.resolve(validateLessThanForty(input, messages.getMessage('errorNotValidLabelName', [input]))),
}),
'plural-label': Flags.string({
char: 'p',
summary: messages.getMessage('flags.plural-label.summary'),
parse: async (input) =>
Promise.resolve(validateLessThanForty(input, messages.getMessage('errorNotValidPluralLabelName', [input]))),
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',
}),
'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<CmdtCreateResponse> {
const { flags } = await this.parse(Create);
const label = flags.label ?? flags['type-name'].replace('__mdt', ''); // If a label is not provided default using the dev name. trim __mdt out
const pluralLabel = flags['plural-label'] ?? label;
const objectXML = createObjectXML({ label, pluralLabel }, flags.visibility);
const saveResults = await writeTypeFile(fs, flags['output-directory'], flags['type-name'], objectXML);
this.log(messages.getMessage('targetDirectory', [saveResults.dir]));
this.log(messages.getMessage(saveResults.updated ? 'fileUpdate' : 'fileCreated', [saveResults.fileName]));
return {
typename: flags['type-name'],
label,
pluralLabel,
visibility: flags.visibility,
};
}
}