@ez-aem/policies
Version:
Easily Manage AEM Policies and Style System with EZ-AEM Policies.
85 lines (84 loc) • 3.29 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePolicies = void 0;
var lodash_1 = require("lodash");
var fs_1 = require("fs");
var path_1 = require("path");
var fast_xml_parser_1 = require("fast-xml-parser");
var child_process_1 = require("child_process");
var Builder = function (tabWidth) {
if (tabWidth === void 0) { tabWidth = 2; }
return new fast_xml_parser_1.XMLBuilder({
format: true,
ignoreAttributes: false,
indentBy: " ".repeat(tabWidth),
attributeNamePrefix: "@",
attributesGroupName: "attributes",
suppressBooleanAttributes: false,
});
};
var generate = function (policies) {
var componentPolicies = {};
var jcrPrimary = { "jcr:primaryType": "nt:unstructured" };
policies.forEach(function (policy) {
var policyNodes = {};
var resourceType = policy.component;
resourceType.split("/").reverse().forEach(function (node, index) {
var _a;
if (index === 0)
policyNodes[node] = __assign({ attributes: jcrPrimary }, policy.format(false));
else
policyNodes = (_a = {}, _a[node] = __assign({ attributes: jcrPrimary }, policyNodes), _a);
});
componentPolicies = (0, lodash_1.merge)({}, componentPolicies, policyNodes);
});
return {
"?xml": {
attributes: {
version: "1.0",
encoding: "UTF-8"
}
},
"jcr:root": __assign({ attributes: {
"xmlns:sling": "http://sling.apache.org/jcr/sling/1.0",
"xmlns:cq": "http://www.day.com/jcr/cq/1.0",
"xmlns:jcr": "http://www.jcp.org/jcr/1.0",
"xmlns:nt": "http://www.jcp.org/jcr/nt/1.0",
"xmlns:rep": "internal",
"jcr:mixinTypes": "[rep:AccessControllable]",
"jcr:primaryType": "cq:Page",
}, "rep:policy": "" }, componentPolicies)
};
};
var generatePolicies = function (policies, outputFile, tabWidth) {
if (tabWidth === void 0) { tabWidth = 2; }
if (!policies.length || outputFile === "") {
throw new Error("No policies to generate, or no output file specified");
}
;
var xmlBuilder = Builder(tabWidth);
var outputFolder = (0, path_1.join)(process.cwd(), outputFile);
(0, fs_1.mkdirSync)(outputFolder, { recursive: true });
(0, fs_1.writeFileSync)((0, path_1.join)(outputFolder, ".content.xml"), xmlBuilder.build(generate(policies)));
(0, child_process_1.exec)([
"./node_modules/.bin/prettier",
"--tab-width ".concat(tabWidth),
"--xmlWhitespaceSensitivity ignore",
"--write",
outputFolder + "/.content.xml",
].join(" "), function (err, stdout, stderr) {
console.log(stdout);
});
};
exports.generatePolicies = generatePolicies;