Replace minimist, convert build scripts to ES modules + misc refactoring

This commit is contained in:
hensm
2022-08-19 03:09:59 +01:00
parent 170b80283d
commit a186570dc8
17 changed files with 702 additions and 505 deletions

View File

@@ -6,7 +6,7 @@ import { handleCastMessage } from "./components/cast";
import { startDiscovery, stopDiscovery } from "./components/discovery";
import { startMediaServer, stopMediaServer } from "./components/mediaServer";
import { __applicationVersion } from "../../package.json";
import { applicationVersion } from "../../config.json";
process.on("SIGTERM", () => {
stopDiscovery();
@@ -24,7 +24,7 @@ messaging.on("message", (message: Message) => {
switch (message.subject) {
case "bridge:getInfo":
case "bridge:/getInfo": {
messaging.send(__applicationVersion);
messaging.send(applicationVersion);
break;
}

View File

@@ -6,24 +6,28 @@ import WebSocket from "ws";
import { spawn } from "child_process";
import { Readable } from "stream";
import { DecodeTransform, EncodeTransform } from "./transforms";
import { DecodeTransform, EncodeTransform } from "./transforms.js";
interface DaemonOpts {
host: string;
port: number;
password: string;
password?: string;
}
export function init(opts: DaemonOpts) {
const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true });
process.stdout.write("Starting WebSocket server... ");
process.stdout.write(
`Starting WebSocket server at ws://${
opts.host.includes(":") ? `[${opts.host}]` : opts.host
}:${opts.port}... `
);
server.on("listening", () => {
process.stdout.write("Done!\n");
});
wss.on("error", err => {
server.on("error", err => {
console.error("Failed!");
console.error(err.message);
});

View File

@@ -1,64 +1,55 @@
"use strict";
import path from "path";
import minimist from "minimist";
import { __applicationVersion } from "../package.json";
import yargs from "yargs";
import { applicationName, applicationVersion } from "../config.json";
const argv = minimist(process.argv.slice(2), {
boolean: ["daemon", "help", "version"],
string: ["__name", "host", "port", "password"],
alias: {
d: "daemon",
h: "help",
v: "version",
n: "host",
p: "port",
P: "password"
},
default: {
__name: path.basename(process.argv[0]),
daemon: false,
host: "localhost",
port: "9556"
}
});
const argv = yargs()
.scriptName(applicationName)
.usage("$0 [args]")
.help()
.alias("help", "h")
.version(`v${applicationVersion}`)
.alias("version", "v")
.option("daemon", {
alias: "d",
describe: `Launch in daemon mode. This starts a WebSocket server that \
the extension can be configured to connect to under bridge options.`,
type: "boolean"
})
.option("host", {
alias: "n",
describe: `Host for daemon WebSocket server. This must match the host \
set in the extension options.`,
default: "localhost"
})
.option("port", {
alias: "p",
describe: `Port number for daemon WebSocket server. This must match \
the port set in the extension options.`,
default: 9556
})
.option("password", {
alias: "P",
describe: `Set an optional password for the daemon WebSocket server. \
This must match the password set in the extension options.
WARNING: This password is intended only as a basic access control measure and \
is transmitted in plain text even over remote connections!`,
type: "string"
})
.check(argv => {
if (argv.port < 1025 || argv.port > 65535) {
throw new Error("Invalid port specified!");
}
if (argv.version) {
// eslint-disable-next-line no-console
console.log(`v${__applicationVersion}`);
} else if (argv.help) {
// eslint-disable-next-line no-console
console.log(
`Usage: ${argv.__name} [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.
-n, --host Host for daemon WebSocket server. This must match the host
set in the extension options.
-p, --port Port number for daemon WebSocket server. This must match the
port set in the extension options.
-P, --password Set an optional password for the daemon WebSocket server.
This must match the password set in the extension options.
WARNING: This password is intended only as a basic access
control measure and is transmitted in plain text even over
remote connections!
`
);
} else if (argv.daemon) {
const port = parseInt(argv.port);
if (!port || port < 1025 || port > 65535) {
console.error("Invalid port specified!");
process.exit(1);
}
return true;
})
.parseSync(process.argv);
if (argv.daemon) {
import("./daemon").then(daemon => {
daemon.init({
host: argv.host,
port,
port: argv.port,
password: argv.password
});
});