1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
'use strict'
const hookApi = require('libnpmhook')
const npm = require('./npm.js')
const output = require('./utils/output.js')
const otplease = require('./utils/otplease.js')
const relativeDate = require('tiny-relative-date')
const Table = require('cli-table3')
const validate = require('aproba')
hook.usage = [
'npm hook add <pkg> <url> <secret> [--type=<type>]',
'npm hook ls [pkg]',
'npm hook rm <id>',
'npm hook update <id> <url> <secret>'
].join('\n')
hook.completion = (opts, cb) => {
validate('OF', [opts, cb])
return cb(null, []) // fill in this array with completion values
}
function UsageError () {
throw Object.assign(new Error(hook.usage), {code: 'EUSAGE'})
}
// XXX once completely de-figged this can be changed to
// (args, cb) => hook(args).then ...
module.exports = (args, cb) => Promise.resolve().then(() => {
return hook(args)
.then(val => cb(null, val))
.catch(err => err.code === 'EUSAGE' ? cb(err.message) : cb(err))
})
module.exports.usage = hook.usage
function hook (args) {
return otplease(npm.flatOptions, 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)
default:
UsageError()
}
})
}
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, opts) {
return hookApi.ls({ ...opts, 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 {
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([
{
colSpan: 1,
content: `triggered ${relativeDate(hook.last_delivery)}`
},
hook.response_code
])
} else {
table.push([{colSpan: 2, content: 'never triggered'}])
}
})
output(table.toString())
}
})
}
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, 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) {
let target = hook.name
if (hook.type === 'scope') { target = '@' + target }
if (hook.type === 'owner') { target = '~' + target }
return target
}
|