Permalink
Browse files

feat: create a pnpm-debug.log file on fail

close #336
  • Loading branch information...
1 parent 05d389c commit e1abc46bd11d2671c0ed73cb39a644962a0b4a77 @zkochan zkochan committed Sep 1, 2016
Showing with 79 additions and 0 deletions.
  1. +4 −0 .github/ISSUE_TEMPLATE.md
  2. +1 −0 bin/pnpm.js
  3. +61 −0 lib/file_logger.js
  4. +1 −0 package.json
  5. +12 −0 test/index.js
@@ -8,6 +8,10 @@ If your issue is a bug, please follow the format below:
### Code to reproduce the issue:
+<!--
+If there was a fatal error also include a gist of your pnpm-debug.log file.
+-->
+
### Expected behavior:
### Actual behavior:
View
@@ -11,6 +11,7 @@ const rc = require('rc')
const camelcaseKeys = require('camelcase-keys')
const spawnSync = require('cross-spawn').sync
const isCI = require('is-ci')
+require('../lib/file_logger')
const pnpmCmds = {
install: require('../lib/cmd/install'),
View
@@ -0,0 +1,61 @@
+'use strict'
+const logger = require('@zkochan/logger')
+const fs = require('fs')
+const YAML = require('json2yaml')
+const slice = Array.prototype.slice
+
+const logFilePath = 'pnpm-debug.log'
+
+const logs = []
+
+logger.onAny(function () {
+ const args = slice.call(arguments).slice(2)
+ if (isUsefulLog.apply(null, args)) {
+ logs.push(args)
+ }
+})
+
+function isUsefulLog (level) {
+ return level !== 'progress' || arguments[2] !== 'downloading'
+}
+
+process.on('exit', code => {
+ if (code === 0) {
+ fs.unlink(logFilePath)
+ return
+ }
+
+ const prettyLogs = getPrettyLogs()
+ const yamlLogs = YAML.stringify(prettyLogs)
+ fs.writeFileSync(logFilePath, yamlLogs, 'UTF8')
+})
+
+function getPrettyLogs () {
+ const logObj = {}
+ logs.forEach((args, i) => {
+ const key = `${i} ${args[0]} ${args[1]}`
+ const rest = mergeStrings(args.slice(2).map(stringify))
+ logObj[key] = rest.length === 1 ? rest[0] : rest
+ })
+ return logObj
+}
+
+function stringify (obj) {
+ if (obj instanceof Error) return obj.toString()
+ return obj
+}
+
+function mergeStrings (arr) {
+ const mergedArr = []
+ let prevWasString = false
+ arr.forEach(el => {
+ const currentIsString = typeof el === 'string'
+ if (currentIsString && prevWasString) {
+ mergedArr[mergedArr.length - 1] += ' ' + el
+ } else {
+ mergedArr.push(el)
+ }
+ prevWasString = currentIsString
+ })
+ return mergedArr
+}
View
@@ -28,6 +28,7 @@
"gunzip-maybe": "1.3.1",
"is-ci": "1.0.9",
"is-retry-allowed": "1.1.0",
+ "json2yaml": "1.1.0",
"lockfile": "1.0.1",
"meow": "3.7.0",
"mkdirp": "0.5.1",
View
@@ -768,6 +768,18 @@ test("don't fail when peer dependency is fetched from GitHub", t => {
.catch(t.end)
})
+test('create a pnpm-debug.log file when the command fails', t => {
+ prepare()
+
+ const result = spawnSync('node', [pnpmBin, 'install', '@zkochan/i-do-not-exist'])
+
+ t.equal(result.status, 1, 'install failed')
+
+ exists('pnpm-debug.log')
+
+ t.end()
+})
+
function extendPathWithLocalBin () {
return {
PATH: [

0 comments on commit e1abc46

Please sign in to comment.