-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebdav-calls.js
80 lines (63 loc) · 3.41 KB
/
webdav-calls.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
76
77
78
79
80
'use strict';
const { createClient } = require('webdav');
const fs = require('fs');
const path = require('path');
const getFormFilesIfPathExists = async (client, webdavPath, locales, foundFormXMlFileNames, localeFolderName) => {
let foundFiles = [];
for (let j = 0, jj = locales.length; j !== jj; j++) {
try {
let basePath = webdavPath + '/' + locales[j];
let contents = await client.getDirectoryContents(basePath);
for (let i = 0, ii = contents.length; i !== ii; i++) {
if (contents[i].filename.indexOf('xml') !== -1) {
let filePath = contents[i].filename;
let fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
if (!foundFormXMlFileNames.includes(fileName)) {
foundFiles.push(filePath);
foundFormXMlFileNames.push(fileName);
fs.appendFileSync(path.join(localeFolderName, 'fullpaths.txt'), fileName + ',' + basePath + '/' + fileName + '\r\n');
}
} else {
let val = await getFormFilesIfPathExists(client, contents[i].filename, foundFormXMlFileNames, localeFolderName);
foundFiles = foundFiles.concat(val);
}
}
} catch (e) {
if (!e.response)
{
console.log(e);
}
else if (e.response.status !== 404) {
console.log(webdavPath + '\r\n' + e + '\r\n');
}
}
}
return foundFiles;
}
const getFormXMLsOfSiteCartridges = async (baseURL, bmUser, bmPass, activeCodeVersion, cartridges, locales) => {
console.log('starting to get form xmls\r\n');
let client = createClient(baseURL + '/on/demandware.servlet/webdav/Sites/Cartridges/', { username: bmUser, password: bmPass });
let preparedLocales = locales.map(locale => [locale.language + '_' + locale.country, locale.language, 'default']);
let paths = cartridges.map(cartridge => '/' + activeCodeVersion + '/' + cartridge + '/cartridge/forms');
for (let j = 0, jj = preparedLocales.length; j !== jj; j++) {
let foundFromXMLFilePaths = [];
let foundFormXMlFileNames = [];
var localeName = preparedLocales[j][1];
var localeFolderName = path.join(__dirname, 'files', localeName);
if (!fs.existsSync(localeFolderName)) {
fs.mkdirSync(localeFolderName, { recursive: true }, (r) => { if (r != null) { console.log(r); } });
}
for (let i = 0, ii = paths.length; i !== ii; i++) {
foundFromXMLFilePaths = foundFromXMLFilePaths.concat(await getFormFilesIfPathExists(client, paths[i], preparedLocales[j], foundFormXMlFileNames, localeFolderName));
}
console.log('\r\n' + foundFromXMLFilePaths.length + ' form found for ' + localeName + ' locale\r\n');
for (let i = 0, ii = foundFromXMLFilePaths.length; i !== ii; i++) {
var formXMLContent = await client.getFileContents(foundFromXMLFilePaths[i], { format: 'text' });
let fileName = foundFromXMLFilePaths[i].substring(foundFromXMLFilePaths[i].lastIndexOf('/') + 1);
await fs.writeFile(path.join(localeFolderName, fileName), formXMLContent, (r) => { if (r != null) { console.log(r); } });
}
}
};
module.exports = {
getFormXMLsOfSiteCartridges: getFormXMLsOfSiteCartridges
};