-
Notifications
You must be signed in to change notification settings - Fork 1
/
explore.ts
55 lines (47 loc) · 1.63 KB
/
explore.ts
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
import Command from '../lib/base'
import {flags} from '@oclif/command'
import {AxiosInstance} from 'axios'
const chalk = require('chalk')
import cli from 'cli-ux'
export default class Explore extends Command {
static description = 'explore talks on giki.app'
static flags = {
help: flags.help({char: 'h'}),
// flag with a value (-n, --number=VALUE)
number: flags.integer({char: 'n', description: 'number of talks to explore, [1-20], default 5'}),
}
async doCommand(userConfig: { token: any }, client: AxiosInstance) {
const {flags} = this.parse(Explore)
let n = 5
if (flags.number) {
const input_n = flags.number
if (input_n >= 1 && input_n <= 20) {
n = input_n
} else if (input_n > 20) {
n = 20
} else {
n = 5
}
}
// start the spinner
cli.action.start(`getting latest ${n} talks...`)
const resp = await client.get('talks/explore')
// stop the spinner
cli.action.stop() // shows 'starting a process... done'
// sort by id desc
const origData = resp.data.data
origData.slice(0, n).reverse().forEach((talk: any) => {
// likes
let likes_str = ''
if (talk.likes && talk.likes > 0) {
likes_str = chalk.redBright(`\t♥ ${talk.likes}`)
}
this.log(`${chalk.blue(talk.name)}\t${chalk.grey(talk.created_at)}${likes_str}:`)
if (talk.tags && talk.tags.length > 0) {
const tag_str = talk.tags.map((t: any) => '#' + t + '#').reduce((a: any, b: any) => a + ', ' + b)
this.log(`${chalk.yellow(tag_str)}`)
}
this.log(`${talk.text}\n------------------------------\n`)
})
}
}