scripting engine

This commit is contained in:
Jan Prochazka
2020-06-11 10:09:04 +02:00
parent ca079d5dce
commit f68bdafd9f
11 changed files with 127 additions and 28 deletions

View File

@@ -1,13 +1,36 @@
const csv = require('csv');
const fs = require('fs');
const stream = require('stream');
async function csvWriter({ fileName, encoding = 'utf-8', ...options }) {
class CsvPrepareStream extends stream.Transform {
constructor({ header }) {
super({ objectMode: true });
this.structure = null;
this.header = header;
}
_transform(chunk, encoding, done) {
if (this.structure) {
this.push(this.structure.columns.map((col) => chunk[col.columnName]));
done();
} else {
this.structure = chunk;
if (this.header) {
this.push(chunk.columns.map((x) => x.columnName));
}
done();
}
}
}
async function csvWriter({ fileName, encoding = 'utf-8', header = true, delimiter, quoted }) {
console.log(`Writing file ${fileName}`);
const csvStream = csv.stringify(options);
const csvPrepare = new CsvPrepareStream({ header });
const csvStream = csv.stringify({ delimiter, quoted });
const fileStream = fs.createWriteStream(fileName, encoding);
csvPrepare.pipe(csvStream);
csvStream.pipe(fileStream);
csvStream['finisher'] = fileStream;
return csvStream;
csvPrepare['finisher'] = fileStream;
return csvPrepare;
}
module.exports = csvWriter;

View File

@@ -5,6 +5,7 @@ async function fakeObjectReader({ delay = 0 } = {}) {
objectMode: true,
});
function doWrite() {
pass.write({ columns: [{ columnName: 'id' }, { columnName: 'country' }] });
pass.write({ id: 1, country: 'Czechia' });
pass.write({ id: 2, country: 'Austria' });
pass.write({ country: 'Germany', id: 3 });