Skip to content

Commit

Permalink
bin: Only JSON.stringify by default if an object
Browse files Browse the repository at this point in the history
Fix #17

It's very annoying to have to strip off the " chars when piping to other
unix cli tools.
  • Loading branch information
isaacs committed Nov 27, 2019
1 parent 0018eda commit f28888e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
15 changes: 11 additions & 4 deletions lib/bin.js
Expand Up @@ -68,6 +68,16 @@ options passed to Pacote. Additional flags for this executable:
For example '--cache=/path/to/folder' will use that folder as the cache.
`

const shouldJSON = (conf, result) =>
conf.json ||
!process.stdout.isTTY &&
conf.json === undefined &&
result &&
typeof result === 'object'

const pretty = (conf, result) =>
shouldJSON(conf, result) ? JSON.stringify(result, 0, 2) : result

const main = args => {
const conf = parse(args)
if (conf.help || conf.h)
Expand All @@ -77,9 +87,7 @@ const main = args => {

try {
return run(conf)
.then(result => result && console.log(
conf.json ? JSON.stringify(result, 0, 2) : result
))
.then(result => result && console.log(pretty(conf, result)))
.catch(er => {
console.error(er)
process.exit(1)
Expand All @@ -104,7 +112,6 @@ const parseArg = arg => {
const parse = args => {
const conf = {
_: [],
json: !process.stdout.isTTY,
cache: process.env.HOME + '/.npm/_cacache',
}
let dashdash = false
Expand Down
34 changes: 29 additions & 5 deletions tap-snapshots/test-bin.js-TAP.test.js
Expand Up @@ -38,7 +38,7 @@ Object {
"exitlog": Array [],
"loglog": Array [
Array [
"{\\n \\"method\\": \\"extract\\",\\n \\"spec\\": \\"npm@latest-6\\",\\n \\"dest\\": \\"folder\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"extract\\",\\n \\"npm@latest-6\\",\\n \\"folder\\"\\n ],\\n \\"json\\": true,\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n }\\n}",
"{\\n \\"method\\": \\"extract\\",\\n \\"spec\\": \\"npm@latest-6\\",\\n \\"dest\\": \\"folder\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"extract\\",\\n \\"npm@latest-6\\",\\n \\"folder\\"\\n ],\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\",\\n \\"json\\": true\\n }\\n}",
],
],
}
Expand Down Expand Up @@ -75,7 +75,7 @@ Object {
"exitlog": Array [],
"loglog": Array [
Array [
"{\\n \\"method\\": \\"manifest\\",\\n \\"spec\\": \\"bar@foo\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"manifest\\",\\n \\"bar@foo\\"\\n ],\\n \\"json\\": true,\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n },\\n \\"_resolved\\": \\"manifest resolved\\",\\n \\"_integrity\\": \\"manifest integrity\\",\\n \\"_from\\": \\"manifest from\\"\\n}",
"{\\n \\"method\\": \\"manifest\\",\\n \\"spec\\": \\"bar@foo\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"manifest\\",\\n \\"bar@foo\\"\\n ],\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n },\\n \\"_resolved\\": \\"manifest resolved\\",\\n \\"_integrity\\": \\"manifest integrity\\",\\n \\"_from\\": \\"manifest from\\"\\n}",
],
],
}
Expand All @@ -87,7 +87,7 @@ Object {
"exitlog": Array [],
"loglog": Array [
Array [
"{\\n \\"method\\": \\"packument\\",\\n \\"spec\\": \\"paku@mint\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"packument\\",\\n \\"paku@mint\\"\\n ],\\n \\"json\\": true,\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n }\\n}",
"{\\n \\"method\\": \\"packument\\",\\n \\"spec\\": \\"paku@mint\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"packument\\",\\n \\"paku@mint\\"\\n ],\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n }\\n}",
],
],
}
Expand Down Expand Up @@ -125,7 +125,31 @@ Object {
"exitlog": Array [],
"loglog": Array [
Array [
"{\\n \\"method\\": \\"resolve\\",\\n \\"spec\\": \\"foo@bar\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"resolve\\",\\n \\"foo@bar\\"\\n ],\\n \\"json\\": true,\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n }\\n}",
"{\\n \\"method\\": \\"resolve\\",\\n \\"spec\\": \\"foo@bar\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"resolve\\",\\n \\"foo@bar\\"\\n ],\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n }\\n}",
],
],
}
`

exports[`test/bin.js TAP main resolve string --json > must match snapshot 1`] = `
Object {
"errorlog": Array [],
"exitlog": Array [],
"loglog": Array [
Array [
"\\"just a string\\"",
],
],
}
`

exports[`test/bin.js TAP main resolve string > must match snapshot 1`] = `
Object {
"errorlog": Array [],
"exitlog": Array [],
"loglog": Array [
Array [
"just a string",
],
],
}
Expand All @@ -137,7 +161,7 @@ Object {
"exitlog": Array [],
"loglog": Array [
Array [
"{\\n \\"method\\": \\"tarball\\",\\n \\"spec\\": \\"tar@ball\\",\\n \\"file\\": \\"file.tgz\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"tarball\\",\\n \\"tar@ball\\",\\n \\"file.tgz\\"\\n ],\\n \\"json\\": true,\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n }\\n}",
"{\\n \\"method\\": \\"tarball\\",\\n \\"spec\\": \\"tar@ball\\",\\n \\"file\\": \\"file.tgz\\",\\n \\"conf\\": {\\n \\"_\\": [\\n \\"tarball\\",\\n \\"tar@ball\\",\\n \\"file.tgz\\"\\n ],\\n \\"cache\\": \\"{HOME}/.npm/_cacache\\"\\n }\\n}",
],
],
}
Expand Down
14 changes: 12 additions & 2 deletions test/bin.js
Expand Up @@ -11,6 +11,7 @@ const pacote = require('../')
const called = []
pacote.resolve = (spec, conf) =>
spec === 'fail' ? Promise.reject(new Error('fail'))
: spec === 'string' ? Promise.resolve('just a string')
: Promise.resolve({method: 'resolve', spec, conf})
pacote.manifest = (spec, conf) => Promise.resolve({
method: 'manifest',
Expand Down Expand Up @@ -53,9 +54,14 @@ t.test('parse', t => {
json: false,
cache: process.env.HOME + '/.npm/_cacache',
})
t.same(parse(['a', 'b', '--foo', '--json']), {
_: ['a', 'b'],
foo: true,
json: true,
cache: process.env.HOME + '/.npm/_cacache',
})
t.same(parse(['a', 'b', '--', '--json']), {
_: ['a', 'b', '--json'],
json: !process.stdout.isTTY,
cache: process.env.HOME + '/.npm/_cacache',
})
t.match(parse(['-h']), { help: true })
Expand Down Expand Up @@ -104,13 +110,17 @@ t.test('main', t => {
cb()
})

Object.defineProperty(process.stdout, 'isTTY', { value: false })

const test = (...args) =>
t.test(args.join(' '), t => Promise.resolve(main(['--json', ...args]))
t.test(args.join(' '), t => Promise.resolve(main(args))
.then(() => t.matchSnapshot({errorlog, loglog, exitlog})))

test('--help')
test('resolve', 'foo@bar')
test('resolve', 'foo@bar', '--long')
test('resolve', 'string')
test('resolve', 'string', '--json')
test('manifest', 'bar@foo')
test('packument', 'paku@mint')
test('tarball', 'tar@ball', 'file.tgz')
Expand Down

0 comments on commit f28888e

Please sign in to comment.