Skip to content

Commit

Permalink
#1302: --addKeySuffix logic for fixKeys and deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed May 6, 2024
1 parent f701d5d commit 966620e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
12 changes: 12 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:',
Expand Down Expand Up @@ -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:',
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Mcdev {
static setOptions(argv) {
const knownOptions = [
'api',
'addKeySuffix',
'changeKeyField',
'changeKeyValue',
'commitHistory',
Expand Down
73 changes: 51 additions & 22 deletions lib/metadataTypes/MetadataType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
Expand Down Expand Up @@ -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(
Expand All @@ -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 = {
Expand Down

0 comments on commit 966620e

Please sign in to comment.