-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (53 loc) · 2.18 KB
/
index.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
const xlsx = require('node-xlsx');
const fs = require('fs');
const getValuesFromFile = require('./utils/getValuesFromFile');
const getValuesFromArray = require('./utils/getValuesFromArray');
const config = require('./utils/getConfig');
/**
* Close app when folder with xlsx not found
*/
if (!fs.existsSync(config.xlsxDir)) {
console.log(`Folder ${config.xlsxDir} with Excel files doesn't exist. Checking your config file or directory.`);
process.exit();
}
/**
* Creating json directory if doesn't exist
*/
try {
fs.mkdirSync(config.jsonDir);
} catch(err) {
// No need to do something
}
fs.readdir(config.xlsxDir, (err, files) => {
files.forEach(file => {
if (file.toString().includes('.xls')) {
const filename = file.replace(/((.xlsx)|(.xls))/g, '');
const excelArray = xlsx.parse(`${config.xlsxDir + file}`);
const list = (listNumber) => {
return typeof excelArray[listNumber - 1] !== 'undefined' ? excelArray[listNumber - 1] : console.log('List empty');
};
const currentList = list(config.listNumber);
if (typeof currentList !== 'undefined' && currentList.data.length > 0) {
const langs = currentList.data[0];
const langsCount = langs.length;
const tt = getValuesFromArray(currentList.data, langsCount);
for (let i = 0; i < langs.length; i++) {
const file = `${config.jsonDir + langs[i]}${config.withFilenames ? `_${filename}` : ''}.json`;
let valuesArr = tt[`${langs[i]}`];
let valuesToWrite, status;
valuesToWrite = !fs.existsSync(file) ? valuesArr : Object.assign({}, getValuesFromFile(file), valuesArr);
status = !fs.existsSync(file) ? 'created' : 'edited';
valuesToWrite = config.fileTemplate(langs[i], valuesToWrite);
try {
fs.writeFileSync(file, valuesToWrite);
console.log(`${langs[i]}${config.withFilenames ? `_${filename}` : ''}.json was ${status} in ${config.jsonDir}`);
} catch(err) {
return console.log('Error: ' + err);
}
}
} else {
return console.log('File ' + file + ' has no list ' + config.listNumber + '.\nTry another file or list.');
}
}
});
});