-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleconverter.js
47 lines (38 loc) · 1.32 KB
/
sampleconverter.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
// Script to import sample demandware xml file
// Command to execute : node sampleconverter.js <input xml file path> <output json file path>
var fs = require('fs'),
xml2jsonConverter = require('./lib/xml2jsonconverter.js'),
path = require('path');
var xmlFileDir = '';
var xmlFileName = '';
var jsonFileName = '';
var jsonFilePath = '';
if (process && process.argv && process.argv.length > 2) {
var inputFilePath = path.parse(process.argv[2]);
xmlFileDir = inputFilePath.dir;
xmlFileName = path.join(xmlFileDir, inputFilePath.base);
if (process.argv.length > 3) {
jsonFilePath = process.argv[3];
} else {
jsonFileName = inputFilePath.name + ".json";
jsonFilePath = path.join(xmlFileDir, jsonFileName);
}
} else {
console.log('XML File parameter is missing');
process.exit(0);
}
var accountsObj = {};
accountsObj.accounts = [];
var converter = new xml2jsonConverter(xmlFileName, ["address", "custom-attribute"])
converter.convert(function(account) {
// Custom Logic Placeholder
account.data.customField = "customValue";
accountsObj.accounts.push(account);
}, function() {
fs.writeFile(jsonFilePath, JSON.stringify(accountsObj), function(err) {
if (err) {
console.log(err);
}
console.log("account data saved");
});
});