-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (40 loc) · 1.1 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
const program = require('commander')
const { prompt } = require('inquirer')
const {newContactPrompts} = require('./prompts')
const {getContacts, saveContacts} = require('./utils')
program
.version('0.0.1')
.description('Address book CLI program')
program
.command('new')
.alias('n')
.description('add a new contact')
.action(() => {
prompt(newContactPrompts)
.then(({firstName, lastName, phoneNumber}) => {
const key = firstName + ' ' + lastName
const contacts = getContacts()
contacts[key] = {firstName, lastName, phoneNumber}
saveContacts(contacts)
})
})
program
.command('list')
.alias('l')
.description('list all contacts')
.action(() => {
const contacts = getContacts()
prompt([
{
type: 'list',
name: 'selected',
message: 'Select a contact',
choices: Object.keys(contacts)
}
])
.then(({selected}) => {
const contact = contacts[selected]
console.log(JSON.stringify(contact, null, 2))
})
})
program.parse(process.argv)