Skip to content

Commit

Permalink
Merge pull request #1328 from Accenture/bug/1324-buildtemplate-for-as…
Browse files Browse the repository at this point in the history
…sets-copies-non-json-files-into-subfolders-that-have-template-variable-in-them

Bug/1324 buildTemplate/buildDefinition for assets copies non json files into subfolders that have template variable in them + Bug/798 replacing MIDs in JSONs break buildTemplate
  • Loading branch information
JoernBerkefeld authored May 16, 2024
2 parents 56e3141 + 94317a3 commit a3e87f5
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 69 deletions.
6 changes: 4 additions & 2 deletions lib/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ saved
);
})
);
if (result && type === result[0].type) {
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) {
Expand Down Expand Up @@ -151,7 +152,8 @@ saved
);
})
);
if (result && type === result[0].type) {
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) {
Expand Down
45 changes: 21 additions & 24 deletions lib/metadataTypes/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ class Asset extends MetadataType {
if (
!Util.OPTIONS.noMidSuffix &&
this.buObject.mid &&
metadata.memberId !== this.buObject.mid &&
metadata.memberId != this.buObject.mid && // soft comparison to accomodate for string-version of mid
!metadata[this.definition.keyField].endsWith(this.buObject.mid)
) {
// #3 make sure customer key is unique by suffixing it with target MID (unless we are deploying to the same MID)
Expand Down Expand Up @@ -922,6 +922,9 @@ class Asset extends MetadataType {
}: ${extractedFile.fileName}.${extractedFile.fileExt}.`
);
}
extractedFile.subFolder = extractedFile.subFolder
.map((el) => (el === templateName ? metadata[this.definition.keyField] : el))
.map((el) => this.applyTemplateValues(el, templateVariables));
}

// #2 binary extracts
Expand Down Expand Up @@ -964,7 +967,6 @@ class Asset extends MetadataType {
);
nestedFilePaths.push([
File.normalizePath([targetDir, ...extractedFile.subFolder]),
this.definition.type,
extractedFile.fileName +
'.' +
this.definition.type +
Expand Down Expand Up @@ -1026,13 +1028,14 @@ class Asset extends MetadataType {
let readDirArr;
// unfortunately, asset's key can contain spaces at beginning/end which can break the file system when folders are created with it
const customerKey = metadata.customerKey.trim();
const templateFileName = templateName || customerKey;
switch (metadata.assetType.name) {
case 'templatebasedemail': // message
case 'htmlemail': {
// message
// this complex type always creates its own subdir per asset
subDirArr = [this.definition.type, subType];
readDirArr = [deployDir, ...subDirArr, templateName || customerKey];
readDirArr = [deployDir, ...subDirArr, templateFileName];

// metadata.views.html.content (mandatory)
// the main content can be empty (=not set up yet) hence check if we did extract sth or else readFile() will print error msgs
Expand All @@ -1054,7 +1057,7 @@ class Asset extends MetadataType {
if (templateName) {
// to use this method in templating, store a copy of the info in fileList
fileList.push({
subFolder: [...subDirArr, customerKey],
subFolder: [...subDirArr, templateFileName],
fileName: fileName,
fileExt: 'html',
content: metadata.views.html.content,
Expand Down Expand Up @@ -1082,7 +1085,7 @@ class Asset extends MetadataType {
// template-template
// this complex type always creates its own subdir per asset
subDirArr = [this.definition.type, subType];
readDirArr = [deployDir, ...subDirArr, templateName || customerKey];
readDirArr = [deployDir, ...subDirArr, templateFileName];
const fileName = 'content' + subtypeExtension;

const fileExtArr = ['html']; // eslint-disable-line no-case-declarations
Expand All @@ -1103,7 +1106,7 @@ class Asset extends MetadataType {
if (templateName) {
// to use this method in templating, store a copy of the info in fileList
fileList.push({
subFolder: subDirArr,
subFolder: [...subDirArr, templateFileName],
fileName: fileName,
fileExt: ext,
content: metadata.content,
Expand Down Expand Up @@ -1140,23 +1143,23 @@ class Asset extends MetadataType {
await File.pathExists(
File.normalizePath([
...readDirArr,
`${templateName || customerKey}${subtypeExtension}.html`,
`${templateFileName}${subtypeExtension}.html`,
])
)
) {
// the main content can be empty (=not set up yet) hence check if we did extract sth or else readFile() will print error msgs
if (!fileListOnly) {
metadata.views.text.content = await File.readFilteredFilename(
readDirArr,
(templateName || customerKey) + subtypeExtension,
templateFileName + subtypeExtension,
'html'
);
}
if (templateName) {
// to use this method in templating, store a copy of the info in fileList
fileList.push({
subFolder: subDirArr,
fileName: customerKey + subtypeExtension,
fileName: templateFileName + subtypeExtension,
fileExt: 'html',
content: metadata.views.text.content,
});
Expand All @@ -1168,7 +1171,7 @@ class Asset extends MetadataType {
// asset
// this complex type always creates its own subdir per asset
subDirArr = [this.definition.type, subType];
readDirArr = [deployDir, ...subDirArr, templateName || customerKey];
readDirArr = [deployDir, ...subDirArr, templateFileName];

// metadata.views.html.slots.<>.blocks.<>.content (optional) (pre & post 20222)
if (metadata?.views?.html?.slots) {
Expand Down Expand Up @@ -1206,7 +1209,7 @@ class Asset extends MetadataType {
if (templateName) {
// to use this method in templating, store a copy of the info in fileList
fileList.push({
subFolder: [...subDirArr, customerKey],
subFolder: [...subDirArr, templateFileName],
fileName: 'views.html.content' + subtypeExtension,
fileExt: 'html',
content: metadata.views.html.content,
Expand All @@ -1232,7 +1235,7 @@ class Asset extends MetadataType {
if (templateName) {
// to use this method in templating, store a copy of the info in fileList
fileList.push({
subFolder: [...subDirArr, customerKey],
subFolder: [...subDirArr, templateFileName],
fileName: 'content' + subtypeExtension,
fileExt: 'html',
content: metadata.views.html.content,
Expand Down Expand Up @@ -1260,15 +1263,15 @@ class Asset extends MetadataType {
await File.pathExists(
File.normalizePath([
...readDirArr,
`${templateName || customerKey}${subtypeExtension}.${ext}`,
`${templateFileName}${subtypeExtension}.${ext}`,
])
)
) {
// the main content can be empty (=not set up yet) hence check if we did extract sth or else readFile() will print error msgs
if (!fileListOnly) {
metadata.content = await File.readFilteredFilename(
readDirArr,
(templateName || customerKey) + subtypeExtension,
templateFileName + subtypeExtension,
ext
);
if (ext === 'ssjs') {
Expand All @@ -1279,7 +1282,7 @@ class Asset extends MetadataType {
// to use this method in templating, store a copy of the info in fileList
fileList.push({
subFolder: subDirArr,
fileName: (templateName || customerKey) + subtypeExtension,
fileName: templateFileName + subtypeExtension,
fileExt: ext,
content: metadata.content,
});
Expand Down Expand Up @@ -1318,6 +1321,8 @@ class Asset extends MetadataType {
templateName,
fileListOnly = false
) {
const templateFileName = templateName || customerKey;

for (const slot in metadataSlots) {
if (Object.prototype.hasOwnProperty.call(metadataSlots, slot)) {
const slotObj = metadataSlots[slot];
Expand Down Expand Up @@ -1347,7 +1352,7 @@ class Asset extends MetadataType {
if (templateName) {
// to use this method in templating, store a copy of the info in fileList
fileList.push({
subFolder: [...subDirArr, customerKey, 'blocks'],
subFolder: [...subDirArr, templateFileName, 'blocks'],
fileName: fileName,
fileExt: 'html',
content: slotObj.blocks[block].content,
Expand Down Expand Up @@ -1662,14 +1667,6 @@ class Asset extends MetadataType {
break;
}
}
if (!subType) {
throw new Error(
`Could not find asset with name ${templateName} in ${File.normalizePath([
templateDir,
...typeDirArr,
])}`
);
}
return subType;
}
/**
Expand Down
57 changes: 26 additions & 31 deletions lib/metadataTypes/MetadataType.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,21 +359,15 @@ class MetadataType {
ex
);
} catch {
throw new Error(
`${this.definition.type}:: Could not find ./${File.normalizePath([
retrieveDir,
...typeDirArr,
fileName + '.json',
])}.`
// only happening for types that use readSecondaryFolder (e.g. asset)
// if we still have no metadataStr then we have to skip this metadata for all types and hence handle it outside of this catch
}
if (!metadataStr) {
Util.logger.warn(
Util.getGrayMsg(`- skipped ${this.definition.type} ${key}: not found`)
);
return;
}
// return;
}
if (!metadataStr) {
Util.logger.info(
Util.getGrayMsg(`- skipped ${this.definition.type} ${key}: not found`)
);
return;
}

if (this.definition.stringifyFieldsBeforeTemplate) {
Expand All @@ -396,6 +390,12 @@ class MetadataType {
}
metadataStr = JSON.stringify(metadataStr);
}
if (this.definition.type === 'asset') {
// turn memberId integer to string to ensure it does not get replaced with a templateVariable
const temp = JSON.parse(metadataStr);
temp.memberId = temp.memberId.toString();
metadataStr = JSON.stringify(temp);
}
const metadata = JSON.parse(Util.replaceByObject(metadataStr, templateVariables));
this.keepTemplateFields(metadata);

Expand Down Expand Up @@ -1958,30 +1958,26 @@ class MetadataType {
ex
);
} catch {
throw new Error(
`${this.definition.type}:: Could not find ./${File.normalizePath([
templateDir,
...typeDirArr,
fileName + '.json',
])}.`
// only happening for types that use readSecondaryFolder (e.g. asset)
// if we still have no metadataStr then we have to skip this metadata for all types and hence handle it outside of this catch
}
if (!metadataStr) {
Util.logger.warn(
Util.getGrayMsg(
`- skipped ${this.definition.type} ${templateName}: template not found`
)
);
return;
}
// return;
}
if (!metadataStr) {
Util.logger.info(
Util.getGrayMsg(
`- skipped ${this.definition.type} ${templateName}: template not found`
)
);
return;
}

let metadata;
try {
// update all initial variables & create metadata object
metadata = JSON.parse(Mustache.render(metadataStr, variables, {}, ['{{{', '}}}']));
typeDirArr = typeDirArr.map((el) => Mustache.render(el, variables, {}, ['{{{', '}}}']));
metadata = JSON.parse(this.applyTemplateValues(metadataStr, variables));
typeDirArr = typeDirArr
.map((el) => (el === templateName ? metadata[this.definition.keyField] : el))
.map((el) => this.applyTemplateValues(el, variables));
} catch {
throw new Error(
`${this.definition.type}:: Error applying template variables on ${
Expand All @@ -2004,7 +2000,6 @@ class MetadataType {
try {
// write to file
const targetDirArr = Array.isArray(targetDir) ? targetDir : [targetDir];

for (const targetDir of targetDirArr) {
await File.writeJSONToFile(
[targetDir, ...typeDirArr],
Expand Down
13 changes: 1 addition & 12 deletions lib/metadataTypes/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Util } from '../util/util.js';
import MetadataType from './MetadataType.js';
import File from '../util/file.js';
import cache from '../util/cache.js';
import Mustache from 'mustache';

/**
* @typedef {import('../../types/mcdev.d.js').BuObject} BuObject
Expand All @@ -24,16 +23,6 @@ import Mustache from 'mustache';
* @typedef {import('../../types/mcdev.d.js').QueryMap} QueryMap
*/

/**
* ensure that Mustache does not escape any characters
*
* @param {string} text -
* @returns {string} text
*/
Mustache.escape = function (text) {
return text;
};

/**
* Query MetadataType
*
Expand Down Expand Up @@ -292,7 +281,7 @@ class Query extends MetadataType {
.join('}}}');

// replace template variables with their values
return Mustache.render(code, templateVariables, {}, ['{{{', '}}}']);
return super.applyTemplateValues(code, templateVariables);
}
/**
* helper for {@link MetadataType.buildDefinition}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<a href="%%ftaf_url%%"
><img
src="https://www.exacttarget.com/members/newsletters/gfx/forwardafriendicon.gif"
border="0" /></a
><a href="%%profile_center_url%%" alias="Update Profile">Update Profile</a>
<table cellpadding="2" cellspacing="0" width="600" id="Table5" border="0">
<tr>
<td>
<font face="verdana" size="1" color="#444444"
>This email was sent by: <b>%%Member_Busname%%</b><br />%%Member_Addr%%
%%Member_City%%, %%Member_State%%, %%Member_PostalCode%%, %%Member_Country%%<br /><br
/></font>
</td>
</tr>
</table>
<a href="%%subscription_center_url%%" alias="Manage Subscriptions">Manage Subscriptions</a
><img src="https://www.exacttarget.com/images/Powered_By_1206.jpg" border="0" /><custom
name="opencounter"
type="tracking"
/>
Loading

0 comments on commit a3e87f5

Please sign in to comment.