Repository: honojs/hono
File: src/utils/color.ts (TypeScript)

Code before:
 *
 * @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`.
 * Unlike getColorEnabled(), this can check Cloudflare environment variables.
 * @see {@link https://no-color.org/}
 *
 * @returns {boolean}
 */
export async function getColorEnabledAsync(): Promise<boolean> {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const { process, Deno, navigator } = 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
      : navigator.userAgent === 'Cloudflare-Workers'
      ? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        'NO_COLOR' in ((await import('cloudflare:workers')).env ?? {}) // ?? {} is for backward compat
      : false

  return !isNoColor
}


Review comment (suggestion):
The `getColorEnabled` has some similar logic, so how about the following implementation?

```diff
diff --git a/src/utils/color.ts b/src/utils/color.ts
index 65ea4626..cf228348 100644
--- a/src/utils/color.ts
+++ b/src/utils/color.ts
@@ -36,19 +36,14 @@ export function getColorEnabled(): boolean {
  */
 export async function getColorEnabledAsync(): Promise<boolean> {
   // eslint-disable-next-line @typescript-eslint/no-explicit-any
-  const { process, Deno, navigator } = globalThis as any
+  const { navigator } = 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
-      : navigator.userAgent === 'Cloudflare-Workers'
+    navigator.userAgent === 'Cloudflare-Workers'
       ? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
         // @ts-ignore
         'NO_COLOR' in ((await import('cloudflare:workers')).env ?? {}) // ?? {} is for backward compat
-      : false
+      : !getColorEnabled()

   return !isNoColor
 }
```

Code after:
 *
 * @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

  const isNoColor =
    navigator !== undefined && navigator.userAgent === 'Cloudflare-Workers'
      ? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        'NO_COLOR' in ((await import('cloudflare:workers')).env ?? {}) // ?? {} is for backward compat
      : !getColorEnabled()

  return !isNoColor
}