Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { release } from 'node:os';
import { platform } from 'node:process';
import { type Writable } from 'node:stream';
import { inspect } from 'node:util';
import { inspect, styleText } from 'node:util';

import type { ResMetaData } from './types.d.ts';
import { clamp, fwdSlash, getEnv, getRuntime, trimSlash, withResolvers } from './utils.ts';
Expand Down Expand Up @@ -32,15 +32,21 @@ export class ColorUtils {
return parts.join('');
}
const formats = format.split(',');
return parts
.map((part, index) => (formats[index] ? this.style(part, formats[index]) : part))
.join('');
return parts.map((part, i) => this.style(part, formats[i])).join('');
};

style = (text: string, format: string = ''): string => {
if (!this.enabled) return text;
return styleText(format.trim().split(/\s+/g), text);
if (!format || !this.enabled) return text;
return styleText(this.#stFormat(format), text, { validateStream: false });
};

#stFormat(format: string): Parameters<typeof styleText>[0] {
const fmt = format
.trim()
.split(/\s+/g)
.filter((s) => Object.hasOwn(inspect.colors, s));
return fmt as any[];
}
}

export class Logger {
Expand Down Expand Up @@ -155,21 +161,6 @@ function pathSuffix(urlPath: string, localPath: string): [string, string] | unde
}
}

/**
Basic implementation of 'node:util' styleText to support Node 18 + Deno.
*/
function styleText(format: string | string[], text: string): string {
let before = '';
let after = '';
for (const style of Array.isArray(format) ? format : [format]) {
const codes = inspect.colors[style.trim()];
if (!codes) continue;
before = `${before}\x1b[${codes[0]}m`;
after = `\x1b[${codes[1]}m${after}`;
}
return `${before}${text}${after}`;
}

function supportsColor(): boolean {
// Avoid reading env variables in Deno to limit prompts;
// instead rely on its built-in parsing of the NO_COLOR env variable
Expand Down