Skip to content

Commit a35ccda

Browse files
committed
Fix parser as npm module. Add config tests
1 parent d9cd8a2 commit a35ccda

8 files changed

+97
-58
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ xlsx to JSON parser. Also support i18next format. Relies on node-xlsx.
1414
### Yep, it can append new files to exist now! *Cheers* 😎
1515

1616
### What's new
17+
* v.0.3.0 - Add config tests. Fix parser. Now it's work as NPM module correctly
1718
* v.0.2.5 - Codystyle fixes
1819
* v.0.2.4 - Remove async write file & some refactoring
1920
* v.0.2.3 - Add author's contacts

__tests__/test.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
2-
{
3-
"en": {
4-
"translations": {
5-
"three": "three",
6-
"two": "two"
7-
}
8-
}
9-
}
1+
{
2+
"en": {
3+
"translations": {
4+
"three": "three",
5+
"two": "two"
6+
}
7+
}
8+
}

__tests__/tests.js

+37-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
const getValuesFromArray = require('../utils/getValuesFromArray');
22
const getValuesFromFile = require('../utils/getValuesFromFile');
33
const recursiveSearchValues = require('../utils/recursiveSearchValues');
4-
const path = require('path');
4+
const getConfig = require('../utils/getConfig');
5+
const path = process.cwd() + '/';
6+
57
const mockArray = [
68
['one', 'two'],
79
['two', 4],
810
['three', true]
911
];
1012

11-
const file = path.resolve(__dirname, 'test.json');
13+
const file = path + '__tests__/test.json';
1214
const mockObject = {
1315
"one":
1416
{
@@ -30,26 +32,50 @@ const emptyObj = {};
3032

3133
describe('getValuesFromArray', () => {
3234
it('getValuesFromArray converting array to Object', () => {
33-
expect(getValuesFromArray(mockArray, 2)).toEqual(mockObject)
35+
expect(getValuesFromArray(mockArray, 2)).toEqual(mockObject);
3436
});
3537

3638
it('getValuesFromFile get values from json and return Object', () => {
37-
expect(getValuesFromFile(file)).toEqual(mockSecondObj)
39+
expect(getValuesFromFile(file)).toEqual(mockSecondObj);
40+
});
41+
42+
it('getValuesFromFile without correct file return false', () => {
43+
expect(getValuesFromFile('/fake/' + file)).toEqual(false);
3844
});
3945

4046
it('getValuesFromFile return error when file not found', () => {
41-
expect(getValuesFromFile('test.json')).toEqual(false)
47+
expect(getValuesFromFile('test.json')).toEqual(false);
4248
});
4349

4450
it('recursiveSearchValues return Array or Object with only first deepest childs', () => {
45-
expect(recursiveSearchValues(mockObject)).toEqual(mockSecondObj)
51+
expect(recursiveSearchValues(mockObject)).toEqual(mockSecondObj);
4652
});
4753

4854
it('recursiveSearchValues return deepest children value' , () => {
49-
expect(recursiveSearchValues(mockSecondObj)).toEqual("three")
50-
})
55+
expect(recursiveSearchValues(mockSecondObj)).toEqual("three");
56+
});
57+
58+
it('recursiveSearchValues with empty object return undefined' , () => {
59+
expect(recursiveSearchValues(emptyObj)).toEqual(undefined);
60+
});
61+
62+
it('is getConfig still object', () => {
63+
expect(typeof getConfig === 'object').toEqual(true);
64+
});
5165

52-
it('recursiveSearchValues return empty obj' , () => {
53-
expect(recursiveSearchValues(emptyObj)).toEqual(undefined)
54-
})
66+
it('getConfig contains json dir', () => {
67+
expect(getConfig.jsonDir).toEqual(path + 'json/');
68+
});
69+
70+
it('getConfig contains xlsx dir', () => {
71+
expect(getConfig.xlsxDir).toEqual(path + 'xlsx/');
72+
});
73+
74+
it('getConfig contains withFilenames argument', () => {
75+
expect(getConfig.withFilenames).toEqual(false);
76+
});
77+
78+
it('getConfig contains listNumber argument', () => {
79+
expect(getConfig.listNumber).toEqual(1);
80+
});
5581
});

index.js

+11-27
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,45 @@
11
const xlsx = require('node-xlsx');
22
const fs = require('fs');
3-
const path = require('path');
43
const getValuesFromFile = require('./utils/getValuesFromFile');
54
const getValuesFromArray = require('./utils/getValuesFromArray');
6-
const config = fs.existsSync(path.resolve(__dirname, './../../xlsx-json-parser.config.js')) ? require('../../xlsx-json-parser.config.js') : require('./xlsx-json-parser.config.js');
7-
let listNumber = config.listNumber;
8-
let withFilenames = config.withFilenames;
9-
let xlsxDir = config.xlsxDir;
10-
let jsonDir = config.jsonDir;
11-
12-
/**
13-
* Get arguments from command line
14-
* >node index listNumber xlsxDir jsonDir --wfn
15-
*/
16-
process.argv.forEach(function (val) {
17-
listNumber = val.includes('--ln=') ? parseInt(val.split('=')[1]) : config.listNumber;
18-
withFilenames = val.includes('--wfn') ? val : config.withFilenames;
19-
xlsxDir = val.includes('--xlsx=') ? val.split('=')[1] : config.xlsxDir;
20-
jsonDir = val.includes('--json=') ? val.split('=')[1] : config.jsonDir;
21-
});
5+
const config = require('./utils/getConfig');
226

