Skip to content

Commit

Permalink
fix content of slingxml serialization for sling-initial-content (#31)
Browse files Browse the repository at this point in the history
Merged.
  • Loading branch information
mrozati authored Jun 25, 2020
1 parent d2469e2 commit 53bf2d2
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Changelog

## 1.7.3

### Fixed

- Filter out duplicated entry files #30
- Fix Sling-Initial-Content XML content (serialization format: slingxml)

## 1.7.2

Expand Down
78 changes: 70 additions & 8 deletions lib/clientlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,12 @@ function writeClientLibJson(item, options) {
}

/**
* Write a configuration XML file for a clientlib
* Write a configuration FileVault XML file for a clientlib
* 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, serializationFormat) {
function writeClientLibXml(item, options) {
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"';
Expand Down Expand Up @@ -228,7 +227,7 @@ function writeClientLibXml(item, options, serializationFormat) {
});

content += "/>\n";
var contentXml = getXmlOutputFile(item, serializationFormat);
var contentXml = getXmlOutputFile(item, SERIALIZATION_FORMAT_XML);

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

Expand All @@ -237,6 +236,67 @@ function writeClientLibXml(item, options, serializationFormat) {
}
}

/**
* Write a configuration Sling-Initial-Content XML file for a clientlib
* with the given properties in `item`
* @param {ClientLibItem} item - clientlib configuration properties
* @param {Object} options - further options
*/
function writeClientLibSlingXml(item, options) {
var content = '<?xml version="1.0" encoding="UTF-8"?>' +
'\n<node>' +
'\n <name>' + item.name + '</name>' +
'\n <primaryNodeType>cq:ClientLibraryFolder</primaryNodeType>' +
'\n <property>' +
'\n <name>categories</name>' +
'\n <values>';
if (item.hasOwnProperty('categories')) {
item.categories.forEach(category => {
content += '\n <value>' + category + '</value>';
});
} else {
content += '\n <value>' + item.name + '</value>';
}
content +=
'\n </values>' +
'\n <type>String</type>' +
'\n </property>';

clientLibDirectoryFields.forEach(function (fieldKey) {
if (item.hasOwnProperty(fieldKey)) {
content += '\n <property>\n <name>' + fieldKey + '</name>';
if (typeof item[fieldKey] === 'boolean') {
// Boolean value
content +=
'\n <value>' + item[fieldKey] + '</value>' +
'\n <type>Boolean</type>';
} else if (Array.isArray(item[fieldKey])) {
// Array of strings
content += '\n <values>';
item[fieldKey].forEach(value => {
content += '\n <value>' + value + '</value>';
});
content += '\n </values>\n <type>String</type>';
} else if (typeof item[fieldKey] === 'string') {
// String value
content +=
'\n <value>' + item[fieldKey] + '</value>' +
'\n <type>String</type>';
}
content += '\n </property>';
}
});
content += '\n</node>\n'

var contentXml = getXmlOutputFile(item, SERIALIZATION_FORMAT_SLING_XML);

options.verbose && console.log("write clientlib Sling-Initial-Content XML 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
Expand Down Expand Up @@ -422,17 +482,19 @@ function processItem(item, options, processDone) {
// create clientlib directory
fse.mkdirsSync(clientLibPath);

var serializationFormat = (item.serializationFormat === SERIALIZATION_FORMAT_XML) ? SERIALIZATION_FORMAT_XML
: (item.serializationFormat === SERIALIZATION_FORMAT_SLING_XML) ? SERIALIZATION_FORMAT_SLING_XML
: SERIALIZATION_FORMAT_JSON;
var serializationFormat = item.serializationFormat || 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 if (serializationFormat === SERIALIZATION_FORMAT_SLING_XML) {
// write Sling-Initial-Content configuration
writeClientLibSlingXml(item, options);
} else {
writeClientLibXml(item, options, serializationFormat);
// write FileVault XML configuration
writeClientLibXml(item, options);
}

var assetList = normalizeAssets(clientLibPath, item.assets);
Expand Down
5 changes: 4 additions & 1 deletion test/clientlib.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,17 @@ module.exports = {
name: "test.base.apps.serializationFormatSlingXML",
serializationFormat: "slingxml",
allowProxy: true,
longCacheKey: "${project.version}-${buildNumber}",
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",
dependencies: [
"test.base.apps.mainapp"
],
assets: {
js: {
base: "js", // by default the `base` is the asset key property
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
<?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"/>
<node>
<name>test.base.apps.serializationFormatSlingXML</name>
<primaryNodeType>cq:ClientLibraryFolder</primaryNodeType>
<property>
<name>categories</name>
<values>
<value>test.base.apps.six</value>
<value>test.categorie.in.config</value>
</values>
<type>String</type>
</property>
<property>
<name>embed</name>
<values>
<value>test.base.apps.thirdapp</value>
</values>
<type>String</type>
</property>
<property>
<name>dependencies</name>
<values>
<value>test.base.apps.mainapp</value>
</values>
<type>String</type>
</property>
<property>
<name>allowProxy</name>
<value>true</value>
<type>Boolean</type>
</property>
<property>
<name>longCacheKey</name>
<value>${project.version}-${buildNumber}</value>
<type>String</type>
</property>
</node>

0 comments on commit 53bf2d2

Please sign in to comment.