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