[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / storage / 28-tail.js
1 /**
2  * Copyright 2013, 2014 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16
17 module.exports = function(RED) {
18     "use strict";
19     var spawn = require('child_process').spawn;
20     var plat = require('os').platform();
21
22     if (plat.match(/^win/)) {
23         throw "Info : Currently not supported on Windows.";
24     }
25
26     function TailNode(n) {
27         RED.nodes.createNode(this,n);
28
29         this.filename = n.filename;
30         this.split = n.split;
31         var node = this;
32
33         var err = "";
34         // TODO: rewrite to use node-tail
35         var tail = spawn("tail", ["-F", "-n", "0", this.filename]);
36         tail.stdout.on("data", function (data) {
37             if (node.split) {
38                 // TODO: allow customisation of the line break - as we do elsewhere
39                 var strings = data.toString().split("\n");
40                 for (var s in strings) {
41                     //TODO: should we really filter blanks? Is that expected?
42                     if (strings[s] !== "") {
43                         node.send({
44                             topic: node.filename,
45                             payload: strings[s]
46                         });
47                     }
48                 }
49             }
50             else {
51                 var msg = {
52                     topic:node.filename,
53                     payload: data.toString()
54                 };
55                 node.send(msg);
56             }
57         });
58
59         tail.stderr.on("data", function(data) {
60             node.warn(data.toString());
61         });
62
63         this.on("close", function() {
64             if (tail) { tail.kill(); }
65         });
66     }
67
68     RED.nodes.registerType("tail",TailNode);
69 }