UNPKG

1.82 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3// repl - evaluate pickup
4
5const fs = require('fs')
6const http = require('http')
7const https = require('https')
8const repl = require('repl')
9const { Pickup, Feed, Entry } = require('./')
10const { URL } = require('url')
11const { clear, log, dir } = require('console')
12const { pipeline, Writable } = require('readable-stream')
13
14const server = repl.start({
15 ignoreUndefined: true,
16 input: process.stdin,
17 output: process.stdout,
18 prompt: 'pickup> ',
19 useColors: true
20})
21
22function file (path) {
23 return fs.createReadStream(path).pipe(
24 new Pickup({ objectMode: true })
25 )
26}
27
28function 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// Reads all data of type from parser matching key. You can skip type or pass
42// Feed or Entry for only seeing to those. You might also limit messages.
43function 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
81const { context } = server
82
83context.Entry = Entry
84context.Feed = Feed
85context.clear = clear
86context.file = file
87context.get = get
88context.read = read