1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 | const fs = require('fs')
|
6 | const http = require('http')
|
7 | const https = require('https')
|
8 | const repl = require('repl')
|
9 | const { Pickup, Feed, Entry } = require('./')
|
10 | const { URL } = require('url')
|
11 | const { clear, log, dir } = require('console')
|
12 | const { pipeline, Writable } = require('readable-stream')
|
13 |
|
14 | const server = repl.start({
|
15 | ignoreUndefined: true,
|
16 | input: process.stdin,
|
17 | output: process.stdout,
|
18 | prompt: 'pickup> ',
|
19 | useColors: true
|
20 | })
|
21 |
|
22 | function file (path) {
|
23 | return fs.createReadStream(path).pipe(
|
24 | new Pickup({ objectMode: true })
|
25 | )
|
26 | }
|
27 |
|
28 | function get (uri) {
|
29 | const urlObj = new URL(uri)
|
30 | const mod = urlObj.protocol === 'http:' ? http : https
|
31 |
|
32 | const parser = new Pickup({ objectMode: true })
|
33 |
|
34 | mod.get(urlObj, (res) => {
|
35 | res.pipe(parser)
|
36 | })
|
37 |
|
38 | return parser
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 | function read (parser, type, key, limit = Infinity) {
|
44 | let count = 0
|
45 |
|
46 | pipeline(parser, new Writable({
|
47 | write (obj, enc, cb) {
|
48 | if (typeof type === 'string') key = type
|
49 |
|
50 | if (typeof type === 'number') {
|
51 | limit = type
|
52 | type = undefined
|
53 | }
|
54 |
|
55 | if (typeof key === 'number') {
|
56 | limit = key
|
57 | key = undefined
|
58 | }
|
59 |
|
60 | if (count >= limit) {
|
61 | return cb()
|
62 | }
|
63 |
|
64 | if (type instanceof Object && !(obj instanceof type)) {
|
65 | return cb()
|
66 | }
|
67 |
|
68 | dir(key ? obj[key] : obj, { colors: true })
|
69 |
|
70 | count++
|
71 |
|
72 | cb()
|
73 | },
|
74 | objectMode: true
|
75 | }), er => {
|
76 | log(er || 'ok')
|
77 | server.displayPrompt()
|
78 | })
|
79 | }
|
80 |
|
81 | const { context } = server
|
82 |
|
83 | context.Entry = Entry
|
84 | context.Feed = Feed
|
85 | context.clear = clear
|
86 | context.file = file
|
87 | context.get = get
|
88 | context.read = read
|