Skip to content

Commit

Permalink
#19
Browse files Browse the repository at this point in the history
  • Loading branch information
tulionatale committed Apr 26, 2022
1 parent 59511b0 commit 189feca
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/Retriever.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Retriever {
);
}
}

if (result) {
if (templateVariables && Array.isArray(result)) {
// so far we are only doing this for templates, hence the above if-check
Expand Down
1 change: 0 additions & 1 deletion lib/RetrieverLocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class RetrieverLocal {
// with npx and powershell spaces are not parsed correctly as part of a string
// we hence require users to put %20 in their stead and have to convert that back
name = name.split('%20').join(' ');
// console.log('####################subtype', subType);
return MetadataTypeInfo[type].buildTemplate(
this.templateDir,
name,
Expand Down
57 changes: 57 additions & 0 deletions lib/metadataTypes/DataExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,63 @@ class DataExtension extends MetadataType {
return { metadata: metadata, type: 'dataExtension' };
}

/**
* Retrieves dataExtension metadata in template format.
* @param {string} templateDir Directory where retrieved metadata directory will be saved
* @param {string} name name of the metadata item
* @param {Util.TemplateMap} templateVariables variables to be replaced in the metadata
* @returns {Promise<{metadata:DataExtensionMap,type:string}>} Promise of items
*/
static async buildTemplate(templateDir, name, templateVariables) {
const options = {
filter: {
leftOperand: 'Name',
operator: 'equals',
rightOperand: name,
},
};
/** @type DataExtensionMap */
const metadata = await this._retrieveAll(null, options);

if (!Object.keys(metadata).length) {
Util.logger.error(`${this.definition.type} '${name}' not found on server.`);
Util.logger.info('Downloaded: dataExtension (0)');
return { metadata: {}, type: 'dataExtension' };
}
const customerKey = Object.keys(metadata)[0];
await this._retrieveFieldsForSingleDe(metadata, customerKey);

for (const key in metadata) {
try {
// API returns field unsorted
metadata[key].Fields.sort((a, b) => a.Ordinal - b.Ordinal);

const originalKey = key;
const metadataCleaned = JSON.parse(
JSON.stringify(
await this.postRetrieveTasks(metadata[key], null, !!templateVariables)
)
);

this.keepTemplateFields(metadataCleaned);
const metadataTemplated = JSON.parse(
Util.replaceByObject(JSON.stringify(metadataCleaned), templateVariables)
);
File.writeJSONToFile(
[templateDir, this.definition.type].join('/'),
originalKey + '.' + this.definition.type + '-meta',
metadataTemplated
);
} catch (ex) {
Util.metadataLogger('error', this.definition.type, 'retrieve', ex, key);
}
}

Util.logger.info(
`DataExtension.buildTemplat:: All records written to filesystem (${customerKey})`
);
return { metadata: metadata, type: 'dataExtension' };
}
/**
* parses retrieved Metadata before saving
* @private
Expand Down
2 changes: 0 additions & 2 deletions lib/metadataTypes/MetadataType.js
Original file line number Diff line number Diff line change
Expand Up @@ -1139,9 +1139,7 @@ class MetadataType {
typeDirArr.push(subType);
}
const suffix = subType ? `-${subType}-meta` : '-meta';
// console.log('########################', typeDirArr);
const fileName = templateName + '.' + this.definition.type + suffix;
// console.log('########################', fileName);
try {
// ! do not load via readJSONFile to ensure we get a string, not parsed JSON
// templated files might contain illegal json before the conversion back to the file that shall be saved
Expand Down

0 comments on commit 189feca

Please sign in to comment.