-
Notifications
You must be signed in to change notification settings - Fork 0
/
records.ts
147 lines (134 loc) · 5.98 KB
/
records.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
/*
* 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, SfCommand } from '@salesforce/sf-plugins-core';
import { Messages, SfError } from '@salesforce/core';
import type { Record } from '@jsforce/jsforce-node';
import { parse } from 'csv-parse/sync';
import { getFieldNames, appendDirectorySuffix, createRecord, getFileData } from '../../../shared/helpers/createUtil.js';
import { CreateConfig, CreateConfigs } from '../../../shared/interfaces/createConfig.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-custom-metadata', 'records');
export default class Insert extends SfCommand<CreateConfigs> {
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:insert', 'cmdt:record:insert'];
public static readonly deprecateAliases = true;
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
loglevel,
csv: Flags.string({
char: 'f',
summary: messages.getMessage('flags.csv.summary'),
required: true,
aliases: ['filepath'],
}),
'type-name': Flags.string({
char: 't',
summary: messages.getMessage('flags.type-name.summary'),
description: messages.getMessage('flags.type-name.description'),
required: true,
parse: (input) => Promise.resolve(input.endsWith('__mdt') ? input.replace('__mdt', '') : input),
aliases: ['typename'],
}),
'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'],
}),
'name-column': Flags.string({
char: 'n',
summary: messages.getMessage('flags.name-column.summary'),
default: 'Name',
aliases: ['namecolumn'],
}),
};
public async run(): Promise<CreateConfigs> {
const { flags } = await this.parse(Insert);
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);
const metadataTypeFields = getFieldNames(fileData, flags['name-column']);
const parsedRecords = parse(await fs.promises.readFile(flags.csv), {
columns: (header: string[]) => columnValidation(metadataTypeFields, header, flags['type-name']),
}) as Record[];
// Transforms on the recordname are to match the behavior of adding a new Custom Metadata Type record in the UI
const recordConfigs: CreateConfig[] = validateUniqueNames(
parsedRecords.map((record) => ({
typename: flags['type-name'],
recordname: (record[flags['name-column']] as string)
.replace(/[^a-zA-Z0-9]/g, '_') // replace all non-alphanumeric characters with _
.replace(/^(\d)/, 'X$1') // prepend an X if the first character is a number
.replace(/_{2,}/g, '_') // replace multiple underscores with single underscore
.replace(/_$/, ''), // remove trailing underscore (if any)
label: record[flags['name-column']] as string,
inputdir: flags['input-directory'],
outputdir: flags['output-directory'],
protected: false,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
varargs: Object.fromEntries(
// TODO: throw an error if any of the fields in the csvDataAry do not exist in the fileData
fileData.map((file) => {
if (file.fullName) {
return record[file.fullName] ? [file.fullName, record[file.fullName]] : [];
} else {
throw new SfError('No fullName found in fileData');
}
})
),
fileData,
}))
);
// find the cmdt in the inputdir.
// loop through files and create records that match fields
await Promise.all(recordConfigs.map((r) => createRecord(r)));
this.log(messages.getMessage('successResponse', [flags.csv, flags['output-directory']]));
return recordConfigs;
}
}
/** validate name fields are unique, otherwise they'll be trying to write to the same file */
const validateUniqueNames = (recordConfigs: CreateConfig[]): CreateConfig[] => {
const recordNameSet = new Set<string>();
const dupes = recordConfigs
.map((rc) => {
if (recordNameSet.has(rc.recordname)) {
return rc.recordname;
} else {
recordNameSet.add(rc.recordname);
return undefined;
}
})
.filter((rc): rc is string => rc !== undefined);
if (dupes.length > 0) {
throw new SfError(
`Your CSV has duplicate values: ${[...new Set(dupes)].join(', ')}. CMDT require unique names in the name field.`
);
}
return recordConfigs;
};
/** Validate that every column in the CSV has known metadata */
const columnValidation = (requiredFields: string[], columnList: string[], typeNameFlag: string): string[] => {
columnList.forEach((column) => {
if (!requiredFields.includes(column)) {
throw new SfError(messages.getMessage('fieldNotFoundError', [column, typeNameFlag]));
}
});
return columnList;
};