Skip to content

Commit

Permalink
simplify logging a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
schlawg committed Jan 19, 2025
1 parent 675028b commit 9d04daa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
17 changes: 6 additions & 11 deletions ui/.build/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const env = new (class {
exitCode: Map<Builder, number | false> = new Map();
startTime: number | undefined = Date.now();
logTime = true;
logContext = true;
logCtx = true;
logColor = true;

packages: Map<string, Package> = new Map();
Expand Down Expand Up @@ -110,18 +110,13 @@ export const env = new (class {
? d.join('\n')
: JSON.stringify(d);

if (!this.logColor) text = stripColorEscapes(text);

const prefix = (
(this.logTime === false ? '' : prettyTime()) +
(!ctx || !this.logContext ? '' : `[${escape(ctx, colorForCtx(ctx))}] `)
(this.logTime ? prettyTime() : '') + (ctx && this.logCtx ? `[${escape(ctx, colorForCtx(ctx))}]` : '')
).trim();

lines(text).forEach(line =>
lines(this.logColor ? text : stripColorEscapes(text)).forEach(line =>
console.log(
`${prefix ? prefix + ' - ' : ''}${
error ? escape(line, codes.error) : warn ? escape(line, codes.warn) : line
}`,
`${prefix ? prefix + ' - ' : ''}${escape(line, error ? codes.error : warn ? codes.warn : undefined)}`,
),
);
}
Expand All @@ -146,8 +141,8 @@ export const env = new (class {

export const lines = (s: string): string[] => s.split(/[\n\r\f]+/).filter(x => x.trim());

const escape = (text: string, code: string): string =>
env.logColor ? `\x1b[${code}m${stripColorEscapes(text)}\x1b[0m` : text;
const escape = (text: string, code?: string): string =>
env.logColor && code ? `\x1b[${code}m${stripColorEscapes(text)}\x1b[0m` : text;

const colorLines = (text: string, code: string) =>
lines(text)
Expand Down
4 changes: 2 additions & 2 deletions ui/.build/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ if (['--tsc', '--sass', '--esbuild', '--sync', '--i18n'].filter(x => argv.includ
env.i18n = argv.includes('--i18n');
env.sync = argv.includes('--sync');
}
if (argv.includes('--no-color')) env.color = undefined;

env.logTime = !argv.includes('--no-time');
env.logContext = !argv.includes('--no-context');
env.logCtx = !argv.includes('--no-context');
env.logColor = !argv.includes('--no-color');
env.watch = argv.includes('--watch') || oneDashArgs.includes('w');
env.prod = argv.includes('--prod') || oneDashArgs.includes('p');
env.debug = argv.includes('--debug') || oneDashArgs.includes('d');
Expand Down

0 comments on commit 9d04daa

Please sign in to comment.