Files
pnpm/lib/logger/pretty.js
Zoltan Kochan 9bc97e76d4 style: use more ES6 syntax
Use lebab to transpile ES5 code to ES6.
Use eslint to fix styling issues afterwards.
Run all js in strict mode to allow let/const
in Node 4.
2016-08-23 21:12:01 +03:00

94 lines
2.7 KiB
JavaScript

'use strict'
const chalk = require('chalk')
const observatory = require('observatory')
observatory.settings({ prefix: ' ', width: 74 })
/*
* Logger.
*
* add = logger()
*
* log = add({ name: 'rimraf', rawSpec: 'rimraf@2.5.1' })
* log('resolved', pkgData)
* log('downloading')
* log('downloading', { done: 1, total: 200 })
* log('depnedencies')
* log('error', err)
*/
module.exports = function logger () {
const tasks = {}
function getTask (pkg) {
if (tasks[pkg.rawSpec]) return tasks[pkg.rawSpec]
const task = observatory.add(
(pkg.name ? (pkg.name + ' ') : '') +
chalk.gray(pkg.rawSpec || ''))
task.status(chalk.gray('·'))
tasks[pkg.rawSpec] = task
return task
}
return pkg => {
let pkgData // package.json
let res // resolution
// lazy get task
function t () {
return getTask(pkg)
}
// the first thing it (probably) does is wait in queue to query the npm registry
return status
function status (status, args) {
if (status === 'resolving') {
t().status(chalk.yellow('finding ·'))
} else if (status === 'resolved') {
res = args
} else if (status === 'download-queued') {
if (res.version) {
t().status(chalk.gray('queued ' + res.version + ' ↓'))
} else {
t().status(chalk.gray('queued ↓'))
}
} else if (status === 'downloading' || status === 'download-start') {
if (res.version) {
t().status(chalk.yellow('downloading ' + res.version + ' ↓'))
} else {
t().status(chalk.yellow('downloading ↓'))
}
if (args && args.total && args.done < args.total) {
t().details('' + Math.round(args.done / args.total * 100) + '%')
} else {
t().details('')
}
} else if (status === 'done') {
if (pkgData) {
t().status(chalk.green('' + pkgData.version + ' ✓'))
.details('')
} else {
t().status(chalk.green('OK ✓'))
.details('')
}
} else if (status === 'package.json') {
pkgData = args
} else if (status === 'dependencies') {
t().status(chalk.gray('' + pkgData.version + ' ·'))
.details('')
} else if (status === 'error') {
t().status(chalk.red('ERROR ✗'))
.details('')
} else if (status === 'stdout') {
observatory.add(chalk.blue(args.name) + ' ' + chalk.gray(args.line))
} else if (status === 'stderr') {
observatory.add(chalk.blue(args.name) + '! ' + chalk.gray(args.line))
} else {
t().status(status)
.details('')
}
}
}
}