-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
118 lines (111 loc) · 3.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
var defaultConfigFile = home + '/.recli.yml';
var r = require('rethinkdb'),
rhist = require('repl.history'),
coffee = require('coffee-script'),
repl = require('repl'),
util = require('util'),
os = require('os'),
fs = require('fs'),
yaml = require('js-yaml'),
misc = require('./lib/misc'),
pj = require('./package.json');
opts = require('optimist')
.boolean(['c', 'colors', 'j', 'n', 'r', 'v', 's'])
.default('colors', true)
.default('file', defaultConfigFile)
.alias('coffee', 'c')
.alias('database', 'd')
.alias('file', 'f')
.alias('host', 'h')
.alias('user', 'u')
.alias('json', 'j')
.alias('port', 'p')
.alias('raw', 'r')
.alias('stream', 's')
.alias('version', 'v')
.argv;
var writer = function(rawResult) {
var result;
if (opts.stream) {
var i = 0;
result = '';
for (i in rawResult) {
result += JSON.stringify(rawResult[i]) + os.EOL;
}
} else if (opts.raw) {
result = JSON.stringify(rawResult);
} else if (opts.json) {
result = JSON.stringify(rawResult, null, 2);
} else {
result = util.inspect(rawResult, {depth: null, colors: opts.colors});
}
return result;
}
exports.recli = function() {
if (opts.help) {
misc.usage();
} else if (opts.version) {
console.log(pj.version);
} else {
var globalSettings = {};
var userSettings = {};
if (opts.file) {
// Only load global config file if a file has not been specified
if (opts.file === defaultConfigFile) {
try {
globalSettings = yaml.safeLoad(fs.readFileSync('/etc/recli.yml', 'utf8'));
} catch (e) {}
}
try {
userSettings = yaml.safeLoad(fs.readFileSync(opts.file, 'utf8'));
} catch (e) {}
}
opts = misc.setupOptions(opts, globalSettings, userSettings);
r.connect({
host: opts.host,
port: opts.port,
db: opts.database,
user: opts.user,
password: opts.password,
authKey: opts.auth
}, function(err, conn) {
if (err) {
throw err;
} else {
if (opts._.length) {
var code = opts._.join(' ');
if (opts.coffee) {
code = coffee.compile(code, {bare: true});
}
var re = eval(code);
misc.evalResult(conn, re, function(e, result) {
if (e) {
throw e;
} else {
// Don't use console.log here. console.log is asynchronous so
// big results won't be fully written if we call process.exit
// too early.
process.stdout.write(writer(result), function() {
process.exit();
});
}
});
} else {
var cli = repl.start({prompt: "recli> ",
eval: misc.replEval,
writer: writer});
cli.context.r = r;
cli.context.conn = conn;
cli.context.coffee = opts.coffee;
cli.on('exit', function () {
console.log('');
process.exit();
});
rhist(cli, home + '/.recli_history');
}
}
});
}
};
exports.recli();