Skip to content

Commit 83aaab3

Browse files
committed
Adds messageOnClose callback option to add custom message when server closes
1 parent 4739161 commit 83aaab3

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

server.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ const DEFAULT_OPTIONS = {
4949

5050
return `Server${hostsStr}${options.showVersion ? ` (v${version})` : ""}`;
5151
},
52-
52+
messageOnClose() {
53+
return `Server closed.`;
54+
},
5355
onRequest: {}, // Maps URLPatterns to dynamic callback functions that run on a request from a client.
5456

5557
// Example:
@@ -175,6 +177,7 @@ export default class EleventyDevServer {
175177
}
176178

177179
debug("Watching files: %O", this.options.watch);
180+
178181
// TODO if using Eleventy and `watch` option includes output folder (_site) this will trigger two update events!
179182
this.#watcher = chokidar.watch(this.options.watch, {
180183
// TODO allow chokidar configuration extensions (or re-use the ones in Eleventy)
@@ -199,6 +202,11 @@ export default class EleventyDevServer {
199202
this.reloadFiles([path]);
200203
});
201204

205+
this.#watcher.on("unlink", (path) => {
206+
this.logger.log( `File deleted: ${path} (skips build)` );
207+
this.reloadFiles([path]);
208+
});
209+
202210
return this.#watcher;
203211
}
204212

@@ -807,21 +815,7 @@ export default class EleventyDevServer {
807815

808816
this._server.on("listening", (e) => {
809817
this.setupReloadNotifier();
810-
811-
let logMessageCallback = typeof this.options.messageOnStart === "function" ? this.options.messageOnStart : () => false;
812-
let hosts = this.getHosts();
813-
let message = logMessageCallback({
814-
hosts,
815-
localhostUrl: this.getServerUrl("localhost"),
816-
options: this.options,
817-
version: pkg.version,
818-
startupTime: Date.now() - this.start,
819-
});
820-
821-
if(message) {
822-
this.logger.info(message);
823-
}
824-
818+
this.logStartMessage();
825819
this.#readyResolve();
826820
});
827821

@@ -974,13 +968,42 @@ export default class EleventyDevServer {
974968
this.#serverClosing = Promise.all(promises).then(() => {
975969
this.#serverState = "CLOSED";
976970
this.#serverClosing = undefined;
971+
this.logCloseMessage();
977972
});
978973

979974
this.#serverState = "CLOSING";
980975

981976
return this.#serverClosing;
982977
}
983978

979+
logStartMessage() {
980+
let logMessageCallback = typeof this.options.messageOnStart === "function" ? this.options.messageOnStart : () => false;
981+
let hosts = this.getHosts();
982+
let message = logMessageCallback({
983+
hosts,
984+
localhostUrl: this.getServerUrl("localhost"),
985+
options: this.options,
986+
version: pkg.version,
987+
startupTime: Date.now() - this.start,
988+
});
989+
990+
if(message && typeof this.logger?.info === "function") {
991+
this.logger.info(message);
992+
}
993+
}
994+
995+
logCloseMessage() {
996+
let logMessageCallback = typeof this.options.messageOnClose === "function" ? this.options.messageOnClose : () => false;
997+
let message = logMessageCallback({
998+
options: this.options,
999+
version: pkg.version,
1000+
});
1001+
1002+
if(message && typeof this.logger?.info === "function") {
1003+
this.logger.info(message);
1004+
}
1005+
}
1006+
9841007
sendError({ error }) {
9851008
this.sendUpdateNotification({
9861009
type: "eleventy.error",

0 commit comments

Comments
 (0)