-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-list.js
43 lines (41 loc) · 1.53 KB
/
read-list.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
module.exports = function (RED) {
"use strict";
var util = require("util");
var vm = require("vm");
var fs = require("fs");
var readListFs = require("./list-fs.js")
function ReadList(config) {
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function (msg) {
console.log("### Read list ###")
//Read File
var filename = msg.filename
var listname = msg.listname
var fileContent = ''
if (!filename || !listname || filename.size == 0 || listname.size == 0) {
console.error("Filename or listname is empty");
console.log("### error ###")
return;
}
console.log("### filename ###" + filename)
fileContent = fs.readFileSync(filename, 'utf-8')
console.log("1 -" + fileContent)
console.log("### error ###")
if (fileContent && fileContent.length > 0) {
var listcontent = []
listcontent = readListFs.splitList(readListFs.getList(fileContent, listname, "\n"), 3500)
for (const item of listcontent) {
msg.payload = item
node.send(msg)
}
} else {
node.warn("File " + filename + " is empty")
console.log("### empty ###")
msg.payload = ''
node.send(msg)
}
});
}
RED.nodes.registerType("read-list", ReadList);
}