From fc99376ad28432c3a2559335f7d61ebf9a18b35e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 23 Aug 2023 02:05:38 +0200 Subject: [PATCH 1/2] feat(cli): add listhen options for `dev` command --- package.json | 2 +- pnpm-lock.yaml | 23 ++++---- src/cli/commands/dev.ts | 128 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 137 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 1cefc5c94b..7bf5a5dfa9 100644 --- a/package.json +++ b/package.json @@ -95,7 +95,7 @@ "jiti": "^1.19.3", "klona": "^2.0.6", "knitwork": "^1.0.0", - "listhen": "^1.3.0", + "listhen": "^1.3.1", "magic-string": "^0.30.3", "mime": "^3.0.0", "mlly": "^1.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5c9a206d20..7df643a78f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -121,8 +121,8 @@ importers: specifier: ^1.0.0 version: 1.0.0 listhen: - specifier: ^1.3.0 - version: 1.3.0 + specifier: ^1.3.1 + version: 1.3.1 magic-string: specifier: ^0.30.3 version: 0.30.3 @@ -1531,14 +1531,15 @@ packages: dev: false optional: true - /@parcel/watcher-wasm@2.3.0-alpha.1: - resolution: {integrity: sha512-wo6065l1MQ6SJPPchYw/q8J+pFL40qBXLu4Td2CXeQ/+mUk8NenNqC75P/P1Cyvpam0kfk91iszd+XL+xKDQww==} + /@parcel/watcher-wasm@2.3.0-alpha.3: + resolution: {integrity: sha512-kTkqlYhGhCM9EaoZMyjNzqKRSdTyp/vN+/uoJ2fzN+UCiWUI+jMKacwnBgu13T1F0mrcNqEWtYEXTRaZmnml1w==} engines: {node: '>= 10.0.0'} dependencies: is-glob: 4.0.3 micromatch: 4.0.5 - napi-wasm: 1.1.0 dev: false + bundledDependencies: + - napi-wasm /@parcel/watcher-win32-arm64@2.2.0: resolution: {integrity: sha512-z225cPn3aygJsyVUOWwfyW+fY0Tvk7N3XCOl66qUPFxpbuXeZuiuuJemmtm8vxyqa3Ur7peU/qJxrpC64aeI7Q==} @@ -5043,12 +5044,12 @@ packages: dev: true optional: true - /listhen@1.3.0: - resolution: {integrity: sha512-QhlP01ReqSXpu8OgBaFQjYMU/4YJTCWLFtoDTxBhitPQWfu0UuBoG2HizMysaRkUEAr/CVxB/20T8ni0zQDPtw==} + /listhen@1.3.1: + resolution: {integrity: sha512-9EQXJXPz+EqJGxFJrXG5pnihRbpjDD5DGh3gPHI3Re6/6UXd27ZVRb4JyPrAztdV9Ntf2gKbT5yG9LUs3aI9kQ==} hasBin: true dependencies: '@parcel/watcher': 2.2.0 - '@parcel/watcher-wasm': 2.3.0-alpha.1 + '@parcel/watcher-wasm': 2.3.0-alpha.3 citty: 0.1.2 clipboardy: 3.0.0 consola: 3.2.3 @@ -5439,10 +5440,6 @@ packages: hasBin: true dev: true - /napi-wasm@1.1.0: - resolution: {integrity: sha512-lHwIAJbmLSjF9VDRm9GoVOy9AGp3aIvkjv+Kvz9h16QR3uSVYH78PNQUnT2U4X53mhlnV2M7wrhibQ3GHicDmg==} - dev: false - /natural-compare-lite@1.4.0: resolution: {integrity: sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==} dev: true @@ -6997,7 +6994,7 @@ packages: destr: 2.0.1 h3: 1.8.0 ioredis: 5.3.2 - listhen: 1.3.0 + listhen: 1.3.1 lru-cache: 10.0.1 mri: 1.2.0 node-fetch-native: 1.3.3 diff --git a/src/cli/commands/dev.ts b/src/cli/commands/dev.ts index 5cd897e25e..147a038b16 100644 --- a/src/cli/commands/dev.ts +++ b/src/cli/commands/dev.ts @@ -1,6 +1,7 @@ -import { defineCommand } from "citty"; +import { ParsedArgs, defineCommand } from "citty"; import { resolve } from "pathe"; import { consola } from "consola"; +import type { ListenOptions } from "listhen"; import { createNitro } from "../../nitro"; import { build, prepare } from "../../build"; import { createDevServer } from "../../dev/server"; @@ -16,6 +17,7 @@ export default defineCommand({ }, args: { ...commonArgs, + ..._listhenArgs(), }, async run({ args }) { const rootDir = resolve((args.dir || args._dir || ".") as string); @@ -58,10 +60,132 @@ export default defineCommand({ ); nitro.hooks.hookOnce("restart", reload); const server = createDevServer(nitro); - await server.listen({}); + const listhenOptions = _listhenOptions(args); + await server.listen(listhenOptions.port, listhenOptions); await prepare(nitro); await build(nitro); }; await reload(); }, }); + +// -- Internal utils --- + +// TODO: Reuse from unjs/listhen cli + +function _listhenArgs() { + return { + port: { + type: "string", + description: + "Port to listen on (use PORT environment variable to override)", + }, + host: { + type: "string", + description: + "Host to listen on (use HOST environment variable to override)", + }, + clipboard: { + type: "boolean", + description: "Copy the URL to the clipboard", + default: false, + }, + open: { + type: "boolean", + 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", + default: false, + }, + "https.cert": { + type: "string", + description: "Path to TLS certificate used with HTTPS in PEM format", + }, + "https.key": { + type: "string", + description: "Path to TLS key used with HTTPS in PEM format", + }, + "https.pfx": { + type: "string", + description: + "Path to PKCS#12 (.p12/.pfx) keystore containing a TLS certificate and Key", + }, + "https.passphrase": { + type: "string", + description: "Passphrase used for TLS key or keystore", + }, + "https.validityDays": { + type: "string", + description: + "Validity in days of the autogenerated TLS certificate (https: true)", + }, + "https.domains": { + type: "string", + description: + "Comma seperated list of domains and IPs, the autogenerated certificate should be valid for (https: true)", + }, + publicURL: { + type: "string", + description: "Displayed public URL (used for qr code)", + required: false, + }, + qr: { + type: "boolean", + description: "Display The QR code of public URL when available", + required: false, + }, + public: { + type: "boolean", + description: "Listen to all network interfaces", + required: false, + }, + tunnel: { + type: "boolean", + description: "Open a tunnel using https://github.com/unjs/untun", + required: false, + }, + } as const; +} + +type ParsedListhenArgs = ParsedArgs>; + +function _listhenOptions(args: ParsedListhenArgs): Partial { + return { + ...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 + ? { + 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, + tunnel: args.tunnel, + }; +} From a2c2b84152cde26fca243edc954a582ff6820b5f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 23 Aug 2023 02:29:50 +0200 Subject: [PATCH 2/2] reuse from listhen --- package.json | 2 +- pnpm-lock.yaml | 10 ++-- src/cli/commands/dev.ts | 127 +--------------------------------------- 3 files changed, 9 insertions(+), 130 deletions(-) diff --git a/package.json b/package.json index 7bf5a5dfa9..f308a6c564 100644 --- a/package.json +++ b/package.json @@ -95,7 +95,7 @@ "jiti": "^1.19.3", "klona": "^2.0.6", "knitwork": "^1.0.0", - "listhen": "^1.3.1", + "listhen": "^1.4.0", "magic-string": "^0.30.3", "mime": "^3.0.0", "mlly": "^1.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7df643a78f..a123bbe039 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -121,8 +121,8 @@ importers: specifier: ^1.0.0 version: 1.0.0 listhen: - specifier: ^1.3.1 - version: 1.3.1 + specifier: ^1.4.0 + version: 1.4.0 magic-string: specifier: ^0.30.3 version: 0.30.3 @@ -5044,8 +5044,8 @@ packages: dev: true optional: true - /listhen@1.3.1: - resolution: {integrity: sha512-9EQXJXPz+EqJGxFJrXG5pnihRbpjDD5DGh3gPHI3Re6/6UXd27ZVRb4JyPrAztdV9Ntf2gKbT5yG9LUs3aI9kQ==} + /listhen@1.4.0: + resolution: {integrity: sha512-gEOMJKTak+WLjPITBVbv2kR0WKVUSnl5XPwvoFYheyaQPzh/jdA+pRZeUujJkjabNMDsBxwuaYH7HYLpzzGEJA==} hasBin: true dependencies: '@parcel/watcher': 2.2.0 @@ -6994,7 +6994,7 @@ packages: destr: 2.0.1 h3: 1.8.0 ioredis: 5.3.2 - listhen: 1.3.1 + listhen: 1.4.0 lru-cache: 10.0.1 mri: 1.2.0 node-fetch-native: 1.3.3 diff --git a/src/cli/commands/dev.ts b/src/cli/commands/dev.ts index 147a038b16..7f107dbf9b 100644 --- a/src/cli/commands/dev.ts +++ b/src/cli/commands/dev.ts @@ -1,7 +1,7 @@ import { ParsedArgs, defineCommand } from "citty"; import { resolve } from "pathe"; import { consola } from "consola"; -import type { ListenOptions } from "listhen"; +import { getArgs, parseArgs } from "listhen/cli"; import { createNitro } from "../../nitro"; import { build, prepare } from "../../build"; import { createDevServer } from "../../dev/server"; @@ -17,7 +17,7 @@ export default defineCommand({ }, args: { ...commonArgs, - ..._listhenArgs(), + ...getArgs(), }, async run({ args }) { const rootDir = resolve((args.dir || args._dir || ".") as string); @@ -60,7 +60,7 @@ export default defineCommand({ ); nitro.hooks.hookOnce("restart", reload); const server = createDevServer(nitro); - const listhenOptions = _listhenOptions(args); + const listhenOptions = parseArgs(args); await server.listen(listhenOptions.port, listhenOptions); await prepare(nitro); await build(nitro); @@ -68,124 +68,3 @@ export default defineCommand({ await reload(); }, }); - -// -- Internal utils --- - -// TODO: Reuse from unjs/listhen cli - -function _listhenArgs() { - return { - port: { - type: "string", - description: - "Port to listen on (use PORT environment variable to override)", - }, - host: { - type: "string", - description: - "Host to listen on (use HOST environment variable to override)", - }, - clipboard: { - type: "boolean", - description: "Copy the URL to the clipboard", - default: false, - }, - open: { - type: "boolean", - 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", - default: false, - }, - "https.cert": { - type: "string", - description: "Path to TLS certificate used with HTTPS in PEM format", - }, - "https.key": { - type: "string", - description: "Path to TLS key used with HTTPS in PEM format", - }, - "https.pfx": { - type: "string", - description: - "Path to PKCS#12 (.p12/.pfx) keystore containing a TLS certificate and Key", - }, - "https.passphrase": { - type: "string", - description: "Passphrase used for TLS key or keystore", - }, - "https.validityDays": { - type: "string", - description: - "Validity in days of the autogenerated TLS certificate (https: true)", - }, - "https.domains": { - type: "string", - description: - "Comma seperated list of domains and IPs, the autogenerated certificate should be valid for (https: true)", - }, - publicURL: { - type: "string", - description: "Displayed public URL (used for qr code)", - required: false, - }, - qr: { - type: "boolean", - description: "Display The QR code of public URL when available", - required: false, - }, - public: { - type: "boolean", - description: "Listen to all network interfaces", - required: false, - }, - tunnel: { - type: "boolean", - description: "Open a tunnel using https://github.com/unjs/untun", - required: false, - }, - } as const; -} - -type ParsedListhenArgs = ParsedArgs>; - -function _listhenOptions(args: ParsedListhenArgs): Partial { - return { - ...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 - ? { - 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, - tunnel: args.tunnel, - }; -}