From 650b9b12c1e00ed42b0d5b4caac42a6cbc5ed0a1 Mon Sep 17 00:00:00 2001 From: Pranav Karawale <52596591+obnoxiousnerd@users.noreply.github.com> Date: Wed, 29 Sep 2021 23:37:12 +0530 Subject: [PATCH] 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 --- .changeset/hungry-wolves-shave.md | 5 +++ packages/astro/src/logger.ts | 51 ++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 .changeset/hungry-wolves-shave.md diff --git a/.changeset/hungry-wolves-shave.md b/.changeset/hungry-wolves-shave.md new file mode 100644 index 000000000..ca925661c --- /dev/null +++ b/.changeset/hungry-wolves-shave.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix logger locale parsing diff --git a/packages/astro/src/logger.ts b/packages/astro/src/logger.ts index a8c224db4..d9475fe6e 100644 --- a/packages/astro/src/logger.ts +++ b/packages/astro/src/logger.ts @@ -9,7 +9,21 @@ type ConsoleStream = Writable & { 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({ objectMode: true, @@ -74,7 +88,12 @@ export const levels: Record = { }; /** Full logging API */ -export function log(opts: LogOptions = {}, level: LoggerLevel, type: string | null, ...args: Array) { +export function log( + opts: LogOptions = {}, + level: LoggerLevel, + type: string | null, + ...args: Array +) { const logLevel = opts.level ?? defaultLogOptions.level; const dest = opts.dest ?? defaultLogOptions.dest; 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 */ -export function debug(opts: LogOptions, type: string | null, ...messages: Array) { +export function debug( + opts: LogOptions, + type: string | null, + ...messages: Array +) { return log(opts, 'debug', type, ...messages); } /** Emit a general info message (be careful using this too much!) */ -export function info(opts: LogOptions, type: string | null, ...messages: Array) { +export function info( + opts: LogOptions, + type: string | null, + ...messages: Array +) { return log(opts, 'info', type, ...messages); } /** Emit a warning a user should be aware of */ -export function warn(opts: LogOptions, type: string | null, ...messages: Array) { +export function warn( + opts: LogOptions, + type: string | null, + ...messages: Array +) { return log(opts, 'warn', type, ...messages); } /** Emit a fatal error message the user should address. */ -export function error(opts: LogOptions, type: string | null, ...messages: Array) { +export function error( + opts: LogOptions, + type: string | null, + ...messages: Array +) { return log(opts, 'error', type, ...messages); } @@ -139,7 +174,9 @@ export function parseError(opts: LogOptions, err: CompileError) { opts, '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}`))} ${frame} `