forked from onyx-and-iris/meld-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·184 lines (174 loc) · 5.85 KB
/
index.js
File metadata and controls
executable file
·184 lines (174 loc) · 5.85 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env node
import { QWebChannel } from 'qwebchannel'
import WebSocket from 'ws'
import cli from './utils/cli.js'
import { sceneHelp, sceneList, sceneSwitch, sceneCurrent } from './utils/scene.js'
import { audioHelp, audioList, audioMute, audioUnmute, audioToggle, audioStatus } from './utils/audio.js'
import { streamHelp, streamStart, streamStop, streamToggle, streamStatus } from './utils/stream.js'
import { recordHelp, recordStart, recordStop, recordToggle, recordStatus } from './utils/record.js'
import { clipHelp, saveClip } from './utils/clip.js'
import { screenshotHelp, takeScreenshot } from './utils/screenshot.js'
const input = cli.input
const flags = cli.flags
const address = flags.host ?? process.env.MELD_CLI_HOST ?? 'localhost'
const port = flags.port ?? process.env.MELD_CLI_PORT ?? 13376
const socket = new WebSocket(`ws://${address}:${port}`)
/**
* Print help information.
* @param {string} helpText
*/
function printHelp(helpText) {
console.log(helpText)
process.exit(0)
}
/**
* Helper to wrap QWebChannel usage and handle promise-based command execution.
* @param {WebSocket} socket - The websocket instance.
* @param {function} fn - The function to execute with the channel.
*/
function withChannel(socket, fn) {
new QWebChannel(socket, function (channel) {
fn(channel)
.then((result) => {
if (typeof result === 'object' && result !== null && typeof result.toString === 'function') {
console.log(result.toString())
} else {
if (result !== undefined) {
console.log(result)
}
}
socket.close()
process.exit(0)
})
.catch((err) => {
console.error(`${err}`)
socket.close()
process.exit(1)
})
})
}
socket.onopen = function () {
(() => {
try {
const command = input[0]
const helpMap = {
scene: sceneHelp,
audio: audioHelp,
stream: streamHelp,
record: recordHelp,
clip: clipHelp,
screenshot: screenshotHelp
}
if (flags.help && helpMap[command]) {
printHelp(helpMap[command])
}
if (command === 'scene') {
const [sceneCommand, ...sceneArguments] = input.slice(1)
switch (sceneCommand) {
case 'list':
withChannel(socket, (channel) => sceneList(channel, flags.id))
break
case 'switch':
if (!sceneArguments[0]) {
console.error('Error: Scene name is required for the switch command.')
process.exit(1)
}
withChannel(socket, (channel) => sceneSwitch(channel, sceneArguments[0]))
break
case 'current':
withChannel(socket, (channel) => sceneCurrent(channel, flags.id))
break
default:
printHelp(sceneHelp)
}
} else if (command === 'audio') {
const [audioCommand, ...audioArguments] = input.slice(1)
switch (audioCommand) {
case 'list':
withChannel(socket, (channel) => audioList(channel, flags.id))
break
case 'mute':
if (!audioName) {
console.error('Error: Audio name is required for the mute command.')
process.exit(1)
}
withChannel(socket, (channel) => audioMute(channel, audioArguments[0]))
break
case 'unmute':
if (!audioArguments[0]) {
console.error('Error: Audio name is required for the unmute command.')
process.exit(1)
}
withChannel(socket, (channel) => audioUnmute(channel, audioArguments[0]))
break
case 'toggle':
withChannel(socket, (channel) => audioToggle(channel, audioArguments[0]))
break
case 'status':
withChannel(socket, (channel) => audioStatus(channel, audioArguments[0]))
break
default:
printHelp(audioHelp)
}
} else if (command === 'stream') {
const streamCommand = input[1]
switch (streamCommand) {
case 'start':
withChannel(socket, (channel) => streamStart(channel))
break
case 'stop':
withChannel(socket, (channel) => streamStop(channel))
break
case 'toggle':
withChannel(socket, (channel) => streamToggle(channel))
break
case 'status':
withChannel(socket, (channel) => streamStatus(channel))
break
default:
console.log(streamHelp)
socket.close()
process.exit(0)
}
} else if (command === 'record') {
const recordCommand = input[1]
switch (recordCommand) {
case 'start':
withChannel(socket, (channel) => recordStart(channel))
break
case 'stop':
withChannel(socket, (channel) => recordStop(channel))
break
case 'toggle':
withChannel(socket, (channel) => recordToggle(channel))
break
case 'status':
withChannel(socket, (channel) => recordStatus(channel))
break
default:
printHelp(recordHelp)
}
} else if (command === 'clip') {
const clipCommand = input[1]
if (clipCommand === 'save') {
withChannel(socket, (channel) => saveClip(channel))
} else {
printHelp(clipHelp)
}
} else if (command === 'screenshot') {
const screenshotCommand = input[1]
if (screenshotCommand === 'take') {
withChannel(socket, (channel) => takeScreenshot(channel))
} else {
printHelp(screenshotHelp)
}
} else {
printHelp(cli.help)
socket.close()
process.exit(1)
}
} catch (error) {
console.error(`Error: ${error.message}`)
}
})()
}