-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin.js
106 lines (94 loc) · 2.81 KB
/
bin.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { promises: { readFile } } = require('fs')
const parse = require('yargs-parser')
const levelheaded = require('levelheaded')
const { load } = require('js-yaml')
const notifier = require('.')
const formatter = require('./lib/formatter')
const [, , ...argv] = process.argv
const trim = i => i.trim()
async function readYaml (file) {
if (!file) {
return {}
}
const content = await readFile(file, 'utf8')
return load(content)
}
const count = items => Object.entries(
items.reduce(
(accumulator, item) => {
accumulator[item] = accumulator[item] || 0
accumulator[item]++
return accumulator
},
{}
)
).map(
entry => entry.join(': ')
).join(', ')
start(argv)
async function start (argv) {
const args = parse(argv)
const options = {}
const config = args.config || process.env.CONFIG
options.logLevel = args.logLevel || process.env.LOG_LEVEL || 'warn'
options.logFormat = args.logFormat || process.env.LOG_FORMAT || 'plain'
const logger = levelheaded({ minimal: options.logLevel })
const _logger = formatter(logger, options.logFormat)
process.on(
'unhandledRejection',
(error) => _logger.error(
error instanceof Error
? [error.message, error.stack].join('\n')
: `${error}`
)
)
process.on(
'uncaughtException',
(error, origin) => _logger.error(
error instanceof Error
? [error.message, error.stack, origin].join('\n')
: `${error} ${origin}`
)
)
try {
const configData = await readYaml(config)
// Priority: CLI > YAML > ENV
options.interval = args.interval || configData.interval || process.env.INTERVAL
options.webhook = args.webhook || configData.webhook || process.env.WEBHOOK
options.channel = args.channel || configData.channel || process.env.CHANNEL
options.emoji = args.emoji || configData.emoji || process.env.EMOJI
options.dryRun = Boolean(args.dryRun ?? configData.dryRun ?? process.env.DRY_RUN)
options.list = (() => {
const feed = args.feed || args.feeds || configData.feed || configData.feeds || process.env.FEED || process.env.FEEDS
if (Array.isArray(feed)) {
return feed
}
if (typeof feed === 'string') {
return feed.split(',').map(trim)
}
return []
})()
} catch (error) {
_logger.error(error)
process.exit(1)
}
if (!options.list) {
_logger.error('No feed specified', {
options: JSON.stringify(options)
})
process.exit(1)
}
Promise.all(
options.list.map(
(feed) => notifier({ ...options, logger, feed })
)
).then(
results => _logger.info(`${options.list.length} feeds produced ${results.length} results: ${count(results)}`)
).catch(
error => _logger.error(
error instanceof Error
? [error.message, error.stack].join('\n')
: `${error}`
)
)
}