Skip to content

Commit d7a855d

Browse files
committed
feat: change autodetect output to a function
1 parent 34ccc2e commit d7a855d

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

lib/bindings.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assert, shouldReject } from '../test/assert'
22
import { makeTestFeature } from '../test/makeTestFeature'
33
import { BindingInterface, OpenOptions, PortInfo, SetOptions } from './binding-interface'
4-
import Binding, { AllBindingClasses } from './index'
4+
import { autoDetect, AllBindingClasses } from './index'
55
import MockBinding from '@serialport/binding-mock'
66

77
const defaultOpenOptions: OpenOptions = Object.freeze({
@@ -37,7 +37,7 @@ const listFields = ['path', 'manufacturer', 'serialNumber', 'pnpId', 'locationId
3737
const readyData = Buffer.from('READY')
3838

3939
testBinding('mock', MockBinding, '/dev/exists')
40-
testBinding(process.platform, Binding, process.env.TEST_PORT)
40+
testBinding(process.platform, autoDetect(), process.env.TEST_PORT)
4141

4242
function testBinding(bindingName: string, Binding: AllBindingClasses, testPort?: string) {
4343
const { testFeature, testHardware, describeHardware } = makeTestFeature(bindingName, testPort)
@@ -52,7 +52,6 @@ function testBinding(bindingName: string, Binding: AllBindingClasses, testPort?:
5252
describe('static method', () => {
5353
describe('.list', () => {
5454
it('returns an array', async () => {
55-
console.log({ Binding })
5655
const ports = await Binding.list()
5756
assert.isArray(ports)
5857
})

lib/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ export * from './binding-interface'
1212

1313
export type AllBindingClasses = typeof WindowsBinding | typeof DarwinBinding | typeof LinuxBinding
1414
export type AllBindings = WindowsBinding | DarwinBinding | LinuxBinding
15-
let binding: AllBindingClasses
16-
switch (process.platform) {
17-
case 'win32':
18-
debug('loading WindowsBinding')
19-
binding = WindowsBinding
20-
break
21-
case 'darwin':
22-
debug('loading DarwinBinding')
23-
binding = DarwinBinding
24-
break
25-
default:
26-
debug('loading LinuxBinding')
27-
binding = LinuxBinding
28-
}
2915

30-
export default binding
16+
/**
17+
* This is an auto detected binding for your current platform
18+
*/
19+
export const autoDetect = (): AllBindingClasses => {
20+
switch (process.platform) {
21+
case 'win32':
22+
debug('loading WindowsBinding')
23+
return WindowsBinding
24+
case 'darwin':
25+
debug('loading DarwinBinding')
26+
return DarwinBinding
27+
default:
28+
debug('loading LinuxBinding')
29+
return LinuxBinding
30+
}
31+
}

0 commit comments

Comments
 (0)