Skip to content

Commit

Permalink
unified merge functionality in a single function
Browse files Browse the repository at this point in the history
  • Loading branch information
manix committed Nov 10, 2017
1 parent c010e07 commit 5882da6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -41,6 +41,7 @@
"mergexml": "^1.1.2",
"moment": "^2.18.1",
"multimatch": "^2.1.0",
"pretty-data": "^0.40.0",
"progress": "^1.1.8",
"request": "^2.79.0",
"request-debug": "^0.2.0",
Expand Down
29 changes: 8 additions & 21 deletions src/davos.js
Expand Up @@ -2,7 +2,7 @@
'use strict';

// Constants
const SITES_META_FOLDER = '/sites';
let SITES_META_FOLDER = '/sites';
const META_FOLDER = "/meta";

// Imports
Expand All @@ -29,6 +29,9 @@
this.config = (Object.keys(this.ConfigManager.config).length === 0)
? this.ConfigManager.loadConfiguration().getActiveProfile(config)
: this.ConfigManager.mergeConfiguration(config);

SITES_META_FOLDER = "/" + (this.config.metaDir || "sites");

return this;
}

Expand Down Expand Up @@ -578,7 +581,7 @@
}

let clone = node.cloneNode();

Array.from(node.childNodes).forEach(child => {
let childClone;

Expand Down Expand Up @@ -624,15 +627,13 @@
});
}

mergeLibraries(pattern) {
const xdom = require("xmldom");
const x = require("xpath");
merge(pattern, out) {
const xmlm = require("xmlappend");

let currentRoot = (this.config.basePath || process.cwd()) + SITES_META_FOLDER;
let dir;

return globby(currentRoot + pattern + "/*.xml").then(files => {
return globby(currentRoot + pattern).then(files => {

return Promise.all(files.map(file => {
dir = path.dirname(file);
Expand All @@ -643,28 +644,14 @@
return e(err);
}

let document = new xdom.DOMParser().parseFromString(xml.toString().replace('xmlns="http://www.demandware.com/xml/impex/library/2006-10-31"', ''));
let nodes = x.select("//content", document);
let library = document.getElementsByTagName("library")[0];

if (!library) {
return e(new Error("Not a library"));
}

if (nodes.length > 1) {
Log.warn(chalk.yellow("Library " + file + " contains more than 1 content tag thus is considered a bundle and will be skipped"));

return r("");
}

r(xml.toString());
});
});
})).then(contents => {

if (dir) {
return new Promise((r, e) => {
fs.writeFile(dir + "/library.bundle.xml", xmlm(...contents.filter(c => !!c)), function (err) {
fs.writeFile(out || (dir + "/bundle.xml"), xmlm(...contents.filter(c => !!c)), function (err) {
err ? e(err) : r();
});
});
Expand Down

0 comments on commit 5882da6

Please sign in to comment.