-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
68 lines (60 loc) · 1.69 KB
/
cli.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
#!/usr/bin/env node
'use strict';
const exec = require('child_process').exec;
const platform = require('os').platform();
const meow = require('meow');
const prompt = require('prompt');
const chalk = require('chalk');
const moment = require('moment');
const ccnews = require('.');
// Borrowed from https://github.com/mtharrison/hackernews
const shellOpenCommand = {
win32: 'start ',
linux: 'xdg-open ',
darwin: 'open '
}[platform];
prompt.message = '';
const cli = meow(`
Usage
$ ccnews [symbol]
Example:
$ ccnews eth
`);
ccnews(cli.input[0])
.then(articles => {
articles.forEach((article, index) => {
const now = moment(new Date());
const end = moment(article.pubDate);
const duration = moment.duration(now.diff(end));
const hours = duration.asHours();
const days = duration.asDays();
let since;
const chalkIt = chalk.blue;
if (hours > 24) {
const unit = days < 2 ? 'day' : 'days';
since = `${Math.floor(days)} ${unit} ago...`;
} else {
const unit = hours < 2 ? 'hour' : 'hours';
since = `${Math.floor(hours)} ${unit} ago...`;
}
const selector = chalk.bgBlue(`[ ${index + 1} ]`);
console.log((`${selector} ${article.title}`));
console.log(chalkIt(`\t${article.publisher} - ${since}`));
});
prompt.get({
name: 'articleIndex',
description: 'Enter the associated number to open article',
pattern: /[0-9]/g, // Create pattern for less than article length
before: value => value - 1
}, (err, {articleIndex}) => {
if (err) {
throw new Error(err.message);
}
exec(`${shellOpenCommand}${articles[articleIndex].shortLink}`, err => {
if (err) {
throw new Error(err.message);
}
});
});
})
.catch(console.log);