-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.ts
134 lines (124 loc) · 4.67 KB
/
record.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
/*
* 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, parseVarArgs, SfCommand } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import type { CustomField } from '@jsforce/jsforce-node/lib/api/metadata.js';
import { appendDirectorySuffix, createRecord, getFileData } from '../../../shared/helpers/createUtil.js';
import {
validateMetadataRecordName,
validateMetadataTypeName,
validateLessThanForty,
} from '../../../shared/helpers/validationUtil.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-custom-metadata', 'record');
export type CmdtRecordCreateResponse = {
typename: string;
recordname: string;
label: string;
inputdir: string;
outputdir: string;
protectedFlag: boolean;
varargs: Record<string, unknown>;
fileData: CustomField[];
};
export default class Create extends SfCommand<CmdtRecordCreateResponse> {
public static readonly strict = false;
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly requiresProject = true;
public static readonly aliases = ['force:cmdt:record:create', 'cmdt:record:create'];
public static readonly deprecateAliases = true;
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
loglevel,
'type-name': Flags.string({
char: 't',
summary: messages.getMessage('flags.type-name.summary'),
required: true,
parse: async (input) => Promise.resolve(validateMetadataTypeName(input)),
aliases: ['typename'],
}),
'record-name': Flags.string({
char: 'n',
summary: messages.getMessage('flags.record-name.summary'),
required: true,
parse: async (input) => Promise.resolve(validateMetadataRecordName(input)),
aliases: ['recordname'],
}),
label: Flags.string({
char: 'l',
summary: messages.getMessage('flags.label.summary'),
parse: async (input) =>
Promise.resolve(validateLessThanForty(input, messages.getMessage('notAValidLabelNameError', [input]))),
}),
// I hate this flag so much, but have to preserve it
protected: Flags.string({
char: 'p',
summary: messages.getMessage('flags.protected.summary'),
description: messages.getMessage('flags.protected.description'),
options: ['true', 'false'],
default: 'false',
}),
'input-directory': Flags.directory({
char: 'i',
summary: messages.getMessage('flags.input-directory.summary'),
default: path.join('force-app', 'main', 'default', 'objects'),
aliases: ['inputdir', 'inputdirectory'],
exists: true,
}),
'output-directory': Flags.directory({
char: 'd',
summary: messages.getMessage('flags.output-directory.summary'),
default: path.join('force-app', 'main', 'default', 'customMetadata'),
aliases: ['outputdir', 'outputdirectory'],
}),
};
public async run(): Promise<CmdtRecordCreateResponse> {
const { flags, args, argv } = await this.parse(Create);
const varargs = parseVarArgs(args, argv as string[]);
const label = flags.label ?? flags['record-name'];
const protectedFlag = flags.protected === 'true';
const dirName = appendDirectorySuffix(flags['type-name']);
const fieldDirPath = path.join(flags['input-directory'], dirName, 'fields');
const fileNames = await fs.promises.readdir(fieldDirPath);
// if customMetadata folder does not exist, create it
await fs.promises.mkdir(flags['output-directory'], { recursive: true });
const fileData = await getFileData(fieldDirPath, fileNames);
await createRecord({
typename: flags['type-name'],
recordname: flags['record-name'],
label,
inputdir: flags['input-directory'],
outputdir: flags['output-directory'],
protected: protectedFlag,
varargs,
fileData,
});
this.log(
messages.getMessage('successResponse', [
flags['type-name'],
flags['record-name'],
label,
protectedFlag,
flags['output-directory'],
])
);
// Return an object to be displayed with --json
return {
typename: flags['type-name'],
recordname: flags['record-name'],
label,
inputdir: flags['input-directory'],
outputdir: flags['output-directory'],
protectedFlag,
varargs,
fileData,
};
}
}