Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
feat(cmd): add ping command
Browse files Browse the repository at this point in the history
  • Loading branch information
emkay authored and zkat committed Nov 2, 2018
1 parent 0135065 commit 28c32b5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/tink.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (require.main === module) {
}
module.exports = main
function main () {
require('npmlog').heading = 'tink'
return require('yargs')
.commandDir('../lib/yargs-modules')
.demandCommand()
Expand Down
37 changes: 37 additions & 0 deletions lib/commands/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const npmConfig = require('../config.js')
const libnpm = require('libnpm')
const figgyPudding = require('figgy-pudding')
const log = require('npmlog')

module.exports = ping

const PingConfig = figgyPudding({
json: {},
registry: {},
silent: {}
})

function ping (argv) {
const opts = PingConfig(npmConfig().concat(argv))

log.notice('PING', opts.registry)
const start = Date.now()
return libnpm.fetch.json('/-/ping?write=true', opts).catch(() => ({})).then(details => {
if (opts.silent) {
} else {
const time = Date.now() - start
log.notice('PONG', `${time / 1000}ms`)
if (opts.json) {
console.log(JSON.stringify({
registry: opts.registry,
time,
details
}, null, 2))
} else if (Object.keys(details).length) {
log.notice('PONG', `${JSON.stringify(details, null, 2)}`)
}
}
})
}
34 changes: 34 additions & 0 deletions lib/yargs-modules/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

module.exports = {
command: 'ping',
describe: 'ping registry',
builder (yargs) {
return yargs.help().alias('help', 'h').options({
json: {
alias: 'j',
default: false,
describe: 'Output in JSON format',
choices: [false, true]
},
loglevel: {
default: 'warn',
describe: 'Logger output level',
choices: ['silent', 'error', 'warn', 'http', 'verbose', 'info', 'notice']
},
silent: {
alias: 's',
default: false,
describe: 'Do not display the PONG response',
choices: [false, true]
},
registry: {
alias: 'r',
default: 'https://registry.npmjs.org',
describe: 'Registry to ping'
}
})
},
// lazy-load subcommands
handler (argv) { return require('../commands/ping.js')(argv) }
}

0 comments on commit 28c32b5

Please sign in to comment.