-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathindex.js
executable file
·44 lines (35 loc) · 975 Bytes
/
index.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
#!/usr/bin/env node
const request = require('request')
const chalk = require('chalk')
const Spinner = require('cli-spinner').Spinner
const isChinese = require('is-chinese')
const urlencode = require('urlencode')
const noCase = require('no-case')
const config = require('./lib/config')
const Parser = require('./lib/parser')
let word = process.argv.slice(2).join(' ')
if (!word) {
console.log('Usage: yd <WORD_TO_QUERY>')
process.exit()
}
const spinner = new Spinner('努力查询中... %s')
if (config.spinner) {
spinner.setSpinnerString('|/-\\')
spinner.start()
}
const isCN = isChinese(word)
word = isCN ? word : noCase(word)
const options = {
'url': config.getURL(word) + urlencode(word),
'proxy': config.proxy || null
}
const ColorOutput = chalk.keyword(config.color)
request(options, (error, response, body) => {
if (error) {
console.error(error)
}
if (config.spinner) {
spinner.stop(true)
}
console.log(ColorOutput(Parser.parse(isCN, body)))
})