-
Notifications
You must be signed in to change notification settings - Fork 36
/
Builder.js
284 lines (267 loc) · 11.2 KB
/
Builder.js
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
'use strict';
import { Util } from './util/util.js';
import File from './util/file.js';
import config from './util/config.js';
import Cli from './util/cli.js';
import auth from './util/auth.js';
import MetadataTypeInfo from './MetadataTypeInfo.js';
/**
* @typedef {import('../types/mcdev.d.js').BuObject} BuObject
* @typedef {import('../types/mcdev.d.js').CodeExtract} CodeExtract
* @typedef {import('../types/mcdev.d.js').CodeExtractItem} CodeExtractItem
* @typedef {import('../types/mcdev.d.js').Mcdevrc} Mcdevrc
* @typedef {import('../types/mcdev.d.js').MetadataTypeItem} MetadataTypeItem
* @typedef {import('../types/mcdev.d.js').MetadataTypeItemDiff} MetadataTypeItemDiff
* @typedef {import('../types/mcdev.d.js').MetadataTypeItemObj} MetadataTypeItemObj
* @typedef {import('../types/mcdev.d.js').MetadataTypeMap} MetadataTypeMap
* @typedef {import('../types/mcdev.d.js').MetadataTypeMapObj} MetadataTypeMapObj
* @typedef {import('../types/mcdev.d.js').MultiMetadataTypeList} MultiMetadataTypeList
* @typedef {import('../types/mcdev.d.js').SoapRequestParams} SoapRequestParams
* @typedef {import('../types/mcdev.d.js').TemplateMap} TemplateMap
*/
/**
* Builds metadata from a template using market specific customisation
*/
class Builder {
/**
* Creates a Builder, uses v2 auth if v2AuthOptions are passed.
*
* @param {Mcdevrc} properties properties for auth
saved
* @param {BuObject} buObject properties for auth
*/
constructor(properties, buObject) {
this.properties = properties;
this.templateDir = properties.directories.template;
this.retrieveDir = File.normalizePath([
properties.directories.retrieve,
buObject.credential,
buObject.businessUnit,
]);
this.buObject = buObject;
// allow multiple target directories
const templateBuildsArr = Array.isArray(properties.directories.templateBuilds)
? properties.directories.templateBuilds
: [properties.directories.templateBuilds];
this.targetDir = templateBuildsArr.map(
(directoriesTemplateBuilds) =>
directoriesTemplateBuilds + buObject.credential + '/' + buObject.businessUnit
);
/**
* @type {MultiMetadataTypeList}
*/
this.metadata = {};
}
/**
* Builds a specific metadata file by name
*
* @param {string} metadataType metadata type to build
* @param {string[]} nameArr name of metadata to build
* @param {TemplateMap} templateVariables variables to be replaced in the metadata
* @returns {Promise.<MultiMetadataTypeList>} Promise
*/
async _buildDefinition(metadataType, nameArr, templateVariables) {
const type = metadataType;
try {
const result = await Promise.all(
nameArr.map((name) => {
// with npx and powershell spaces are not parsed correctly as part of a string
// we hence require users to put %20 in their stead and have to convert that back
name = name.split('%20').join(' ');
MetadataTypeInfo[type].client = auth.getSDK(this.buObject);
MetadataTypeInfo[type].properties = this.properties;
MetadataTypeInfo[type].buObject = this.buObject;
return MetadataTypeInfo[type].buildDefinition(
this.templateDir,
this.targetDir,
name,
templateVariables
);
})
);
if (result && type === result[0]?.type) {
// result elements can be undefined for each key that we did not find
this.metadata[type] = result.filter(Boolean).map((element) => element.metadata);
}
} catch (ex) {
Util.logger.errorStack(ex, 'mcdev.buildDefinition');
}
return this.metadata;
}
/**
* Build a template based on a list of metadata files in the retrieve folder.
*
* @param {string} businessUnit references credentials from properties.json
* @param {string} selectedType supported metadata type
* @param {string[]} keyArr customerkey of the metadata
* @param {string[]} marketArr market localizations
* @returns {Promise.<MultiMetadataTypeList>} -
*/
static async buildTemplate(businessUnit, selectedType, keyArr, marketArr) {
const properties = await config.getProperties();
if (!(await config.checkProperties(properties))) {
return null;
}
if (!Util._isValidType(selectedType)) {
return;
}
if (selectedType.includes('-')) {
Util.logger.error(
`:: '${selectedType}' is not a valid metadata type. Please don't include subtypes.`
);
return;
}
/** @type {TemplateMap} */
const templateVariables = {};
for (const market of marketArr) {
if (Util.checkMarket(market, properties)) {
Object.assign(templateVariables, properties.markets[market]);
} else {
// do not execute the rest of this method if a market was invalid
return;
}
}
const buObject = await Cli.getCredentialObject(properties, businessUnit);
if (buObject !== null) {
const builder = new Builder(properties, buObject);
return builder._buildTemplate(selectedType, keyArr, templateVariables);
}
}
/**
* Build a template based on a list of metadata files in the retrieve folder.
*
* @param {string} metadataType metadata type to create a template of
* @param {string[]} keyArr customerkey of metadata to create a template of
* @param {TemplateMap} templateVariables variables to be replaced in the metadata
* @returns {Promise.<MultiMetadataTypeList>} Promise
*/
async _buildTemplate(metadataType, keyArr, templateVariables) {
const type = metadataType;
try {
/** @type {MetadataTypeItemObj[]} */
const result = await Promise.all(
keyArr.map(async (key) => {
MetadataTypeInfo[type].properties = this.properties;
MetadataTypeInfo[type].buObject = this.buObject;
try {
/** @type {MetadataTypeItemObj} */
const response = await MetadataTypeInfo[type].buildTemplate(
this.retrieveDir,
this.templateDir,
key,
templateVariables
);
return response;
} catch (ex) {
Util.logger.errorStack(ex, ` ☇ skipping template asset: ${key}`);
}
})
);
if (result && type === result[0]?.type) {
// result elements can be undefined for each key that we did not find
this.metadata[type] = result.filter(Boolean).map((element) => element.metadata);
}
} catch (ex) {
Util.logger.errorStack(ex, 'mcdev.buildTemplate');
}
return this.metadata;
}
/**
* Build a specific metadata file based on a template.
*
* @param {string} businessUnit references credentials from properties.json
* @param {string} selectedType supported metadata type
* @param {string[]} nameArr name of the metadata
* @param {string[]} marketArr market localizations
* @returns {Promise.<MultiMetadataTypeList>} -
*/
static async buildDefinition(businessUnit, selectedType, nameArr, marketArr) {
const properties = await config.getProperties();
if (!(await config.checkProperties(properties))) {
return null;
}
if (!Util._isValidType(selectedType)) {
return;
}
if (selectedType.includes('-')) {
Util.logger.error(
`:: '${selectedType}' is not a valid metadata type. Please don't include subtypes.`
);
return;
}
/** @type {TemplateMap} */
const templateVariables = {};
for (const market of marketArr) {
if (Util.checkMarket(market, properties)) {
Object.assign(templateVariables, properties.markets[market]);
} else {
// do not execute the rest of this method if a market was invalid
return;
}
}
const buObject = await Cli.getCredentialObject(properties, businessUnit);
if (buObject !== null) {
const builder = new Builder(properties, buObject);
return builder._buildDefinition(selectedType, nameArr, templateVariables);
}
}
/**
* Build a specific metadata file based on a template using a list of bu-market combos
*
* @param {string} listName name of list of BU-market combos
* @param {string} type supported metadata type
* @param {string[]} nameArr name of the metadata
* @returns {Promise.<object>} -
*/
static async buildDefinitionBulk(listName, type, nameArr) {
const properties = await config.getProperties();
if (!(await config.checkProperties(properties))) {
return null;
}
try {
Util.verifyMarketList(listName, properties);
} catch (ex) {
Util.logger.error(ex.message);
return;
}
if (type && !MetadataTypeInfo[type]) {
Util.logger.error(`:: '${type}' is not a valid metadata type`);
return;
}
let i = 0;
const responseObj = {};
for (const businessUnit in properties.marketList[listName]) {
if (businessUnit === 'description') {
// skip, it's just a metadata on this list and not a BU
continue;
}
i++;
/** @type {string | string[] | string[][]} */
const market = properties.marketList[listName][businessUnit];
const marketList = 'string' === typeof market ? [market] : market;
for (const market of marketList) {
// one can now send multiple markets to buildTemplate/buildDefinition and hence that also needs to work for marketLists
const marketArr = 'string' === typeof market ? [market] : market;
for (const market of marketArr) {
if (!Util.checkMarket(market, properties)) {
return;
}
}
Util.logger.info(`Executing for '${businessUnit}': '${marketArr.join('-')}'`);
// omitting "await" to speed up creation
responseObj[businessUnit] ||= {};
responseObj[businessUnit][marketArr.join('-')] = await this.buildDefinition(
businessUnit,
type,
nameArr,
marketArr
);
}
}
if (!i) {
Util.logger.error('Please define properties.marketList in your config');
}
return responseObj;
}
}
export default Builder;