Skip to content

Commit

Permalink
feat(https): CLI Options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mastercuber committed Aug 12, 2023
1 parent 56173e5 commit a541d74
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,47 @@ function detectStackblitzURL(entry?: string) {
console.error(error);
}
}

// @ts-ignore
export function mergeHttpsOptions(args) {
const options = { https: args.https } as any;
if (!args.https) {
return args;
}

if (args.tlsCert && args.tlsKey) {
options.https = {
cert: args.tlsCert,
key: args.tlsKey,
};
} else if (args.keystore) {
options.https = {
pfx: args.keystore,
};
}

if (args.passphrase) {
options.https.passphrase = args.passphrase;
}

if (!(args.keystore || (args.tlsCert && args.tlsKey))) {
if (args.validity) {
options.https = {
validityDays: +args.validity,
};
}

if (args.domains) {
const domains = args.domains.split(",").map((s: string) => s.trim());
if (typeof options.https === "object") {
options.https.domains = domains;
} else {
options.https = {
domains,
};
}
}
}

return options;
}

Check warning on line 126 in src/_utils.ts

View check run for this annotation

Codecov / codecov/patch

src/_utils.ts#L86-L126

Added lines #L86 - L126 were not covered by tests
34 changes: 31 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { defineCommand, runMain as _runMain } from "citty";
import { isAbsolute } from "pathe";
import { name, description, version } from "../package.json";
import { listen } from "./listen";
import { listenAndWatch } from "./server";
import { listenAndWatch, DevServerOptions, createDevServer } from "./server";
import type { ListenOptions } from "./types";
import { DevServerOptions, createDevServer } from "./server/dev";
import { mergeHttpsOptions } from "./_utils";

export const main = defineCommand({
meta: {
Expand Down Expand Up @@ -56,6 +56,33 @@ export const main = defineCommand({
description: "Enable HTTPS",
default: false,
},
tlsCert: {
type: "string",
description: "Path to TLS certificate used with HTTPS in PEM format",
},
tlsKey: {
type: "string",
description: "Path to TLS key used with HTTPS in PEM format",
},
keystore: {
type: "string",
description:
"Path to PKCS#12 (.p12/.pfx) keystore containing a TLS certificate and Key",
},
passphrase: {
type: "string",
description: "Passphrase used for TLS key or keystore",
},
validity: {
type: "string",
description:
"Validity in days of the autogenerated TLS certificate (https: true)",
},
domains: {
type: "string",
description:
"Comma seperated list of domains and IPs, the autogenerated certificate should be valid for (https: true)",
},
watch: {
type: "boolean",
description: "Watch for changes",
Expand All @@ -79,6 +106,7 @@ export const main = defineCommand({
},
},
async run({ args }) {
mergeHttpsOptions(args);
const opts: Partial<ListenOptions & WatchOptions & DevServerOptions> = {
...args,
port: args.port,
Expand All @@ -90,7 +118,7 @@ export const main = defineCommand({
qr: args.qr,
publicURL: args.publicURL,
public: args.public,
https: args.https, // TODO: Support custom cert
https: args.https,
};

const entry =
Expand Down

0 comments on commit a541d74

Please sign in to comment.