-
Notifications
You must be signed in to change notification settings - Fork 0
/
toJSON.js
133 lines (117 loc) · 3.09 KB
/
toJSON.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* toJSON - Converts xls to json file
*
* Headers order expected (names doesn't matter):
* [ 'Etiqueta', 'Traducción Español', 'Traducción Inglés' ]
*
*
* TODO:
* - validations
* - use name page instead of index
* - config headers format
* - config languaje index
*/
const fs = require('fs')
const xlsx = require('node-xlsx')
const ARGV = require('minimist')(process.argv.slice(2))
if (Object.keys(ARGV).length === 1) exit(`Generates json object from xlsx.
Usage:
toJSON --lang esp||eng --input <file> [ --output <filename> --page <number> ]
--lang (esp or eng):
Language to generate json (should change in the future).
--input (filename path)
Local file path to xlsx file.
--page (number)
Page index inside the xlsx file.
--output (name)
Filename to save content to, default tho stdout.
------- NOTE -------
Use xlsx headers:
Etiqueta - Traducción Español - Traducción Inglés
`)
if (!ARGV.lang) exit('Include --lang parameter with \'esp\' or \'eng\' option.')
if (!ARGV.input) exit('Include --input parameter with path to local xlsx file.')
const PAGE = (typeof ARGV.page !== 'undefined') ? ARGV.page : null
const FILE_INPUT = ARGV.input
const LANG = ARGV.lang
const OUTPUT_NAME = ARGV.output || null
// -- RUN {{{
const parsedXlsx = xlsx.parse(FILE_INPUT)
let pages
if (PAGE === null) {
const headers = parsedXlsx.map(p => p.data[0])
testEqualHeaders(headers)
pages = concatPages(parsedXlsx)
} else {
pages = concatPages([ parsedXlsx[PAGE] ])
}
const json = JSON.stringify(
langObject(pages, LANG),
null,
2
)
// output - file or stdout
if (OUTPUT_NAME !== null) {
const FILE = OUTPUT_NAME + '.json'
const wstream = fs.createWriteStream(FILE)
.on('finish', () => console.log('File created: %s', FILE))
wstream.write(json)
wstream.end()
} else {
console.log(json)
}
// }}}
// --- FNs {{{
/*
* Generate object from array
* - must follow current header order
*/
function langObject (pages, lang) {
return pages.reduce((obj, row) => {
// !! - Consider header order
const [ label = null, esp, eng ] = row
if (label !== null) {
switch (lang) {
case 'esp':
obj[label] = esp
break
case 'eng':
obj[label] = eng
break
}
}
return obj
}, {})
}
/**
* Check if headers from all pages are equal
* - compares to page 0
*
* test break > headers[2] = [ 'Traducción Español','Etiqueta', 'Traducción Inglés' ]
*/
function testEqualHeaders (headers) {
const equalHeaders = headers.every(header =>
header.every((item, i) =>
item === headers[0][i]
)
)
if (!equalHeaders) exit('Pages headers are not equal. Use --page param or set same headers on all the pages.')
}
/**
* Concat all pages rows with no headers
* - pages[].data = all the rows (headers as first element)
*/
function concatPages (pages, lang) {
return pages.reduce((all, page) => {
const [ headers, ...rows ] = page.data
return all.concat(rows)
}, [])
}
/**
* Exit process with message, helper
*/
function exit (message, cb) {
console.log('\n' + message + '\n')
process.exit()
}
// }}}