diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index da7a8d6..979d8a0 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -16,7 +16,7 @@ jobs: - name: Use Node.js 14.x uses: actions/setup-node@v1 with: - version: 14.x + node-version: 14.x - name: Lint run: | npm install diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f5278fa..e69254a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,14 +10,17 @@ on: jobs: build: - runs-on: macOS-latest + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [macos-latest, ubuntu-latest] steps: - uses: actions/checkout@master - name: Use Node.js 14.x uses: actions/setup-node@v1 with: - version: 14.x - - name: npm install, build, and test + node-version: 14.x + - name: npm install, build, and test (${{ matrix.os }}) run: | npm install npm test \ No newline at end of file diff --git a/README.md b/README.md index af0a31e..5a93736 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ This native Node.js module allows you to manage an app's access to: * Speech Recognition * Protected Folders +Note: Always will return `undefined` when imported on a non-Mac platform + If you need to ask for permissions, your app must be allowed to ask for permission : * for a Nodejs script/app, you can use a terminal app such as [iTerm2](https://iterm2.com/) (it won't work on macOS Terminal.app) diff --git a/index.js b/index.js index 40563f0..41c7d1a 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,22 @@ -const permissions = require('bindings')('permissions.node') +const nonMacResponse = () => undefined +const stub = { + askForCalendarAccess: nonMacResponse, + askForContactsAccess: nonMacResponse, + askForFoldersAccess: nonMacResponse, + askForFullDiskAccess: nonMacResponse, + askForRemindersAccess: nonMacResponse, + askForCameraAccess: nonMacResponse, + askForMicrophoneAccess: nonMacResponse, + askForMusicLibraryAccess: nonMacResponse, + askForPhotosAccess: nonMacResponse, + askForSpeechRecognitionAccess: nonMacResponse, + askForScreenCaptureAccess: nonMacResponse, + askForAccessibilityAccess: nonMacResponse, + getAuthStatus: nonMacResponse, +} + +const { platform } = require('os') +const permissions = platform() === 'darwin' ? require('bindings')('permissions.node') : stub function getAuthStatus(type) { const validTypes = [ @@ -35,17 +53,7 @@ function askForFoldersAccess(folder) { } module.exports = { - askForCalendarAccess: permissions.askForCalendarAccess, - askForContactsAccess: permissions.askForContactsAccess, + ...permissions, askForFoldersAccess, - askForFullDiskAccess: permissions.askForFullDiskAccess, - askForRemindersAccess: permissions.askForRemindersAccess, - askForCameraAccess: permissions.askForCameraAccess, - askForMicrophoneAccess: permissions.askForMicrophoneAccess, - askForMusicLibraryAccess: permissions.askForMusicLibraryAccess, - askForPhotosAccess: permissions.askForPhotosAccess, - askForSpeechRecognitionAccess: permissions.askForSpeechRecognitionAccess, - askForScreenCaptureAccess: permissions.askForScreenCaptureAccess, - askForAccessibilityAccess: permissions.askForAccessibilityAccess, getAuthStatus, } diff --git a/package.json b/package.json index f87dddc..5e9a978 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "scripts": { "build": "node-gyp rebuild", "clean": "node-gyp clean", - "lint": "prettier --check index.js", - "format": "clang-format -i permissions.mm && prettier --write index.js", + "lint": "prettier --check index.js test/module.spec.js", + "format": "clang-format -i permissions.mm && prettier --write index.js test/module.spec.js", "test": "./node_modules/.bin/mocha --reporter spec" }, "repository": { diff --git a/test/module.spec.js b/test/module.spec.js index b557199..31cd799 100644 --- a/test/module.spec.js +++ b/test/module.spec.js @@ -1,15 +1,20 @@ const { expect } = require('chai') -const { askForFoldersAccess, getAuthStatus } = require('../index') +const permissions = require('../index') + +const { platform } = require('os') +const isMac = platform() === 'darwin' +it.ifMac = isMac ? it : it.skip +it.ifNotMac = isMac ? it.skip : it describe('node-mac-permissions', () => { describe('getAuthStatus()', () => { it('should throw on invalid types', () => { expect(() => { - getAuthStatus('bad-type') + permissions.getAuthStatus('bad-type') }).to.throw(/bad-type is not a valid type/) }) - it('should return a string', () => { + it.ifMac('should return a string', () => { const types = [ 'contacts', 'calendar', @@ -27,7 +32,7 @@ describe('node-mac-permissions', () => { const statuses = ['not determined', 'denied', 'authorized', 'restricted'] for (const type of types) { - const status = getAuthStatus(type) + const status = permissions.getAuthStatus(type) expect(statuses).to.contain(status) } }) @@ -36,8 +41,34 @@ describe('node-mac-permissions', () => { describe('askForFoldersAccess()', () => { it('should throw on invalid types', () => { expect(() => { - askForFoldersAccess('bad-type') + permissions.askForFoldersAccess('bad-type') }).to.throw(/bad-type is not a valid protected folder/) }) }) + + describe('conditional binding', () => { + it.ifNotMac('always return undefined for non-mac OS', async () => { + const asyncModuleExports = [ + 'askForCalendarAccess', + 'askForContactsAccess', + 'askForFullDiskAccess', + 'askForRemindersAccess', + 'askForCameraAccess', + 'askForMicrophoneAccess', + 'askForPhotosAccess', + 'askForSpeechRecognitionAccess', + 'askForScreenCaptureAccess', + 'askForAccessibilityAccess', + 'askForMusicLibraryAccess' + ] + + for (const func of asyncModuleExports) { + const auth = await permissions[func]() + expect(auth).to.be.undefined + } + + expect(permissions.getAuthStatus('contacts')).to.be.undefined + expect(permissions.askForFoldersAccess('desktop')).to.be.undefined + }) + }) })