color.ts
This file
Path
src/utils/color.ts
56 lines 路 1.7 KiB 路 TypeScript
Last changed by
Not known.
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657/** * @module * Color utility. */
/** * Get whether color change on terminal is enabled or disabled. * If `NO_COLOR` environment variable is set, this function returns `false`. * Unlike getColorEnabledAsync(), this cannot check Cloudflare environment variables. * @see {@link https://no-color.org/} * * @returns {boolean} */export function getColorEnabled(): boolean { // eslint-disable-next-line @typescript-eslint/no-explicit-any const { process, Deno } = globalThis as any
const isNoColor = typeof Deno?.noColor === 'boolean' ? (Deno.noColor as boolean) : process !== undefined ? // eslint-disable-next-line no-unsafe-optional-chaining 'NO_COLOR' in process?.env : false
return !isNoColor}
/** * Get whether color change on terminal is enabled or disabled. * If `NO_COLOR` environment variable is set, this function returns `false`. * @see {@link https://no-color.org/} * * @returns {boolean} */export async function getColorEnabledAsync(): Promise<boolean> { // eslint-disable-next-line @typescript-eslint/no-explicit-any const { navigator } = globalThis as any // Avoid analysis of cloudflare scheme by bundlers const cfWorkers = 'cloudflare:workers'
const isNoColor = navigator !== undefined && navigator.userAgent === 'Cloudflare-Workers' ? await (async () => { try { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return 'NO_COLOR' in ((await import(cfWorkers)).env ?? {}) // {} is for backward compat } catch { return false } })() : !getColorEnabled()
return !isNoColor}