-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·202 lines (177 loc) · 5.46 KB
/
cli.js
File metadata and controls
executable file
·202 lines (177 loc) · 5.46 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env node
'use strict';
const dns = require('dns');
const got = require('got');
const cheerio = require('cheerio');
const chalk = require('chalk');
const ora = require('ora');
const logUpdate = require('log-update');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
updateNotifier({pkg}).notify();
const spinner = ora();
const arg = process.argv[2];
const optionalArg = process.argv[3];
const quotePre = `${chalk.bold.cyan('›')} `;
const authorPre = `${chalk.bold.cyan('\n~ ')} `;
const showMessage = () => {
logUpdate();
spinner.text = `${chalk.dim('Fetching')} ${chalk.bold('Quote of the day')}`;
spinner.start();
};
const showError = () => {
logUpdate(`\n${quotePre} ${chalk.dim('Could not find the Quote. Please try again!')}\n`);
process.exit(1);
};
const showQuotes = (arg, optional) => {
logUpdate();
console.log(`${quotePre}${arg}`);
if (optionalArg === '-u' || optionalArg === '--author') {
console.log(`${authorPre}${optional}`);
}
console.log();
spinner.stop();
};
const quoteOftheDay = () => {
return chalk.dim('Quote of the Day');
};
const dnsBrainy = () => {
dns.lookup('brainyquote.com', err => {
if (err) {
logUpdate(`\n${chalk.bold.red('› ')}${chalk.dim('Please check your internet connection')}\n`);
process.exit(1);
}
});
};
const args = ['-b', '--brainyquote', '-e', '--eduro', '-l', '--love', '-a', '--art', '-n', '--nature', '-f', '--funny', '-v', '--version', '-s', '--source', '--author'];
if (!arg || arg === '-h' || arg === '--help' || args.indexOf(arg) === -1) {
console.log(`
${chalk.cyan('Usage:')} kote <command> <option>
${chalk.cyan('Command: ')}
-b, --brainyquote ${quoteOftheDay()} : BrainyQuotes
-e, --eduro ${quoteOftheDay()} : Eduro
${chalk.cyan('Extra :')}
-l, --love ${quoteOftheDay()} : Love
-a, --art ${quoteOftheDay()} : Art
-n, --nature ${quoteOftheDay()} : Nature
-f, --funny ${quoteOftheDay()} : Funny
${chalk.cyan('Options: ')}
--author Display Author's name
${chalk.cyan('Kote: ')}
-h, --help display help
-s, --soruce extra information
-v, --version display kote's version
`);
}
if (arg === '-s' || arg === '--source') {
console.log(`
${chalk.bold.cyan('⚫')} ${chalk.bold('BrainyQuotes')}
${chalk.bold.red('⚫')} ${chalk.bold('Eduro')}
`);
}
if (arg === '-b' || arg === '--brainyquote') {
dnsBrainy();
const url = 'http://www.brainyquote.com/quotes_of_the_day.html';
showMessage();
got(url).then(res => {
const $ = cheerio.load(res.body);
const author = $('.bq-aut:link').eq(0).text().trim();
const quote = $('.b-qt:link').eq(0).text().trim();
showQuotes(`"${quote}"`, `${author}`);
}).catch(err => {
if (err) {
showError();
}
});
}
if (arg === '-e' || arg === '--eduro') {
dns.lookup('eduro.com', err => {
if (err) {
logUpdate(`\n${chalk.bold.red('› ')}${chalk.dim('Please check your internet connection')}\n`);
process.exit(1);
}
});
const url = 'http://www.eduro.com/';
showMessage();
got(url).then(res => {
const $ = cheerio.load(res.body);
const author = $('.article dailyquote p.author').eq(0).text().trim().substring(2);
const quote = $('.article dailyquote p').eq(0).text().trim();
showQuotes(`"${quote}"`, `${author}`);
}).catch(err => {
if (err) {
showError();
}
});
}
if (arg === '-l' || arg === '--love') {
dnsBrainy();
const url = 'http://www.brainyquote.com/quotes_of_the_day.html';
logUpdate();
spinner.text = `${chalk.dim('Fetching')} ${chalk.bold('Love quote of the day')}`;
spinner.start();
got(url).then(res => {
const $ = cheerio.load(res.body);
const author = $('.bq-aut:link').eq(1).text().trim();
const quote = $('.b-qt:link').eq(1).text().trim();
showQuotes(`"${quote}"`, `${author}`);
}).catch(err => {
if (err) {
showError();
}
});
}
if (arg === '-a' || arg === '--art') {
dnsBrainy();
const url = 'http://www.brainyquote.com/quotes_of_the_day.html';
logUpdate();
spinner.text = `${chalk.dim('Fetching')} ${chalk.bold('Art quote of the day')}`;
spinner.start();
got(url).then(res => {
const $ = cheerio.load(res.body);
const author = $('.bq-aut:link').eq(2).text().trim();
const quote = $('.b-qt:link').eq(2).text().trim();
showQuotes(`"${quote}"`, `${author}`);
}).catch(err => {
if (err) {
showError();
}
});
}
if (arg === '-n' || arg === '--nature') {
dnsBrainy();
const url = 'http://www.brainyquote.com/quotes_of_the_day.html';
logUpdate();
spinner.text = `${chalk.dim('Fetching')} ${chalk.bold('Nature quote of the day')}`;
spinner.start();
got(url).then(res => {
const $ = cheerio.load(res.body);
const author = $('.bq-aut:link').eq(3).text().trim();
const quote = $('.b-qt:link').eq(3).text().trim();
showQuotes(`"${quote}"`, `${author}`);
}).catch(err => {
if (err) {
showError();
}
});
}
if (arg === '-f' || arg === '--funny') {
dnsBrainy();
const url = 'http://www.brainyquote.com/quotes_of_the_day.html';
logUpdate();
spinner.text = `${chalk.dim('Fetching')} ${chalk.bold('Funny quote of the day')}`;
spinner.start();
got(url).then(res => {
const $ = cheerio.load(res.body);
const author = $('.bq-aut:link').eq(4).text().trim();
const quote = $('.b-qt:link').eq(4).text().trim();
showQuotes(`"${quote}"`, `${author}`);
}).catch(err => {
if (err) {
showError();
}
});
}
if (arg === '--version' || arg === '-v') {
console.log(`\n${quotePre}Current kote version:`, require('./package.json').version, `\n`);
}