|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -import { program } from "commander"; |
| 3 | +import { parseArgs } from "node:util"; |
4 | 4 | import { readFile } from "node:fs/promises"; |
5 | 5 | import Client from "../index.js"; |
6 | 6 |
|
7 | 7 | const { version } = JSON.parse( |
8 | 8 | await readFile(new URL("../package.json", import.meta.url)), |
9 | 9 | ); |
10 | 10 |
|
11 | | -program |
12 | | - .version(version, "-v, --version") |
13 | | - .usage("[options]") |
14 | | - .option( |
15 | | - "-u, --url <url>", |
16 | | - "URL of the webhook proxy service. Default: https://smee.io/new", |
17 | | - ) |
18 | | - .option( |
19 | | - "-t, --target <target>", |
20 | | - "Full URL (including protocol and path) of the target service the events will forwarded to. Default: http://127.0.0.1:PORT/PATH", |
21 | | - ) |
22 | | - .option("-p, --port <n>", "Local HTTP server port", process.env.PORT || 3000) |
23 | | - .option("-P, --path <path>", "URL path to post proxied requests to`", "/") |
24 | | - .parse(process.argv); |
25 | | - |
26 | | -const opts = program.opts(); |
27 | | - |
28 | | -const { target = `http://127.0.0.1:${opts.port}${opts.path}` } = opts; |
29 | | - |
30 | | -async function setup() { |
31 | | - let source = opts.url; |
32 | | - |
33 | | - if (!source) { |
34 | | - source = await Client.createChannel(); |
| 11 | +const { values: options } = parseArgs({ |
| 12 | + options: { |
| 13 | + version: { |
| 14 | + type: "boolean", |
| 15 | + short: "v", |
| 16 | + default: false, |
| 17 | + }, |
| 18 | + help: { |
| 19 | + type: "boolean", |
| 20 | + short: "h", |
| 21 | + default: false, |
| 22 | + }, |
| 23 | + url: { |
| 24 | + type: "string", |
| 25 | + short: "u", |
| 26 | + default: "https://smee.io/new", |
| 27 | + }, |
| 28 | + target: { |
| 29 | + type: "string", |
| 30 | + short: "t", |
| 31 | + }, |
| 32 | + path: { |
| 33 | + type: "string", |
| 34 | + short: "P", |
| 35 | + default: "/", |
| 36 | + }, |
| 37 | + port: { |
| 38 | + type: "string", |
| 39 | + short: "p", |
| 40 | + default: process.env.PORT || "3000", |
| 41 | + }, |
| 42 | + }, |
| 43 | +}); |
| 44 | + |
| 45 | +if (options.help) { |
| 46 | + console.log(`Usage: smee [options] |
| 47 | +
|
| 48 | +Options: |
| 49 | + -v, --version Display the version number |
| 50 | + -u, --url <url> URL of the webhook proxy service. Default: https://smee.io/new |
| 51 | + -t, --target <target> Full URL (including protocol and path) of the target service the events will forwarded to. |
| 52 | + Default: http://127.0.0.1:PORT/PATH |
| 53 | + -p, --port <n> Local HTTP server port. Default: 3000 |
| 54 | + -P, --path <path> URL path to post proxied requests to. Default: "/" |
| 55 | + -h, --help Display this help message`); |
| 56 | +} else if (options.version) { |
| 57 | + console.log(version); |
| 58 | +} else { |
| 59 | + const { target = `http://127.0.0.1:${options.port}${options.path}` } = |
| 60 | + options; |
| 61 | + |
| 62 | + async function setup() { |
| 63 | + const source = options.url ?? (await Client.createChannel()); |
| 64 | + |
| 65 | + const client = new Client({ source, target }); |
| 66 | + client.start(); |
35 | 67 | } |
36 | 68 |
|
37 | | - const client = new Client({ source, target }); |
38 | | - client.start(); |
| 69 | + setup(); |
39 | 70 | } |
40 | | - |
41 | | -setup(); |
|
0 commit comments