-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpetl-tap-csv.js
51 lines (45 loc) · 1.63 KB
/
gulpetl-tap-csv.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
// const tapCsv = require('./src/plugin'); // Error: Cannot find module './src/plugin'
// import { csvStringifyNdjson } from 'gulp-etl-tap-csv'; // SyntaxError: Cannot use import statement outside a module (line:2)
const tapCsv = require('gulp-etl-tap-csv');
const extractConfig = require('@gulpetl/node-red-core').extractConfig;
module.exports = function (RED) {
function TapCsvNode(config) {
RED.nodes.createNode(this, config);
this.config = config.config;
var node = this;
node.on('input', function (msg, send, done) {
let configObj;
try {
if (this.config.trim())
configObj = JSON.parse(this.config);
}
catch (err) {
done(`Unable to parse ${tapCsv.PLUGIN_NAME}.config: ` + err);
return;
}
configObj = extractConfig(configObj, msg?.config, tapCsv.PLUGIN_NAME, tapCsv.localDefaultConfigObj);
if (!msg.topic?.startsWith("gulp")) {
let fileNameStem = msg?.filename.split(/[\\/]/).pop().split('.')[0];
tapCsv.csvParseText(msg.payload, fileNameStem, configObj)
.then((data) => {
msg.payload = data;
// console.log("msg.config", msg.config)
// console.log("configObj", configObj)
})
.catch((err) => {
node.error(err.message);
})
.finally(() => {
node.send(msg);
})
}
else {
if (msg.topic == "gulp-initialize") {
msg.plugins.push({ name: config.type, init: () => tapCsv.tapCsv(configObj) });
}
node.send(msg);
}
})
}
RED.nodes.registerType("gulpetl-tap-csv", TapCsvNode);
}