ping.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const log = require('npmlog')
  2. const pingUtil = require('./utils/ping.js')
  3. const BaseCommand = require('./base-command.js')
  4. class Ping extends BaseCommand {
  5. /* istanbul ignore next - see test/lib/load-all-commands.js */
  6. static get description () {
  7. return 'Ping npm registry'
  8. }
  9. /* istanbul ignore next - see test/lib/load-all-commands.js */
  10. static get params () {
  11. return ['registry']
  12. }
  13. /* istanbul ignore next - see test/lib/load-all-commands.js */
  14. static get name () {
  15. return 'ping'
  16. }
  17. exec (args, cb) {
  18. this.ping(args).then(() => cb()).catch(cb)
  19. }
  20. async ping (args) {
  21. log.notice('PING', this.npm.config.get('registry'))
  22. const start = Date.now()
  23. const details = await pingUtil(this.npm.flatOptions)
  24. const time = Date.now() - start
  25. log.notice('PONG', `${time}ms`)
  26. if (this.npm.config.get('json')) {
  27. this.npm.output(JSON.stringify({
  28. registry: this.npm.config.get('registry'),
  29. time,
  30. details,
  31. }, null, 2))
  32. } else if (Object.keys(details).length)
  33. log.notice('PONG', `${JSON.stringify(details, null, 2)}`)
  34. }
  35. }
  36. module.exports = Ping