237
/**
248
* Close app when folder with xlsx not found
259
*/
26-
if (!fs.existsSync(xlsxDir)) {
27-
console.log(`Folder ${xlsxDir} with xlsx files doesn't exist. Checking your config file or directory.`);
10+
if (!fs.existsSync(config.xlsxDir)) {
11+
console.log(`Folder ${config.xlsxDir} with Excel files doesn't exist. Checking your config file or directory.`);
2812
process.exit();
2913
}
3014

3115
/**
3216
* Creating json directory if doesn't exist
3317
*/
3418
try {
35-
fs.mkdirSync(jsonDir);
19+
fs.mkdirSync(config.jsonDir);
3620
} catch(err) {
37-
console.log(`Directory ${jsonDir} already exist. And it's okay.`);
21+
// No need to do something
3822
}
3923

40-
fs.readdir(xlsxDir, (err, files) => {
24+
fs.readdir(config.xlsxDir, (err, files) => {
4125
files.forEach(file => {
4226
if (file.toString().includes('.xls')) {
4327
const filename = file.replace(/((.xlsx)|(.xls))/g, '');
44-
const excelArray = xlsx.parse(`./${xlsxDir}/${file}`);
28+
const excelArray = xlsx.parse(`${config.xlsxDir + file}`);
4529

4630
const list = (listNumber) => {
4731
return typeof excelArray[listNumber - 1] !== 'undefined' ? excelArray[listNumber - 1] : console.log('List empty');
4832
};
4933

50-
const currentList = list(listNumber);
34+
const currentList = list(config.listNumber);
5135

5236
if (typeof currentList !== 'undefined' && currentList.data.length > 0) {
5337
const langs = currentList.data[0];
5438
const langsCount = langs.length;
5539
const tt = getValuesFromArray(currentList.data, langsCount);
5640

5741
for (let i = 0; i < langs.length; i++) {
58-
const file = `${jsonDir}/${langs[i]}${withFilenames ? `_${filename}` : ''}.json`;
42+
const file = `${config.jsonDir + langs[i]}${config.withFilenames ? `_${filename}` : ''}.json`;
5943
let valuesArr = tt[`${langs[i]}`];
6044
let valuesToWrite, status;
6145

@@ -66,13 +50,13 @@ fs.readdir(xlsxDir, (err, files) => {
6650
try {
6751
fs.writeFileSync(file, valuesToWrite);
6852

69-
console.log(`${langs[i]}${withFilenames ? `_${filename}` : ''}.json was ${status} in ${jsonDir}`);
53+
console.log(`${langs[i]}${config.withFilenames ? `_${filename}` : ''}.json was ${status} in ${config.jsonDir}`);
7054
} catch(err) {
7155
return console.log('Error: ' + err);
7256
}
7357
}
7458
} else {
75-
return console.log('File ' + file + ' has no list ' + listNumber + '.\nTry another file or list.');
59+
return console.log('File ' + file + ' has no list ' + config.listNumber + '.\nTry another file or list.');
7660
}
7761
}
7862
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xlsx-json-parser",
3-
"version": "0.2.5",
3+
"version": "0.3.0",
44
"description": "Parse xls/xlsx to json. i18next format",
55
"main": "index.js",
66
"author": "Roman iRodger Dvoryanov <[email protected]> (http://irodger.ru)",

utils/getConfig.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
let configFile;
2+
const selfConfig = require('../xlsx-json-parser.config.js');
3+
const path = process.cwd() + '/';
4+
5+
try {
6+
configFile = require(path + 'xlsx-json-parser.config.js');
7+
} catch(err) {
8+
configFile = require('../xlsx-json-parser.config.js');
9+
}
10+
11+
const config = {
12+
listNumber: configFile.listNumber || selfConfig.listNumber,
13+
withFilenames: configFile.withFilenames || selfConfig.withFilenames,
14+
xlsxDir: path + (configFile.xlsxDir || selfConfig.xlsxDir) + '/',
15+
jsonDir: path + (configFile.jsonDir || selfConfig.jsonDir) + '/',
16+
fileTemplate(lang, template) {
17+
return configFile.fileTemplate ? configFile.fileTemplate(lang, template) : selfConfig.fileTemplate(lang, template)
18+
}
19+
};
20+
21+
/**
22+
* Get arguments from command line
23+
* >node index listNumber xlsxDir jsonDir --wfn
24+
*/
25+
process.argv.forEach(function (val) {
26+
config.listNumber = val.includes('--ln=') ? parseInt(val.split('=')[1]) : config.listNumber;
27+
config.withFilenames = val.includes('--wfn') ? val : config.withFilenames;
28+
config.xlsxDir = val.includes('--xlsx=') ? val.split('=')[1] : config.xlsxDir;
29+
config.jsonDir = val.includes('--json=') ? val.split('=')[1] : config.jsonDir;
30+
});
31+
32+
module.exports = config;

utils/getValuesFromFile.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const fs = require('fs');
2-
const path = require('path');
32
const recursiveSearchValues = require('./recursiveSearchValues');
43

54
/**
65
* Get values from file
76
* @param file
87
*/
98
function getValuesFromFile(file) {
10-
file = path.resolve(__dirname + '/..', file);
119
let contents;
1210

1311
try {

xlsx-json-parser.config.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
const config = {
2-
xlsxDir: './xlsx/',
3-
jsonDir: './json/',
2+
xlsxDir: 'xlsx',
3+
jsonDir: 'json',
44
listNumber: 1,
55
withFilenames: false,
66
fileTemplate(lang, array) {
7-
return `
8-
{
9-
"${lang}": {
10-
"translations": ${JSON.stringify(array)}
11-
}
12-
}`;
7+
return `{
8+
"${lang}": {
9+
"translations": ${JSON.stringify(array)}
10+
}
11+
}`;
1312
},
1413
};
1514

0 commit comments

Comments
 (0)