Skip to content

Commit

Permalink
feat(cli): expose getArgs and parseArgs (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 23, 2023
1 parent 5fda128 commit 00408f3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 85 deletions.
1 change: 1 addition & 0 deletions cli.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist/cli'
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"files": [
"dist",
"lib",
"bin"
"bin",
"cli.d.ts"
],
"scripts": {
"build": "unbuild",
Expand Down Expand Up @@ -69,4 +70,4 @@
"vitest": "^0.34.2"
},
"packageManager": "pnpm@8.6.12"
}
}
161 changes: 78 additions & 83 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WatchOptions } from "node:fs";
import { defineCommand, runMain as _runMain } from "citty";
import { defineCommand, runMain as _runMain, ParsedArgs } from "citty";
import { isAbsolute } from "pathe";
import { name, description, version } from "../package.json";
import { listen } from "./listen";
Expand All @@ -22,6 +22,53 @@ export const main = defineCommand({
description: "Listener entry file (./app.ts)",
required: true,
},
name: {
type: "string",
description: "Name to use in the banner",
},
baseURL: {
type: "string",
description: "Base URL to use",
},
watch: {
type: "boolean",
description: "Watch for changes",
alias: "w",
default: false,
},
...getArgs(),
},
async run({ args }) {
const opts: Partial<ListenOptions & WatchOptions & DevServerOptions> = {
...args,
...parseArgs(args),
baseURL: args.baseURL,
name: args.name,
};

const entry =
isAbsolute(args.entry) || args.entry.startsWith(".")
? args.entry
: `./${args.entry}`;

if (args.watch) {
await listenAndWatch(entry, opts);
} else {
const devServer = await createDevServer(entry, opts);
await listen(devServer.nodeListener, {
...opts,
_entry: devServer._entry,
});
await devServer.reload(true);
}
},
});

export const runMain = () => _runMain(main);

/** Returns unjs/citty compatible args object */
export function getArgs() {
return {
port: {
type: "string",
description:
Expand All @@ -42,14 +89,6 @@ export const main = defineCommand({
description: "Open the URL in the browser",
default: false,
},
baseURL: {
type: "string",
description: "Base URL to use",
},
name: {
type: "string",
description: "Name to use in the banner",
},
https: {
type: "boolean",
description: "Enable HTTPS",
Expand Down Expand Up @@ -82,15 +121,9 @@ export const main = defineCommand({
description:
"Comma seperated list of domains and IPs, the autogenerated certificate should be valid for (https: true)",
},
watch: {
type: "boolean",
description: "Watch for changes",
alias: "w",
default: false,
},
publicURL: {
type: "string",
description: "Displayed public URL (used for qr code)",
description: "Displayed public URL (used for QR code)",
required: false,
},
qr: {
Expand All @@ -108,73 +141,35 @@ export const main = defineCommand({
description: "Open a tunnel using https://github.com/unjs/untun",
required: false,
},
},
async run({ args }) {
const opts: Partial<ListenOptions & WatchOptions & DevServerOptions> = {
...args,
port: args.port,
hostname: args.host,
clipboard: args.clipboard,
open: args.open,
baseURL: args.baseURL,
name: args.name,
qr: args.qr,
publicURL: args.publicURL,
public: args.public,
https: args.https ? _parseHTTPSArgs(args) : false,
tunnel: args.tunnel,
};

const entry =
isAbsolute(args.entry) || args.entry.startsWith(".")
? args.entry
: `./${args.entry}`;

if (args.watch) {
await listenAndWatch(entry, opts);
} else {
const devServer = await createDevServer(entry, opts);
await listen(devServer.nodeListener, {
...opts,
_entry: devServer._entry,
});
await devServer.reload(true);
}
},
});

export const runMain = () => _runMain(main);

// --- internal utils ---

function _parseHTTPSArgs(args: Record<string, any>): HTTPSOptions {
const https: HTTPSOptions = {};

if (args["https.cert"]) {
https.cert = args["https.cert"];
}

if (args["https.key"]) {
https.key = args["https.key"];
}

if (args["https.pfx"]) {
https.pfx = args["https.pfx"];
}

if (args["https.passphrase"]) {
https.passphrase = args["https.passphrase"];
}

if (args["https.validityDays"]) {
https.validityDays = args["https.validityDays"];
}
} as const;
}

if (args["https.domains"]) {
https.domains = args["https.domains"]
.split(",")
.map((s: string) => s.trim());
}
type ParsedListhenArgs = ParsedArgs<ReturnType<typeof getArgs>>;

return https;
/** Convert unjs/citty compatible args to listhen options */
export function parseArgs(args: ParsedListhenArgs): Partial<ListenOptions> {
return {
port: args.port,
hostname: args.host,
clipboard: args.clipboard,
open: args.open,
qr: args.qr,
publicURL: args.publicURL,
public: args.public,
tunnel: args.tunnel,
https: args.https
? <HTTPSOptions>{
cert: args["https.cert"],
key: args["https.key"],
pfx: args["https.pfx"],
passphrase: args["https.passphrase"],
validityDays: args["https.validityDays"]
? +args["https.validityDays"]
: undefined,
domains: args["https.domains"]
? args["https.domains"].split(",")
: undefined,
}
: false,
};
}

0 comments on commit 00408f3

Please sign in to comment.