Skip to content

Commit 92e80cd

Browse files
author
Mike Maietta
committed
Adding conditional binding so that we can retain type-safety of the import for things like permissions.AuthType as function params with a generic import that is platform-agnostic. Enhancement and fixes: #23
Adding additional test to run on a separate linux node
1 parent d17c249 commit 92e80cd

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

.github/workflows/lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Use Node.js 14.x
1717
uses: actions/setup-node@v1
1818
with:
19-
version: 14.x
19+
node-version: 14.x
2020
- name: Lint
2121
run: |
2222
npm install

.github/workflows/test.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ on:
1010

1111
jobs:
1212
build:
13-
runs-on: macOS-latest
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
matrix:
16+
os: [macos-latest, ubuntu-latest]
1417
steps:
1518
- uses: actions/checkout@master
1619
- name: Use Node.js 14.x
1720
uses: actions/setup-node@v1
1821
with:
19-
version: 14.x
20-
- name: npm install, build, and test
22+
node-version: 14.x
23+
- name: npm install, build, and test (${{ matrix.os }})
2124
run: |
2225
npm install
2326
npm test

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This native Node.js module allows you to manage an app's access to:
2222
* Speech Recognition
2323
* Protected Folders
2424

25+
Note: Always will return `undefined` when imported on a non-Mac platform
26+
2527
## API
2628

2729
## `permissions.getAuthStatus(type)`

index.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
const permissions = require('bindings')('permissions.node')
1+
const nonMacResponse = () => undefined
2+
const stub = {
3+
askForCalendarAccess: nonMacResponse,
4+
askForContactsAccess: nonMacResponse,
5+
askForFoldersAccess: nonMacResponse,
6+
askForFullDiskAccess: nonMacResponse,
7+
askForRemindersAccess: nonMacResponse,
8+
askForCameraAccess: nonMacResponse,
9+
askForMicrophoneAccess: nonMacResponse,
10+
askForPhotosAccess: nonMacResponse,
11+
askForSpeechRecognitionAccess: nonMacResponse,
12+
askForScreenCaptureAccess: nonMacResponse,
13+
askForAccessibilityAccess: nonMacResponse,
14+
getAuthStatus: nonMacResponse,
15+
}
16+
17+
const os = require('os')
18+
const permissions = os.platform() === 'darwin' ? require('bindings')('permissions.node') : stub
219

320
function getAuthStatus(type) {
421
const validTypes = [
@@ -34,16 +51,7 @@ function askForFoldersAccess(folder) {
3451
}
3552

3653
module.exports = {
37-
askForCalendarAccess: permissions.askForCalendarAccess,
38-
askForContactsAccess: permissions.askForContactsAccess,
54+
...permissions,
3955
askForFoldersAccess,
40-
askForFullDiskAccess: permissions.askForFullDiskAccess,
41-
askForRemindersAccess: permissions.askForRemindersAccess,
42-
askForCameraAccess: permissions.askForCameraAccess,
43-
askForMicrophoneAccess: permissions.askForMicrophoneAccess,
44-
askForPhotosAccess: permissions.askForPhotosAccess,
45-
askForSpeechRecognitionAccess: permissions.askForSpeechRecognitionAccess,
46-
askForScreenCaptureAccess: permissions.askForScreenCaptureAccess,
47-
askForAccessibilityAccess: permissions.askForAccessibilityAccess,
4856
getAuthStatus,
4957
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"scripts": {
88
"build": "node-gyp rebuild",
99
"clean": "node-gyp clean",
10-
"lint": "prettier --check index.js",
11-
"format": "clang-format -i permissions.mm && prettier --write index.js",
10+
"lint": "prettier --check '**/*.js'",
11+
"format": "clang-format -i permissions.mm && prettier --write '**/*.js'",
1212
"test": "./node_modules/.bin/mocha --reporter spec"
1313
},
1414
"repository": {

test/module.spec.js

+36-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
const { expect } = require('chai')
2-
const {
3-
askForFoldersAccess,
4-
getAuthStatus,
5-
} = require('../index')
2+
const permissions = require('../index')
3+
4+
const { platform } = require('os')
5+
const isMac = platform() === 'darwin'
6+
it.ifMac = isMac ? it : it.skip
7+
it.ifNotMac = isMac ? it.skip : it
68

79
describe('node-mac-permissions', () => {
810
describe('getAuthStatus()', () => {
911
it('should throw on invalid types', () => {
1012
expect(() => {
11-
getAuthStatus('bad-type')
13+
permissions.getAuthStatus('bad-type')
1214
}).to.throw(/bad-type is not a valid type/)
1315
})
1416

15-
it('should return a string', () => {
17+
it.ifMac('should return a string', () => {
1618
const types = [
1719
'contacts',
1820
'calendar',
@@ -24,12 +26,12 @@ describe('node-mac-permissions', () => {
2426
'microphone',
2527
'accessibility',
2628
'location',
27-
'screen'
29+
'screen',
2830
]
2931

3032
const statuses = ['not determined', 'denied', 'authorized', 'restricted']
3133
for (const type of types) {
32-
const status = getAuthStatus(type)
34+
const status = permissions.getAuthStatus(type)
3335
expect(statuses).to.contain(status)
3436
}
3537
})
@@ -38,8 +40,33 @@ describe('node-mac-permissions', () => {
3840
describe('askForFoldersAccess()', () => {
3941
it('should throw on invalid types', () => {
4042
expect(() => {
41-
askForFoldersAccess('bad-type')
43+
permissions.askForFoldersAccess('bad-type')
4244
}).to.throw(/bad-type is not a valid protected folder/)
4345
})
4446
})
47+
48+
describe('conditional binding', () => {
49+
it.ifNotMac('always return undefined for non-mac OS', async () => {
50+
const asyncModuleExports = [
51+
'askForCalendarAccess',
52+
'askForContactsAccess',
53+
'askForFullDiskAccess',
54+
'askForRemindersAccess',
55+
'askForCameraAccess',
56+
'askForMicrophoneAccess',
57+
'askForPhotosAccess',
58+
'askForSpeechRecognitionAccess',
59+
'askForScreenCaptureAccess',
60+
'askForAccessibilityAccess',
61+
]
62+
63+
for (const func of asyncModuleExports) {
64+
const auth = await permissions[func]()
65+
expect(auth).to.be.undefined
66+
}
67+
68+
expect(permissions.getAuthStatus('contacts')).to.be.undefined
69+
expect(permissions.askForFoldersAccess('desktop')).to.be.undefined
70+
})
71+
})
4572
})

0 commit comments

Comments
 (0)