diff --git a/deno_dist/helper/dev/index.ts b/deno_dist/helper/dev/index.ts index 22ef29be8..c32683be7 100644 --- a/deno_dist/helper/dev/index.ts +++ b/deno_dist/helper/dev/index.ts @@ -39,6 +39,16 @@ export const inspectRoutes = (hono: Hono): RouteData[] => { } export const showRoutes = (hono: Hono, opts?: ShowRoutesOptions) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const { process, Deno } = globalThis as any + const isNoColor = + typeof process !== 'undefined' + ? // eslint-disable-next-line no-unsafe-optional-chaining + 'NO_COLOR' in process?.env + : typeof Deno?.noColor === 'boolean' + ? (Deno.noColor as boolean) + : false + const colorEnabled = opts?.colorize ?? !isNoColor const routeData: Record = {} let maxMethodLength = 0 let maxPathLength = 0 @@ -61,7 +71,7 @@ export const showRoutes = (hono: Hono, opts?: ShowRoutesOption } const { method, path, routes } = data - const methodStr = opts?.colorize ?? true ? `\x1b[32m${method}\x1b[0m` : method + const methodStr = colorEnabled ? `\x1b[32m${method}\x1b[0m` : method console.log(`${methodStr} ${' '.repeat(maxMethodLength - method.length)} ${path}`) if (!opts?.verbose) { diff --git a/src/helper/dev/index.test.ts b/src/helper/dev/index.test.ts index ba3bbb6f2..54bb2071e 100644 --- a/src/helper/dev/index.test.ts +++ b/src/helper/dev/index.test.ts @@ -126,6 +126,51 @@ describe('showRoutes()', () => { }) }) +describe('showRoutes() in NO_COLOR', () => { + let logs: string[] = [] + + let originalLog: typeof console.log + beforeAll(() => { + vi.stubEnv('NO_COLOR', '1') + originalLog = console.log + console.log = (...args) => logs.push(...args) + }) + afterAll(() => { + vi.unstubAllEnvs() + console.log = originalLog + }) + + beforeEach(() => { + logs = [] + }) + it('should render not colorized output', async () => { + showRoutes(app) + expect(logs).toEqual([ + 'GET /', + 'GET /named', + 'POST /', + 'PUT /', + 'PATCH /', + 'DELETE /', + 'OPTIONS /', + 'GET /static', + ]) + }) + it('should render colorized output if colorize: true', async () => { + showRoutes(app, { colorize: true }) + expect(logs).toEqual([ + '\x1b[32mGET\x1b[0m /', + '\x1b[32mGET\x1b[0m /named', + '\x1b[32mPOST\x1b[0m /', + '\x1b[32mPUT\x1b[0m /', + '\x1b[32mPATCH\x1b[0m /', + '\x1b[32mDELETE\x1b[0m /', + '\x1b[32mOPTIONS\x1b[0m /', + '\x1b[32mGET\x1b[0m /static', + ]) + }) +}) + describe('geRouterName()', () => { it('Should return the correct router name', async () => { const app = new Hono({ diff --git a/src/helper/dev/index.ts b/src/helper/dev/index.ts index ed07a5927..ed8fa90a9 100644 --- a/src/helper/dev/index.ts +++ b/src/helper/dev/index.ts @@ -39,6 +39,16 @@ export const inspectRoutes = (hono: Hono): RouteData[] => { } export const showRoutes = (hono: Hono, opts?: ShowRoutesOptions) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const { process, Deno } = globalThis as any + const isNoColor = + typeof process !== 'undefined' + ? // eslint-disable-next-line no-unsafe-optional-chaining + 'NO_COLOR' in process?.env + : typeof Deno?.noColor === 'boolean' + ? (Deno.noColor as boolean) + : false + const colorEnabled = opts?.colorize ?? !isNoColor const routeData: Record = {} let maxMethodLength = 0 let maxPathLength = 0 @@ -61,7 +71,7 @@ export const showRoutes = (hono: Hono, opts?: ShowRoutesOption } const { method, path, routes } = data - const methodStr = opts?.colorize ?? true ? `\x1b[32m${method}\x1b[0m` : method + const methodStr = colorEnabled ? `\x1b[32m${method}\x1b[0m` : method console.log(`${methodStr} ${' '.repeat(maxMethodLength - method.length)} ${path}`) if (!opts?.verbose) {