-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·138 lines (119 loc) · 4.34 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
import { homedir } from 'os'
import * as fs from 'fs';
import { deleteMessages, getDetailedMessages, getMessage, getMessages, replyToMessage, sendMessage } from './Controllers/MessageController.js';
import { connectToChannel, createChannel, getChannels, updateChannel } from './Controllers/ChannelController.js';
import { register, login, logout, getMe, wave } from './Controllers/UserController.js';
import { Command } from 'commander';
const userHomeDir = homedir()
const args = process.argv
const [one, two, ...cmds] = args
const program = new Command();
fs.readFile(userHomeDir + '/.boxrc.json', 'utf8', async function(err, data) {
let currentChannel = "global"
let session = undefined
if (data) {
let sessionData = JSON.parse(data)
currentChannel = sessionData?.channel;
session = sessionData?.session;
}
program
.command('open')
.argument('<box-channel>', 'open live chat for specified box channel')
.action(async (targetChannel) => {
await connectToChannel(targetChannel, currentChannel, session)
});
program
.command('me')
.description('returns your authenticated user details')
.action(async () => {
await getMe(session)
});
program
.command('pwd')
.description('prints the current box name')
.action(async () => {
console.log("current box: " + currentChannel)
});
program
.command('logout')
.description('logs out of box and deletes current session data')
.action(async (targetChannel) => {
await logout(currentChannel)
});
program
.command('login')
.argument('<email>', 'email associated with a registered box account')
.action(async (email) => {
await login(email, {channel: currentChannel})
});
program
.command('ls')
.description('list ids in current box')
.option('-l', 'list ids, sender box and timestamp')
.option('-c', 'list all box channels')
.action(async ({l, c}) => {
if (l) {
await getDetailedMessages(currentChannel, session)
} else if (c) {
await getChannels();
} else {
await getMessages(currentChannel, session)
}
});
program
.command('checkout')
.description('checkout into a box channel')
.argument('<box-channel>', 'box channel you want to checkout into')
.option('-b', 'create and checkout into a new box channel')
.action(async (targetChannel, {b}) => {
if(b) {
await createChannel(targetChannel, session)
}
await updateChannel(targetChannel, session)
});
program
.command('reply')
.description('reply to a message')
.argument('<message-id>', 'box id you want to respond to')
.argument('<message-string>', 'a message you want to send as a reply')
.action(async (messageId, replyMessage) => {
await replyToMessage(messageId, replyMessage, currentChannel, session)
});
program
.command('register')
.description('register an account')
.argument('<email>', 'email you want to associate with your box account')
.action(async (email) => {
await register(email, currentChannel)
});
program
.command('cat')
.description('print the contents of a message given a message id')
.argument('<message-id>', 'id of message you want to view')
.action(async (messageId) => {
await getMessage(messageId, currentChannel, session)
});
program
.command('rm')
.description('delete a message given comma seperated message ids')
.argument('<message-id>', 'one or more comma seperated ids of messages you want to delete')
.action(async (messageIds) => {
await deleteMessages(messageIds, session)
});
program
.command('ship')
.description('send a message to a box')
.argument('<box-channel>', 'box channel you want to send a message to')
.argument('<message-string>', 'a message you want to send to the specified box channel')
.action(async (targetChannel, message) => {
await sendMessage(targetChannel, currentChannel, message, session)
});
program
.command('wave')
.description('open a broadcast signaling you are using the box')
.action(async () => {
await wave(session)
})
await program.parseAsync(process.argv);
});