diff --git a/lib/cli.js b/lib/cli.js index d79d73cd3..24154b665 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -87,6 +87,12 @@ yargs(hideBin(process.argv)) describe: 'type or type:key or type:i:id or type:n:name to deploy; if not provided, all metadata will be deploy', }) + .option('addKeySuffix', { + type: 'string', + group: 'Options for deploy:', + describe: + 'allows you to add a suffix to the key of the metadata to be deployed. Only works together with changeKeyField', + }) .option('changeKeyField', { type: 'string', group: 'Options for deploy:', @@ -644,6 +650,12 @@ yargs(hideBin(process.argv)) describe: 'filter metadata components (can include % as wildcard or _ for a single character)', }) + .option('addKeySuffix', { + type: 'string', + group: 'Options for fixKeys:', + describe: + 'allows you to add a suffix to the key of the metadata to be deployed.', + }) .option('execute', { type: 'boolean', group: 'Options for fixKeys:', diff --git a/lib/index.js b/lib/index.js index 87171109c..d5444717c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -77,6 +77,7 @@ class Mcdev { static setOptions(argv) { const knownOptions = [ 'api', + 'addKeySuffix', 'changeKeyField', 'changeKeyValue', 'commitHistory', diff --git a/lib/metadataTypes/MetadataType.js b/lib/metadataTypes/MetadataType.js index 0159a1a84..cd5a3d657 100644 --- a/lib/metadataTypes/MetadataType.js +++ b/lib/metadataTypes/MetadataType.js @@ -739,11 +739,14 @@ class MetadataType { ); } else if (metadataMap[metadataKey][Util.OPTIONS.changeKeyField]) { // NOTE: trim twice while getting the newKey value to remove leading spaces before limiting the length - const newKey = (metadataMap[metadataKey][Util.OPTIONS.changeKeyField] + '') - .trim() - .slice(0, maxKeyLength) - .trim(); - if (metadataMap[metadataKey][Util.OPTIONS.changeKeyField] + '' > maxKeyLength) { + const newKey = this.getNewKey(metadataMap[metadataKey], maxKeyLength); + + if ( + metadataMap[metadataKey][Util.OPTIONS.changeKeyField] + + '' + + Util.OPTIONS.addKeySuffix > + maxKeyLength + ) { Util.logger.warn( `${this.definition.type} ${this.definition.keyField} may not exceed ${maxKeyLength} characters. Truncated the value in field ${Util.OPTIONS.changeKeyField} to ${newKey}` ); @@ -2166,30 +2169,33 @@ class MetadataType { Util.logger.info( `Searching for ${this.definition.type} keys among downloaded items that need fixing:` ); + Util.OPTIONS.addKeySuffix = Util.OPTIONS.addKeySuffix + ? Util.OPTIONS.addKeySuffix.trim() + : ''; + const maxKeyLength = this.definition.maxKeyLength || 36; + for (const item of Object.values(metadataMap)) { - if (item[this.definition.nameField].length > this.definition.maxKeyLength) { + if ( + (item[this.definition.nameField].endsWith(Util.OPTIONS.addKeySuffix) && + item[this.definition.nameField].length > maxKeyLength) || + (!item[this.definition.nameField].endsWith(Util.OPTIONS.addKeySuffix) && + item[this.definition.nameField].length + Util.OPTIONS.addKeySuffix.length > + maxKeyLength) + ) { Util.logger.warn( - `Name of the item ${ - item[this.definition.keyField] - } is too long for a key. Consider renaming your item. Key will be equal first ${ - this.definition.maxKeyLength - } characters of the name` - ); - item[this.definition.nameField] = item[this.definition.nameField].slice( - 0, - this.definition.maxKeyLength + `Name of the item ${item[this.definition.keyField]} (${ + item[this.definition.nameField] + }) is too long for a key${Util.OPTIONS.addKeySuffix.length ? ' (including the suffix ' + Util.OPTIONS.addKeySuffix + ')' : ''}. Consider renaming your item. Key will be equal first ${maxKeyLength} characters of the name` ); } - - if ( - item[this.definition.nameField] != item[this.definition.keyField] && - !this.definition.keyIsFixed - ) { - keysForDeploy.push(item[this.definition.keyField]); + const newKey = this.getNewKey(item, maxKeyLength); + if (newKey != item[this.definition.keyField] && !this.definition.keyIsFixed) { + // add key but make sure to turn it into string or else numeric keys will be filtered later + keysForDeploy.push(item[this.definition.keyField] + ''); Util.logger.info( ` - added ${this.definition.type} to fixKey queue: ${ item[this.definition.keyField] - }` + } >> ${newKey}` ); } else { Util.logger.info( @@ -2205,6 +2211,29 @@ class MetadataType { } return keysForDeploy; } + /** + * helper for getKeysForFixing and createOrUpdate + * + * @param {MetadataTypeItem} metadataItem - + * @param {number} maxKeyLength - + * @returns {string} newKey + */ + static getNewKey(metadataItem, maxKeyLength) { + let newKey; + newKey = (metadataItem[this.definition.nameField] + '') + .trim() + .slice(0, maxKeyLength) + .trim(); + if (Util.OPTIONS.addKeySuffix.length && !newKey.endsWith(Util.OPTIONS.addKeySuffix)) { + newKey = + (metadataItem[this.definition.nameField] + '') + .trim() + .slice(0, maxKeyLength - Util.OPTIONS.addKeySuffix.length) + .trim() + Util.OPTIONS.addKeySuffix; + } + + return newKey; + } } MetadataType.definition = {