-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv-renderer.js
executable file
·161 lines (125 loc) · 3.74 KB
/
csv-renderer.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const fs = require("fs");
const jsonfile = require('jsonfile');
const jsonld = require('jsonld');
const camelCase = require('camelcase');
const papaparse = require('papaparse');
var program = require('commander');
program
.version('0.8.0')
.usage('node csv-renderer.js renders the content of a CSV file into a jsonld template')
.option('-t, --template <template>', 'jsonld template to render')
.option('-h, --contextbase <hostname>', 'the public base url on which the context of the jsons are published.')
.option('-r, --documentpath <path>', 'the document path on which the jsons are is published')
.option('-x, --debug <path>', 'dump the intermediate json which will be used by the templaterenderer')
.option('-i, --input <path>', 'input file (a csv file)')
.option('-o, --output <path>', 'output file (a json file)')
program.on('--help', function(){
console.log('')
console.log('Examples:');
console.log(' $ csv-renderer --help');
console.log(' $ csv-renderer -i <input> -o <output>');
});
program.parse(process.argv);
var output = program.output || 'output.json';
var csvoptions = {
header: true,
skipEmptyLines: true,
complete: function(results) {
console.log("Finished:");
}
}
render_csv(program.template, program.input, output);
console.log('done');
function render_csv(templatefile, csvfilename, output) {
console.log('start reading');
var template = fs.readFileSync(templatefile, 'utf-8');
var csvf = fs.readFileSync(csvfilename, 'utf-8');
var csv = papaparse.parse(csvf, csvoptions);
var pt = parse_template(template);
// var ren = render_template(pt, {'ID':'een identifier', 'STRING' : 'een string waarde', 'BOOLEAN': 'true', 'VAL' : 'I do not know'});
var ren = render_template(pt, csv.data);
// console.log(ren);
/*
jsonfile.writeFile(output, ren, function (err) {
if (err) {
// Set the exit code if there's a problem so bash sees it
process.exitCode = 1;
console.error(err);
throw err;
}
});
*/
let writeStream = fs.createWriteStream(output);
write_data(writeStream, ren);
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
// close the stream
writeStream.end();
console.log('finished rendering to ' + output);
};
function parse_template(file) {
var parsed_template = {
pt_full: [],
pt_vars :[]
};
var file1 = file.split('{{');
var file2 = [];
for (i in file1) {
file2 = file2.concat(file1[i].split('}}'));
};
parsed_template.pt_full = file2;
return parsed_template
}
function render_template(parsed_template, data){
var renderedData = [];
for (i in data) {
renderedData[i] = render_template_single(parsed_template,data[i]);
}
return renderedData;
};
function render_template_single(parsed_template, data){
let render = '';
for (i in parsed_template.pt_full) {
let reminder = i % 2;
if (reminder == 0) {
render = render + parsed_template.pt_full[i];
} else {
render = render + data[parsed_template.pt_full[i]];
}
}
return render;
}
function write_data(stream, data){
stream.write("[");
for (i in data) {
stream.write(data[i]);
stream.write(",")
}
stream.write("]");
return true;
};
/*
const { Transform } = require('stream');
const transformRow2Json = new Transform({
transform(chunk, encoding, callback) {
process.stdout.write('.');
callback(null, chunk);
}
});
*/
function streamcsv(template, input, output) {
var out = Papa.parse(input, {
header: true,
skipEmptyLines: true,
step: function(row) {
console.log("Row:", row.data);
console.log(render_template_single(template, data));
console.log("--------");
},
complete: function() {
console.log("All done!");
}
});
console.log(out);
}