-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.js
executable file
·135 lines (127 loc) · 3.67 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/******************************************************************************
*
* 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 {WebSocketServer, table} = require("@finos/perspective");
const {read_stdin, open_browser} = require("./utils.js");
const fs = require("fs");
const path = require("path");
const program = require("commander");
/**
* Convert data from one format to another.
*
* @param {*} filename
* @param {*} options
*/
async function convert(filename, options) {
let file;
if (filename) {
file = fs.readFileSync(filename).toString();
} else {
file = await read_stdin();
}
try {
file = JSON.parse(file);
} catch {}
let tbl = await table(file);
let view = await tbl.view();
let out;
options.format = options.format || "arrow";
if (options.format === "csv") {
out = await view.to_csv();
} else if (options.format === "columns") {
out = JSON.stringify(await view.to_columns());
} else if (options.format === "json") {
out = JSON.stringify(await view.to_json());
} else {
out = await view.to_arrow();
}
if (options.format === "arrow") {
if (options.output) {
fs.writeFileSync(options.output, Buffer.from(out), "binary");
} else {
console.log(Buffer.from(out).toString());
}
} else {
if (options.output) {
fs.writeFileSync(options.output, out);
} else {
console.log(out);
}
}
view.delete();
tbl.delete();
}
/**
* Host a Perspective on a web server.
*
* @param {*} filename
* @param {*} options
*/
async function host(filename, options) {
let files = [path.join(__dirname, "html")];
if (options.assets) {
files = [options.assets, ...files];
}
const server = new WebSocketServer({assets: files, port: options.port});
let file;
if (filename) {
file = await table(fs.readFileSync(filename).toString());
} else {
file = await read_stdin();
}
server.host_table("data_source_one", file);
if (options.open) {
open_browser(options.port);
}
}
program
.version(
JSON.parse(
fs
.readFileSync(path.join(__dirname, "..", "package.json"))
.toString()
).version
)
.description(
"A convenient command-line client for Perspective.js. Can convert between Perspective supported format, or host a local web server."
)
.action(() => program.help());
program
.command("convert [filename]")
.description(
"Convert a file into a new format. Reads from STDIN if no filename is provided."
)
.option(
"-f, --format <format>",
"Which output format to use: arrow, csv, columns, json.",
/^(arrow|json|columns|csv)$/i,
"arrow"
)
.option(
"-o, --output <filename>",
"Filename to write to. If not supplied, writes to STDOUT."
)
.action(convert);
program
.command("host [filename]")
.description(
"Host a file on a local Websocket/HTTP server using a server-side Perspective. Reads from STDIN if no filename is provided"
)
.option(
"-p, --port <port>",
"Which port to bind to.",
(x) => parseInt(x),
8080
)
.option("-a, --assets <path>", "Host from a working directory")
.option("-o, --open", "Open a browser automagically.")
.action(host);
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.help();
}