-
Notifications
You must be signed in to change notification settings - Fork 36
/
cli.js
672 lines (655 loc) · 28.1 KB
/
cli.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
'use strict';
const TYPE = require('../../types/mcdev.d');
const BuHelper = require('./businessUnit');
const File = require('./file');
const config = require('./config');
const inquirer = require('inquirer');
const MetadataDefinitions = require('./../MetadataTypeDefinitions');
const Util = require('./util');
const auth = require('./auth');
require('console.table');
/**
* CLI helper class
*/
const Cli = {
/**
* used when initially setting up a project.
* loads default config and adds first credential
*
* @returns {Promise.<boolean>} success of init
*/
async initMcdevConfig() {
Util.logger.info('-- Initialising server connection --');
Util.logger.info('Please enter a name for your "Installed Package" credentials:');
const propertiesTemplate = await config.getDefaultProperties();
delete propertiesTemplate.credentials.default;
// wait for the interaction to finish or else an outer await will run before this is done
return this._setCredential(propertiesTemplate, null);
},
/**
* Extends template file for properties.json
*
* @param {TYPE.Mcdevrc} properties config file's json
* @returns {Promise.<boolean | string>} status
*/
async addExtraCredential(properties) {
const skipInteraction = Util.skipInteraction;
if (await config.checkProperties(properties)) {
this.logExistingCredentials(properties);
Util.logger.info('\nPlease enter your new credentials');
if (skipInteraction && properties.credentials[skipInteraction.credentialName]) {
Util.logger.error(
`Credential '${skipInteraction.credentialName}' already existing. If you tried updating please provide run 'mcdev init ${skipInteraction.credentialName}'`
);
return null;
}
return this._setCredential(properties, null);
} else {
// return null here to avoid seeing 2 error messages for the same issue
return null;
}
},
/**
*
* @param {TYPE.SupportedMetadataTypes} type limit execution to given metadata type
* @param {TYPE.SupportedMetadataTypes[]} dependentTypes types that depent on type
* @returns {Promise.<boolean>} true if user wants to continue with retrieve
*/
async postFixKeysReretrieve(type, dependentTypes) {
if (Util.isTrue(Util.skipInteraction?.fixKeysReretrieve)) {
return true;
} else if (Util.isFalse(Util.skipInteraction?.fixKeysReretrieve)) {
return false;
} else {
const now = await inquirer.prompt([
{
type: 'confirm',
name: 'fixKeysReretrieve',
message: `Do you want to re-retrieve dependent types (${dependentTypes.join(
', '
)}) now?`,
default: true,
},
]);
if (Util.OPTIONS._multiBuExecution) {
const remember = await inquirer.prompt([
{
type: 'confirm',
name: 'rememberFixKeysReretrieve',
message: `Remember answer for other BUs?`,
default: true,
},
]);
if (remember.rememberFixKeysReretrieve) {
Util.skipInteraction ||= {};
Util.skipInteraction.fixKeysReretrieve = now.fixKeysReretrieve;
}
}
return now.fixKeysReretrieve;
}
},
/**
* helper that logs to cli which credentials are already existing in our config file
*
* @param {TYPE.Mcdevrc} properties config file's json
* @returns {void}
*/
logExistingCredentials(properties) {
Util.logger.info('Found the following credentials in your config file:');
for (const cred in properties.credentials) {
if (Object.prototype.hasOwnProperty.call(properties.credentials, cred)) {
Util.logger.info(` - ${cred}`);
}
}
},
/**
* Extends template file for properties.json
* update credentials
*
* @param {TYPE.Mcdevrc} properties config file's json
* @param {string} credName name of credential that needs updating
* @returns {Promise.<boolean>} success of update
*/
async updateCredential(properties, credName) {
const skipInteraction = Util.skipInteraction;
if (credName) {
if (!skipInteraction) {
Util.logger.info(`Please enter the details for '${credName}'`);
}
return await this._setCredential(properties, credName);
}
},
/**
* Returns Object with parameters required for accessing API
*
* @param {TYPE.Mcdevrc} properties object of all configuration including credentials
* @param {string} target code of BU to use
* @param {boolean|string} [isCredentialOnly] true:don't ask for BU | string: name of BU
* @param {boolean} [allowAll] Offer ALL as option in BU selection
* @returns {Promise.<TYPE.BuObject>} credential to be used for Business Unit
*/
async getCredentialObject(properties, target, isCredentialOnly, allowAll) {
try {
if (!(await config.checkProperties(properties))) {
// return null here to avoid seeing 2 error messages for the same issue
return null;
}
let [credential, businessUnit] = target ? target.split('/') : [null, null];
if (
credential &&
properties.credentials[credential] &&
!businessUnit &&
'string' === typeof isCredentialOnly
) {
// correct credential provided and BU pre-selected
businessUnit = isCredentialOnly;
} else if (!credential || !properties.credentials[credential]) {
// no or unknown credential provided; BU either to be selected or pre-selected
if (credential !== null) {
Util.logger.warn(`Credential '${credential}' not found`);
}
const response = await this._selectBU(properties, null, isCredentialOnly, allowAll);
credential = response.credential;
businessUnit = response.businessUnit;
if (!isCredentialOnly) {
Util.logger.info(
`You could directly pass in this info with '${credential}/${businessUnit}'`
);
} else if (credential && !businessUnit && 'string' === typeof isCredentialOnly) {
// BU pre-selected
businessUnit = isCredentialOnly;
}
} else if (
!isCredentialOnly &&
(!businessUnit || !properties.credentials[credential].businessUnits[businessUnit])
) {
// correct credential provided but BU still needed
if (businessUnit && businessUnit !== 'undefined') {
Util.logger.warn(
`Business Unit '${businessUnit}' not found for credential '${credential}'`
);
}
const response = await this._selectBU(properties, credential, null, allowAll);
businessUnit = response.businessUnit;
Util.logger.info(
`You could directly pass in this info with '${credential}/${businessUnit}'`
);
}
return {
eid: properties.credentials[credential].eid,
mid: properties.credentials[credential].businessUnits[businessUnit],
businessUnit: businessUnit,
credential: credential,
};
} catch (ex) {
Util.logger.error(ex.message);
return null;
}
},
/**
* helps select the right credential in case of bad initial input
*
* @param {TYPE.Mcdevrc} properties config file's json
* @param {string} [credential] name of valid credential
* @param {boolean} [isCredentialOnly] don't ask for BU if true
* @param {boolean} [allowAll] Offer ALL as option in BU selection
* @returns {Promise.<Array>} selected credential/BU combo
*/
async _selectBU(properties, credential, isCredentialOnly, allowAll) {
const credList = [];
const buList = [];
const questions = [];
const allBUsAnswer = '* (All BUs)';
// no proper credential nor BU was given. ask for credential first
if (!credential) {
for (const cred in properties.credentials) {
if (Object.keys(properties.credentials[cred].businessUnits).length) {
// only add credentials that have BUs
credList.push(cred);
}
}
questions.push({
type: 'list',
name: 'credential',
message: 'Please select the credential you were looking for:',
choices: credList,
// eslint-disable-next-line jsdoc/require-jsdoc
filter: function (answer) {
if (!isCredentialOnly) {
for (const bu in properties.credentials[answer].businessUnits) {
buList.push(bu);
}
if (!buList.length) {
// unlikely error as we are filtering for this already while creating credList
throw new Error('No Business Unit defined for this credential');
} else if (allowAll) {
// add ALL option to beginning of list
buList.unshift(allBUsAnswer);
}
}
return answer;
},
});
} else if (credential) {
for (const bu in properties.credentials[credential].businessUnits) {
buList.push(bu);
}
if (!buList.length) {
// that could only happen if config is faulty
throw new Error('No Business Unit defined for this credential');
} else if (allowAll) {
// add ALL option to beginning of list
buList.unshift(allBUsAnswer);
}
}
if ((credential && buList.length) || (!credential && !isCredentialOnly)) {
questions.push({
type: 'list',
name: 'businessUnit',
message: 'Please select the right BU:',
choices: buList,
});
}
let responses = null;
if (questions.length) {
try {
responses = await inquirer.prompt(questions);
} catch (ex) {
Util.logger.info(ex);
}
if (responses.businessUnit && responses.businessUnit === allBUsAnswer) {
// remove textual explanation of *
responses.businessUnit = '*';
}
} else {
throw new Error('credentials / BUs not configured');
}
return responses;
},
/**
* helper around _askCredentials
*
* @param {TYPE.Mcdevrc} properties from config file
* @param {string} [credName] name of credential that needs updating
* @returns {Promise.<boolean | string>} success of refresh or credential name
*/
async _setCredential(properties, credName) {
const skipInteraction = Util.skipInteraction;
// Get user input
let credentialsGood = null;
let inputData;
do {
if (skipInteraction) {
if (
skipInteraction.client_id &&
skipInteraction.client_secret &&
skipInteraction.auth_url &&
skipInteraction.account_id &&
skipInteraction.credentialName
) {
// assume skipInteraction=={client_id,client_secret,auth_url,credentialName}
inputData = skipInteraction;
} else {
throw new Error(
'--skipInteraction flag found but not defined for all required inputs: client_id,client_secret,auth_url,account_id,credentialName'
);
}
} else {
inputData = await this._askCredentials(properties, credName);
}
// test if credentials are valid
try {
await auth.saveCredential(
{
client_id: inputData.client_id,
client_secret: inputData.client_secret,
auth_url: inputData.auth_url,
account_id: Number.parseInt(inputData.account_id),
},
inputData.credentialName
);
credentialsGood = true;
// update central config now that the credentials are verified
properties.credentials[inputData.credentialName] = {
eid: Number.parseInt(inputData.account_id),
businessUnits: {},
};
} catch (ex) {
Util.logger.error(ex.message);
credentialsGood = false;
if (skipInteraction) {
// break the otherwise infinite loop
return;
}
}
} while (!credentialsGood);
// Get all business units and add them to the properties
const status = await BuHelper.refreshBUProperties(properties, inputData.credentialName);
return status ? inputData.credentialName : status;
},
/**
* helper for {@link Cli.addExtraCredential}
*
* @param {TYPE.Mcdevrc} properties from config file
* @param {string} [credName] name of credential that needs updating
* @returns {Promise.<object>} credential info
*/
async _askCredentials(properties, credName) {
const questions = [];
if (!credName) {
questions.push({
type: 'input',
name: 'credentialName',
message: 'Credential name (your choice)',
// eslint-disable-next-line jsdoc/require-jsdoc
validate: function (value) {
if (!value || value.trim().length < 2) {
return 'Please enter at least 2 characters';
}
if (properties && properties.credentials[value]) {
return `There already is an account with the name '${value}' in your config.`;
}
const converted = encodeURIComponent(value).replaceAll(/[*]/g, '_STAR_');
if (value != converted) {
return 'Please do not use any special chars';
}
return true;
},
});
}
const tenantRegex =
/^https:\/\/([\w-]{28})\.(auth|soap|rest)\.marketingcloudapis\.com[/]?$/iu;
questions.push(
{
type: 'input',
name: 'client_id',
message: 'Client Id',
// eslint-disable-next-line jsdoc/require-jsdoc
validate: function (value) {
if (!value || value.trim().length < 10) {
return 'Please enter valid client id';
}
return true;
},
},
{
type: 'input',
name: 'client_secret',
message: 'Client Secret',
// eslint-disable-next-line jsdoc/require-jsdoc
validate: function (value) {
if (!value || value.trim().length < 10) {
return 'Please enter valid client secret';
}
return true;
},
},
{
type: 'input',
name: 'auth_url',
message: 'Authentication Base URI',
validate: (value) => {
if (!value || value.trim().length < 10) {
return 'Please enter a valid tenant identifier';
} else if (tenantRegex.test(value.trim())) {
// all good
return true;
} else {
return `Please copy the URI directly from the installed package's "API Integration" section. It looks like this: https://a1b2b3xy56z.auth.marketingcloudapis.com/`;
}
},
},
{
type: 'number',
name: 'account_id',
message: 'MID of Parent Business Unit',
}
);
const responses = await inquirer.prompt(questions);
// remove extra white space
responses.client_id = responses.client_id.trim();
responses.client_secret = responses.client_secret.trim();
responses.auth_url = responses.auth_url.trim();
if (credName) {
// if credential name was provided as parameter, we didn't ask the user for it
responses.credentialName = credName;
}
return responses;
},
/**
* allows updating the metadata types that shall be retrieved
*
* @param {TYPE.Mcdevrc} properties config file's json
* @param {string[]} [setTypesArr] skip user prompt and overwrite with this list if given
* @returns {Promise.<void>} -
*/
async selectTypes(properties, setTypesArr) {
let responses;
if (setTypesArr) {
responses = {
selectedTypes: setTypesArr,
};
} else {
if (Util.logger.level === 'debug') {
Util.logger.warn(
'Debug mode enabled. Allowing selection of "disabled" types. Please be aware that these might be unstable.'
);
} else {
Util.logger.info(
'Run mcdev selectTypes --debug if you need to use "disabled" types.'
);
}
const flattenedDefinitions = [];
for (const el in MetadataDefinitions) {
// if subtypes on metadata (eg. Assets) then add each nested subtype
if (
MetadataDefinitions[el].subTypes &&
Array.isArray(MetadataDefinitions[el].typeRetrieveByDefault)
) {
for (const subtype of MetadataDefinitions[el].subTypes) {
flattenedDefinitions.push({
typeName:
MetadataDefinitions[el].typeName.replace('-[Subtype]', ': ') +
subtype,
type: MetadataDefinitions[el].type + '-' + subtype,
mainType: MetadataDefinitions[el].type,
typeRetrieveByDefault:
MetadataDefinitions[el].typeRetrieveByDefault.includes(subtype),
});
}
}
// else just return normal type
else {
flattenedDefinitions.push({
typeName: MetadataDefinitions[el].typeName,
type: MetadataDefinitions[el].type,
typeRetrieveByDefault: MetadataDefinitions[el].typeRetrieveByDefault,
});
}
}
// walk through all definitions (sub and main) and select them if already selected
const typeChoices = flattenedDefinitions.map((def) => ({
name:
def.typeName +
(Util.logger.level === 'debug' && !def.typeRetrieveByDefault
? ' \x1B[1;30;40m(non-default)\u001B[0m'
: ''),
value: def.type,
disabled:
Util.logger.level === 'debug' || def.typeRetrieveByDefault ? false : 'disabled',
// subtypes can be activated through their main type
checked:
properties.metaDataTypes.retrieve.includes(def.type) ||
(properties.metaDataTypes.retrieve.includes(def.mainType) &&
def.typeRetrieveByDefault)
? true
: false,
}));
// sort types by 1) initially selected and 2) alphabetically
typeChoices.sort((a, b) => {
if (a.name && b.name && a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
}
if (a.name && b.name && a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
if (a.value.toLowerCase() < b.value.toLowerCase()) {
return -1;
}
if (a.value.toLowerCase() > b.value.toLowerCase()) {
return 1;
}
return 0;
});
// add end-of-list marker
typeChoices.push(new inquirer.Separator(' ==== '));
responses = await inquirer.prompt([
{
type: 'checkbox',
message: 'Select Metadata types for retrieval',
name: 'selectedTypes',
pageSize: 10,
choices: typeChoices,
},
]);
}
if (responses.selectedTypes) {
this._summarizeSubtypes(responses, 'asset');
// update config
properties.metaDataTypes.retrieve = responses.selectedTypes;
await File.saveConfigFile(properties);
}
},
/**
* helper for {@link Cli.selectTypes} that converts subtypes back to main type if all and only defaults were selected
* this keeps the config automatically upgradable when we add new subtypes or change what is selected by default
*
* @param {object} responses wrapper object for respones
* @param {string[]} responses.selectedTypes what types the user selected
* @param {string} type metadata type
* @returns {void}
*/
_summarizeSubtypes(responses, type) {
const selectedAssetSubtypes = responses.selectedTypes.filter((str) =>
str.includes(type + '-')
);
if (
selectedAssetSubtypes.length === MetadataDefinitions[type].typeRetrieveByDefault.length
) {
const nonDefaultSelectedAssetSubtypes = selectedAssetSubtypes
.map((subtype) => subtype.replace(type + '-', ''))
.filter(
(subtype) => !MetadataDefinitions[type].typeRetrieveByDefault.includes(subtype)
);
if (!nonDefaultSelectedAssetSubtypes.length) {
// found all defaults and nothing else. replace with main type
responses.selectedTypes = responses.selectedTypes.filter(
(str) => !str.includes(type + '-')
);
responses.selectedTypes.push(type);
responses.selectedTypes.sort();
}
}
},
/**
* shows metadata type descriptions
*
* @returns {object[]} list of supported types with their apiNames
*/
explainTypes() {
const MetadataTypeInfo = require('./../MetadataTypeInfo');
const TransactionalMessage = require('./../metadataTypes/TransactionalMessage');
const json = [];
const apiNameArr = Object.keys(MetadataDefinitions);
for (const apiName of apiNameArr) {
const details = MetadataDefinitions[apiName];
const supportCheckClass = apiName.startsWith('transactional')
? TransactionalMessage
: MetadataTypeInfo[apiName];
json.push({
name: details.typeName,
apiName: details.type,
retrieveByDefault: details.typeRetrieveByDefault,
supports: {
retrieve: Object.prototype.hasOwnProperty.call(supportCheckClass, 'retrieve'),
create: Object.prototype.hasOwnProperty.call(supportCheckClass, 'create'),
update: Object.prototype.hasOwnProperty.call(supportCheckClass, 'update'),
delete: Object.prototype.hasOwnProperty.call(supportCheckClass, 'deleteByKey'),
changeKey:
supportCheckClass.definition.keyIsFixed === false &&
supportCheckClass.definition.keyField !==
supportCheckClass.definition.idField &&
supportCheckClass.definition.fields[supportCheckClass.definition.keyField]
.isUpdateable &&
Object.prototype.hasOwnProperty.call(supportCheckClass, 'update'),
buildTemplate: Object.prototype.hasOwnProperty.call(
supportCheckClass,
'create'
), // supported for all types that can be created
retrieveAsTemplate: Object.prototype.hasOwnProperty.call(
supportCheckClass,
'retrieveAsTemplate'
),
},
description: details.typeDescription,
});
}
if (Util.OPTIONS.json) {
if (Util.OPTIONS.loggerLevel !== 'error') {
console.log(JSON.stringify(json, null, 2)); // eslint-disable-line no-console
}
return json;
}
const typeChoices = [];
for (const el in MetadataDefinitions) {
if (MetadataDefinitions[el].subTypes && MetadataDefinitions[el].extendedSubTypes) {
// used for assets to show whats available by default
typeChoices.push({
Name: MetadataDefinitions[el].typeName,
Default: '┐',
Description: MetadataDefinitions[el].typeDescription,
});
let lastCountdown = MetadataDefinitions[el].subTypes.length;
for (const subtype of MetadataDefinitions[el].subTypes) {
lastCountdown--;
const subTypeRetrieveByDefault =
Array.isArray(MetadataDefinitions[el].typeRetrieveByDefault) &&
MetadataDefinitions[el].typeRetrieveByDefault.includes(subtype);
const definition =
' ' + MetadataDefinitions[el].extendedSubTypes?.[subtype]?.join(', ');
typeChoices.push({
Name:
MetadataDefinitions[el].typeName.replace('-[Subtype]', ': ') + subtype,
Default:
(lastCountdown > 0 ? '├ ' : '└ ') +
(subTypeRetrieveByDefault ? 'yes' : '-'),
Description:
definition.length > 90 ? definition.slice(0, 90) + '...' : definition,
});
}
// change leading symbol of last subtype to close the tree visually
} else {
// types without subtypes
typeChoices.push({
Name: MetadataDefinitions[el].typeName,
Default: MetadataDefinitions[el].typeRetrieveByDefault ? 'yes' : '-',
Description: MetadataDefinitions[el].typeDescription,
});
}
}
typeChoices.sort((a, b) => {
if (a.Name.toLowerCase() < b.Name.toLowerCase()) {
return -1;
}
if (a.Name.toLowerCase() > b.Name.toLowerCase()) {
return 1;
}
return 0;
});
if (Util.OPTIONS.loggerLevel !== 'error') {
console.table(typeChoices); // eslint-disable-line no-console
}
return json;
},
};
module.exports = Cli;