ping.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict'
  2. const npmConfig = require('./config/figgy-config.js')
  3. const fetch = require('libnpm/fetch')
  4. const figgyPudding = require('figgy-pudding')
  5. const log = require('npmlog')
  6. const npm = require('./npm.js')
  7. const output = require('./utils/output.js')
  8. const PingConfig = figgyPudding({
  9. json: {},
  10. registry: {}
  11. })
  12. module.exports = ping
  13. ping.usage = 'npm ping\nping registry'
  14. function ping (args, silent, cb) {
  15. if (typeof cb !== 'function') {
  16. cb = silent
  17. silent = false
  18. }
  19. const opts = PingConfig(npmConfig())
  20. const registry = opts.registry
  21. log.notice('PING', registry)
  22. const start = Date.now()
  23. return fetch('/-/ping?write=true', opts).then(
  24. res => res.json().catch(() => ({}))
  25. ).then(details => {
  26. if (silent) {
  27. } else {
  28. const time = Date.now() - start
  29. log.notice('PONG', `${time / 1000}ms`)
  30. if (npm.config.get('json')) {
  31. output(JSON.stringify({
  32. registry,
  33. time,
  34. details
  35. }, null, 2))
  36. } else if (Object.keys(details).length) {
  37. log.notice('PONG', `${JSON.stringify(details, null, 2)}`)
  38. }
  39. }
  40. }).nodeify(cb)
  41. }