forked from Dancephy/Mixxx-Pioneer-DDJ-SB3
-
Notifications
You must be signed in to change notification settings - Fork 4
/
compile-xml.js
75 lines (64 loc) · 2.33 KB
/
compile-xml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const xml2js = require('xml2js');
const fs = require('fs');
const path = require('path');
const readFilePromise = file => new Promise((res, rej) => {
fs.readFile(file, 'utf8', (err, content) => {
if (err) {
console.error(err);
rej(err);
return;
}
res(content);
});
});
// List all files in a directory in Node.js recursively in a synchronous fashion
var walkSync = function(dir, filelist) {
var path = path || require('path');
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), filelist);
}
else {
filelist.push(path.join(dir, file));
}
});
return filelist;
};
const mappingFiles = walkSync('xmls/mappings', []);
readFilePromise('xmls/root.xml').then(rootContent => {
return xml2js.parseStringPromise(rootContent).then(rootXml => {
rootXml.MixxxControllerPreset.controller = {
scriptfiles: rootXml.MixxxControllerPreset.controller[0].scriptfiles,
controls: [{ control: [] }],
outputs: [{ output: [] }],
};
const maps = mappingFiles.map(mappingFile => {
return readFilePromise(mappingFile).then(mappingData => {
return xml2js.parseStringPromise(mappingData);
}).catch(err => {
console.log("File failed: ", mappingFile, err);
throw err;
});
});
Promise.all(maps).then(mappingXmls => {
mappingXmls.forEach(mappingXml => {
rootXml.MixxxControllerPreset.controller.controls[0].control = [
...rootXml.MixxxControllerPreset.controller.controls[0].control,
...((mappingXml.controller.controls || [])[0] || {}).control,
];
rootXml.MixxxControllerPreset.controller.outputs[0].output = [
...rootXml.MixxxControllerPreset.controller.outputs[0].output,
...(((mappingXml.controller.outputs || [])[0] || {}).output || []),
];
});
var builder = new xml2js.Builder();
var xml = builder.buildObject(rootXml);
fs.writeFile('Pioneer-DDJ-SB3.midi.xml', xml, 'utf8', () => {
console.log("XML Compile Success");
});
});
});
}).catch(console.log);