Skip to content

Commit 44181f9

Browse files
committed
first commit
0 parents  commit 44181f9

File tree

1,156 files changed

+54305
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,156 files changed

+54305
-0
lines changed

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
This was a fun exercise, learning to create a CLI app using Node.js. This "package" could be published and installed by others as a global package. This makes it usable by anyone, on any platform, running Node.js (unlike a bash or PowerShell script).
2+
3+
Using the `commander` and `inquirer` packages provides a simple way to create responses and collect input.
4+
5+
If installed as a global package this is an example of what you get when running the `qotd` command. This is an automatically generated help screen.
6+
7+
```sh
8+
> qotd
9+
10+
Usage: qotd [options] [command]
11+
12+
Options:
13+
-h, --help display help for command
14+
15+
Commands:
16+
list|ls List qotd options
17+
quick|q Return a random quote
18+
author|a Choose an author and get a quote
19+
help [command] display help for command
20+
```
21+
22+
If you use the `author` option you get a list to choose from:
23+
24+
```sh
25+
> qotd a
26+
27+
Author Quote
28+
----------------
29+
? Choose an author
30+
Buddha
31+
Mark Twain
32+
❯ Yogi Berra
33+
William Shakespeare
34+
```
35+
36+
The app is pulling from a free API of 1,600+ famous quotes. Like I said, a fun little exercise!

bin/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env node
2+
const program = require('commander')
3+
4+
const colors = require('colors')
5+
const qotd = require('../lib/qotd')
6+
const list = require('../lib/list')
7+
const author = require('../lib/author')
8+
9+
/**
10+
* List QOTD options
11+
*/
12+
program
13+
.command('list')
14+
.alias('ls')
15+
.description('List qotd options')
16+
.action(function () {
17+
list()
18+
})
19+
20+
/**
21+
* Return a random quote
22+
*/
23+
program
24+
.command('quick')
25+
.alias('q')
26+
.description('Return a random quote')
27+
.action(function () {
28+
qotd()
29+
})
30+
31+
/**
32+
* Return a quote by specific author
33+
*/
34+
program
35+
.command('author')
36+
.alias('a')
37+
.description('Choose an author and get a quote')
38+
.action(function () {
39+
author()
40+
})
41+
42+
program.parse(process.argv)

lib/author.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const inquirer = require('inquirer')
2+
const colors = require('colors')
3+
const pad = require('pad')
4+
const quotes = require('./quotes')
5+
const values = require('./values')
6+
7+
const questions = [{ type: 'list', name: 'author', message: 'Choose an author', choices: values.authors }]
8+
9+
module.exports = function () {
10+
console.log('Author Quote')
11+
console.log('----------------')
12+
13+
inquirer.prompt(questions).then(function (answers) {
14+
quotes.author(answers.author).then((msg) => {
15+
console.log(colors.bold(msg.text))
16+
console.log(colors.grey('/ ' + msg.author))
17+
console.log('')
18+
})
19+
})
20+
}

lib/list.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const colors = require('colors')
2+
const { options } = require('./values')
3+
4+
module.exports = function () {
5+
console.log('QOTD OPTIONS')
6+
console.log('----------------')
7+
8+
options.map((o) => {
9+
console.log('%s %s', colors.bold(o.name), colors.grey('/ ' + o.descr))
10+
console.log('')
11+
})
12+
}

lib/qotd.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const colors = require('colors')
2+
const quotes = require('./quotes')
3+
4+
module.exports = function () {
5+
console.log('Random Quote')
6+
console.log('----------------')
7+
8+
quotes.random().then((msg) => {
9+
console.log(colors.bold(msg.text))
10+
console.log(colors.grey('/ ' + msg.author ? msg.author : 'Unknown'))
11+
console.log('')
12+
})
13+
}

lib/quotes.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const axios = require('axios')
2+
3+
const BASEURL = 'https://type.fit/api/quotes'
4+
5+
async function getQuotes() {
6+
return axios.get(BASEURL).then((result) => {
7+
return result.data
8+
})
9+
}
10+
11+
async function picker(author) {
12+
let quotes = await getQuotes()
13+
if (author) {
14+
quotes = quotes.filter((q) => q.author === author)
15+
}
16+
return quotes[Math.floor(Math.random() * quotes.length)]
17+
}
18+
19+
const aggregate = async () => {
20+
const quotes = await getQuotes()
21+
let counts = {}
22+
quotes.map((q) => {
23+
if (!q.author) return false
24+
if (counts[q.author]) {
25+
counts[q.author] += 1
26+
} else {
27+
counts[q.author] = 1
28+
}
29+
})
30+
let popular = {}
31+
Object.keys(counts).map((key) => {
32+
if (!popular.author) {
33+
popular = {
34+
author: key,
35+
count: counts[key],
36+
}
37+
}
38+
if (counts[key] > popular.count) {
39+
popular = {
40+
author: key,
41+
count: counts[key],
42+
}
43+
}
44+
})
45+
return popular
46+
}
47+
48+
exports.random = async function () {
49+
return picker()
50+
}
51+
52+
exports.popular = async function () {
53+
return aggregate()
54+
}
55+
56+
exports.author = async function (author) {
57+
return picker(author)
58+
}

lib/values.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exports.options = [
2+
{ name: 'help', descr: 'Show how to use QOTD' },
3+
{ name: 'quick', descr: 'Return a random quote' },
4+
{ name: 'author', descr: 'Return a quote from specific author' },
5+
{ name: 'popular', descr: 'Show the most popular author' },
6+
]
7+
8+
exports.authors = ['Buddha', 'Mark Twain', 'Yogi Berra', 'William Shakespeare']

node_modules/.yarn-integrity

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)