diff --git a/lib/metadataTypes/Asset.js b/lib/metadataTypes/Asset.js index 6334e21fa..a2d0d65ea 100644 --- a/lib/metadataTypes/Asset.js +++ b/lib/metadataTypes/Asset.js @@ -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 @@ -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.} 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