forked from lukeocodes/enable-audio-in-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-script
executable file
·172 lines (145 loc) · 4.24 KB
/
setup-script
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
const inquirer = require('inquirer')
const { spawnSync, spawn } = require( 'child_process' )
const nexmo = require('nexmo')
const fs = require('fs')
class SetupScript {
constructor() {
this.welcome()
this.run = this.run.bind(this)
}
welcome() {
this.output('Let\'s set up the Nexmo demo app')
}
run() {
const questions = [
{
type: 'input',
name: 'applicationName',
message: 'What will you call the application?',
default: 'Nexmo Demo App'
},
{
type: 'input',
name: 'conversationName',
message: 'What will you call the conversation?',
default: 'Demo Conversation'
},
{
type: 'input',
name: 'user1',
message: 'What will you call the first user?',
default: 'luke'
},
{
type: 'input',
name: 'user2',
message: 'What will you call the second user?',
default: 'alex'
}
]
inquirer.prompt(questions).then((answers) => {
this.output(`Setting up ${answers.applicationName} application...`)
const applicationId = this.createApplication(answers.applicationName)
const conversationId = this.createConversation(answers.conversationName)
const user1 = this.createUserObject(answers.user1, conversationId, applicationId)
const user2 = this.createUserObject(answers.user2, conversationId, applicationId)
this.createConfig(conversationId, user1, user2)
console.log('----------------------------------------------------------------------------')
console.log('Set up complete.')
console.log('Config file: config.js')
console.log(`Application: ${applicationId}`)
console.log(`Conversation: ${conversationId}`)
console.log('User 1:', user1)
console.log('User 2:', user2)
})
}
createApplication(applicationName) {
const output = this.syncProcess('nexmo', [
'app:create',
applicationName,
'--capabilities=rtc',
'--rtc-event-url=http://example.com',
'--keyfile=private.key'
])
const [, applicationId] = output.stdout.toString().match(/(?:Application created: )(.*)/)
return applicationId
}
createConversation(conversationName) {
const output = this.syncProcess('nexmo', [
'conversation:create',
`display_name="${conversationName}"`
])
const [, conversationId] = output.stdout.toString().match(/(?:Conversation created: )(.*)/)
return conversationId
}
createUserObject(username, conversationId, applicationId) {
const userOutput = this.syncProcess('nexmo', [
'user:create',
`name="${username}"`
])
const [, userId] = userOutput.stdout.toString().match(/(?:User created: )(.*)/)
const channel = {
type:"app"
}
const joinOutput = this.syncProcess('nexmo', [
'member:add',
conversationId,
'action=join',
`channel=${JSON.stringify(channel)}`,
`user_id=${userId}`
])
const [, memId] = joinOutput.stdout.toString().match(/(?:Member added: )(.*)/)
const acl = {
paths: {
"/*/users/**": {},
"/*/conversations/**": {},
"/*/sessions/**":{},
"/*/devices/**":{},
"/*/image/**":{},
"/*/media/**":{},
"/*/applications/**":{},
"/*/push/**":{},
"/*/knocking/**":{}
}
}
const jwt = nexmo.generateJwt(fs.readFileSync('./private.key'), {
application_id: applicationId,
sub: username,
exp: Math.round(new Date().getTime()/1000)+86400,
acl: acl
})
return {
username: username,
userId: userId,
memId: memId,
jwt: jwt
}
}
createConfig(conversationId, user1, user2) {
const data = `const USERS = {
${user1.username}: '${user1.jwt}',
${user2.username}: '${user2.jwt}',
}
const CONVERSATION_ID = '${conversationId}'`
fs.writeFile('config.js', data, (err, data) => {
if (err) console.log(err)
})
}
syncProcess(process, args) {
try {
const ps = spawnSync(process, args)
return ps
} catch(error) {
console.error(error);
}
}
asyncProcess(process, args) {
return spawn(process, args)
}
output(text) {
console.log(text)
}
}
script = new SetupScript()
script.run()