Skip to content

Commit 6f2eb82

Browse files
authored
Migrate to TypesScript (#45)
1 parent 2b079d1 commit 6f2eb82

File tree

8 files changed

+5178
-1149
lines changed

8 files changed

+5178
-1149
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ android/gradlew
3030
android/gradlew.bat
3131

3232
.vscode
33+
34+
# generated by bob
35+
lib/

index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

package-lock.json

Lines changed: 5087 additions & 1125 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
"title": "React Native Serial Port API",
44
"version": "1.3.4",
55
"description": "React Native Serial Port API",
6-
"main": "index.js",
6+
"main": "lib/commonjs/index.js",
7+
"react-native": "src/index.ts",
8+
"module": "lib/module/index.js",
9+
"types": "lib/typescript/src/index.d.ts",
710
"files": [
8-
"index.js",
9-
"src/",
11+
"src",
12+
"lib",
13+
"!**/__tests__",
14+
"!**/__fixtures__",
15+
"!**/__mocks__",
1016
"android/src/",
1117
"android/build.gradle"
1218
],
1319
"scripts": {
1420
"test": "echo \"Error: no test specified\" && exit 1",
15-
"generate-docs": "node_modules/.bin/jsdoc src -r -R README.md -t node_modules/docdash -d ./docs"
21+
"generate-docs": "node_modules/.bin/jsdoc src -r -R README.md -t node_modules/docdash -d ./docs",
22+
"prepack": "bob build"
1623
},
1724
"repository": {
1825
"type": "git",
@@ -39,12 +46,27 @@
3946
"docdash": "^1.1.1",
4047
"jsdoc": "^3.6.3",
4148
"react": "^16.8.3",
42-
"react-native": "^0.59.10"
49+
"react-native": "^0.59.10",
50+
"react-native-builder-bob": "^0.20.3",
51+
"typescript": "^4.9.5"
4352
},
4453
"dependencies": {
4554
"buffer": "^5.4.3"
4655
},
4756
"publishConfig": {
4857
"registry": "https://registry.npmjs.org/"
49-
}
58+
},
59+
"react-native-builder-bob": {
60+
"source": "src",
61+
"output": "lib",
62+
"targets": [
63+
"commonjs",
64+
"module",
65+
"typescript"
66+
]
67+
},
68+
"eslintIgnore": [
69+
"node_modules/",
70+
"lib/"
71+
]
5072
}

src/API.js renamed to src/API.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
2-
import SerialPort from './SerialPort';
2+
import SerialPort, { SerialPortWrapper } from './SerialPort';
33

