UNPKG

1 kBJavaScriptView Raw
1'use strict';
2
3// Small utility that pings a URL and waits for 5 successful pings
4
5const request = require('./index.js');
6const commander = require('commander');
7
8// We use process.exit() intentionally here
9/* eslint no-process-exit: 1 */
10
11// Parse command line uri arg into a URI string or an options object
12const parse = (uri) => {
13 try {
14 return JSON.parse(uri);
15 } catch (e) {
16 return uri;
17 }
18};
19
20// Command line interface
21const runCLI = () => {
22 // Parse command line options
23 commander
24 .arguments('<uri>')
25 .action((uri) => {
26 commander.uri = uri;
27 })
28 .parse(process.argv);
29
30 // Ping the target URI every 250 msec, for 5 sec max
31 const opt = parse(commander.uri);
32 console.log('Pinging %s', typeof opt === 'object' ? opt.uri : opt);
33 request.waitFor(opt, function(err, val) {
34 if (err) {
35 console.log('Timed out');
36 process.exit(1);
37 }
38 console.log('Ready');
39 process.exit(0);
40 });
41};
42
43// Export our public functions
44exports.runCLI = runCLI;