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
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–56why, one step back1/**2 * @module3 * Color utility.4 */56/**7 * Get whether color change on terminal is enabled or disabled.8 * If `NO_COLOR` environment variable is set, this function returns `false`.9 * Unlike getColorEnabledAsync(), this cannot check Cloudflare environment variables.10 * @see {@link https://no-color.org/}11 *12 * @returns {boolean}13 */14export function getColorEnabled(): boolean {15 // eslint-disable-next-line @typescript-eslint/no-explicit-any16 const { process, Deno } = globalThis as any1718 const isNoColor =19 typeof Deno?.noColor === 'boolean'20 ? (Deno.noColor as boolean)21 : process !== undefined22 ? // eslint-disable-next-line no-unsafe-optional-chaining23 'NO_COLOR' in process?.env24 : false2526 return !isNoColor27}2829/**30 * Get whether color change on terminal is enabled or disabled.31 * If `NO_COLOR` environment variable is set, this function returns `false`.32 * @see {@link https://no-color.org/}33 *34 * @returns {boolean}35 */36export async function getColorEnabledAsync(): Promise<boolean> {37 // eslint-disable-next-line @typescript-eslint/no-explicit-any38 const { navigator } = globalThis as any39 // Avoid analysis of cloudflare scheme by bundlers40 const cfWorkers = 'cloudflare:workers'4142 const isNoColor =43 navigator !== undefined && navigator.userAgent === 'Cloudflare-Workers'44 ? await (async () => {45 try {46 // eslint-disable-next-line @typescript-eslint/ban-ts-comment47 // @ts-ignore48 return 'NO_COLOR' in ((await import(cfWorkers)).env ?? {}) // {} is for backward compat49 } catch {50 return false51 }52 })()53 : !getColorEnabled()5455 return !isNoColor56}