mirror of
https://github.com/onyx-and-iris/meld-cli.git
synced 2025-07-12 02:11:46 +00:00
use if else blocks to avoid nested switch.
This commit is contained in:
parent
730cf77707
commit
af64508ca5
526
index.js
526
index.js
@ -21,287 +21,283 @@ socket.onopen = function() {
|
|||||||
let channel;
|
let channel;
|
||||||
(() => {
|
(() => {
|
||||||
try {
|
try {
|
||||||
switch (input[0]) {
|
if (input[0] === "scene") {
|
||||||
case "scene":
|
if (flags.help) {
|
||||||
if (flags.help) {
|
console.log(sceneHelp);
|
||||||
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sceneCommand = input[1];
|
||||||
|
switch (sceneCommand) {
|
||||||
|
case "list":
|
||||||
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
|
sceneList(channel)
|
||||||
|
.then((scenes) => {
|
||||||
|
console.log("Available scenes:");
|
||||||
|
scenes.forEach(scene => {
|
||||||
|
console.log(`- ${scene.name} (ID: ${scene.id})`);
|
||||||
|
});
|
||||||
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(`${err}`);
|
||||||
|
socket.close();
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "switch":
|
||||||
|
const sceneName = input[2];
|
||||||
|
if (!sceneName) {
|
||||||
|
console.error("Error: Scene name is required for the switch command.");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
|
sceneSwitch(channel, sceneName)
|
||||||
|
.then(() => {
|
||||||
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(`${err}`);
|
||||||
|
socket.close();
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
case "current":
|
||||||
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
|
sceneCurrent(channel)
|
||||||
|
.then((currentScene) => {
|
||||||
|
if (currentScene) {
|
||||||
|
console.log(`Current scene: ${currentScene.name} (ID: ${currentScene.id})`);
|
||||||
|
} else {
|
||||||
|
console.log("No current scene found.");
|
||||||
|
}
|
||||||
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(`${err}`);
|
||||||
|
socket.close();
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
default:
|
||||||
console.log(sceneHelp);
|
console.log(sceneHelp);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
} else if (input[0] === "audio") {
|
||||||
|
if (flags.help) {
|
||||||
|
console.log(audioHelp);
|
||||||
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
const sceneCommand = input[1];
|
const audioCommand = input[1];
|
||||||
switch (sceneCommand) {
|
const audioName = input[2];
|
||||||
case "list":
|
switch (audioCommand) {
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
case "mute":
|
||||||
sceneList(channel)
|
if (!audioName) {
|
||||||
.then((scenes) => {
|
console.error("Error: Audio name is required for the mute command.");
|
||||||
if (scenes.length === 0) {
|
process.exit(1);
|
||||||
console.log("No scenes found.");
|
}
|
||||||
} else {
|
|
||||||
console.log("Available scenes:");
|
|
||||||
scenes.forEach(scene => {
|
|
||||||
console.log(`- ${scene.name} (ID: ${scene.id})`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
socket.close();
|
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`${err}`);
|
|
||||||
socket.close();
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "switch":
|
|
||||||
const sceneName = input[2];
|
|
||||||
if (!sceneName) {
|
|
||||||
console.error("Error: Scene name is required for the switch command.");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
sceneSwitch(channel, sceneName)
|
audioMute(channel, audioName)
|
||||||
.then(() => {
|
.then((message) => {
|
||||||
socket.close();
|
console.log(message);
|
||||||
process.exit(0);
|
socket.close();
|
||||||
})
|
process.exit(0);
|
||||||
.catch((err) => {
|
})
|
||||||
console.error(`${err}`);
|
.catch((err) => {
|
||||||
socket.close();
|
console.error(`${err}`);
|
||||||
process.exit(1);
|
socket.close();
|
||||||
});
|
process.exit(1);
|
||||||
});
|
});
|
||||||
break;
|
});
|
||||||
case "current":
|
return;
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
case "unmute":
|
||||||
sceneCurrent(channel)
|
if (!audioName) {
|
||||||
.then((currentScene) => {
|
console.error("Error: Audio name is required for the mute command.");
|
||||||
if (currentScene) {
|
process.exit(1);
|
||||||
console.log(`Current scene: ${currentScene.name} (ID: ${currentScene.id})`);
|
}
|
||||||
} else {
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
console.log("No current scene found.");
|
audioUnmute(channel, audioName)
|
||||||
}
|
.then((message) => {
|
||||||
socket.close();
|
console.log(message);
|
||||||
process.exit(0);
|
socket.close();
|
||||||
})
|
process.exit(0);
|
||||||
.catch((err) => {
|
})
|
||||||
console.error(`${err}`);
|
.catch((err) => {
|
||||||
socket.close();
|
console.error(`${err}`);
|
||||||
process.exit(1);
|
socket.close();
|
||||||
});
|
process.exit(1);
|
||||||
});
|
});
|
||||||
break;
|
});
|
||||||
default:
|
return;
|
||||||
console.log(sceneHelp);
|
case "toggle":
|
||||||
socket.close();
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
process.exit(0);
|
audioToggle(channel, audioName)
|
||||||
}
|
.then((message) => {
|
||||||
|
console.log(message);
|
||||||
case "audio":
|
socket.close();
|
||||||
if (flags.help) {
|
process.exit(0);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(`${err}`);
|
||||||
|
socket.close();
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
case "status":
|
||||||
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
|
audioStatus(channel, audioName)
|
||||||
|
.then((status) => {
|
||||||
|
console.log(`${audioName} is ${status ? "muted" : "unmuted"}`);
|
||||||
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(`Error fetching audio status: ${err}`);
|
||||||
|
socket.close();
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
default:
|
||||||
console.log(audioHelp);
|
console.log(audioHelp);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
} else if (input[0] === "stream") {
|
||||||
|
if (flags.help) {
|
||||||
|
console.log(streamHelp);
|
||||||
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
const audioCommand = input[1];
|
const streamCommand = input[1];
|
||||||
const audioName = input[2];
|
switch (streamCommand) {
|
||||||
switch (audioCommand) {
|
case "start":
|
||||||
case "mute":
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
if (!audioName) {
|
streamStart(channel)
|
||||||
console.error("Error: Audio name is required for the mute command.");
|
.then((message) => {
|
||||||
process.exit(1);
|
console.log(message);
|
||||||
}
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
})
|
||||||
audioMute(channel, audioName)
|
.catch((err) => {
|
||||||
.then((message) => {
|
console.error(`${err}`);
|
||||||
console.log(message);
|
socket.close();
|
||||||
socket.close();
|
process.exit(1);
|
||||||
process.exit(0);
|
});
|
||||||
})
|
});
|
||||||
.catch((err) => {
|
return;
|
||||||
console.error(`${err}`);
|
case "stop":
|
||||||
socket.close();
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
process.exit(1);
|
streamStop(channel)
|
||||||
});
|
.then((message) => {
|
||||||
});
|
console.log(message);
|
||||||
break;
|
socket.close();
|
||||||
case "unmute":
|
process.exit(0);
|
||||||
if (!audioName) {
|
})
|
||||||
console.error("Error: Audio name is required for the mute command.");
|
.catch((err) => {
|
||||||
process.exit(1);
|
console.error(`${err}`);
|
||||||
}
|
socket.close();
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
process.exit(1);
|
||||||
audioUnmute(channel, audioName)
|
});
|
||||||
.then((message) => {
|
});
|
||||||
console.log(message);
|
break;
|
||||||
socket.close();
|
case "status":
|
||||||
process.exit(0);
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
})
|
streamStatus(channel)
|
||||||
.catch((err) => {
|
.then((isStreaming) => {
|
||||||
console.error(`${err}`);
|
console.log(`Streaming is currently ${isStreaming ? "active" : "inactive"}`);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(1);
|
process.exit(0);
|
||||||
});
|
})
|
||||||
});
|
.catch((err) => {
|
||||||
break;
|
console.error(`Error fetching stream status: ${err}`);
|
||||||
case "toggle":
|
socket.close();
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
process.exit(1);
|
||||||
audioToggle(channel, audioName)
|
});
|
||||||
.then((message) => {
|
});
|
||||||
console.log(message);
|
return;
|
||||||
socket.close();
|
default:
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`${err}`);
|
|
||||||
socket.close();
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "status":
|
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
|
||||||
audioStatus(channel, audioName)
|
|
||||||
.then((status) => {
|
|
||||||
console.log(`${audioName} is ${status ? "muted" : "unmuted"}`);
|
|
||||||
socket.close();
|
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`Error fetching audio status: ${err}`);
|
|
||||||
socket.close();
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
console.log(audioHelp);
|
|
||||||
socket.close();
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
case "stream":
|
|
||||||
if (flags.help) {
|
|
||||||
console.log(streamHelp);
|
console.log(streamHelp);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
} else if (input[0] === "record") {
|
||||||
|
if (flags.help) {
|
||||||
|
console.log(recordHelp);
|
||||||
|
socket.close();
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
const streamCommand = input[1];
|
const recordCommand = input[1];
|
||||||
switch (streamCommand) {
|
switch (recordCommand) {
|
||||||
case "start":
|
case "start":
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
streamStart(channel)
|
recordStart(channel)
|
||||||
.then((message) => {
|
.then((message) => {
|
||||||
console.log(message);
|
console.log(message);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(`${err}`);
|
console.error(`${err}`);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
return;
|
||||||
case "stop":
|
case "stop":
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
streamStop(channel)
|
recordStop(channel)
|
||||||
.then((message) => {
|
.then((message) => {
|
||||||
console.log(message);
|
console.log(message);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(`${err}`);
|
console.error(`${err}`);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
return;
|
||||||
case "status":
|
case "status":
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
channel = new QWebChannel(socket, function (channel) {
|
||||||
streamStatus(channel)
|
recordStatus(channel)
|
||||||
.then((isStreaming) => {
|
.then((isRecording) => {
|
||||||
console.log(`Streaming is currently ${isStreaming ? "active" : "inactive"}`);
|
console.log(`Recording is currently ${isRecording ? "active" : "inactive"}`);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(`Error fetching stream status: ${err}`);
|
console.error(`Error fetching recording status: ${err}`);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
return;
|
||||||
default:
|
default:
|
||||||
console.log(streamHelp);
|
|
||||||
socket.close();
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
case "record":
|
|
||||||
if (flags.help) {
|
|
||||||
console.log(recordHelp);
|
console.log(recordHelp);
|
||||||
socket.close();
|
socket.close();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
const recordCommand = input[1];
|
console.log("Unknown command. Use --help for available commands.");
|
||||||
switch (recordCommand) {
|
socket.close();
|
||||||
case "start":
|
process.exit(0);
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
|
||||||
recordStart(channel)
|
|
||||||
.then((message) => {
|
|
||||||
console.log(message);
|
|
||||||
socket.close();
|
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`${err}`);
|
|
||||||
socket.close();
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "stop":
|
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
|
||||||
recordStop(channel)
|
|
||||||
.then((message) => {
|
|
||||||
console.log(message);
|
|
||||||
socket.close();
|
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`${err}`);
|
|
||||||
socket.close();
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case "status":
|
|
||||||
channel = new QWebChannel(socket, function (channel) {
|
|
||||||
recordStatus(channel)
|
|
||||||
.then((isRecording) => {
|
|
||||||
console.log(`Recording is currently ${isRecording ? "active" : "inactive"}`);
|
|
||||||
socket.close();
|
|
||||||
process.exit(0);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`Error fetching recording status: ${err}`);
|
|
||||||
socket.close();
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
console.log(recordHelp);
|
|
||||||
socket.close();
|
|
||||||
process.exit(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error handling CLI flags:", error);
|
console.error("Error handling CLI flags:", error);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user