44
const { SerialPortAPI } = NativeModules;
55
const eventEmitter = Platform.OS === 'android' ? new NativeEventEmitter(SerialPortAPI) : null;
@@ -28,7 +28,7 @@ export default class API {
2828
* Get serial port device names
2929
* @param {stringArrayCallback} callback
3030
*/
31-
static deviceNames(callback) {
31+
static deviceNames(callback: (names: string[]) => void) {
3232
if (Platform.OS !== 'android') throw new Error(`Not support ${Platform.OS}`)
3333
SerialPortAPI.deviceNames(callback);
3434
}
@@ -37,7 +37,7 @@ export default class API {
3737
* Get serial port device paths
3838
* @param {stringArrayCallback} callback
3939
*/
40-
static devicePaths(callback) {
40+
static devicePaths(callback: (paths: string[]) => void) {
4141
if (Platform.OS !== 'android') throw new Error(`Not support ${Platform.OS}`)
4242
SerialPortAPI.devicePaths(callback);
4343
}
@@ -48,7 +48,7 @@ export default class API {
4848
*/
4949
static devicePathsAsync() {
5050
if (Platform.OS !== 'android') throw new Error(`Not support ${Platform.OS}`)
51-
return new Promise((resolve, reject) => {
51+
return new Promise((resolve) => {
5252
SerialPortAPI.devicePaths(resolve);
5353
});
5454
}
@@ -57,7 +57,7 @@ export default class API {
5757
* set su binary path
5858
* @param {string} suPath
5959
*/
60-
static setSuPath(suPath) {
60+
static setSuPath(suPath: string) {
6161
if (Platform.OS !== 'android') throw new Error(`Not support ${Platform.OS}`)
6262
SerialPortAPI.setSuPath(suPath);
6363
}
@@ -66,7 +66,7 @@ export default class API {
6666
* get su binary path
6767
* @param {stringCallback} callback
6868
*/
69-
static getSuPath(callback) {
69+
static getSuPath(callback: (suPath: string) => void) {
7070
if (Platform.OS !== 'android') throw new Error(`Not support ${Platform.OS}`)
7171
SerialPortAPI.getSuPath(callback)
7272
}
@@ -77,11 +77,11 @@ export default class API {
7777
* @param {openOptions} options
7878
* @returns {Promise<SerialPort>} connected serial port
7979
*/
80-
static open(devicePath, {baudRate, parity = 0, dataBits = 8, stopBits = 1}) {
80+
static open(devicePath: string, {baudRate, parity = 0, dataBits = 8, stopBits = 1}: { baudRate: number, parity?: number, dataBits?: number, stopBits?: number }) {
8181
if (Platform.OS !== 'android') throw new Error(`Not support ${Platform.OS}`)
8282
return SerialPortAPI.open(devicePath, baudRate, parity, dataBits, stopBits)
83-
.then(serialPort => {
84-
return Promise.resolve(new SerialPort(serialPort, eventEmitter));
83+
.then((serialPort: SerialPortWrapper) => {
84+
return Promise.resolve(new SerialPort(serialPort, eventEmitter!));
8585
})
8686
}
8787
}

src/SerialPort.js renamed to src/SerialPort.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
import { NativeModules } from 'react-native';
1+
import { EventEmitter, EventSubscription, NativeModules } from 'react-native';
22
import { Buffer } from 'buffer';
33

44
const { SerialPortAPI } = NativeModules;
55
const DataReceivedEvent = 'dataReceived';
66

7+
export interface SerialPortWrapper {
8+
path: string;
9+
}
10+
11+
export interface EventData {
12+
data: string;
13+
}
14+
15+
export interface ListenerProxy {
16+
event: string;
17+
listener: (data: EventData) => void;
18+
}
19+
720
/**
821
* @name Subscription
922
* @class
@@ -27,9 +40,13 @@ const DataReceivedEvent = 'dataReceived';
2740
* @hideconstructor
2841
*/
2942
class SerialPort {
30-
constructor(serialPort, eventEmitter) {
43+
private path: string;
44+
private eventEmitter: any;
45+
private listeners: ListenerProxy[];
46+
private subscriptions: EventSubscription[];
47+
48+
constructor(serialPort: SerialPortWrapper, eventEmitter: EventEmitter) {
3149
this.path = serialPort.path;
32-
this.serialPort = serialPort;
3350
this.eventEmitter = eventEmitter;
3451
this.listeners = [];
3552
this.subscriptions = [];
@@ -47,7 +64,7 @@ class SerialPort {
4764
* @param {string} hex the hex of data
4865
* @returns {Promise} success promise
4966
*/
50-
send(hex) {
67+
send(hex: string) {
5168
return SerialPortAPI.send(this.path, hex)
5269
}
5370

@@ -56,8 +73,8 @@ class SerialPort {
5673
* @param {listener} listener
5774
* @returns {Subscription} subscription
5875
*/
59-
onReceived(listener) {
60-
const listenerProxy = (event) => {
76+
onReceived(listener: (data: Buffer) => void): EventSubscription {
77+
const listenerProxy = (event: EventData) => {
6178
if (!event.data) {
6279
return;
6380
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import SerialPortAPI from './API';
2+
export { default as SerialPort } from './SerialPort';
3+
4+
export default SerialPortAPI;

tsconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"allowUnreachableCode": false,
4+
"allowUnusedLabels": false,
5+
"esModuleInterop": true,
6+
"forceConsistentCasingInFileNames": true,
7+
"jsx": "react",
8+
"lib": [
9+
"esnext"
10+
],
11+
"module": "esnext",
12+
"moduleResolution": "node",
13+
"noFallthroughCasesInSwitch": true,
14+
"noImplicitReturns": true,
15+
"noImplicitUseStrict": false,
16+
"noStrictGenericChecks": false,
17+
"noUnusedLocals": true,
18+
"noUnusedParameters": true,
19+
"resolveJsonModule": true,
20+
"skipLibCheck": true,
21+
"strict": true,
22+
"target": "esnext"
23+
}
24+
}

0 commit comments

Comments
 (0)