Skip to content

Commit a062485

Browse files
committed
feat: print Dev Server URL on 'u' keypress to reduce terminal clutter
1 parent 183c20d commit a062485

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lib/Server.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const os = require("os");
44
const path = require("path");
55
const url = require("url");
66
const util = require("util");
7+
const readline = require("readline");
78
const fs = require("graceful-fs");
89
const ipaddr = require("ipaddr.js");
910
const { validate } = require("schema-utils");
@@ -309,6 +310,24 @@ function useFn(route, fn) {
309310

310311
const DEFAULT_ALLOWED_PROTOCOLS = /^(file|.+-extension):/i;
311312

313+
/**
314+
* @param {(host: string) => string} prettyPrintURL
315+
*/
316+
function setupKeypressLogger(prettyPrintURL) {
317+
readline.emitKeypressEvents(process.stdin);
318+
if (process.stdin.isTTY) {
319+
process.stdin.setRawMode(true);
320+
process.stdin.on("keypress", (str, key) => {
321+
if (key.name === "u") {
322+
console.log(`🔗 Dev Server: ${prettyPrintURL("localhost")}`);
323+
} else if (key.name === "q" || (key.ctrl && key.name === "c")) {
324+
process.exit();
325+
}
326+
});
327+
console.log("🔑 Press [u] to show Dev Server URL, [q] to quit");
328+
}
329+
}
330+
312331
/**
313332
* @typedef {Object} BasicApplication
314333
* @property {typeof useFn} use
@@ -2917,6 +2936,11 @@ class Server {
29172936
const prettyPrintURL = (newHostname) =>
29182937
url.format({ protocol, hostname: newHostname, port, pathname: "/" });
29192938

2939+
// @ts-expect-error: we just added this option, TS types need updating
2940+
if (this.options.printUrlOnKeypress) {
2941+
setupKeypressLogger(prettyPrintURL);
2942+
}
2943+
29202944
let host;
29212945
let localhost;
29222946
let loopbackIPv4;

lib/options.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,11 @@
495495
"description": "Allows to specify a port to use.",
496496
"link": "https://webpack.js.org/configuration/dev-server/#devserverport"
497497
},
498+
"PrintUrlOnKeypress": {
499+
"type": "boolean",
500+
"default": false,
501+
"description": "Only print the Dev Server URL when the user presses 'u' in the terminal."
502+
},
498503
"Proxy": {
499504
"type": "array",
500505
"items": {
@@ -976,6 +981,9 @@
976981
"compress": {
977982
"$ref": "#/definitions/Compress"
978983
},
984+
"printUrlOnKeypress": {
985+
"$ref": "#/definitions/PrintUrlOnKeypress"
986+
},
979987
"devMiddleware": {
980988
"$ref": "#/definitions/DevMiddleware"
981989
},

types/lib/Server.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@ declare class Server<
575575
description: string;
576576
link: string;
577577
};
578+
PrintUrlOnKeypress: {
579+
type: string;
580+
default: boolean;
581+
description: string;
582+
};
578583
Proxy: {
579584
type: string;
580585
items: {
@@ -1066,6 +1071,9 @@ declare class Server<
10661071
compress: {
10671072
$ref: string;
10681073
};
1074+
printUrlOnKeypress: {
1075+
$ref: string;
1076+
};
10691077
devMiddleware: {
10701078
$ref: string;
10711079
};

0 commit comments

Comments
 (0)