Fix logger locale parsing (#1439)

* fix(logger): locale parsing
* Fixed issue of compiler crash when "c" locale was encountered
* Return default locale if parsed locale is less than 2 chars long

* chore: add changeset
This commit is contained in:
Pranav Karawale 2021-09-29 23:37:12 +05:30 committed by Matthew Phillips
parent 863bf58c46
commit 650b9b12c1
2 changed files with 49 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix logger locale parsing

View file

@ -9,7 +9,21 @@ type ConsoleStream = Writable & {
fd: 1 | 2; fd: 1 | 2;
}; };
const dt = new Intl.DateTimeFormat(process.env.LANG ? process.env.LANG.split('.')[0].replace(/_/g, '-') : 'en-US', { hour: '2-digit', minute: '2-digit' }); function getLoggerLocale(): string {
const defaultLocale = 'en-US';
if (process.env.LANG) {
const extractedLocale = process.env.LANG.split('.')[0].replace(/_/g, '-');
// Check if language code is atleast two characters long (ie. en, es).
// NOTE: if "c" locale is encountered, the default locale will be returned.
if (extractedLocale.length < 2) return defaultLocale;
else return extractedLocale;
} else return defaultLocale;
}
const dt = new Intl.DateTimeFormat(getLoggerLocale(), {
hour: '2-digit',
minute: '2-digit',
});
export const defaultLogDestination = new Writable({ export const defaultLogDestination = new Writable({
objectMode: true, objectMode: true,
@ -74,7 +88,12 @@ export const levels: Record<LoggerLevel, number> = {
}; };
/** Full logging API */ /** Full logging API */
export function log(opts: LogOptions = {}, level: LoggerLevel, type: string | null, ...args: Array<any>) { export function log(
opts: LogOptions = {},
level: LoggerLevel,
type: string | null,
...args: Array<any>
) {
const logLevel = opts.level ?? defaultLogOptions.level; const logLevel = opts.level ?? defaultLogOptions.level;
const dest = opts.dest ?? defaultLogOptions.dest; const dest = opts.dest ?? defaultLogOptions.dest;
const event: LogMessage = { const event: LogMessage = {
@ -93,22 +112,38 @@ export function log(opts: LogOptions = {}, level: LoggerLevel, type: string | nu
} }
/** Emit a message only shown in debug mode */ /** Emit a message only shown in debug mode */
export function debug(opts: LogOptions, type: string | null, ...messages: Array<any>) { export function debug(
opts: LogOptions,
type: string | null,
...messages: Array<any>
) {
return log(opts, 'debug', type, ...messages); return log(opts, 'debug', type, ...messages);
} }
/** Emit a general info message (be careful using this too much!) */ /** Emit a general info message (be careful using this too much!) */
export function info(opts: LogOptions, type: string | null, ...messages: Array<any>) { export function info(
opts: LogOptions,
type: string | null,
...messages: Array<any>
) {
return log(opts, 'info', type, ...messages); return log(opts, 'info', type, ...messages);
} }
/** Emit a warning a user should be aware of */ /** Emit a warning a user should be aware of */
export function warn(opts: LogOptions, type: string | null, ...messages: Array<any>) { export function warn(
opts: LogOptions,
type: string | null,
...messages: Array<any>
) {
return log(opts, 'warn', type, ...messages); return log(opts, 'warn', type, ...messages);
} }
/** Emit a fatal error message the user should address. */ /** Emit a fatal error message the user should address. */
export function error(opts: LogOptions, type: string | null, ...messages: Array<any>) { export function error(
opts: LogOptions,
type: string | null,
...messages: Array<any>
) {
return log(opts, 'error', type, ...messages); return log(opts, 'error', type, ...messages);
} }
@ -139,7 +174,9 @@ export function parseError(opts: LogOptions, err: CompileError) {
opts, opts,
'parse-error', 'parse-error',
` `
${underline(bold(grey(`${err.filename || ''}:${err.start.line}:${err.start.column}`)))} ${underline(
bold(grey(`${err.filename || ''}:${err.start.line}:${err.start.column}`))
)}
${bold(red(`𝘅 ${err.message}`))} ${bold(red(`𝘅 ${err.message}`))}
${frame} ${frame}
` `