Skip to content

Commit

Permalink
Merge pull request #57 from rstacruz/improved-logging
Browse files Browse the repository at this point in the history
Improved logging
  • Loading branch information
rstacruz committed Jan 31, 2016
2 parents 8895a8a + 168df51 commit 2f33814
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 95 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [#41] - Support `--save`, `--save-dev` and `--save-exact`. ([@davej])
- [#46] - Support modules being able to `require()` their peer dependencies (aka, emulate npm3-style flatness)
- [#57] - Support simpler Windows terminals.

[v0.13.0]: https://github.com/rstacruz/pnpm/compare/v0.12.1...v0.13.0

Expand Down Expand Up @@ -167,6 +168,7 @@
[#38]: https://github.com/rstacruz/pnpm/issues/38
[#41]: https://github.com/rstacruz/pnpm/issues/41
[#46]: https://github.com/rstacruz/pnpm/issues/46
[#57]: https://github.com/rstacruz/pnpm/issues/57
[@indexzero]: https://github.com/indexzero
[@rstacruz]: https://github.com/rstacruz
[@davej]: https://github.com/davej
2 changes: 1 addition & 1 deletion lib/cmd/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function run (cli) {
function updateContext (packageJson) {
var root = packageJson ? dirname(packageJson) : process.cwd()
ctx.root = root
ctx.store = join(root, config.pnpm_store_path)
ctx.store = join(root, config.store_path)
if (!cli.flags.quiet) ctx.log = logger()
else ctx.log = function () { return function () {} }
}
Expand Down
7 changes: 4 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = require('rc')('npm', {
pnpm_concurrency: 16,
pnpm_store_path: 'node_modules/.store'
module.exports = require('rc')('pnpm', {
concurrency: 16,
store_path: 'node_modules/.store',
logger: 'pretty'
})
2 changes: 1 addition & 1 deletion lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var tar = require('tar-fs')
*/

module.exports = function fetch (dir, tarball, shasum, log) {
log('download-queued')
return got.getStream(tarball)
.then(stream => fetchStream(dir, tarball, shasum, log, stream))
}
Expand All @@ -33,6 +32,7 @@ function fetchStream (dir, tarball, shasum, log, stream) {
return reject(new Error('' + tarball + ': invalid response ' + res.statusCode))
}

log('download-start')
if ('content-length' in res.headers) {
size = +res.headers['content-length']
res.on('data', chunk => {
Expand Down
20 changes: 11 additions & 9 deletions lib/got.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ var config = require('./config')

var cache = {}

var throater = throat(+config.pnpm_concurrency)
var throater = throat(+config.concurrency)

/*
* Interface for 'got' with debug logging
* waits in line
*/

function Got (url, options) {
exports.get = function (url, options) {
var key = JSON.stringify([ url, options ])
if (!cache[key]) {
cache[key] = throater(_ => {
debug(url)
return got(url, extend(options))
cache[key] = new Promise((resolve, reject) => {
throater(_ => {
debug(url)
var promise = got(url, extend(options))
resolve(promise)
promise.then(resolve, reject)
})
})
}
return cache[key]
Expand All @@ -28,7 +32,7 @@ function Got (url, options) {
* like require('got').stream, but throated
*/

Got.getStream = function (url, options) {
exports.getStream = function (url, options) {
return new Promise(resolve => {
throater(_ => {
debug(url, '[stream]')
Expand Down Expand Up @@ -59,5 +63,3 @@ function extend (options) {
})
})
}

module.exports = Got
4 changes: 2 additions & 2 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = function install (ctx, pkgSpec, modules, options) {
.then(_ => _
? saveCachedResolution()
.then(data => log('package.json', data))
: resolve(pkg.spec)
: resolve(pkg.spec, log)
.then(saveResolution)
.then(_ => log('resolved', pkg))
.then(_ => buildToStoreCached(ctx, paths, pkg, log))
Expand Down Expand Up @@ -194,7 +194,7 @@ function fetchToStore (ctx, paths, pkg, log) {
.then(_ => symlink(paths.tmp, paths.target))

// download and untar
.then(_ => log('downloading'))
.then(_ => log('download-queued'))
.then(_ => mkdirp(ctx.store))
.then(_ => mkdirp(join(paths.tmp, '_')))
.then(_ => fs.writeFile(join(paths.tmp, '.pnpm_inprogress'), '', 'utf-8'))
Expand Down
80 changes: 6 additions & 74 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,7 @@
var chalk = require('chalk')
var config = require('./config')
var supportsColor = require('supports-color')

var observatory = require('observatory')
observatory.settings({ prefix: ' ', width: 74 })

module.exports = function logger () {
return function (pkg) {
var pkgData // package.json
var res // resolution
var t

if (pkg.name) {
t = observatory.add(pkg.name + ' ' +
chalk.gray(pkg.rawSpec || ''))
} else {
t = observatory.add('' +
chalk.gray(pkg.rawSpec || ''))
}

// resolve() is the first thing it does.
t.status(chalk.yellow('finding ·'))
return status

// log('resolved', pkgData)
// log('downloading')
// log('downloading', { done: 1, total: 200 })
// log('depnedencies')
// log('error', err)
function status (status, args) {
if (status === 'resolved') {
res = args
} else if (status === 'download-queued') {
if (res.version) {
t.status(chalk.gray('wait ' + res.version + ' ·'))
} else {
t.status(chalk.gray('wait ·'))
}
} else if (status === 'downloading') {
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('')
}
}
}
}
module.exports =
(config.logger === 'pretty' && supportsColor)
? require('./logger/pretty')
: require('./logger/simple')
77 changes: 77 additions & 0 deletions lib/logger/pretty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var chalk = require('chalk')

var observatory = require('observatory')
observatory.settings({ prefix: ' ', width: 74 })

module.exports = function logger () {
return function (pkg) {
var pkgData // package.json
var res // resolution
var t

if (pkg.name) {
t = observatory.add(pkg.name + ' ' +
chalk.gray(pkg.rawSpec || ''))
} else {
t = observatory.add('' +
chalk.gray(pkg.rawSpec || ''))
}

// the first thing it (probably) does is wait in queue to query the npm registry
t.status(chalk.gray('·'))
return status

// log('resolved', pkgData)
// log('downloading')
// log('downloading', { done: 1, total: 200 })
// log('depnedencies')
// log('error', err)
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') {
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('')
}
}
}
}
88 changes: 88 additions & 0 deletions lib/logger/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var chalk = require('chalk')

var UPDATERS = [
'resolving', 'resolved', 'download-start', 'dependencies'
]

var BAR_LENGTH = 20

var s = {
gray: chalk.gray,
green: chalk.green,
bold: chalk.bold
}

/*
* Simple percent logger
*/

module.exports = function logger () {
var out = process.stdout
var progress = { done: 0, total: 0 }
var lastStatus
var done = {}

process.on('exit', _ => {
out.write(reset())
})

return function (pkg) {
var name = pkg.name
? (pkg.name + ' ' + pkg.rawSpec)
: pkg.rawSpec

update()
progress.total += UPDATERS.length + 20
var left = UPDATERS.length + 20
var pkgData

return function (status, args) {
if (status === 'done') progress.done += left

if (~UPDATERS.indexOf(status)) {
progress.done += 1
left -= 1
}

if (status === 'package.json') {
pkgData = args
}

lastStatus = name

if (status === 'done') {
if (pkgData && pkgData.version) {
update(pkgData.name + ' ' + s.gray(pkgData.version))
} else {
update(pkgData && pkgData.name || name)
}
} else {
update()
}
}

function update (line) {
if (line && !done[line]) {
done[line] = true
out.write(reset() + line + '\n')
}

var percent = progress.done / progress.total
if (progress.total > 0 && out.isTTY) {
var bar = Math.round(percent * BAR_LENGTH)
out.write(
reset() +
s.bold(Math.round(percent * 100) + '%') + ' ' +
s.green(Array(bar).join('=') + '>') +
Array(BAR_LENGTH - bar).join(' ') + ' ' +
s.gray(lastStatus.substr(0, 40))) + ' '
}
}
}

function reset () {
return out.isTTY
? '\r' + Array(out.columns).join(' ') + '\r'
: ''
}
}
6 changes: 3 additions & 3 deletions lib/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ var resolveTarball = require('./resolve/tarball')
* })
*/

module.exports = function resolve (pkg) {
module.exports = function resolve (pkg, log) {
if (pkg.type === 'range' || pkg.type === 'version' || pkg.type === 'tag') {
return resolveNpm(pkg)
return resolveNpm(pkg, log)
} else if (pkg.type === 'remote') {
return resolveTarball(pkg)
return resolveTarball(pkg, log)
} else {
throw new Error('' + pkg.rawSpec + ': ' + pkg.type + ' packages not supported')
}
Expand Down
9 changes: 7 additions & 2 deletions lib/resolve/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ var registryUrl = require('registry-url')
* })
*/

module.exports = function resolveNpm (pkg) {
module.exports = function resolveNpm (pkg, log) {
// { raw: 'rimraf@2', scope: null, name: 'rimraf', rawSpec: '2' || '' }
return Promise.resolve()
.then(_ => toUri(pkg))
.then(url => got(url))
.then(url => {
if (log) log('resolving')
return got.get(url).then(promise => {
return promise
})
})
.then(res => JSON.parse(res.body))
.then(res => {
return {
Expand Down

0 comments on commit 2f33814

Please sign in to comment.