@viewdo/dxp-story-cli
Version:
DXP Story Management CLI
66 lines • 2.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataTemplateMutator = void 0;
const path_1 = __importDefault(require("path"));
const FileService_1 = require("./FileService");
const JSONFormatService_1 = require("./JSONFormatService");
const typedi_1 = require("typedi");
class DataTemplateMutator {
constructor(directory, name, template_directory = 'unpacked') {
this.directory = directory;
this.name = name;
this.template_directory = template_directory;
this.relative_path = `./${template_directory}/${name}`;
this.file_service = typedi_1.Container.get(FileService_1.FileService);
this.format_service = typedi_1.Container.get(JSONFormatService_1.JSONFormatService);
}
// NOTE: This function MUTATES the passed in object
stripHtml(node, path_key = `${this.relative_path}`) {
Object.keys(node).forEach((key) => {
let htmlPropTester = /(^.*html)$/gim;
if (typeof key == 'string' && htmlPropTester.test(key)) {
let htmlPath = `${path_key}.html`;
this.file_service.write(path_1.default.join(this.directory, htmlPath), node[key]);
node[key] = htmlPath;
}
else if (typeof node[key] === 'object') {
let new_key = `${path_key}/${node[key].id || key}`;
this.stripHtml(node[key], new_key);
}
});
}
// NOTE: This function MUTATES the passed in object
restoreHtml(node) {
if (node)
Object.keys(node).forEach((key) => {
let htmlPropTester = /(^.*html)$/gi;
let pathValueTester = /^(.*\.(html|liquid))$/gi;
if (typeof key == 'string' && htmlPropTester.test(key)) {
let value = node[key];
if (value && pathValueTester.test(value)) {
let html = this.file_service.read(path_1.default.join(this.directory, value));
node[key] = html || '';
}
}
if (typeof node[key] === 'object') {
this.restoreHtml(node[key]);
}
});
}
fixXAPITokens(node) {
Object.keys(node).forEach((key) => {
let xapiTokenTester = /.template\/([\w0-9])\..*$/gim;
if (key == 'url' && xapiTokenTester.test(node[key])) {
node[key] = node[key].replace(xapiTokenTester, '[story.html-templates.$1.url]');
}
else if (typeof node[key] === 'object') {
this.fixXAPITokens(node[key]);
}
});
}
}
exports.DataTemplateMutator = DataTemplateMutator;
//# sourceMappingURL=DataTemplateMutator.js.map