-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·122 lines (110 loc) · 3.05 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
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
'use strict';
var co_fs = require('co-fs')
var fs = require('fs')
var xlsx = require('node-xlsx')
var co = require('co')
var csv = require('csv-parse/lib/sync')
var _ = require('co-lodash')
var tsv = require("node-tsv-json")
var parser = require('xml2json')
var jsonexport = require('jsonexport')
var exec = require('child_process').exec
var randomstring = require("randomstring")
var Excel = function(){
this.readDirectory = co.wrap(function*(directory){
var self = this
var sourcesData = []
var files = yield co_fs.readdir(directory)
yield* _.coEach(files,function*(file){
var full_path = directory+"/"+file
var sheet = yield self.readFile(full_path)
sourcesData.push(sheet)
})
return Promise.resolve(sourcesData)
})
this.readFile = co.wrap(function*(file){
var re = new RegExp(".*\\/(.*)\\.","i")
var fileName = (file.match(re)) ? file.match(re)[1] : file
if(this.isExcel(file)){
var sourceData = []
var sheets = xlsx.parse(yield co_fs.readFile(file))
sheets.some(function(sheet){ sourceData.push(sheet) })
return Promise.resolve(sourceData)
} else if(this.isCsv(file)){
var fileData = yield co_fs.readFile(file)
try{
sourceData = [{
'name':'Hoja1',
'fileName':fileName,
'filePath':file,
'data': yield csv(fileData,{relax_column_count:true})
}]
} catch(e){ console.log(e) }
return Promise.resolve(sourceData)
} else if(this.isTsv(file)){
var tsvData = yield this.parseTsv(file)
sourceData = [{
'name':'Hoja1',
'fileName':fileName,
'filePath':file,
'data': tsvData
}]
return Promise.resolve(sourceData)
} else if(this.isXml(file)){
var csvData = yield this.parseXml2Csv(file)
var jsonData = yield csv(csvData,{relax_column_count:true})
sourceData = [{
'name':'Hoja1',
'fileName':fileName,
'filePath':file,
'data': jsonData
}]
return Promise.resolve(sourceData)
}
return Promise.resolve(null)
})
this.parseXml2Csv = function(file){
return new Promise(function(resolve, reject){
var randomId = randomstring.generate(7)
var filePath = '/tmp/'+randomId+'.xml'
exec('xsltproc '+__dirname+'/xslt/removeSoap.xsl "'+file+'"', (error, stdout, stderr) => {
fs.writeFile(filePath, `${stdout}`, (err) => {
exec('xsltproc '+__dirname+'/xslt/CR1toCSV.xsl "'+filePath+'"', (error, csvData, stderr) => {
fs.unlink(filePath, (err) => {
if (err) throw err
resolve(csvData)
})
})
})
})
})
}
this.parseTsv = function(file){
return new Promise(function(resolve, reject){
tsv({
input: file,
output: "output.json"
,parseRows: true
}, function(err, result) {
if(err) {
console.error(err)
}else {
resolve(result)
}
})
})
}
this.isExcel = function(filePath){
return filePath.indexOf('.xls') != -1
}
this.isCsv = function(filePath){
return filePath.indexOf('.csv') != -1
}
this.isTsv = function(filePath){
return filePath.indexOf('.tsv') != -1
}
this.isXml = function(filePath){
return filePath.indexOf('.xml') != -1
}
}
module.exports = Excel;