-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·65 lines (59 loc) · 2.07 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
'use strict';
const fs = require('fs');
const once = require('node:events').once;
const csv = require('@fast-csv/parse');
const JsonStreamStringify = require('json-stream-stringify').JsonStreamStringify;
const Executor = require('@runnerty/module-core').Executor;
class csv2jsonExecutor extends Executor {
constructor(process) {
super(process);
this.endOptions = { end: 'end' };
}
async exec(params) {
const jsonResult = [];
try {
if (!params.inputPath) throw new Error('Missing inputPath (csv2json).');
await fs.promises.access(params.inputPath, fs.constants.R_OK);
const streamCSV = fs.createReadStream(params.inputPath);
if (!params.options) params.options = {};
if (params.options?.headers) {
if (params.options.headers instanceof Array) {
params.options.headers = params.options.headers.map(item => {
return item == null ? undefined : item;
});
}
} else {
// Headers options default: true.
if (params.options.headers === undefined) params.options.headers = true;
}
csv
.parseStream(streamCSV, params.options || {})
.on('error', error => {
throw error;
})
.on('data', row => {
jsonResult.push(row);
})
.on('end', async rowCount => {
// JSON Export
if (params.outputPath) {
const streamJSON = fs.createWriteStream(params.outputPath);
const jsonStream = new JsonStreamStringify(jsonResult).pipe(streamJSON);
await once(jsonStream, 'close');
}
//STANDARD OUPUT:
this.endOptions.data_output = jsonResult || '';
//EXTRA DATA OUTPUT:
this.endOptions.extra_output = {};
this.endOptions.extra_output.rowcount = rowCount;
this.end(this.endOptions);
});
} catch (err) {
this.endOptions.end = 'error';
this.endOptions.messageLog = err.message;
this.endOptions.err_output = err.message;
this.end(this.endOptions);
}
}
}
module.exports = csv2jsonExecutor;