Used to tell the consumer that the terminal is going to begin reading user input. Returns a function that will call input.end(...args) for convenience.
If the first argument is a function, it will be used as a callback which runs input.end() on its completion. If the callback returns a Promise then input.end(...args) will be run during finally().
Used to tell the consumer that the terminal is reading user input and returns a Promise that the producer can await until the consumer has finished its async action.
This emits resolve and reject functions (in addition to all passed in arguments) which the consumer must use to resolve the returned Promise.
Used to start a timer with the specified name. Returns a function that will call time.end() for convenience.
This also takes an optional callback which will run time.end() on its completion. If the callback returns a Promise then time.end() will be run during finally().
log.pause and log.resume are included so you have the ability to tell your consumer that you want to pause or resume your display of logs. In the npm CLI we use this to buffer all logs on init until we know the correct loglevel to display. But we also setup a second handler that writes everything to a file even if paused.
let paused = trueconst buffer = []// this handler will buffer and replay logs only after `procLog.resume()` is calledprocess.on('log', (level, ...args) => { if (level === 'resume') { buffer.forEach((item) => console.log(...item)) paused = false return } if (paused) { buffer.push([level, ...args]) } else { console.log(level, ...args) }
input
start and end
producer.js
const { output, input } = require('proc-log')const { readFromUserInput } = require('./my-read')// Using callback passed to `start`try { const res = await input.start( readFromUserInput({ prompt: 'OK?', default: 'y' }) ) output.standard(`User said ${res}`)} catch (err) { output.error(`User cancelled: ${err}`)}// Manually calling `start` and `end`try { input.start
consumer.js
const { read } = require('read')process.on('input', (level) => { if (level === 'start') { // Hide UI to make room for user input being read } else if (level === 'end') { // Restore UI now that reading is ended }})