Skip to content

Commit

Permalink
Adds support for messageOnStart callback to customize startup log m…
Browse files Browse the repository at this point in the history
…essage. Fixes #79
  • Loading branch information
zachleat committed Jun 4, 2024
1 parent 5ea1b54 commit b4551f1
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b4551f1

Please sign in to comment.