Skip to content

Commit 2f7cfaa

Browse files
committed
1 parent 63c3383 commit 2f7cfaa

File tree

2 files changed

+64
-65
lines changed

2 files changed

+64
-65
lines changed

index.js

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,69 @@ const { program } = require("commander");
44
const OBSWebSocket = require("obs-websocket-js");
55
const { version } = require("./package.json");
66

7-
program
8-
.option(
9-
"-a, --address <address>",
10-
"the address to the machine in which OBS is running and the port configured in OBS under Tools > WebSockets Server Settings",
11-
"localhost:4444"
12-
)
13-
.option(
14-
"-p, --password <password>",
15-
"the password configured in OBS under Tools > WebSockets Server Settings"
16-
)
17-
.option(
18-
"-f, --field <field>",
19-
`project a field out of the OBS response, for example, given an OBS response of ‘[{ ..., "streaming": false, ...}]’ and a <field> of ‘0.streaming’, obs-cli outputs just ‘false’; this is a convenience for applications that need only one part of the response`
20-
)
21-
.arguments("<request[=arguments]...>")
22-
.description("Remote control OBS from the command line.", {
23-
"request[=arguments]": `a request name (for example, ‘GetRecordingFolder’), optionally followed by arguments (for example, ‘SetRecordingFolder='{ "rec-folder": "/tmp/" }'’) (see https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md for the complete list of requests and their arguments)`,
24-
})
25-
.action(async (requestsStrings, { address, password, field }) => {
26-
let obs;
27-
try {
28-
const requests = [];
29-
for (const request of requestsStrings) {
30-
const index = request.indexOf("=");
31-
if (index === -1) requests.push([request]);
32-
else
33-
requests.push([
34-
request.slice(0, index),
35-
JSON.parse(request.slice(index + 1)),
36-
]);
37-
}
7+
(async () => {
8+
await program
9+
.option(
10+
"-a, --address <address>",
11+
"the address to the machine in which OBS is running and the port configured in OBS under Tools > WebSockets Server Settings",
12+
"localhost:4444"
13+
)
14+
.option(
15+
"-p, --password <password>",
16+
"the password configured in OBS under Tools > WebSockets Server Settings"
17+
)
18+
.option(
19+
"-f, --field <field>",
20+
`project a field out of the OBS response, for example, given an OBS response of ‘[{ ..., "streaming": false, ...}]’ and a <field> of ‘0.streaming’, obs-cli outputs just ‘false’; this is a convenience for applications that need only one part of the response`
21+
)
22+
.arguments("<request[=arguments]...>")
23+
.description("Remote control OBS from the command line.", {
24+
"request[=arguments]": `a request name (for example, ‘GetRecordingFolder’), optionally followed by arguments (for example, ‘SetRecordingFolder='{ "rec-folder": "/tmp/" }'’) (see https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md for the complete list of requests and their arguments)`,
25+
})
26+
.action(async (requestsStrings, { address, password, field }) => {
27+
try {
28+
const requests = [];
29+
for (const request of requestsStrings) {
30+
const index = request.indexOf("=");
31+
if (index === -1) requests.push([request]);
32+
else
33+
requests.push([
34+
request.slice(0, index),
35+
JSON.parse(request.slice(index + 1)),
36+
]);
37+
}
3838

39-
const responses = [];
40-
obs = new OBSWebSocket();
41-
await obs.connect({ address, password });
42-
for (const request of requests)
43-
responses.push(await obs.send(...request));
44-
await obs.disconnect();
39+
const responses = [];
40+
const obs = new OBSWebSocket();
41+
await obs.connect({ address, password });
42+
for (const request of requests)
43+
responses.push(await obs.send(...request));
44+
await obs.disconnect();
4545

46-
let output = responses;
47-
const fieldParts = field === undefined ? [] : field.split(".");
48-
for (const fieldPart of fieldParts) {
49-
if (typeof output !== "object")
50-
throw new Error(`Failed to find field ‘${fieldPart}’`);
51-
output = output[fieldPart];
52-
if (output === undefined)
53-
throw new Error(`Failed to find field ‘${fieldPart}’`);
54-
}
46+
let output = responses;
47+
const fieldParts = field === undefined ? [] : field.split(".");
48+
for (const fieldPart of fieldParts) {
49+
if (typeof output !== "object")
50+
throw new Error(`Failed to find field ‘${fieldPart}’`);
51+
output = output[fieldPart];
52+
if (output === undefined)
53+
throw new Error(`Failed to find field ‘${fieldPart}’`);
54+
}
5555

56-
console.log(
57-
typeof output === "string"
58-
? output
59-
: JSON.stringify(output, undefined, 2)
60-
);
61-
} catch (error) {
62-
console.error(
63-
error instanceof Error
64-
? error.message
65-
: JSON.stringify(error, undefined, 2)
66-
);
67-
process.exit(1);
68-
}
69-
})
70-
.version(version)
71-
.parseAsync();
56+
console.log(
57+
typeof output === "string"
58+
? output
59+
: JSON.stringify(output, undefined, 2)
60+
);
61+
} catch (error) {
62+
console.error(
63+
error instanceof Error
64+
? error.message
65+
: JSON.stringify(error, undefined, 2)
66+
);
67+
process.exit(1);
68+
}
69+
})
70+
.version(version)
71+
.parseAsync();
72+
})();

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"bugs": "https://github.com/leafac/obs-cli/issues",
1616
"license": "MIT",
1717
"author": "Leandro Facchinetti <[email protected]> (https://leafac.com)",
18-
"bin": {
19-
"obs-cli": "index.js"
20-
},
18+
"bin": "index.js",
2119
"repository": "leafac/obs-cli",
2220
"dependencies": {
2321
"commander": "^6.2.0",

0 commit comments

Comments
 (0)