Skip to content

Commit

Permalink
#19: generic buildTemplate() method
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed May 4, 2022
1 parent 067efe7 commit 0122ebf
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Builds metadata from a template using market specific customisation
* [new Builder(properties, buObject, client)](#new_Builder_new)
* _instance_
* [.buildDefinition(metadataType, name, variables)](#Builder+buildDefinition) ⇒ <code>Promise</code>
* [.buildTemplate(metadataType, name, variables)](#Builder+buildTemplate) ⇒ <code>Promise</code>
* _static_
* [.verifyMarketList(mlName, properties)](#Builder.verifyMarketList) ⇒ <code>void</code>

Expand Down Expand Up @@ -254,6 +255,20 @@ Builds a specific metadata file by name
| name | <code>string</code> | name of metadata to build |
| variables | <code>object</code> | variables to be replaced in the metadata |

<a name="Builder+buildTemplate"></a>

### builder.buildTemplate(metadataType, name, variables) ⇒ <code>Promise</code>
Builds a specific metadata file by name

**Kind**: instance method of [<code>Builder</code>](#Builder)
**Returns**: <code>Promise</code> - Promise

| Param | Type | Description |
| --- | --- | --- |
| metadataType | <code>string</code> | metadata type to build |
| name | <code>string</code> | name of metadata to build |
| variables | <code>object</code> | variables to be replaced in the metadata |

<a name="Builder.verifyMarketList"></a>

### Builder.verifyMarketList(mlName, properties) ⇒ <code>void</code>
Expand Down Expand Up @@ -2683,6 +2698,7 @@ Provides default functionality that can be overwritten by child metadata type cl
* [.retrieveChangelog([buObject], [additionalFields], [subType])](#MetadataType.retrieveChangelog) ⇒ <code>Promise.&lt;{metadata:Util.MetadataTypeMap, type:string}&gt;</code>
* [.retrieveForCache(buObject, [subType])](#MetadataType.retrieveForCache) ⇒ <code>Promise.&lt;{metadata:Util.MetadataTypeMap, type:string}&gt;</code>
* [.retrieveAsTemplate(templateDir, name, templateVariables, [subType])](#MetadataType.retrieveAsTemplate) ⇒ <code>Promise.&lt;{metadata:Util.MetadataTypeItem, type:string}&gt;</code>
* [.buildTemplate(retrieveDir, templateDir, name, templateVariables)](#MetadataType.buildTemplate) ⇒ <code>Promise.&lt;{metadata:Util.MetadataTypeItem, type:string}&gt;</code>
* [.preDeployTasks(metadata, deployDir)](#MetadataType.preDeployTasks) ⇒ <code>Promise.&lt;Util.MetadataTypeItem&gt;</code>
* [.create(metadata, deployDir)](#MetadataType.create) ⇒ <code>void</code>
* [.update(metadata, [metadataBefore])](#MetadataType.update) ⇒ <code>void</code>
Expand Down Expand Up @@ -2865,6 +2881,21 @@ Gets metadata cache with limited fields and does not store value to disk
| templateVariables | <code>Util.TemplateMap</code> | variables to be replaced in the metadata |
| [subType] | <code>string</code> | optionally limit to a single subtype |

<a name="MetadataType.buildTemplate"></a>

### MetadataType.buildTemplate(retrieveDir, templateDir, name, templateVariables) ⇒ <code>Promise.&lt;{metadata:Util.MetadataTypeItem, type:string}&gt;</code>
Gets metadata cache with limited fields and does not store value to disk

**Kind**: static method of [<code>MetadataType</code>](#MetadataType)
**Returns**: <code>Promise.&lt;{metadata:Util.MetadataTypeItem, type:string}&gt;</code> - metadata

| Param | Type | Description |
| --- | --- | --- |
| retrieveDir | <code>string</code> | Directory where retrieved metadata directory will be saved |
| templateDir | <code>string</code> | (List of) Directory where built definitions will be saved |
| name | <code>string</code> | name of the metadata file |
| templateVariables | <code>Util.TemplateMap</code> | variables to be replaced in the metadata |

<a name="MetadataType.preDeployTasks"></a>

### MetadataType.preDeployTasks(metadata, deployDir) ⇒ <code>Promise.&lt;Util.MetadataTypeItem&gt;</code>
Expand Down
74 changes: 74 additions & 0 deletions lib/metadataTypes/MetadataType.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,80 @@ class MetadataType {
);
return { metadata: null, type: this.definition.type };
}
/**
* Gets metadata cache with limited fields and does not store value to disk
*
* @param {string} retrieveDir Directory where retrieved metadata directory will be saved
* @param {string} templateDir (List of) Directory where built definitions will be saved
* @param {string} name name of the metadata file
* @param {Util.TemplateMap} templateVariables variables to be replaced in the metadata
* @returns {Promise<{metadata:Util.MetadataTypeItem,type:string}>} metadata
*/
static async buildTemplate(retrieveDir, templateDir, name, templateVariables) {
// retrieve metadata template
let metadataStr;
const typeDirArr = [this.definition.type];
const subType = this.findSubType(retrieveDir, name);
if (subType) {
typeDirArr.push(subType);
}
const suffix = subType ? `-${subType}-meta` : '-meta';
const fileName = name + '.' + this.definition.type + suffix;
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
metadataStr = await File.readFile([retrieveDir, ...typeDirArr], fileName, 'json');
} catch (ex) {
try {
metadataStr = await this.readSecondaryFolder(
retrieveDir,
typeDirArr,
name,
fileName,
ex
);
} catch (ex) {
throw new Error(
`${this.definition.type}:: Could not find ./${File.normalizePath([
retrieveDir,
...typeDirArr,
fileName + '.json',
])}.`
);
}
// return;
}
const metadata = JSON.parse(metadataStr);

// handle extracted code
// templating to extracted content is applied inside of buildTemplateForExtracts()
await this.buildTemplateForExtracts(
retrieveDir,
templateDir,
metadata,
templateVariables,
name
);

try {
// write to file
File.writeJSONToFile(
[templateDir, ...typeDirArr],
metadata[this.definition.keyField] + '.' + this.definition.type + suffix,
metadata
);
Util.logger.info(
'MetadataType[' +
this.definition.type +
'].buildDefinition:: Complete - ' +
metadata[this.definition.keyField]
);

return { metadata: metadata, type: this.definition.type };
} catch (ex) {
throw new Error(`${this.definition.type}:: ${ex.message}`);
}
}

/**
* Gets executed before deploying metadata
Expand Down

0 comments on commit 0122ebf

Please sign in to comment.