const BB = require('bluebird')
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')
const validate = require('aproba')
const npm = require('./npm')
'npm hook add <pkg> <url> <secret> [--type=<type>]',
'npm hook update <id> <url> <secret>'
hook.completion = (opts, cb) => {
validate('OF', [opts, cb])
return cb(null, []) // fill in this array with completion values
const HookConfig = pudding({
throw Object.assign(new Error(hook.usage), {code: 'EUSAGE'})
module.exports = (args, cb) => BB.try(() => hook(args)).then(
err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
if (args.length === 4) { // secret is passed in the args
// we have the user secret in the CLI args, we need to redact it from the referer.
return otplease(npmConfig(), opts => {
return add(args[1], args[2], args[3], opts)
return update(args[1], args[2], args[3], opts)
function add (pkg, uri, secret, opts) {
return hookApi.add(pkg, uri, secret, opts).then(hook => {
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 ? ' ➜ ' : ' -> '
function ls (pkg, opts) {
return hookApi.ls(opts.concat({package: pkg})).then(hooks => {
output(JSON.stringify(hooks, null, 2))
} else if (opts.parseable) {
output(Object.keys(hooks[0]).join('\t'))
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.')
output(`You have ${hooks.length} hooks configured.`)
const table = new Table({head: ['id', 'target', 'endpoint']})
hooks.forEach((hook) => {
{rowSpan: 2, content: hook.id},
if (hook.last_delivery) {
content: `triggered ${relativeDate(hook.last_delivery)}`
table.push([{colSpan: 2, content: 'never triggered'}])
return hookApi.rm(id, opts).then(hook => {
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 '
function update (id, uri, secret, opts) {
return hookApi.update(id, uri, secret, opts).then(hook => {
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 ? ' ➜ ' : ' -> '
function hookName (hook) {
if (hook.type === 'scope') { target = '@' + target }
if (hook.type === 'owner') { target = '~' + target }
function redactUserSecret () {
const referer = npm.referer
const splittedReferer = referer.split(' ')
splittedReferer[4] = '[REDACTED]'
npm.referer = splittedReferer.join(' ')