-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
63 lines (44 loc) · 2.21 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
'use strict';
const path = require('path');
const execa = require('execa');
const electronUtil = require('electron-util/node');
const binary = path.join(electronUtil.fixPathForAsarUnpack(__dirname), 'audio-devices');
const generateExport = (name, getArgs, callback) => {
module.exports[name] = async (...inputs) => {
const result = await execa(binary, getArgs(...inputs));
return callback(result);
};
module.exports[name].sync = (...inputs) => {
const result = execa.sync(binary, getArgs(...inputs));
return callback(result);
};
};
const throwIfStderr = ({stderr}) => {
if (stderr) {
throw new Error(stderr);
}
};
const parseStdout = ({stdout, stderr}) => {
throwIfStderr({stderr});
return JSON.parse(stdout);
};
generateExport('getAllDevices', () => ['list', '--json'], parseStdout);
generateExport('getInputDevices', () => ['list', '--input', '--json'], parseStdout);
generateExport('getOutputDevices', () => ['list', '--output', '--json'], parseStdout);
generateExport('getDevice', deviceId => ['get', '--json', deviceId], parseStdout);
generateExport('getDefaultOutputDevice', () => ['output', 'get', '--json'], parseStdout);
generateExport('getDefaultInputDevice', () => ['input', 'get', '--json'], parseStdout);
generateExport('getDefaultSystemDevice', () => ['system', 'get', '--json'], parseStdout);
generateExport('setDefaultOutputDevice', deviceId => ['output', 'set', deviceId], throwIfStderr);
generateExport('setDefaultInputDevice', deviceId => ['input', 'set', deviceId], throwIfStderr);
generateExport('setDefaultSystemDevice', deviceId => ['system', 'set', deviceId], throwIfStderr);
generateExport('getOutputDeviceVolume', deviceId => ['volume', 'get', deviceId], ({stdout, stderr}) => stderr ? undefined : stdout);
generateExport('setOutputDeviceVolume', (deviceId, volume) => ['volume', 'set', deviceId, volume], throwIfStderr);
generateExport(
'createAggregateDevice',
(name, mainDeviceId, otherDeviceIds, {multiOutput} = {}) => [
'aggregate', 'create', '--json', (multiOutput && '--multi-output'), name, mainDeviceId, ...otherDeviceIds
].filter(Boolean),
parseStdout
);
generateExport('destroyAggregateDevice', deviceId => ['aggregate', 'destroy', deviceId], throwIfStderr);