@agility/cli
Version:
Agility CLI for working with your content. (Public Beta)
110 lines • 4.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PageMapper = void 0;
var core_1 = require("../../core");
var PageMapper = /** @class */ (function () {
function PageMapper(sourceGuid, targetGuid, locale) {
this.sourceGuid = sourceGuid;
this.targetGuid = targetGuid;
this.directory = 'page';
this.locale = locale;
// this will provide access to the /agility-files/{GUID}/{locale} folder
this.fileOps = new core_1.fileOperations(targetGuid, locale);
this.mappings = this.loadMapping();
}
PageMapper.prototype.getPageMapping = function (page, type) {
var mapping = this.mappings.find(function (m) {
return type === 'source' ? m.sourcePageID === page.pageID : m.targetPageID === page.pageID;
});
if (!mapping)
return null;
return mapping;
};
PageMapper.prototype.getPageMappingByPageID = function (pageID, type) {
var mapping = this.mappings.find(function (m) {
return type === 'source' ? m.sourcePageID === pageID : m.targetPageID === pageID;
});
if (!mapping)
return null;
return mapping;
};
PageMapper.prototype.getPageMappingByPageTemplateName = function (pageTemplateName, type) {
var mapping = this.mappings.find(function (m) {
return type === 'source' ? m.sourcePageTemplateName === pageTemplateName : m.targetPageTemplateName === pageTemplateName;
});
if (!mapping)
return null;
return mapping;
};
PageMapper.prototype.getMappedEntity = function (mapping, type) {
if (!mapping)
return null;
var guid = type === 'source' ? mapping.sourceGuid : mapping.targetGuid;
var pageID = type === 'source' ? mapping.sourcePageID : mapping.targetPageID;
var fileOps = new core_1.fileOperations(guid, this.locale);
var pageData = fileOps.readJsonFile("page/".concat(pageID, ".json"));
if (!pageData)
return null;
return pageData;
};
PageMapper.prototype.addMapping = function (sourcePage, targetPage) {
var mapping = this.getPageMapping(targetPage, 'target');
if (mapping) {
this.updateMapping(sourcePage, targetPage);
}
else {
var newMapping = {
sourceGuid: this.sourceGuid,
targetGuid: this.targetGuid,
sourcePageID: sourcePage.pageID,
targetPageID: targetPage.pageID,
sourceVersionID: sourcePage.properties.versionID,
targetVersionID: targetPage.properties.versionID,
sourcePageTemplateName: sourcePage.templateName,
targetPageTemplateName: targetPage.templateName,
};
this.mappings.push(newMapping);
}
this.saveMapping();
};
PageMapper.prototype.updateMapping = function (sourcePage, targetPage) {
var mapping = this.getPageMapping(targetPage, 'target');
if (mapping) {
mapping.sourceGuid = this.sourceGuid;
mapping.targetGuid = this.targetGuid;
mapping.sourcePageID = sourcePage.pageID;
mapping.targetPageID = targetPage.pageID;
mapping.sourceVersionID = sourcePage.properties.versionID;
mapping.targetVersionID = targetPage.properties.versionID;
mapping.sourcePageTemplateName = sourcePage.templateName;
mapping.targetPageTemplateName = targetPage.templateName;
}
this.saveMapping();
};
PageMapper.prototype.loadMapping = function () {
var mapping = this.fileOps.getMappingFile(this.directory, this.sourceGuid, this.targetGuid, this.locale);
return mapping;
};
PageMapper.prototype.saveMapping = function () {
this.fileOps.saveMappingFile(this.mappings, this.directory, this.sourceGuid, this.targetGuid, this.locale);
};
PageMapper.prototype.hasSourceChanged = function (sourcePage) {
if (!sourcePage)
return false;
var mapping = this.getPageMapping(sourcePage, 'source');
if (!mapping)
return true;
return sourcePage.properties.versionID > mapping.sourceVersionID;
};
PageMapper.prototype.hasTargetChanged = function (targetPage) {
if (!targetPage)
return false;
var mapping = this.getPageMapping(targetPage, 'target');
if (!mapping)
return false;
return targetPage.properties.versionID > mapping.targetVersionID;
};
return PageMapper;
}());
exports.PageMapper = PageMapper;
//# sourceMappingURL=page-mapper.js.map