Skip to main content

@tlyboy/message

šŸ’¬ Labeled terminal messages with zero dependencies

CategoryStack
LanguageTypeScript
Stylingnode:util styleText
Buildtsdown
TestVitest
Lintoxlint, oxfmt

Install#

sh
npm install @tlyboy/message

Requires Node.js 22.13 or later. Published as ESM only.

Usage#

ts
// ESMimport { message } from '@tlyboy/message'
// CommonJS (require(esm) is supported since Node.js 22.12)const { message } = require('@tlyboy/message')
ts
message.info('info')message.success('success')message.error('error')message.warning('warning')
// Each argument is printed on its own linemessage.info('first', 'second')

Custom labels#

message.defaults holds the labels and is read on every call. Set it once, for example at your app's entry, and keep using message.xxx() everywhere. A string is printed as is, so you can pass an emoji or any text. Levels you leave out keep the built-in label.

ts
message.defaults.info = 'šŸ’¬'message.defaults.success = 'āœ…'message.defaults.warning = { text: 'č­¦å‘Š' } // keeps the built-in yellow badgemessage.defaults.error = { text: 'ERR', format: ['white', 'bgMagenta'] } // any styleText format
message.success('done') // āœ… done
  • Pass format: null to print the text without styling.
  • Delete a level or set it to undefined to restore the built-in label. message.defaults = {} resets all of them.
  • message.defaults applies to the whole process, so set it in apps and CLIs. Libraries should use message.create() instead.

Instances#

message.create() returns a new instance whose defaults are a copy of the current message.defaults merged with the given config. After that, changes on either side don't affect the other.

ts
const log = message.create({ info: 'šŸ“¦' })
log.info('installing') // šŸ“¦ installingmessage.info('hi') //  INFO  hi
log.defaults.error = 'šŸ’„' // only affects log

Instances can create instances too: log.create().

Theme file#

End users can theme every CLI built on this package with a JSON file at ~/.config/tlyboy/message.json (or $XDG_CONFIG_HOME/tlyboy/message.json). It takes the same labels as message.defaults:

json
{  "info": "šŸ’¬",  "success": "āœ…",  "error": { "text": "失蓄", "format": ["white", "bgRed"] }}
  • The theme only replaces built-in labels. Labels set in code with message.defaults or message.create() still win.
  • The file is read once, on the first message. A missing or broken file, or an invalid entry, is ignored.
  • Set TLYBOY_MESSAGE_CONFIG to use another file, or to false to turn the theme off.

info and success are written to stdout, error and warning to stderr, so errors stay visible when stdout is redirected. Colors follow NO_COLOR, FORCE_COLOR and whether each stream is a TTY.

Development#

sh
pnpm installpnpm buildpnpm testpnpm lintpnpm formatpnpm typecheck

Release#

sh
pnpm release

bumpp bumps the version, commits, tags and pushes. The tag triggers the release workflow, which stages the package on npm with trusted publishing (provenance included) and creates a GitHub release with changelogithub. The version goes live after a maintainer approves it with 2FA on the package's Staged Packages tab on npmjs.com, or with npm stage approve <stage-id>.

License#

MIT Ā© tlyboy