-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (53 loc) · 1.47 KB
/
index.js
File metadata and controls
62 lines (53 loc) · 1.47 KB
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
#!/usr/bin/env node
const chalk = require('chalk');
const yargs = require('yargs');
const fetch = require('node-fetch');
const prompts = require('prompts');
const opn = require('opn');
(async () => {
const argv = yargs
.usage('Usage: reader [options]')
.alias('e', 'exit')
.describe('e', 'Exit command')
.help('h')
.alias('h', 'help')
.epilog('GitHub: https://github.com/gorillab/reader-cli')
.argv;
let { exit } = argv;
const res = await fetch('https://5mins.fun/api/v1/posts', {
method: 'GET',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
});
const posts = await res.json();
console.log(`Today latest news:\n`);
if (posts.length) {
console.log(posts.map((post, index) => {
return `${chalk.gray(index + 1)}. ${chalk.hex('#1293fe')(post.title)}${post.content ? `\n${chalk.gray(post.content)}` : ''}\n${chalk.gray.underline(post.url)}\n`;
}).join('\n'));
while (!exit) {
await prompts({
type: 'text',
name: 'postNumber',
message: '(Type `exit` to exit) Enter post number to read:',
}, {
onSubmit: (prompt, postNumber) => {
try {
if (postNumber === 'exit') {
exit = true;
}
opn(posts[parseInt(postNumber) - 1].url);
} catch (e) {}
},
onCancel: () => {
exit = true;
},
});
}
process.exit();
} else {
console.log('No posts');
}
})();