-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·166 lines (155 loc) · 4.52 KB
/
index.js
File metadata and controls
executable file
·166 lines (155 loc) · 4.52 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/******************************************************************************
*
* 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");
const puppeteer = require("puppeteer");
/**
* 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,
host_psp: true,
});
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) {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: [
"--incognito",
"--noerrdialogs",
"--disable-translate",
"--no-first-run",
"--fast",
"--fast-start",
"--disable-infobars",
"--window-size=1200,800",
"--disable-features=TranslateUI",
"--disk-cache-dir=/dev/null",
`--app=http://localhost:${options.port}/`,
],
});
const pages = await browser.pages();
const page = pages[0];
page.on("close", () => {
browser.close();
process.exit(0);
});
}
return server;
}
module.exports.host = host;
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);
if (require.main && !process.argv.slice(2).length) {
program.help();
}