From fee3e04721da43382894d364c46e93a0c35926e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Tue, 14 May 2024 11:40:58 +0200 Subject: [PATCH] #1316: enable --metadata and multi-type for fixKeys --- lib/cli.js | 15 +++++++++++++-- lib/index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 47d389015..ece283ce5 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -638,7 +638,7 @@ yargs(hideBin(process.argv)) }) // @ts-expect-error .command({ - command: 'fixKeys [KEY]', + command: 'fixKeys [TYPE] [KEY]', aliases: ['fx'], desc: 'changes the key of the items to match the name', builder: (yargs) => { @@ -655,6 +655,12 @@ yargs(hideBin(process.argv)) type: 'string', describe: 'key(s) of the metadata component(s)', }) + .option('metadata', { + type: 'string', + alias: 'm', + group: 'Options for fixKeys:', + describe: 'type or type:key or type:i:id or type:n:name to fix', + }) .option('like', { type: 'string', group: 'Options for fixKeys:', @@ -686,7 +692,12 @@ yargs(hideBin(process.argv)) }, handler: (argv) => { Mcdev.setOptions(argv); - Mcdev.fixKeys(argv.BU, argv.TYPE, csvToArray(argv.KEY)); + const typeKeyCombo = Mcdev.metadataToTypeKey(argv.metadata); + if ('undefined' === typeof typeKeyCombo) { + Mcdev.fixKeys(argv.BU, csvToArray(argv.TYPE), csvToArray(argv.KEY)); + } else { + Mcdev.fixKeys(argv.BU, typeKeyCombo); + } }, }) // @ts-expect-error diff --git a/lib/index.js b/lib/index.js index 5f0134877..8d0d3252e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -884,12 +884,55 @@ class Mcdev { * Updates the key to match the name field * * @param {string} businessUnit name of BU - * @param {string} selectedType limit to given metadata types + * @param {string[] | TypeKeyCombo} selectedTypesArr limit retrieval to given metadata type * @param {string[]} [keys] customerkey of the metadata - * @returns {Promise.>} key: business unit name, value: list of paused item keys + * @returns {Promise.>} key1: business unit name, key2:type value: list of fixed item keys */ - static async fixKeys(businessUnit, selectedType, keys) { - return this.#runMethod('fixKeys', businessUnit, selectedType, keys); + static async fixKeys(businessUnit, selectedTypesArr, keys) { + if (selectedTypesArr) { + // check if types are valid + for (const selectedType of Array.isArray(selectedTypesArr) + ? selectedTypesArr + : Object.keys(selectedTypesArr)) { + if (!Util._isValidType(selectedType)) { + return; + } + } + } else { + // do it for all standard retrieve types + const properties = await config.getProperties(); + selectedTypesArr = []; + selectedTypesArr.push( + ...new Set( + properties.metaDataTypes.retrieve + .map((type) => type.split('-')[0]) + .filter( + (type) => + !MetadataTypeDefinitions[type].keyIsFixed && + MetadataTypeDefinitions[type].keyField !== + MetadataTypeDefinitions[type].nameField && + MetadataTypeDefinitions[type].keyField !== + MetadataTypeDefinitions[type].idField + ) + ) + ); + Util.logger.info(`:: Fixing keys for ${selectedTypesArr.join(', ')}`); + } + + const response = {}; + for (const selectedType of Array.isArray(selectedTypesArr) + ? selectedTypesArr + : Object.keys(selectedTypesArr)) { + const temp = await this.#runMethod( + 'fixKeys', + businessUnit, + selectedType, + Array.isArray(selectedTypesArr) ? keys : selectedTypesArr[selectedType] + ); + response[businessUnit] ||= {}; + response[businessUnit][selectedType] = temp[businessUnit]; + } + return response; } /** * run a method across BUs