11cb0ef41Sopenharmony_ciconst hookApi = require('libnpmhook')
21cb0ef41Sopenharmony_ciconst otplease = require('../utils/otplease.js')
31cb0ef41Sopenharmony_ciconst relativeDate = require('tiny-relative-date')
41cb0ef41Sopenharmony_ciconst Table = require('cli-table3')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst BaseCommand = require('../base-command.js')
71cb0ef41Sopenharmony_ciclass Hook extends BaseCommand {
81cb0ef41Sopenharmony_ci  static description = 'Manage registry hooks'
91cb0ef41Sopenharmony_ci  static name = 'hook'
101cb0ef41Sopenharmony_ci  static params = [
111cb0ef41Sopenharmony_ci    'registry',
121cb0ef41Sopenharmony_ci    'otp',
131cb0ef41Sopenharmony_ci  ]
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  static usage = [
161cb0ef41Sopenharmony_ci    'add <pkg> <url> <secret> [--type=<type>]',
171cb0ef41Sopenharmony_ci    'ls [pkg]',
181cb0ef41Sopenharmony_ci    'rm <id>',
191cb0ef41Sopenharmony_ci    'update <id> <url> <secret>',
201cb0ef41Sopenharmony_ci  ]
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  async exec (args) {
231cb0ef41Sopenharmony_ci    return otplease(this.npm, { ...this.npm.flatOptions }, (opts) => {
241cb0ef41Sopenharmony_ci      switch (args[0]) {
251cb0ef41Sopenharmony_ci        case 'add':
261cb0ef41Sopenharmony_ci          return this.add(args[1], args[2], args[3], opts)
271cb0ef41Sopenharmony_ci        case 'ls':
281cb0ef41Sopenharmony_ci          return this.ls(args[1], opts)
291cb0ef41Sopenharmony_ci        case 'rm':
301cb0ef41Sopenharmony_ci          return this.rm(args[1], opts)
311cb0ef41Sopenharmony_ci        case 'update':
321cb0ef41Sopenharmony_ci        case 'up':
331cb0ef41Sopenharmony_ci          return this.update(args[1], args[2], args[3], opts)
341cb0ef41Sopenharmony_ci        default:
351cb0ef41Sopenharmony_ci          throw this.usageError()
361cb0ef41Sopenharmony_ci      }
371cb0ef41Sopenharmony_ci    })
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  async add (pkg, uri, secret, opts) {
411cb0ef41Sopenharmony_ci    const hook = await hookApi.add(pkg, uri, secret, opts)
421cb0ef41Sopenharmony_ci    if (opts.json) {
431cb0ef41Sopenharmony_ci      this.npm.output(JSON.stringify(hook, null, 2))
441cb0ef41Sopenharmony_ci    } else if (opts.parseable) {
451cb0ef41Sopenharmony_ci      this.npm.output(Object.keys(hook).join('\t'))
461cb0ef41Sopenharmony_ci      this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t'))
471cb0ef41Sopenharmony_ci    } else if (!this.npm.silent) {
481cb0ef41Sopenharmony_ci      this.npm.output(`+ ${this.hookName(hook)} ${opts.unicode ? ' ➜ ' : ' -> '} ${hook.endpoint}`)
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  async ls (pkg, opts) {
531cb0ef41Sopenharmony_ci    const hooks = await hookApi.ls({ ...opts, package: pkg })
541cb0ef41Sopenharmony_ci    if (opts.json) {
551cb0ef41Sopenharmony_ci      this.npm.output(JSON.stringify(hooks, null, 2))
561cb0ef41Sopenharmony_ci    } else if (opts.parseable) {
571cb0ef41Sopenharmony_ci      this.npm.output(Object.keys(hooks[0]).join('\t'))
581cb0ef41Sopenharmony_ci      hooks.forEach(hook => {
591cb0ef41Sopenharmony_ci        this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t'))
601cb0ef41Sopenharmony_ci      })
611cb0ef41Sopenharmony_ci    } else if (!hooks.length) {
621cb0ef41Sopenharmony_ci      this.npm.output("You don't have any hooks configured yet.")
631cb0ef41Sopenharmony_ci    } else if (!this.npm.silent) {
641cb0ef41Sopenharmony_ci      if (hooks.length === 1) {
651cb0ef41Sopenharmony_ci        this.npm.output('You have one hook configured.')
661cb0ef41Sopenharmony_ci      } else {
671cb0ef41Sopenharmony_ci        this.npm.output(`You have ${hooks.length} hooks configured.`)
681cb0ef41Sopenharmony_ci      }
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci      const table = new Table({ head: ['id', 'target', 'endpoint'] })
711cb0ef41Sopenharmony_ci      hooks.forEach((hook) => {
721cb0ef41Sopenharmony_ci        table.push([
731cb0ef41Sopenharmony_ci          { rowSpan: 2, content: hook.id },
741cb0ef41Sopenharmony_ci          this.hookName(hook),
751cb0ef41Sopenharmony_ci          hook.endpoint,
761cb0ef41Sopenharmony_ci        ])
771cb0ef41Sopenharmony_ci        if (hook.last_delivery) {
781cb0ef41Sopenharmony_ci          table.push([
791cb0ef41Sopenharmony_ci            {
801cb0ef41Sopenharmony_ci              colSpan: 1,
811cb0ef41Sopenharmony_ci              content: `triggered ${relativeDate(hook.last_delivery)}`,
821cb0ef41Sopenharmony_ci            },
831cb0ef41Sopenharmony_ci            hook.response_code,
841cb0ef41Sopenharmony_ci          ])
851cb0ef41Sopenharmony_ci        } else {
861cb0ef41Sopenharmony_ci          table.push([{ colSpan: 2, content: 'never triggered' }])
871cb0ef41Sopenharmony_ci        }
881cb0ef41Sopenharmony_ci      })
891cb0ef41Sopenharmony_ci      this.npm.output(table.toString())
901cb0ef41Sopenharmony_ci    }
911cb0ef41Sopenharmony_ci  }
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  async rm (id, opts) {
941cb0ef41Sopenharmony_ci    const hook = await hookApi.rm(id, opts)
951cb0ef41Sopenharmony_ci    if (opts.json) {
961cb0ef41Sopenharmony_ci      this.npm.output(JSON.stringify(hook, null, 2))
971cb0ef41Sopenharmony_ci    } else if (opts.parseable) {
981cb0ef41Sopenharmony_ci      this.npm.output(Object.keys(hook).join('\t'))
991cb0ef41Sopenharmony_ci      this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t'))
1001cb0ef41Sopenharmony_ci    } else if (!this.npm.silent) {
1011cb0ef41Sopenharmony_ci      this.npm.output(`- ${this.hookName(hook)} ${opts.unicode ? ' ✘ ' : ' X '} ${hook.endpoint}`)
1021cb0ef41Sopenharmony_ci    }
1031cb0ef41Sopenharmony_ci  }
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  async update (id, uri, secret, opts) {
1061cb0ef41Sopenharmony_ci    const hook = await hookApi.update(id, uri, secret, opts)
1071cb0ef41Sopenharmony_ci    if (opts.json) {
1081cb0ef41Sopenharmony_ci      this.npm.output(JSON.stringify(hook, null, 2))
1091cb0ef41Sopenharmony_ci    } else if (opts.parseable) {
1101cb0ef41Sopenharmony_ci      this.npm.output(Object.keys(hook).join('\t'))
1111cb0ef41Sopenharmony_ci      this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t'))
1121cb0ef41Sopenharmony_ci    } else if (!this.npm.silent) {
1131cb0ef41Sopenharmony_ci      this.npm.output(`+ ${this.hookName(hook)} ${opts.unicode ? ' ➜ ' : ' -> '} ${hook.endpoint}`)
1141cb0ef41Sopenharmony_ci    }
1151cb0ef41Sopenharmony_ci  }
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  hookName (hook) {
1181cb0ef41Sopenharmony_ci    let target = hook.name
1191cb0ef41Sopenharmony_ci    if (hook.type === 'owner') {
1201cb0ef41Sopenharmony_ci      target = '~' + target
1211cb0ef41Sopenharmony_ci    }
1221cb0ef41Sopenharmony_ci    return target
1231cb0ef41Sopenharmony_ci  }
1241cb0ef41Sopenharmony_ci}
1251cb0ef41Sopenharmony_cimodule.exports = Hook
126