-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli
More file actions
81 lines (66 loc) · 1.87 KB
/
Copy pathcli
File metadata and controls
81 lines (66 loc) · 1.87 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
#!/usr/bin/env node
'use strict'
const path = require('path')
const ora = require('ora')
const meow = require('meow')
const chalk = require('chalk')
const inquirer = require('inquirer')
const logUpdate = require('log-update')
const osHomedir = require('os-homedir')
const LocalStorage = require('node-localstorage').LocalStorage
const werd = require('./api')
const print = require('./print')
const constants = require('./constants')
const profileDir = path.resolve(osHomedir(), '.werd')
const storage = new LocalStorage(profileDir)
const keyPrompt = {
name: 'key',
type: 'text',
message: 'Enter your Mashape API key:'
}
const cli = meow(`
Usage
$ werd <word> [<operation>]
Inputs
word, the word to query
operation, the query operation (default: get)
Examples
$ werd chump
$ werd program synonyms
$ werd ocean syllables
`)
Promise
.resolve(storage.getItem('mashapeApiKey'))
.then((key) => {
if (key) {
return key
}
logUpdate()
return inquirer
.prompt([keyPrompt])
.then((answers) => answers.key)
})
.then((key) => {
const api = werd(key)
const spinner = ora()
storage.setItem('mashapeApiKey', key)
logUpdate.clear()
setInterval(() => logUpdate(spinner.frame()), 100)
const word = cli.input[0]
const isRandomOp = word === 'random'
const method = isRandomOp ? api.random : cli.input[1] ? api[cli.input[1]] : api
if (!method) {
console.log(`Unknown operation "${cli.input[1]}"`)
process.exit()
}
method(word)
.then((result) => {
logUpdate.clear()
print[cli.input[1] || 'get'](isRandomOp ? result.word : cli.input[0], result)
})
.catch((err) => {
if (/404/.test(err)) logUpdate(`Word "${chalk.yellow(word)}" was not found.`)
else logUpdate('A wild error appeared!\n' + err)
})
.then(() => process.exit())
})