Skip to content

Commit

Permalink
serializationFormat slingxml with xml output for Sling-Initial-Content (
Browse files Browse the repository at this point in the history
#26)

* introduce serializationFormat "slingxml" to be used with the sling initial content instead of FileVault. This only changes the output file name and path to <clientlib name>.xml instead of <clientlib name>/.content.xml

* update change log for serializationFileFormat slingxml

* fix expected xml outputs: now with line breaks

* fix return type
  • Loading branch information
mrozati authored Apr 3, 2020
1 parent a503066 commit c05e576
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.5.0

### Added

- add _serializationFormat_ 'slingxml' which can be used for XML outputs for Sling Initial Content

## 1.4.2

### Fixed
Expand Down
94 changes: 61 additions & 33 deletions lib/clientlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ var glob = require("glob");
var SERIALIZATION_FORMAT_JSON = "json";

/**
* XML serialization format
* XML serialization format for File Vault
*
* @type {string}
*/
var SERIALIZATION_FORMAT_XML = "xml";

/**
* XML serialization format for Sling Initial Content
*
* @type {string}
*/
var SERIALIZATION_FORMAT_SLING_XML = "slingxml";

/**
* List of fields to be evaluated for being added to the {@code cq:ClientLibraryFolder} file descriptor
* @type {String[]}
Expand All @@ -57,7 +64,7 @@ var ALLOWED_EXTENSIONS = {
* @typedef {Object} ClientLibItem
* @property {String} path - Clientlib root path (optional if `options.clientLibRoot` is set)
* @property {String} name - Clientlib name
* @property {String} [serializationFormat=json] - Type of the target archive for which the resources must be generated [json|xml] (optional, default=json)
* @property {String} [serializationFormat=json] - Type of the target archive for which the resources must be generated [json|xml|slingxml] (optional, default=json)
* @property {boolean} [allowProxy] - Is the Clientlib meant to be used as a proxy
* @property {Array<String>} [embed] - other Clientlib names that should be embedded
* @property {Array<String>} [dependencies] - other Clientlib names that should be included
Expand Down Expand Up @@ -190,42 +197,61 @@ function writeClientLibJson(item, options) {
* with the given properties in `item`
* @param {ClientLibItem} item - clientlib configuration properties
* @param {Object} options - further options
* @param {String} serializationFormat serialization format (xml|slingxml)
*/
function writeClientLibXml(item, options) {
var content = '<?xml version="1.0" encoding="UTF-8"?>' +
'<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"' +
' jcr:primaryType="cq:ClientLibraryFolder"';

if (item.hasOwnProperty('categories')) {
var fieldValue = item.categories.join(',');
content += ' categories="[' + fieldValue + ']"';
} else {
content += ' categories="[' + item.name + ']" ';
}
function writeClientLibXml(item, options, serializationFormat) {
var content = '<?xml version="1.0" encoding="UTF-8"?>' +
'\n<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"' +
'\n jcr:primaryType="cq:ClientLibraryFolder"';

clientLibDirectoryFields.forEach(function (fieldKey) {
if (item.hasOwnProperty(fieldKey)) {
if (typeof item[fieldKey] === 'boolean') {
// Boolean value
content += ' ' + fieldKey + '="{Boolean}' + item[fieldKey] + '"';
} else if (Array.isArray(item[fieldKey])) {
// Array of strings
var fieldValue = item[fieldKey].join(',');
content += ' ' + fieldKey + '="[' + fieldValue + ']"';
} else if (typeof item[fieldKey] === 'string') {
// String value
content += ' ' + fieldKey + '="' + item[fieldKey] + '"';
}
if (item.hasOwnProperty('categories')) {
var fieldValue = item.categories.join(',');
content += '\n categories="[' + fieldValue + ']"';
} else {
content += '\n categories="[' + item.name + ']" ';
}

clientLibDirectoryFields.forEach(function (fieldKey) {
if (item.hasOwnProperty(fieldKey)) {
if (typeof item[fieldKey] === 'boolean') {
// Boolean value
content += '\n ' + fieldKey + '="{Boolean}' + item[fieldKey] + '"';
} else if (Array.isArray(item[fieldKey])) {
// Array of strings
var fieldValue = item[fieldKey].join(',');
content += '\n ' + fieldKey + '="[' + fieldValue + ']"';
} else if (typeof item[fieldKey] === 'string') {
// String value
content += '\n ' + fieldKey + '="' + item[fieldKey] + '"';
}
});
}
});

content += "/>";
var outputPath = item.outputPath || path.join(item.path, item.name);
var contentXml = path.join(outputPath + "/.content.xml");
content += "/>\n";
var contentXml = getXmlOutputFile(item, serializationFormat);

options.verbose && console.log("write clientlib json file: " + contentXml);
options.verbose && console.log("write clientlib json file: " + contentXml);

if (!options.dry) {
fse.writeFileSync(contentXml, content);
}
}

/**
* Get the output file name and path for the specified XML serialization format
* @param {ClientLibItem} item - clientlib configuration properties
* @param {String} serializationFormat serialization format (xml|slingxml)
*/
function getXmlOutputFile(item, serializationFormat) {

// Sling initial content: <name>.xml
if (serializationFormat === SERIALIZATION_FORMAT_SLING_XML) {
return item.outputPath || path.join(item.path, item.name + ".xml")
}

// FileVault: a folder with a .content.xml file in it
var outputPath = item.outputPath || path.join(item.path, item.name);
return path.join(outputPath + "/.content.xml");
}

/**
Expand Down Expand Up @@ -383,15 +409,17 @@ function processItem(item, options, processDone) {
// create clientlib directory
fse.mkdirsSync(clientLibPath);

var serializationFormat = (item.serializationFormat === SERIALIZATION_FORMAT_XML) ? SERIALIZATION_FORMAT_XML : SERIALIZATION_FORMAT_JSON;
var serializationFormat = (item.serializationFormat === SERIALIZATION_FORMAT_XML) ? SERIALIZATION_FORMAT_XML
: (item.serializationFormat === SERIALIZATION_FORMAT_SLING_XML) ? SERIALIZATION_FORMAT_SLING_XML
: SERIALIZATION_FORMAT_JSON;

options.verbose && console.log("Write node configuration using serialization format: " + serializationFormat);

if (serializationFormat === SERIALIZATION_FORMAT_JSON) {
// write configuration JSON
writeClientLibJson(item, options);
} else {
writeClientLibXml(item, options);
writeClientLibXml(item, options, serializationFormat);
}

var assetList = normalizeAssets(clientLibPath, item.assets);
Expand Down
31 changes: 31 additions & 0 deletions test/clientlib.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,37 @@ module.exports = {
]
}
},
{
name: "test.base.apps.serializationFormatSlingXML",
serializationFormat: "slingxml",
allowProxy: true,
categories: [
"test.base.apps.six",
"test.categorie.in.config"
],
embed: [
"test.base.apps.thirdapp" // this clientlib will be auto embedded in AEM (kind of `merging`)
],
dependencies: "test.base.apps.mainapp",
assets: {
js: {
base: "js", // by default the `base` is the asset key property
files: [{
src: "src/frontend/secondapp/js/lib.js",
dest: "secondapp-lib.js"
}]
},
css: {
base: "style", // changes the `base` from `css` (default) to `style`
files: [
"src/frontend/secondapp/main.css"
]
},
resources: [
"src/frontend/resources/template.html"
]
}
},
{
name: "test.base.apps.ignoreOption",
assets: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?><jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primaryType="cq:ClientLibraryFolder" categories="[test.base.apps.six,test.categorie.in.config]" embed="[test.base.apps.thirdapp]" dependencies="test.base.apps.mainapp" allowProxy="{Boolean}true"/>
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[test.base.apps.six,test.categorie.in.config]"
embed="[test.base.apps.thirdapp]"
dependencies="test.base.apps.mainapp"
allowProxy="{Boolean}true"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[test.base.apps.six,test.categorie.in.config]"
embed="[test.base.apps.thirdapp]"
dependencies="test.base.apps.mainapp"
allowProxy="{Boolean}true"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#base=style

main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#base=js

secondapp-lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

"use strict";
var Test = function() {
this.test = "test";
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Test file</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: #000;
}
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?><jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primaryType="cq:ClientLibraryFolder" categories="[test.base.apps.six,test.categorie.in.config]" embed="[test.base.apps.thirdapp]" dependencies="test.base.apps.mainapp" allowProxy="{Boolean}true"/>
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:ClientLibraryFolder"
categories="[test.base.apps.six,test.categorie.in.config]"
embed="[test.base.apps.thirdapp]"
dependencies="test.base.apps.mainapp"
allowProxy="{Boolean}true"/>

0 comments on commit c05e576

Please sign in to comment.