-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss2mail.js
executable file
·54 lines (49 loc) · 1.52 KB
/
rss2mail.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
#!/usr/bin/env node
import {pipeline} from 'stream'
import util from 'util'
import fs from 'fs'
import path from 'path'
import FeedParser from 'feedparser'
import lockfile from 'proper-lockfile'
import MailStream from './index.js'
function errx(e) {
console.error(`rss2mail error (${process.env.RSS2MAIL || process.pid}):`, e.message)
process.exit(1)
}
let __dirname = path.dirname(new URL('', import.meta.url).pathname)
let args
try {
args = util.parseArgs({allowPositionals: true, options: {
rnews: { type: 'boolean' }, help: { type: 'boolean', short: 'h' },
history: { type: 'string' }, f: { type: 'string' },
o: { type: 'string' }, 'no-lock': { type: 'boolean' }
}})
} catch (err) {
errx(err)
}
if (args.values.help) {
process.stdout.write(fs.readFileSync(__dirname + '/usage.txt').toString())
process.exit(0)
}
let streams = [process.stdin, new FeedParser(), new MailStream(args)]
let out = process.stdout
let lock = Promise.resolve( () => {/* nop */})
if (args.values.o) {
out = fs.createWriteStream(args.values.o, {flags: 'a'})
out.on('error', errx)
if (!args.values['no-lock']) {
lock = lockfile.lock(args.values.o, {
retries: { // living dangerously
retries: 0, forever: true, minTimeout: 10, maxTimeout: 100,
randomize: true
}
})
}
}
streams.push(out)
lock.then( unlock => {
pipeline(...streams, err => {
if (err && err.code !== 'EPIPE') errx(err)
unlock()
})
}).catch(errx)