From b4551f1ca3162e92a913efa45e95c81f854d724a Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Tue, 4 Jun 2024 12:04:03 -0500 Subject: [PATCH] Adds support for `messageOnStart` callback to customize startup log message. Fixes #79 --- server.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index 8a8614f..7c3ab06 100644 --- a/server.js +++ b/server.js @@ -34,6 +34,15 @@ const DEFAULT_OPTIONS = { aliases: {}, // Aliasing feature indexFileName: "index.html", // Allow custom index file name useCache: false, // Use a cache for file contents + messageOnStart: ({ hosts, startupTime, version, options }) => { + let hostsStr = " started"; + if(Array.isArray(hosts) && hosts.length > 0) { + // TODO what happens when the cert doesn’t cover non-localhost hosts? + hostsStr = ` at ${hosts.join(" or ")}`; + } + + return `Server${hostsStr}${options.showVersion ? ` (v${version})` : ""}`; + }, onRequest: {}, // Maps URLPatterns to dynamic callback functions that run on a request from a client. @@ -672,15 +681,26 @@ class EleventyDevServer { this._server.on("listening", (e) => { this.setupReloadNotifier(); - let hostsStr = ""; + let logMessageCallback = typeof this.options.messageOnStart === "function" ? this.options.messageOnStart : () => false; + let hosts = new Set(); if(this.options.showAllHosts) { - // TODO what happens when the cert doesn’t cover non-localhost hosts? - let hosts = devip().map(host => `${this.getServerUrl(host)} or`); - hostsStr = hosts.join(" ") + " "; + for(let host of devip()) { + hosts.add(this.getServerUrl(host)); + } } + hosts.add(this.getServerUrl("localhost")); + + let message = logMessageCallback({ + hosts: Array.from(hosts), + localhostUrl: this.getServerUrl("localhost"), + options: this.options, + version: pkg.version, + startupTime: Date.now() - this.start, + }); - let startBenchmark = ""; // this.start ? ` ready in ${Date.now() - this.start}ms` : ""; - this.logger.info(`Server at ${hostsStr}${this.getServerUrl("localhost")}${this.options.showVersion ? ` (v${pkg.version})` : ""}${startBenchmark}`); + if(message) { + this.logger.info(message); + } }); return this._server;