Skip to content

Commit

Permalink
feat(dev): support NO_COLOR (#2007)
Browse files Browse the repository at this point in the history
* feat(dev): support `NO_COLOR`

* fmt

* remove unused Bun

* follow informal standard

https://no-color.org/

* fmt

* evade denoify bug

When removing optional-chaining, denoify creates node:node:process, so I disable the eslint error by a comment.

* add test

* fix typo

* fix test
  • Loading branch information
ryuapp authored Jan 23, 2024
1 parent c02b83b commit 39aa582
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
12 changes: 11 additions & 1 deletion deno_dist/helper/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => {
}

export const showRoutes = <E extends Env>(hono: Hono<E>, 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<string, RouteData[]> = {}
let maxMethodLength = 0
let maxPathLength = 0
Expand All @@ -61,7 +71,7 @@ export const showRoutes = <E extends Env>(hono: Hono<E>, 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) {
Expand Down
45 changes: 45 additions & 0 deletions src/helper/dev/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
12 changes: 11 additions & 1 deletion src/helper/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => {
}

export const showRoutes = <E extends Env>(hono: Hono<E>, 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<string, RouteData[]> = {}
let maxMethodLength = 0
let maxPathLength = 0
Expand All @@ -61,7 +71,7 @@ export const showRoutes = <E extends Env>(hono: Hono<E>, 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) {
Expand Down

0 comments on commit 39aa582

Please sign in to comment.