-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·126 lines (101 loc) · 3.61 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
123
124
125
126
#!/usr/bin/env node
const args = require('args')
const COL_FORMAT = require('./col_format').COL_FORMAT
const FORMAT_FORMAT = require('./col_format').FORMAT_FORMAT
//const toJsonFromFile = require("positional-flat-file-to-json").toJsonFromFile
const fs = require('fs')
const readline=require('readline')
const xml2js = require('xml2js');
args
.options([
{
name:"input",
init: content => content,
},
{
name:'output',
init: content=>content
}
])
// .command('serve', 'Serve your static site', ['s'])
const flags = args.parse(process.argv)
const toJsonFromFile = function(sourceFileConfig, sourceFilepath) {
return new Promise(function (resolve) {
var convertedData = [];
var rd = readline.createInterface({ input: fs.createReadStream(sourceFilepath,{encoding:'latin1'}) });
rd.on('line', function (line) {
//console.log(line);
var convertedLine = convertLine(sourceFileConfig, line);
convertedData.push(convertedLine);
});
rd.on('close', function () {
resolve(convertedData);
});
});
};
const addDecimal =function(str,decimal){
const output = str.substring(0,str.length-decimal)+"."+str.substring(str.length-decimal);
return output
}
const parseFormats = function(str){
let format = 1;
let start = 0;
let arr = []
for(format, start;format<7;format++,start += 50){
if(str.substr(start+1,5) !=="00000"){
arr.push(str.substr(start,50).trim())
}
}
arr.forEach((val,ind,arr)=>{
const parsedLine = convertLine(FORMAT_FORMAT,val)
parsedLine.quantite_format = parseFloat(addDecimal(parsedLine.quantite_format,1))
parsedLine.prix_format = parseFloat(addDecimal(parsedLine.prix_format,2))
parsedLine.prix_unitaire = parseFloat(addDecimal(parsedLine.prix_unitaire,4))
parsedLine.ppb_unitaire = parseFloat(addDecimal(parsedLine.ppb_unitaire,4))
parsedLine.pmp_unitaire = parseFloat(addDecimal(parsedLine.pmp_unitaire,4))
arr[ind] = parsedLine
})
return arr
}
const convertLine = function(sourceFileConfig, line) {
var convertedLine = {};
for (var col in sourceFileConfig) {
if (Object.prototype.hasOwnProperty.call(sourceFileConfig, col)) {
convertedLine[col] = line.substr(sourceFileConfig[col].columnPosition - 1, sourceFileConfig[col].size).trim();
}
}
return convertedLine;
}
const writeToFile = function(jsObj,path){
jsString = JSON.stringify(jsObj)
fs.writeFile(path,jsString,'utf8',function(err){
if(err){
console.log("erreur a l;<ecriture du fichier");
return console.log(err);
}
console.log("reussi!");
})
}
// Read a flat file from the file system and return a JavaScript object
if(flags.i.indexOf("ESR2_FIC")!== -1){
toJsonFromFile(COL_FORMAT, flags.i).then(convertedJson => {
// console.log(convertedJson[10]);
const tempArray = convertedJson
tempArray.forEach((val)=>{
val.groupe_formats = parseFormats(val.groupe_formats)
val.pvg = parseFloat(addDecimal(val.pvg,2))
})
// tempObject.groupe_formats
// console.log(tempArray[2380]);
writeToFile(convertedJson,flags.o)
});
}
if(flags.i.indexOf("VAL_INDCN_THERA")!== -1 || flags.i.indexOf("ValIndcnThera") !== -1 ){
var parser = new xml2js.Parser();
fs.readFile(flags.i, function(err, data) {
parser.parseString(data, function (err, result) {
writeToFile(result.INDCN_THERA.OCC_INDCN_THERA,flags.o)
console.log('Done');
});
});
}