Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@poppinss/cliui
Advanced tools
Opinionated UI KIT for Command Line apps
CLI UI is an opinionated UI Kit to log messages, render tables, display spinners, and much more. Following are some of the reasons for creating this package.
Install the package from the npm registry as follows:
npm i @poppinss/cliui
# yarn lovers
yarn add @poppinss/cliui
Import the cliui
and create an instance of it.
import { cliui } from '@poppinss/cliui'
const ui = cliui()
ui.logger.info('This is an info message')
// [ info ] This is an info message
Now, let's say you are testing a command and want to assert that an info message is logged during the command's execution.
Usually, you will have to hack into the process.stdout
stream and collect messages within memory (for assertion) and strip any colors (aka ansi sequences) from the output.
However, with this package, you can turn on the raw
mode to collect the logger messages within memory and turn off all color transformations.
// RAW MODE ON
const ui = cliui({ raw: true })
ui.logger.info('This is an info message')
const logs = ui.logger.getRenderer().getLogs()
assert.deepEqual(logs, [
{
stream: 'stdout',
message: '[ cyan(info) ] This is an info message'
}
])
Similarly, you can assert that an error message is logged to stderr
.
const ui = cliui({ raw: true })
ui.logger.error('Something went wrong')
const logs = ui.logger.getRenderer().getLogs()
assert.deepEqual(logs, [
{
stream: 'stderr',
message: '[ red(error) ] Something went wrong'
}
])
The logger displays all the log messages with consistent styling. Following are the available logging methods.
import { cliui } from '@poppinss/cliui'
const ui = cliui()
// Writes to stdout
logger.debug('Something just happened')
logger.info('This is an info message')
logger.success('Account created')
logger.warning('Running out of disk space')
// Writes to stderr
logger.error(new Error('Unable to write. Disk full'))
logger.fatal(new Error('Unable to write. Disk full'))
You can also define the prefix
and suffix
for the log message. The prefix and suffix are displayed with lower opacity (the dim color transformation is used).
logger.info('Install packages', { suffix: 'npm i --production' })
You can display current time as a prefix using the %time%
keyword.
logger.info('Message with time prefix', { prefix: '%time%' })
You can display a log message with a loading animation using the logger.await
method. The method accepts the initial message to display alongside an optional prefix
or suffix
.
const loader = logger.await('installing packages', { suffix: 'npm i' })
// Start animation
loader.start()
// Update the message
loader.update('unpacking packages', { suffix: undefined })
// Stop loader
loader.stop()
You can also use the logger to just prepare the message (with colors and formatting) without writing it to the output stream. Just prefix the log message with prepare
and it will return a string value.
const debugMessage = logger.prepareDebug('Something just happened')
const infoMessage = logger.prepareInfo('This is an info message')
const successMessage = logger.prepareSuccess('Account created')
const warningMessage = logger.prepareWarning('Running out of disk space')
First, you must instantiate the cliui
in raw mode to collect all logs messages within memory. And then you can access the logs using logger.getRenderer()
to write assertions.
const ui = cliui({ raw: true })
ui.logger.info('Hello world')
const logs = ui.logger.getRenderer().getLogs()
console.log(logs)
Logger actions are pre-styled logs to display the outcome of an action. For example, the action can be to create/update or delete a file.
You can create an action by calling the logger.action
method and pass the message to display. Once, done perfoming the underlying operation, you can either mark the action as succeeded
, skipped
, or failed
.
logger
.action('Creating config/auth.ts')
.displayDuration()
.succeeded()
logger
.action('Updating .tsconfig.json')
.succeeded()
logger
.action('Creating app/Models/User.ts')
.skipped('File already exists')
logger
.action('Creating server.ts')
.failed(new Error('File already exists'))
You can create a table using the ui.table
method. Under the hood, we are using cli-table3 but only expose some of its configuration options for consistency.
const ui = cliui()
const table = ui.table()
table
.head([
'Migration',
'Duration',
'Status',
])
.row([
'1590591892626_tenants.ts',
'2ms',
'DONE'
])
.row([
'1590595949171_entities.ts',
'2ms',
'DONE'
])
.render()
You can apply color transforms to any value when rendering the table. For example:
table.row([
'1590595949171_entities.ts',
'2',
ui.colors.green('DONE')
])
You can right-align the columns by defining them as objects and using the hAlign
property. Also, make sure to align the header column right as well.
table
.head([
'Migration',
'Batch'
{
content: 'Status',
hAlign: 'right'
},
])
table.row([
'1590595949171_entities.ts',
'2',
{
content: ui.colors.green('DONE'),
hAlign: 'right'
}
])
You can render tables in full width (taking all the space of the terminal) by calling the table.fullWidth
method. In full-width mode:
table.fullWidth()
You can also change the column index for the fluid column (the one that takes all the space) by calling the table.fluidColumnIndex
method.
table
.fullWidth()
.fluidColumnIndex(1)
First, you must instantiate the cliui
in raw mode to collect all logs messages within memory. And then you can access the table output using logger.getRenderer()
to write assertions.
const ui = cliui({ raw: true })
const table = ui.table()
table
.head(['Migration','Duration', 'Status'])
.row([ '1590591892626_tenants.ts', '2ms', 'DONE'])
.render()
const logs = table.getRenderer().getLogs()
console.log(logs)
The instructions widget allows you to render a box with steps. Each step gets prefixed with an arrow >
.
const ui = cliui()
const instructions = ui.instructions()
instructions
.add(`cd ${colors.cyan('hello-world')}`)
.add(`Run ${colors.cyan('node ace serve --watch')} to start the server`)
.render()
The sticker widget is the same as the instructions
widget. But it does not prefix each line with an arrow >
.
const ui = cliui()
const sticker = ui.instructions()
sticker
.add('Started HTTP server')
.add('')
.add(`Local address: ${colors.cyan('http://localhost:3333')}`)
.add(`Network address: ${colors.cyan('http://192.168.1.2:3333')}`)
.render()
First, you must instantiate the cliui
in raw mode to collect all logs messages within memory. And then you can access the instructions/sticker output using logger.getRenderer()
to write assertions.
const ui = cliui({ raw: true })
const instructions = ui.instructions()
instructions
.add(`cd ${colors.cyan('hello-world')}`)
.add(`Run ${colors.cyan('node ace serve --watch')} to start the server`)
.render()
const logs = instructions.getRenderer().getLogs()
console.log(logs)
The tasks widget allows rendering a list of tasks to perform. Each task has an associated async callback to perform the task, report its progress, and also mark it as succeeded or failed.
task.error
method to prepare an error from a string value.const ui = cliui()
const tasks = ui.tasks()
await tasks
.add('clone repo', async (task) => {
return 'Completed'
})
.add('update package file', async (task) => {
return task.error('Unable to update package file')
})
.add('install dependencies', async (task) => {
return 'Installed'
})
.run()
Instead of writing the task progress messages to the console directly, you recommend you call the task.update
method.
The method ensures to display of the latest log message only when using the minimal
renderer and logs all messages when using the verbose
renderer.
const sleep = () => new Promise<void>((resolve) => setTimeout(resolve, 50))
tasks
.add('clone repo', async (task) => {
for (let i = 0; i <= 100; i = i + 2) {
await sleep()
task.update(`Downloaded ${i}%`)
}
return 'Completed'
})
The verbose
renderer displays all the log messages instead of just the latest one. Also, the task output is rendered differently from the minimal renderer. Please, check the example file for the same.
You can create the tasks instance with a verbose
renderer as follows. The rest of the API is the same.
const tasks = ui.tasks({ verbose: true })
First, you must instantiate the cliui
in raw mode to collect all logs messages within memory. And then you can access the tasks output using logger.getRenderer()
to write assertions.
const ui = cliui({ raw: true })
const tasks = ui.tasks()
await tasks
.add('clone repo', async (task) => {
return 'Completed'
})
.add('update package file', async (task) => {
return task.error('Unable to update package file')
})
.add('install dependencies', async (task) => {
return 'Installed'
})
.run()
const logs = tasks.getRenderer().getLogs()
console.log(logs)
FAQs
Opinionated UI KIT for Command Line apps
The npm package @poppinss/cliui receives a total of 41,645 weekly downloads. As such, @poppinss/cliui popularity was classified as popular.
We found that @poppinss/cliui demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.