hook.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const hookApi = require('libnpmhook')
  2. const otplease = require('./utils/otplease.js')
  3. const relativeDate = require('tiny-relative-date')
  4. const Table = require('cli-table3')
  5. const BaseCommand = require('./base-command.js')
  6. class Hook extends BaseCommand {
  7. static get description () {
  8. return 'Manage registry hooks'
  9. }
  10. static get name () {
  11. return 'hook'
  12. }
  13. /* istanbul ignore next - see test/lib/load-all-commands.js */
  14. static get params () {
  15. return [
  16. 'registry',
  17. 'otp',
  18. ]
  19. }
  20. static get usage () {
  21. return [
  22. 'add <pkg> <url> <secret> [--type=<type>]',
  23. 'ls [pkg]',
  24. 'rm <id>',
  25. 'update <id> <url> <secret>',
  26. ]
  27. }
  28. exec (args, cb) {
  29. this.hook(args).then(() => cb()).catch(cb)
  30. }
  31. async hook (args) {
  32. return otplease(this.npm.flatOptions, (opts) => {
  33. switch (args[0]) {
  34. case 'add':
  35. return this.add(args[1], args[2], args[3], opts)
  36. case 'ls':
  37. return this.ls(args[1], opts)
  38. case 'rm':
  39. return this.rm(args[1], opts)
  40. case 'update':
  41. case 'up':
  42. return this.update(args[1], args[2], args[3], opts)
  43. default:
  44. throw this.usage
  45. }
  46. })
  47. }
  48. async add (pkg, uri, secret, opts) {
  49. const hook = await hookApi.add(pkg, uri, secret, opts)
  50. if (opts.json)
  51. this.npm.output(JSON.stringify(hook, null, 2))
  52. else if (opts.parseable) {
  53. this.npm.output(Object.keys(hook).join('\t'))
  54. this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t'))
  55. } else if (!opts.silent && opts.loglevel !== 'silent') {
  56. this.npm.output(`+ ${this.hookName(hook)} ${
  57. opts.unicode ? ' ➜ ' : ' -> '
  58. } ${hook.endpoint}`)
  59. }
  60. }
  61. async ls (pkg, opts) {
  62. const hooks = await hookApi.ls({ ...opts, package: pkg })
  63. if (opts.json)
  64. this.npm.output(JSON.stringify(hooks, null, 2))
  65. else if (opts.parseable) {
  66. this.npm.output(Object.keys(hooks[0]).join('\t'))
  67. hooks.forEach(hook => {
  68. this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t'))
  69. })
  70. } else if (!hooks.length)
  71. this.npm.output("You don't have any hooks configured yet.")
  72. else if (!opts.silent && opts.loglevel !== 'silent') {
  73. if (hooks.length === 1)
  74. this.npm.output('You have one hook configured.')
  75. else
  76. this.npm.output(`You have ${hooks.length} hooks configured.`)
  77. const table = new Table({ head: ['id', 'target', 'endpoint'] })
  78. hooks.forEach((hook) => {
  79. table.push([
  80. { rowSpan: 2, content: hook.id },
  81. this.hookName(hook),
  82. hook.endpoint,
  83. ])
  84. if (hook.last_delivery) {
  85. table.push([
  86. {
  87. colSpan: 1,
  88. content: `triggered ${relativeDate(hook.last_delivery)}`,
  89. },
  90. hook.response_code,
  91. ])
  92. } else
  93. table.push([{ colSpan: 2, content: 'never triggered' }])
  94. })
  95. this.npm.output(table.toString())
  96. }
  97. }
  98. async rm (id, opts) {
  99. const hook = await hookApi.rm(id, opts)
  100. if (opts.json)
  101. this.npm.output(JSON.stringify(hook, null, 2))
  102. else if (opts.parseable) {
  103. this.npm.output(Object.keys(hook).join('\t'))
  104. this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t'))
  105. } else if (!opts.silent && opts.loglevel !== 'silent') {
  106. this.npm.output(`- ${this.hookName(hook)} ${
  107. opts.unicode ? ' ✘ ' : ' X '
  108. } ${hook.endpoint}`)
  109. }
  110. }
  111. async update (id, uri, secret, opts) {
  112. const hook = await hookApi.update(id, uri, secret, opts)
  113. if (opts.json)
  114. this.npm.output(JSON.stringify(hook, null, 2))
  115. else if (opts.parseable) {
  116. this.npm.output(Object.keys(hook).join('\t'))
  117. this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t'))
  118. } else if (!opts.silent && opts.loglevel !== 'silent') {
  119. this.npm.output(`+ ${this.hookName(hook)} ${
  120. opts.unicode ? ' ➜ ' : ' -> '
  121. } ${hook.endpoint}`)
  122. }
  123. }
  124. hookName (hook) {
  125. let target = hook.name
  126. if (hook.type === 'scope')
  127. target = '@' + target
  128. if (hook.type === 'owner')
  129. target = '~' + target
  130. return target
  131. }
  132. }
  133. module.exports = Hook