-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin.js
executable file
·77 lines (64 loc) · 1.59 KB
/
bin.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
#!/usr/bin/env node
'use strict'
const yargs = require('yargs')
const chalk = require('chalk')
const Conf = require('conf')
const text = require('text-prompt')
const select = require('select-prompt')
const argv = yargs.argv
const conf = new Conf()
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
sms
\n`)
process.exit()
}
if (argv.number || argv.n) {
process.stdout.write(conf.get('number') + '\n')
process.exit(0)
}
const sms = require('.')
const ui = require('./ui')
const showError = (err) => {
const msg = err.message || err.toString()
process.stdout.write(chalk.red(msg) + '\n')
}
const prompt = (prompt, ...args) => {
return new Promise((resolve, reject) => {
prompt(...args)
.once('submit', resolve)
.once('abort', () => reject('You rejected the prompt.'))
})
}
(async () => {
let sid = conf.get('sid')
if (!sid) {
if (process.env.TWILIO_SID) sid = process.env.TWILIO_SID
else {
sid = await prompt(text, 'Please enter your Twilio SID.')
conf.set('sid', sid)
}
}
let token = conf.get('token')
if (!token) {
if (process.env.TWILIO_TOKEN) token = process.env.TWILIO_TOKEN
else {
token = await prompt(text, 'Please enter your Twilio token.')
conf.set('token', token)
}
}
const client = sms(sid, token)
let number = conf.get('number')
if (!number) {
if (process.env.TWILIO_NUMBER) number = process.env.TWILIO_NUMBER
else {
const choices = (await client.numbers()).map((nr) => ({
title: nr.name, value: nr.nr
}))
number = await prompt(select, 'Please select a number.', choices)
}
}
await ui(client, number)
})()
.catch(showError)