forked from Snipa22/nodejs-pool
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathscript_utils.js
More file actions
62 lines (52 loc) · 1.83 KB
/
script_utils.js
File metadata and controls
62 lines (52 loc) · 1.83 KB
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
"use strict";
const initMini = require("./init_mini.js");
const parseArgv = require("./parse_args.js");
function exitWithError(message) {
console.error(message);
process.exit(1);
}
function forEachEntry(database, reader, iterator, reverse) {
const txn = global.database.env.beginTxn({ readOnly: true });
const cursor = new global.database.lmdb.Cursor(txn, database);
const startMethod = reverse === true ? "goToLast" : "goToFirst";
const nextMethod = reverse === true ? "goToPrev" : "goToNext";
for (let found = cursor[startMethod](); found; found = cursor[nextMethod]()) {
cursor[reader](function onEntry(key, data) {
iterator(key, data);
}); // jshint ignore:line
}
cursor.close();
txn.commit();
}
function createCli(options) {
const argv = parseArgv(process.argv.slice(2), options);
function arg(name, errorMessage) {
const value = argv[name];
if (!value) exitWithError(errorMessage);
return value;
}
return {
argv,
arg,
init: initMini.init,
get(name, fallback = null) {
return typeof argv[name] === "undefined" ? fallback : argv[name];
},
forEachBinaryEntry(database, iterator, options = {}) {
forEachEntry(database, "getCurrentBinary", iterator, options.reverse);
},
forEachStringEntry(database, iterator, options = {}) {
forEachEntry(database, "getCurrentString", iterator, options.reverse);
},
jsonArg(name, missingMessage, invalidPrefix) {
const value = arg(name, missingMessage);
try {
return JSON.parse(value);
} catch (_error) {
exitWithError(invalidPrefix + value);
}
}
};
}
createCli.init = initMini.init;
module.exports = createCli;