Skip to content

Commit

Permalink
#1270: rcb for script
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Jun 2, 2024
1 parent 5699437 commit b06cb02
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion @types/lib/metadataTypes/Script.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion @types/lib/util/replaceContentBlockReference.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 86 additions & 1 deletion lib/metadataTypes/Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import MetadataType from './MetadataType.js';
import { Util } from '../util/util.js';
import File from '../util/file.js';
import ReplaceCbReference from '../util/replaceContentBlockReference.js';

/**
* @typedef {import('../../types/mcdev.d.js').BuObject} BuObject
Expand All @@ -15,6 +16,7 @@ import File from '../util/file.js';
* @typedef {import('../../types/mcdev.d.js').MetadataTypeMapObj} MetadataTypeMapObj
* @typedef {import('../../types/mcdev.d.js').SoapRequestParams} SoapRequestParams
* @typedef {import('../../types/mcdev.d.js').TemplateMap} TemplateMap
* @typedef {import('../../types/mcdev.d.js').ContentBlockConversionTypes} ContentBlockConversionTypes
*/

/**
Expand Down Expand Up @@ -111,7 +113,7 @@ class Script extends MetadataType {
templateName + '.' + this.definition.type + '-meta',
'ssjs'
);
code = `<script runat="server">\n${code}</script>`;
code = `<script runat="server">\n${code.trim()}</script>`;
} else if (await File.pathExists(codePath + '.html')) {
code = await File.readFilteredFilename(
[deployDir, this.definition.type],
Expand Down Expand Up @@ -271,6 +273,15 @@ class Script extends MetadataType {
// folder
super.setFolderPath(metadata);

return this.getCodeExtractItem(metadata);
}
/**
* manages post retrieve steps
*
* @param {ScriptItem} metadata a single item
* @returns {CodeExtractItem} a single item with code parts extracted
*/
static getCodeExtractItem(metadata) {
// extract SSJS
const codeArr = [];
// keep between tags
Expand Down Expand Up @@ -382,6 +393,80 @@ class Script extends MetadataType {
`${this.definition.type}-meta.html`,
]);
}

/**
* Abstract execute method that needs to be implemented in child metadata type
*
* @param {MetadataTypeMap} metadataMap list of metadata (keyField => metadata)
* @param {string} retrieveDir retrieve dir including cred and bu
* @returns {Promise.<string[]>} Returns list of keys for which references were replaced
*/
static async replaceCbReference(metadataMap, retrieveDir) {
const keysForDeploy = [];
if (!metadataMap) {
// if a type was skipped e.g. because it shall only be looked at on the parent then we would expect metadataMap to be undefined
return keysForDeploy;
}
const deployMap = {};
/** @type {ContentBlockConversionTypes[]} */
const referenceFrom = Util.OPTIONS.referenceFrom;
/** @type {ContentBlockConversionTypes} */
const referenceTo = Util.OPTIONS.referenceTo;

const fromDescription = referenceFrom
.map((from) => 'ContentBlockBy' + Util.capitalizeFirstLetter(from))
.join(' and ');

if (Object.keys(metadataMap).length) {
Util.logger.info(`Searching ${this.definition.type} for ${fromDescription}:`);
const baseDir = [retrieveDir, ...this.definition.type.split('-')];

for (const key in metadataMap) {
const item = metadataMap[key];
if (this.isFiltered(item, true) || this.isFiltered(item, false)) {
// we would not have saved these items to disk but they exist in the cache and hence need to be skipped here

continue;
}
const code = await this._mergeCode(item, retrieveDir);

try {
item.script = ReplaceCbReference.replaceReference(
code,
referenceFrom,
referenceTo,
`${this.definition.type} ${item[this.definition.keyField]}`
);
deployMap[key] = this.getCodeExtractItem(item);
// 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 update queue: ${
item[this.definition.keyField]
}`
);
this.saveToDisk(deployMap, key, baseDir);
} catch (ex) {
if (ex.code !== 200) {
// dont print error if we simply did not find relevant content blocks
Util.logger.errorStack(ex, ex.message);
}
Util.logger.info(
Util.getGrayMsg(
` ☇ skipping ${this.definition.type} ${
item[this.definition.keyField]
}: no ${fromDescription} found`
)
);
}
}

Util.logger.info(
`Found ${keysForDeploy.length} ${this.definition.type}${keysForDeploy.length > 1 ? 's' : ''} to update`
);
}
return keysForDeploy;
}
}

// Assign definition & cache to static attributes
Expand Down

0 comments on commit b06cb02

Please sign in to comment.