Skip to content

Commit

Permalink
#1270: rcb support for asset
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Jun 2, 2024
1 parent 88660ea commit 2ce42c9
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/metadataTypes/Asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import cliProgress from 'cli-progress';
import cache from '../util/cache.js';
import TriggeredSend from './TriggeredSend.js';
import Folder from './Folder.js';
import ReplaceCbReference from '../util/replaceContentBlockReference.js';

/**
* @typedef {import('../../types/mcdev.d.js').BuObject} BuObject
Expand Down Expand Up @@ -2099,6 +2100,75 @@ class Asset extends MetadataType {
const fileContent = await File.readJSONFile(pathArr.join('/'), fileName, false);
return fileContent;
}
/**
*
* @param {MetadataTypeItem} item single metadata item
* @param {string} retrieveDir directory where metadata is saved
* @returns {Promise.<MetadataTypeItem>} key of the item that was updated
*/
static async replaceCbReference(item, retrieveDir) {
const responseItem = structuredClone(item);
const parentName = `${this.definition.type} ${item[this.definition.keyField]}`;
let changes = false;
let error;

// *** type specific logic ***
const subType = this.#getMainSubtype(item.assetType.name);
/** @type {CodeExtract[]} */
const fileList = await this._mergeCode(
item,
retrieveDir,
subType,
item[this.definition.keyField],
false
);
const fileListChanged = [];
for (const file of fileList) {
try {
file.content = ReplaceCbReference.replaceReference(file.content, parentName);
changes = true;
fileListChanged.push(file);
} catch (ex) {
if (ex.code !== 200) {
error = ex;
}
}
}

// save what was changed regardless of other errors
for (const extractedFile of fileListChanged) {
File.writeToFile(
[retrieveDir, ...extractedFile.subFolder],
extractedFile.fileName,
extractedFile.fileExt,
extractedFile.content,
extractedFile.encoding || null
);
}

if (error) {
throw error;
}

if (!changes) {
const ex = new Error('No changes made to the code.');
ex.code = 200;
throw ex;
}

// *** finish ***
// replaceReference will throw an error if nothing was updated which will end execution here
// no error means we have a new item to deploy and need to update the item in our retrieve folder
return responseItem;
}
/**
* empty dummy method as assets are note saved via the central method
*
* @returns {MetadataTypeItem} saved metadata
*/
static async saveToDisk() {
return;
}
}

// Assign definition to static attributes
Expand Down

0 comments on commit 2ce42c9

Please sign in to comment.