UNPKG

3.19 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs');
4const path = require('path');
5const url = require('url');
6const util = require('util');
7
8const pkg = require('../package.json');
9const jayson = require('../');
10const program = require('commander');
11const eyes = require('eyes');
12const net = require('net')
13
14// initialize program and define arguments
15program.version(pkg.version)
16 .option('-m, --method [name]', 'Method', String)
17 .option('-p, --params [json]', 'Array or Object to use as parameters', JSON.parse)
18 .option('-u, --url [url]', 'URL to server', url.parse)
19 .option('-q, --quiet', 'Only output the response value and any errors', Boolean)
20 .option('-s, --socket [path] or [ip:port]', 'Path to UNIX socket, or TCP socket address', parseSocket)
21 .option('-j, --json', 'Only output the response value as JSON (implies --quiet)')
22 .option('-c, --color', 'Color output', Boolean)
23 .parse(process.argv);
24
25const inspect = eyes.inspector({
26 stream: null,
27 styles: program.color ? eyes.defaults.styles : {all: false}
28});
29
30// quiet is implied if json is specified
31if(program.json) program.quiet = true;
32
33// wrapper for printing different kinds of output
34const std = {
35 out: getPrinter({ fn: console.log }),
36 err: getPrinter({ fn: console.error })
37};
38
39// do we have all arguments required to do something?
40if(!(program.method && (program.url || program.socket))) {
41 std.err.result(program.helpInformation());
42 return process.exit(-1);
43}
44
45const client = (program.socket && program.socket.host)
46 ? jayson.client.tcp(program.socket)
47 : (program.url && program.url.protocol == 'https:')
48 ? jayson.client.https(program.url || program.socket)
49 : jayson.client.http(program.url || program.socket);
50
51std.out.noise(
52 colorize('magenta', '-> %s(%s)'),
53 program.method,
54 Array.isArray(program.params) ? program.params.join(', ') : JSON.stringify(program.params)
55);
56
57client.request(program.method, program.params, function(err, response) {
58 if(err) {
59 std.err.noise(colorize('red', '<- %s'), err.stack);
60 return process.exit(-1);
61 }
62
63 if(!response) {
64 std.err.noise(colorize('red', '<- %s'), 'empty response');
65 return process.exit(-1);
66 }
67
68 if(program.json) {
69 std.out.result('%s', JSON.stringify(response).replace("\n", ""));
70 return process.exit(0);
71 }
72
73 std.out.noise('<- %s', inspect(response), true);
74 process.exit(response.error ? response.error.code : 0);
75});
76
77function parseSocket(value) {
78 const addr = value.split(":");
79
80 if (addr.length == 2 && (net.isIP(addr[0]) || addr[0].toLowerCase() == "localhost")) {
81 return {port: addr[1], host: addr[0]};
82 }
83
84 return {socketPath: path.normalize(value)};
85}
86
87function colorize(color, format) {
88 return program.color
89 ? eyes.stylize(format, color, {})
90 : format;
91}
92
93function getPrinter(options) {
94
95 const fn = options.fn || console.log;
96
97 return {
98
99 // print noise (printed if program is not quiet)
100 noise: function() {
101 if(program.quiet) return;
102 return fn.apply(console, arguments);
103 },
104
105 // print results (always printed)
106 result: function() {
107 return fn.apply(console, arguments);
108 }
109
110 };
111}