mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-09 09:09:58 +00:00
Add friendlier app CLI
This commit is contained in:
@@ -10,53 +10,52 @@ import { DecodeTransform
|
||||
, EncodeTransform } from "../transforms";
|
||||
|
||||
|
||||
const argv = minimist(process.argv.slice(2), {
|
||||
string: [ "port" ]
|
||||
, default: {
|
||||
port: "9556"
|
||||
}
|
||||
});
|
||||
export function init (port: number) {
|
||||
process.stdout.write("Starting WebSocket server... ");
|
||||
|
||||
const port = parseInt(argv.port);
|
||||
if (!port || port < 1025 || port > 65535) {
|
||||
console.error("Invalid port specified!");
|
||||
process.exit(1);
|
||||
}
|
||||
const wss = new WebSocket.Server({ port }, () => {
|
||||
console.log("Done!");
|
||||
});
|
||||
|
||||
const wss = new WebSocket.Server({ port });
|
||||
|
||||
wss.on("connection", socket => {
|
||||
// Stream for incoming WebSocket messages
|
||||
const messageStream = new Readable({ objectMode: true });
|
||||
messageStream._read = () => {};
|
||||
|
||||
socket.on("message", (message: string) => {
|
||||
messageStream.push(JSON.parse(message));
|
||||
wss.on("error", (err) => {
|
||||
console.log("Failed!");
|
||||
console.error(err);
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Daemon and bridge are the same binary, so spawn a new
|
||||
* version of self in bridge mode.
|
||||
*/
|
||||
const bridge = spawn(process.execPath, [ process.argv[1] ]);
|
||||
wss.on("connection", socket => {
|
||||
// Stream for incoming WebSocket messages
|
||||
const messageStream = new Readable({ objectMode: true });
|
||||
messageStream._read = () => {};
|
||||
|
||||
// socket -> bridge.stdin
|
||||
messageStream
|
||||
.pipe(new EncodeTransform())
|
||||
.pipe(bridge.stdin);
|
||||
|
||||
// bridge.stdout -> socket
|
||||
bridge.stdout
|
||||
.pipe(new DecodeTransform())
|
||||
.on("data", data => {
|
||||
// Socket can be CLOSING here
|
||||
if (socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(JSON.stringify(data));
|
||||
}
|
||||
socket.on("message", (message: string) => {
|
||||
messageStream.push(JSON.parse(message));
|
||||
});
|
||||
|
||||
// Handle termination
|
||||
socket.on("close", () => bridge.kill());
|
||||
bridge.on("exit", () => socket.close());
|
||||
});
|
||||
|
||||
/**
|
||||
* Daemon and bridge are the same binary, so spawn a new
|
||||
* version of self in bridge mode.
|
||||
*/
|
||||
const bridge = spawn(process.execPath, [ process.argv[1] ]);
|
||||
|
||||
// socket -> bridge.stdin
|
||||
messageStream
|
||||
.pipe(new EncodeTransform())
|
||||
.pipe(bridge.stdin);
|
||||
|
||||
// bridge.stdout -> socket
|
||||
bridge.stdout
|
||||
.pipe(new DecodeTransform())
|
||||
.on("data", data => {
|
||||
// Socket can be CLOSING here
|
||||
if (socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(JSON.stringify(data));
|
||||
}
|
||||
});
|
||||
|
||||
// Handle termination
|
||||
socket.on("close", () => bridge.kill());
|
||||
bridge.on("exit", () => socket.close());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,18 +1,53 @@
|
||||
"use strict";
|
||||
|
||||
import meta from "../package.json";
|
||||
|
||||
import dedent from "dedent";
|
||||
import minimist from "minimist";
|
||||
|
||||
import * as daemon from "./daemon";
|
||||
|
||||
|
||||
const argv = minimist(process.argv.slice(2), {
|
||||
boolean: [ "daemon" ]
|
||||
boolean: [ "daemon", "help", "version" ]
|
||||
, string: [ "port" ]
|
||||
, alias: {
|
||||
d: "daemon"
|
||||
, h: "help"
|
||||
, v: "version"
|
||||
, p: "port"
|
||||
}
|
||||
, default: {
|
||||
daemon: false
|
||||
, port: "9556"
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
if (argv.daemon) {
|
||||
import("./daemon");
|
||||
if (argv.version) {
|
||||
console.log(meta.__applicationVersion);
|
||||
} else if (argv.help) {
|
||||
console.log(dedent`
|
||||
Usage: ${meta.__applicationExecutableName} [options]
|
||||
|
||||
Options:
|
||||
-h, --help Print usage info
|
||||
-v, --version Print version info
|
||||
-d, --daemon Launch in daemon mode. This starts a WebSocket server that
|
||||
the extension can be configured to connect to under bridge
|
||||
options.
|
||||
-p, --port Set port number for WebSocket server. This must match the
|
||||
port set in the extension options.
|
||||
|
||||
`);
|
||||
} else if (argv.daemon) {
|
||||
const port = parseInt(argv.port);
|
||||
if (!port || port < 1025 || port > 65535) {
|
||||
console.error("Invalid port specified!");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
daemon.init(port);
|
||||
} else {
|
||||
import("./bridge");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user