-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
utils.js
71 lines (65 loc) · 1.92 KB
/
utils.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
66
67
68
69
70
71
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/
const exec = require("child_process").exec;
const { table } = require("@finos/perspective");
const OPEN = (port) => `
if which xdg-open > /dev/null
then
xdg-open http://localhost:${port}/
elif which gnome-open > /dev/null
then
gnome-open http://localhost:${port}/
elif which open > /dev/null
then
open http://localhost:${port}/
fi`;
function infer_table(buffer) {
if (buffer.slice(0, 6).toString() === "ARROW1") {
console.log("Loaded Arrow");
return table(buffer.buffer);
} else {
let text = buffer.toString();
try {
let json = JSON.parse(text);
console.log("Loaded JSON");
return table(json);
} catch (e) {
console.log("Loaded CSV");
return table(text);
}
}
}
module.exports.read_stdin = function read_stdin() {
const ret = [];
let len = 0;
return new Promise((resolve) => {
process.stdin
.on("readable", function () {
let chunk;
while ((chunk = process.stdin.read())) {
ret.push(Buffer.from(chunk));
len += chunk.length;
}
})
.on("end", function () {
const buffer = Buffer.concat(ret, len);
resolve(infer_table(buffer));
});
});
};
module.exports.execute = function execute(command, callback) {
exec(command, function (error, stdout) {
if (callback) {
callback(stdout);
}
});
};
module.exports.open_browser = function open_browser(port) {
module.exports.execute(OPEN(port), console.log);
};