forked from karaggeorge/mac-screen-capture-permissions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (53 loc) · 1.55 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
64
65
66
67
68
69
70
'use strict';
const path = require('path');
const fs = require('fs');
const execa = require('execa');
const {isElectron, fixPathForAsarUnpack} = require('electron-util/node');
const macosVersion = require('macos-version');
const binary = path.join(fixPathForAsarUnpack(__dirname), 'screen-capture-permissions');
const permissionExists = macosVersion.isGreaterThanOrEqualTo('10.15');
let filePath;
if (isElectron) {
const {api, openSystemPreferences} = require('electron-util');
exports.openSystemPreferences = () => openSystemPreferences('security', 'Privacy_ScreenCapture');
filePath = api.app && path.join(api.app.getPath('userData'), '.has-app-requested-screen-capture-permissions');
}
exports.hasScreenCapturePermission = () => {
if (!permissionExists) {
return true;
}
const {stdout} = execa.sync(binary);
const hasPermission = stdout === 'true';
if (!hasPermission && filePath) {
try {
fs.writeFileSync(filePath, '');
} catch (error) {
if (error.code === 'ENOENT') {
fs.mkdirSync(path.dirname(filePath));
fs.writeFileSync(filePath, '');
}
throw error;
}
}
return hasPermission;
};
exports.hasPromptedForPermission = () => {
if (!permissionExists) {
return false;
}
if (filePath && fs.existsSync(filePath)) {
return true;
}
return false;
};
exports.resetPermissions = ({bundleId = ''} = {}) => {
try {
execa.sync('tccutil', ['reset', 'ScreenCapture', bundleId].filter(Boolean));
if (filePath && fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
return true;
} catch (error) {
return false;
}
};