Skip to content

Commit 0973a59

Browse files
committed
build: better tree-shaking cli colors
1 parent 517491b commit 0973a59

File tree

5 files changed

+39
-63
lines changed

5 files changed

+39
-63
lines changed

src/_color.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Colors support for terminal output
2+
const noColor = /* @__PURE__ */ (() => {
3+
const env = globalThis.process?.env;
4+
return env.NO_COLOR === "1" || env.TERM === "dumb";
5+
})();
6+
7+
const _c =
8+
(c: number, r: number = 39) =>
9+
(t: string) =>
10+
noColor ? t : `\u001B[${c}m${t}\u001B[${r}m`;
11+
12+
type ColorType = (text: string) => string;
13+
14+
export const bold: ColorType = /* @__PURE__ */ _c(1, 22);
15+
export const red: ColorType = /* @__PURE__ */ _c(31);
16+
export const green: ColorType = /* @__PURE__ */ _c(32);
17+
export const yellow: ColorType = /* @__PURE__ */ _c(33);
18+
export const blue: ColorType = /* @__PURE__ */ _c(34);
19+
export const magenta: ColorType = /* @__PURE__ */ _c(35);
20+
export const cyan: ColorType = /* @__PURE__ */ _c(36);
21+
export const gray: ColorType = /* @__PURE__ */ _c(90);
22+
23+
export const url: (title: string, url: string) => string = (title, url) =>
24+
noColor
25+
? `[${title}](${url})`
26+
: `\u001B]8;;${url}\u001B\\${title}\u001B]8;;\u001B\\`;

src/_plugins.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Colors } from "./_utils.cli.ts";
1+
import * as c from "./_color.ts";
22
import type { ServerPlugin } from "./types.ts";
33

44
export const errorPlugin: ServerPlugin = (server) => {
@@ -38,7 +38,7 @@ export const gracefulShutdownPlugin: ServerPlugin = (server) => {
3838
if (isShuttingDown) return;
3939
isShuttingDown = true;
4040
console.log(
41-
Colors.gray(
41+
c.gray(
4242
`\nShutting down server... (timeout in ${gracefulShutdown}+${forceShutdown}s)`,
4343
),
4444
);
@@ -47,20 +47,20 @@ export const gracefulShutdownPlugin: ServerPlugin = (server) => {
4747
// Graceful shutdown
4848
server.close().finally(() => {
4949
clearTimeout(timeout);
50-
console.log(Colors.green("Server closed all connections."));
50+
console.log(c.green("Server closed all connections."));
5151
}),
5252
new Promise<void>((resolve) => {
5353
timeout = setTimeout(() => {
5454
// Graceful shutdown timeout
5555
console.warn(
56-
Colors.yellow(
56+
c.yellow(
5757
`Forcing closing connections to exit... (timeout in ${forceShutdown}s)`,
5858
),
5959
);
6060
timeout = setTimeout(() => {
6161
// Force shutdown timeout
6262
console.error(
63-
Colors.red("Could not close connections in time, force exiting."),
63+
c.red("Could not close connections in time, force exiting."),
6464
);
6565
resolve();
6666
}, 1000);

src/_utils.cli.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as nodeHTTP from "node:http";
1111
import { dirname, extname, relative, resolve } from "node:path";
1212
import { fork } from "node:child_process";
1313
import { existsSync } from "node:fs";
14-
import { Colors as c } from "./_utils.cli.ts";
14+
import * as c from "./_color.ts";
1515

1616
// prettier-ignore
1717
const defaultEntries = ["server", "index", "src/server", "src/index", "server/index"];

src/log.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { Colors as c } from "./_utils.cli.ts";
1+
import * as c from "./_color.ts";
22
import type { ServerMiddleware } from "./types.ts";
33

44
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
55
export interface LogOptions {}
66

7-
const statusColors = { 1: "blue", 2: "green", 3: "yellow" } as Record<
8-
number,
9-
"blue" | "green" | "yellow"
10-
>;
7+
const statusColors = { 1: c.blue, 2: c.green, 3: c.yellow } as const;
118

129
export const log = (_options: LogOptions = {}): ServerMiddleware => {
1310
return async (req, next) => {
1411
const start = performance.now();
1512
const res = await next();
1613
const duration = performance.now() - start;
17-
const statusColor = statusColors[Math.floor(res.status / 100)] || "red";
14+
const statusColor =
15+
statusColors[
16+
Math.floor(res.status / 100) as unknown as keyof typeof statusColors
17+
] || c.red;
1818
console.log(
19-
`${c.gray(`[${new Date().toLocaleTimeString()}]`)} ${c.bold(req.method)} ${c.blue(req.url)} [${c[statusColor](res.status + "")}] ${c.gray(`(${duration.toFixed(2)}ms)`)}`,
19+
`${c.gray(`[${new Date().toLocaleTimeString()}]`)} ${c.bold(req.method)} ${c.blue(req.url)} [${statusColor(res.status + "")}] ${c.gray(`(${duration.toFixed(2)}ms)`)}`,
2020
);
2121
return res;
2222
};

0 commit comments

Comments
 (0)