From ab215530674d7f6123c9572d0ad4ca9e9b5fb184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Thu, 23 Aug 2018 18:13:22 -0700 Subject: [PATCH] hook: clean up and improve hook command --- lib/hook.js | 189 +++++++++++++++++++++++++++------------------------- 1 file changed, 99 insertions(+), 90 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index b0552c74740ea..098d323fd2bf8 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -2,11 +2,10 @@ const BB = require('bluebird') -const crypto = require('crypto') -const hookApi = require('libnpmhook') -const log = require('npmlog') -const npm = require('./npm.js') +const hookApi = require('libnpm/hook') +const npmConfig = require('./config/figgy-config.js') const output = require('./utils/output.js') +const otplease = require('./utils/otplease.js') const pudding = require('figgy-pudding') const relativeDate = require('tiny-relative-date') const Table = require('cli-table3') @@ -25,106 +24,116 @@ hook.completion = (opts, cb) => { return cb(null, []) // fill in this array with completion values } -const npmSession = crypto.randomBytes(8).toString('hex') -const hookConfig = pudding() -function config () { - return hookConfig({ - refer: npm.refer, - projectScope: npm.projectScope, - log, - npmSession - }, npm.config) -} +const HookConfig = pudding({ + json: {}, + loglevel: {}, + parseable: {}, + silent: {}, + unicode: {} +}) module.exports = (args, cb) => BB.try(() => hook(args)).nodeify(cb) function hook (args) { - switch (args[0]) { - case 'add': - return add(args[1], args[2], args[3]) - case 'ls': - return ls(args[1]) - case 'rm': - return rm(args[1]) - case 'update': - case 'up': - return update(args[1], args[2], args[3]) - } + return otplease(npmConfig(), opts => { + opts = HookConfig(opts) + switch (args[0]) { + case 'add': + return add(args[1], args[2], args[3], opts) + case 'ls': + return ls(args[1], opts) + case 'rm': + return rm(args[1], opts) + case 'update': + case 'up': + return update(args[1], args[2], args[3], opts) + } + }) } -function add (pkg, uri, secret) { - return hookApi.add(pkg, uri, secret, config()) - .then((hook) => { - if (npm.config.get('json')) { - output(JSON.stringify(hook, null, 2)) - } else { - output(`+ ${hookName(hook)} ${ - npm.config.get('unicode') ? ' ➜ ' : ' -> ' - } ${hook.endpoint}`) - } - }) +function add (pkg, uri, secret, opts) { + return hookApi.add(pkg, uri, secret, opts).then(hook => { + if (opts.json) { + output(JSON.stringify(hook, null, 2)) + } else if (opts.parseable) { + output(Object.keys(hook).join('\t')) + output(Object.keys(hook).map(k => hook[k]).join('\t')) + } else if (!opts.silent && opts.loglevel !== 'silent') { + output(`+ ${hookName(hook)} ${ + opts.unicode ? ' ➜ ' : ' -> ' + } ${hook.endpoint}`) + } + }) } -function ls (pkg) { - return hookApi.ls(pkg, config()) - .then((hooks) => { - if (npm.config.get('json')) { - output(JSON.stringify(hooks, null, 2)) - } else if (!hooks.length) { - output("You don't have any hooks configured yet.") +function ls (pkg, opts) { + return hookApi.ls(opts.concat({package: pkg})).then(hooks => { + if (opts.json) { + output(JSON.stringify(hooks, null, 2)) + } else if (opts.parseable) { + output(Object.keys(hooks[0]).join('\t')) + hooks.forEach(hook => { + output(Object.keys(hook).map(k => hook[k]).join('\t')) + }) + } else if (!hooks.length) { + output("You don't have any hooks configured yet.") + } else if (!opts.silent && opts.loglevel !== 'silent') { + if (hooks.length === 1) { + output('You have one hook configured.') } else { - if (hooks.length === 1) { - output('You have one hook configured.') - } else { - output(`You have ${hooks.length} hooks configured.`) - } - const table = new Table({head: ['id', 'target', 'endpoint']}) - hooks.forEach((hook) => { + output(`You have ${hooks.length} hooks configured.`) + } + const table = new Table({head: ['id', 'target', 'endpoint']}) + hooks.forEach((hook) => { + table.push([ + {rowSpan: 2, content: hook.id}, + hookName(hook), + hook.endpoint + ]) + if (hook.last_delivery) { table.push([ - {rowSpan: 2, content: hook.id}, - hookName(hook), - hook.endpoint + { + colSpan: 1, + content: `triggered ${relativeDate(hook.last_delivery)}` + }, + hook.response_code ]) - if (hook.last_delivery) { - table.push([ - { - colSpan: 1, - content: `triggered ${relativeDate(hook.last_delivery)}` - }, - hook.response_code - ]) - } else { - table.push([{colSpan: 2, content: 'never triggered'}]) - } - }) - output(table.toString()) - } - }) + } else { + table.push([{colSpan: 2, content: 'never triggered'}]) + } + }) + output(table.toString()) + } + }) } -function rm (id) { - return hookApi.rm(id, config()) - .then((hook) => { - if (npm.config.get('json')) { - output(JSON.stringify(hook, null, 2)) - } else { - output(`- ${hookName(hook)} ${ - npm.config.get('unicode') ? ' ✘ ' : ' X ' - } ${hook.endpoint}`) - } - }) +function rm (id, opts) { + return hookApi.rm(id, opts).then(hook => { + if (opts.json) { + output(JSON.stringify(hook, null, 2)) + } else if (opts.parseable) { + output(Object.keys(hook).join('\t')) + output(Object.keys(hook).map(k => hook[k]).join('\t')) + } else if (!opts.silent && opts.loglevel !== 'silent') { + output(`- ${hookName(hook)} ${ + opts.unicode ? ' ✘ ' : ' X ' + } ${hook.endpoint}`) + } + }) } -function update (id, uri, secret) { - return hookApi.update(id, uri, secret, config()) - .then((hook) => { - if (npm.config.get('json')) { - output(JSON.stringify(hook, null, 2)) - } else { - output(`+ ${hookName(hook)} ${ - npm.config.get('unicode') ? ' ➜ ' : ' -> ' - } ${hook.endpoint}`) - } - }) +function update (id, uri, secret, opts) { + return hookApi.update(id, uri, secret, opts).then(hook => { + if (opts.json) { + output(JSON.stringify(hook, null, 2)) + } else if (opts.parseable) { + output(Object.keys(hook).join('\t')) + output(Object.keys(hook).map(k => hook[k]).join('\t')) + } else if (!opts.silent && opts.loglevel !== 'silent') { + output(`+ ${hookName(hook)} ${ + opts.unicode ? ' ➜ ' : ' -> ' + } ${hook.endpoint}`) + } + }) } function hookName (hook) {