-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·89 lines (74 loc) · 2.03 KB
/
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
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
#!/usr/bin/env node
'use strict'
const meow = require('meow')
const updateNotifier = require('update-notifier')
const wer = require('wer')
const request = require('request-promise-native')
const shoutError = require('shout-error')
const shoutSuccess = require('shout-success')
const { gray } = require('chalk')
const ora = require('ora')
const rightPad = require('./lib/right-pad')
const cli = meow(
`
Usage:
$ workfrom
Example:
$ workfrom
$ workfrom -r=50
$ workfrom -e=starbucks
Options:
-r, --radius Radius
-e, --exclude Exclude a certain pattern
-h, --help Show help options
-v, --version Show version
`,
{
alias: {
r: 'radius',
h: 'help',
v: 'version'
}
}
)
updateNotifier({ pkg: cli.pkg }).notify()
const run = async () => {
const hasRadius = cli.flags.r
const excludePattern = cli.flags.e
const { lat, long } = await wer()
const radius = hasRadius || 10
const spinner = ora(`Finding places to work ${radius} miles near you.`)
try {
spinner.start()
const { meta, response } = JSON.parse(
await request(
`http://api.workfrom.co/places/ll/${lat},${long}&radius=${radius}`
)
)
spinner.stop()
if (meta.results.total === 0) {
return shoutError(
`We couldn't find any place to work ${radius} miles near you.`
)
}
const filteredResponse = excludePattern
? response.filter(({title}) => !title.toLowerCase().includes(excludePattern))
: response
filteredResponse.map(({ title, street, city, description, distance }) => {
const log = `
${gray(rightPad('Name:', 12))} ${title}
${gray(rightPad('Street:', 12))} ${street}
${gray(rightPad('City:', 12))} ${city},
${gray(rightPad('Distance:', 12))} ${distance} miles
${gray(rightPad('Description:', 12))} ${description}
`
return console.log(log)
})
shoutSuccess(
`We found ${filteredResponse.length} places to work ${radius} miles near you.`
)
} catch (err) {
console.log(err)
}
}
run(cli)