/ student-intifada / node_modules / proc-log /

[ICO]NameLast modifiedSizeDescription
[PARENTDIR]Parent Directory  -  
[DIR]lib/a year ago -  
[   ]LICENSEa year ago742  
[TXT]README.mda year ago6.9K595aea1 more query options + view options [كارل مبارك]
[   ]package.jsona year ago1.1Kafd0ccc remove unused [كارل مبارك]
README.md

proc-log

Emits events on the process object which a listener can consume and print to the terminal or log file.

This is used by various modules within the npm CLI stack in order to send log events that can be consumed by a listener on the process object.

Currently emits log, output, input, and time events.

API

const { log, output, input, time } = require('proc-log')

output

log

input

time

Examples

log

Every log method calls process.emit('log', level, ...otherArgs) internally. So in order to consume those events you need to do process.on('log', fn).

Colorize based on level

Here's an example of how to consume proc-log log events and colorize them based on level:

const chalk = require('chalk')

process.on('log', (level, ...args) => {
  if (level === 'error') {
    console.log(chalk.red(level), ...args)
  } else {
    console.log(chalk.blue(level), ...args)
  }
})

Pause and resume

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 = true
const buffer = []

// this handler will buffer and replay logs only after `procLog.resume()` is called
process.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)
  }
})

// this handler will write everything to a file
process.on('log', (...args) => {
  fs.appendFileSync('debug.log', args.join(' '))
})

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()
  const res = await readFromUserInput({ prompt: 'OK?', default: 'y' })
  output.standard(`User said ${res}`)
} catch (err) {
  output.error(`User cancelled: ${err}`)
} finally {
  input.end()
}

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
  }
})

Using read to call read()

producer.js

const { output, input } = require('proc-log')

try {
  const res = await input.read({ prompt: 'OK?', default: 'y' })
  output.standard(`User said ${res}`)
} catch (err) {
  output.error(`User cancelled: ${err}`)
}

consumer.js

const { read } = require('read')

process.on('input', (level, ...args) => {
  if (level === 'read') {
    const [res, rej, opts] = args
    read(opts).then(res).catch(rej)
  }
})
Apache/2.4.38 (Debian) Server at www.karls.computer Port 80