-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a verbose flag to create-astro (#2429)
* Add verbose logging to create-astro * Tell user to use the verbose flag when encountering the MISSING_REF error * Create changeset * Add information in README * Format * Use switch statement instead of if and if else blocks * Update packages/create-astro/src/logger.ts Make if statement easier to read Co-authored-by: Evan Boehs <evan@boehs.org> Co-authored-by: Evan Boehs <evan@boehs.org>
- Loading branch information
Showing
4 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'create-astro': patch | ||
--- | ||
|
||
Added an option to create-astro to use verbose logging which should help debug degit issues |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { bold, blue, dim, red, yellow } from 'kleur/colors'; | ||
import { Writable } from 'stream'; | ||
import { format as utilFormat } from 'util'; | ||
|
||
type ConsoleStream = Writable & { | ||
fd: 1 | 2; | ||
}; | ||
|
||
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, | ||
write(event: LogMessage, _, callback) { | ||
let dest: ConsoleStream = process.stderr; | ||
if (levels[event.level] < levels['error']) dest = process.stdout; | ||
|
||
dest.write(dim(dt.format(new Date()) + ' ')); | ||
|
||
let type = event.type; | ||
if (type) { | ||
switch (event.level) { | ||
case 'info': | ||
type = bold(blue(type)); | ||
break; | ||
case 'warn': | ||
type = bold(yellow(type)); | ||
break; | ||
case 'error': | ||
type = bold(red(type)); | ||
break; | ||
} | ||
|
||
dest.write(`[${type}] `); | ||
} | ||
|
||
dest.write(utilFormat(...event.args)); | ||
dest.write('\n'); | ||
|
||
callback(); | ||
}, | ||
}); | ||
|
||
interface LogWritable<T> extends Writable { | ||
write: (chunk: T) => boolean; | ||
} | ||
|
||
export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; // same as Pino | ||
export type LoggerEvent = 'debug' | 'info' | 'warn' | 'error'; | ||
|
||
export let defaultLogLevel: LoggerLevel; | ||
if (process.argv.includes('--verbose')) { | ||
defaultLogLevel = 'debug'; | ||
} else if (process.argv.includes('--silent')) { | ||
defaultLogLevel = 'silent'; | ||
} else { | ||
defaultLogLevel = 'info'; | ||
} | ||
|
||
export interface LogOptions { | ||
dest?: LogWritable<LogMessage>; | ||
level?: LoggerLevel; | ||
} | ||
|
||
export const defaultLogOptions: Required<LogOptions> = { | ||
dest: defaultLogDestination, | ||
level: defaultLogLevel, | ||
}; | ||
|
||
export interface LogMessage { | ||
type: string | null; | ||
level: LoggerLevel; | ||
message: string; | ||
args: Array<any>; | ||
} | ||
|
||
export const levels: Record<LoggerLevel, number> = { | ||
debug: 20, | ||
info: 30, | ||
warn: 40, | ||
error: 50, | ||
silent: 90, | ||
}; | ||
|
||
/** Full logging API */ | ||
export function log(opts: LogOptions = {}, level: LoggerLevel, type: string | null, ...args: Array<any>) { | ||
const logLevel = opts.level ?? defaultLogOptions.level; | ||
const dest = opts.dest ?? defaultLogOptions.dest; | ||
const event: LogMessage = { | ||
type, | ||
level, | ||
args, | ||
message: '', | ||
}; | ||
|
||
// test if this level is enabled or not | ||
if (levels[logLevel] > levels[level]) { | ||
return; // do nothing | ||
} | ||
|
||
dest.write(event); | ||
} | ||
|
||
/** Emit a message only shown in debug mode */ | ||
export function debug(opts: LogOptions, type: string | null, ...messages: Array<any>) { | ||
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<any>) { | ||
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<any>) { | ||
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<any>) { | ||
return log(opts, 'error', type, ...messages); | ||
} | ||
|
||
// A default logger for when too lazy to pass LogOptions around. | ||
export const logger = { | ||
debug: debug.bind(null, defaultLogOptions, 'debug'), | ||
info: info.bind(null, defaultLogOptions, 'info'), | ||
warn: warn.bind(null, defaultLogOptions, 'warn'), | ||
error: error.bind(null, defaultLogOptions, 'error'), | ||
}; |