UNPKG

@agility/cli

Version:

Agility CLI for working with your content. (Public Beta)

147 lines 7.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContainerMapper = void 0; var date_fns_1 = require("date-fns"); var core_1 = require("../../core"); var ContainerMapper = /** @class */ (function () { function ContainerMapper(sourceGuid, targetGuid) { this.sourceGuid = sourceGuid; this.targetGuid = targetGuid; this.directory = 'containers'; // this will provide access to the /agility-files/{GUID} folder this.fileOps = new core_1.fileOperations(targetGuid); this.mappings = this.loadMapping(); } ContainerMapper.prototype.getContainerMapping = function (container, type) { var mapping = this.mappings.find(function (m) { return type === 'source' ? m.sourceContentViewID === container.contentViewID : m.targetContentViewID === container.contentViewID; }); if (!mapping) return null; return mapping; }; ContainerMapper.prototype.getContainerMappingByContentViewID = function (contentViewID, type) { var mapping = this.mappings.find(function (m) { return type === 'source' ? m.sourceContentViewID === contentViewID : m.targetContentViewID === contentViewID; }); if (!mapping) return null; return mapping; }; ContainerMapper.prototype.getContainerMappingByReferenceName = function (referenceName, type) { var refNameLower = referenceName.toLowerCase(); var mapping = this.mappings.find(function (m) { return type === 'source' ? m.sourceReferenceName.toLowerCase() === refNameLower : m.targetReferenceName.toLowerCase() === refNameLower; }); if (!mapping) return null; return mapping; }; ContainerMapper.prototype.getMappedEntity = function (mapping, type) { if (!mapping) return null; //fetch the container from the file system based on source or target GUID var guid = type === 'source' ? mapping.sourceGuid : mapping.targetGuid; var containerID = type === 'source' ? mapping.sourceContentViewID : mapping.targetContentViewID; var fileOps = new core_1.fileOperations(guid); var containerData = fileOps.readJsonFile("containers/".concat(containerID, ".json")); if (!containerData) return null; return containerData; }; ContainerMapper.prototype.getContainerByReferenceName = function (referenceName, type) { //try to get the mapping first.. var mapping = this.getContainerMappingByReferenceName(referenceName, type); if (mapping) { return this.getMappedEntity(mapping, type); } else { //if there's no mappping, we have to loop through ALL the containers to find it var guid = type === 'source' ? mapping.sourceGuid : mapping.targetGuid; var fileOps = new core_1.fileOperations(guid); var containerFiles = fileOps.listFilesInFolder("containers"); for (var _i = 0, containerFiles_1 = containerFiles; _i < containerFiles_1.length; _i++) { var file = containerFiles_1[_i]; try { var containerData = fileOps.readJsonFile("containers/".concat(file)); if (containerData && containerData.referenceName && containerData.referenceName.toLowerCase() === referenceName.toLowerCase()) { return containerData; } } catch (error) { // If there's an error reading the file, we just skip it } } } return null; }; ContainerMapper.prototype.addMapping = function (sourceContainer, targetContainer) { var mapping = this.getContainerMapping(targetContainer, 'target'); if (mapping) { this.updateMapping(sourceContainer, targetContainer); } else { var newMapping = { sourceGuid: this.sourceGuid, targetGuid: this.targetGuid, sourceContentViewID: sourceContainer.contentViewID, targetContentViewID: targetContainer.contentViewID, sourceLastModifiedDate: sourceContainer.lastModifiedDate, targetLastModifiedDate: targetContainer.lastModifiedDate, sourceReferenceName: sourceContainer.referenceName, targetReferenceName: targetContainer.referenceName }; this.mappings.push(newMapping); } this.saveMapping(); }; ContainerMapper.prototype.updateMapping = function (sourceContainer, targetContainer) { var mapping = this.getContainerMapping(targetContainer, 'target'); if (mapping) { mapping.sourceGuid = this.sourceGuid; mapping.targetGuid = this.targetGuid; mapping.sourceContentViewID = sourceContainer.contentViewID; mapping.targetContentViewID = targetContainer.contentViewID; mapping.sourceLastModifiedDate = sourceContainer.lastModifiedDate; mapping.targetLastModifiedDate = targetContainer.lastModifiedDate; mapping.sourceReferenceName = sourceContainer.referenceName; mapping.targetReferenceName = targetContainer.referenceName; this.saveMapping(); } }; ContainerMapper.prototype.loadMapping = function () { var mapping = this.fileOps.getMappingFile(this.directory, this.sourceGuid, this.targetGuid); return mapping; }; ContainerMapper.prototype.saveMapping = function () { this.fileOps.saveMappingFile(this.mappings, this.directory, this.sourceGuid, this.targetGuid); }; ContainerMapper.prototype.hasSourceChanged = function (sourceContainer) { if (!sourceContainer) return false; var mapping = this.getContainerMapping(sourceContainer, 'source'); if (!mapping) return false; //the date format is: 07/23/2025 08:22PM (MM/DD/YYYY hh:mma) so we need to convert it to a Date object // Note: This assumes the date is in the format MM/DD/YYYY hh:mma // If the date format is different, you may need to adjust the parsing logic accordingly var sourceDate = (0, date_fns_1.parse)(sourceContainer.lastModifiedDate, "MM/dd/yyyy hh:mma", new Date()); var mappedDate = (0, date_fns_1.parse)(mapping.sourceLastModifiedDate, "MM/dd/yyyy hh:mma", new Date()); return sourceDate > mappedDate; }; ContainerMapper.prototype.hasTargetChanged = function (targetContainer) { if (!targetContainer) return false; var mapping = this.getContainerMapping(targetContainer, 'target'); if (!mapping) return false; var targetDate = (0, date_fns_1.parse)(targetContainer.lastModifiedDate, "MM/dd/yyyy hh:mma", new Date()); var mappedDate = (0, date_fns_1.parse)(mapping.targetLastModifiedDate, "MM/dd/yyyy hh:mma", new Date()); return targetDate > mappedDate; }; return ContainerMapper; }()); exports.ContainerMapper = ContainerMapper; //# sourceMappingURL=container-mapper.js.map