-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
62 lines (54 loc) · 1.35 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
'use strict';
const path = require('path');
const execa = require('execa');
const electronUtil = require('electron-util/node');
const macosVersion = require('macos-version');
const PCancelable = require('p-cancelable');
const hasPermissions = require('macos-accessibility-permissions');
const binary = path.join(electronUtil.fixPathForAsarUnpack(__dirname), 'key-cast');
const isSupported = macosVersion.isGreaterThanOrEqualTo('10.14.4');
module.exports = ({
size,
delay,
display,
keyCombinationsOnly,
bounds
} = {}) => new PCancelable(async (resolve, reject, onCancel) => {
if (!isSupported || !hasPermissions()) {
resolve();
return;
}
const worker = execa(binary, [
...(size ? ['-s', size] : []),
...(delay ? ['-t', delay] : []),
...(display ? ['-d', display] : []),
...(
bounds ? [
'-b',
JSON.stringify({bounds: [[bounds.x, bounds.y], [bounds.width, bounds.height]]})
] : []
),
keyCombinationsOnly && '-k'
].filter(Boolean));
onCancel.shouldReject = false;
onCancel(() => {
resolve();
worker.cancel();
});
try {
const {stderr} = await worker;
if (stderr) {
reject(stderr);
} else {
resolve();
}
} catch (error) {
if (error.isCanceled || error.stdout === 'canceled') {
resolve();
} else {
reject(error);
}
}
});
module.exports.isSupported = isSupported;
module.exports.hasPermissions = hasPermissions;