Skip to content

Commit

Permalink
feat: support positionals in node
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Apr 12, 2023
1 parent d9d315b commit 96826ba
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"webpack-http-server": "^0.5.0"
},
"dependencies": {
"is-node-process": "^1.2.0"
"is-node-process": "^1.2.0",
"outvariant": "^1.4.0"
}
}
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isNodeProcess } from 'is-node-process'
import { format } from 'outvariant'
import * as colors from './colors'

const IS_NODE = isNodeProcess()
Expand Down Expand Up @@ -259,7 +260,7 @@ const noop = () => void 0

function log(message: string, ...positionals: Array<unknown>): void {
if (IS_NODE) {
process.stdout.write(message + '\n')
process.stdout.write(format(message, ...positionals) + '\n')
return
}

Expand All @@ -268,7 +269,7 @@ function log(message: string, ...positionals: Array<unknown>): void {

function warn(message: string, ...positionals: Array<unknown>): void {
if (IS_NODE) {
process.stderr.write(message + '\n')
process.stderr.write(format(message, ...positionals) + '\n')
return
}

Expand All @@ -277,7 +278,7 @@ function warn(message: string, ...positionals: Array<unknown>): void {

function error(message: string, ...positionals: Array<unknown>): void {
if (IS_NODE) {
process.stderr.write(message + '\n')
process.stderr.write(format(message, ...positionals) + '\n')
return
}

Expand Down
33 changes: 33 additions & 0 deletions test/node/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,36 @@ it('prints an error message', () => {
`${colors.red('12:34:56:789')} ${colors.red('✖ [parser]')} oops\n`
)
})

it('supports positionals', () => {
logger.debug('hello %s', 'world')
expect(process.stdout.write).toHaveBeenCalledWith(
`${colors.gray('12:34:56:789')} ${colors.gray('[parser]')} ${colors.gray(
'hello world'
)}\n`
)

logger.info('hello %s', 'world')
expect(process.stdout.write).toHaveBeenCalledWith(
`${colors.gray('12:34:56:789')} ${colors.blue('[parser]')} hello world\n`
)

logger.success('hello %s', 'world')
expect(process.stdout.write).toHaveBeenCalledWith(
`${colors.green('12:34:56:789')} ${colors.green(
'✔ [parser]'
)} hello world\n`
)

logger.warning('hello %s', 'world')
expect(process.stderr.write).toHaveBeenCalledWith(
`${colors.yellow('12:34:56:789')} ${colors.yellow(
'⚠ [parser]'
)} hello world\n`
)

logger.error('hello %s', 'world')
expect(process.stderr.write).toHaveBeenCalledWith(
`${colors.red('12:34:56:789')} ${colors.red('✖ [parser]')} hello world\n`
)
})

0 comments on commit 96826ba

Please sign in to comment.