-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
executable file
·77 lines (66 loc) · 1.62 KB
/
repl.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
#!/usr/bin/env node
// repl - kick the tires during development
// @ts-check
const fs = require('fs');
const repl = require('repl');
const split = require('binary-split');
const {Manger, createLevelDB} = require('./');
const {clear, dir, log} = require('console');
// eslint-disable-next-line no-unused-vars
const {Readable} = require('readable-stream');
function createManger() {
const name = process.argv[2] || '/tmp/manger-repl';
const db = createLevelDB(name);
return new Manger(db, {objectMode: true});
}
const server = repl.start({
ignoreUndefined: true,
input: process.stdin,
output: process.stdout,
prompt: 'manger> ',
useColors: true,
});
/**
* Prints objects, or one of their properties, read from stream.
*
* @param {Readable} s A readable stream.
* @param {string} prop A property name for filtering.
*/
function read(s, prop) {
s.once('error', er => {
log(er);
})
.on('readable', () => {
let obj;
while ((obj = s.read()) !== null) {
const item = prop ? obj[prop] : obj;
dir(item, {colors: true});
server.displayPrompt();
}
})
.on('end', () => {
s.removeAllListeners();
log('ok');
server.displayPrompt();
});
}
const cache = createManger();
/**
* Fills the cache with some feeds.
*
* @param {string} prop The propery name to log (`title` by default).
*/
function fill(prop = 'title') {
read(
fs
.createReadStream('./test/data/feeds')
.pipe(split())
.pipe(cache.feeds()),
prop,
);
}
const {context} = server;
context.clear = clear;
context.fill = fill;
context.read = read;
context.cache = cache;