Skip to content

Commit 811eb46

Browse files
committed
feat: use named export for Chromedriver
this avoids having to write stuff like `@type {import("appium-chromedriver").default}` when referring to the class. also exports more types. now depends on `appium-adb`, as the type is needed for `ChromedriverOpts`.
1 parent 8753e64 commit 811eb46

10 files changed

+50
-71
lines changed

index.js

-14
This file was deleted.

lib/chromedriver.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const WEBDRIVER_VERSION_PATTERN = /Starting (ChromeDriver|Microsoft Edge WebDriv
3232

3333
const CD_VERSION_TIMEOUT = 5000;
3434

35-
class Chromedriver extends events.EventEmitter {
35+
export class Chromedriver extends events.EventEmitter {
3636
/**
3737
*
3838
* @param {import('./types').ChromedriverOpts} args
@@ -864,9 +864,6 @@ Chromedriver.STATE_ONLINE = 'online';
864864
Chromedriver.STATE_STOPPING = 'stopping';
865865
Chromedriver.STATE_RESTARTING = 'restarting';
866866

867-
export {Chromedriver};
868-
export default Chromedriver;
869-
870867
/**
871868
* @typedef {import('./types').ChromedriverVersionMapping} ChromedriverVersionMapping
872869
*/

lib/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Chromedriver} from './chromedriver';
2+
export {ChromedriverStorageClient} from './storage-client';
3+
export default Chromedriver;
4+
5+
export type * from './types';

lib/storage-client.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function extractNodeText(node) {
8080
: node.firstChild.nodeValue;
8181
}
8282

83-
class ChromedriverStorageClient {
83+
export class ChromedriverStorageClient {
8484
/**
8585
*
8686
* @param {import('./types').ChromedriverStorageClientOpts} args
@@ -93,20 +93,12 @@ class ChromedriverStorageClient {
9393
this.mapping = {};
9494
}
9595

96-
/**
97-
* @typedef {Object} AdditionalDriverDetails
98-
* @property {string?} version - Chromedriver version
99-
* or `null` if it cannot be found
100-
* @property {string?} minBrowserVersion - The minimum browser version
101-
* supported by chromedriver or `null` if it cannot be found
102-
*/
103-
10496
/**
10597
* Gets additional chromedriver details from chromedriver
10698
* release notes
10799
*
108100
* @param {string} content - Release notes of the corresponding chromedriver
109-
* @returns {AdditionalDriverDetails}
101+
* @returns {import('./types').AdditionalDriverDetails}
110102
*/
111103
parseNotes(content) {
112104
const result = {};

lib/types.ts

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type ADB from 'appium-adb';
2+
13
export interface ChromedriverOpts {
24
host?: string;
35
port?: string;
@@ -7,7 +9,7 @@ export interface ChromedriverOpts {
79
bundleId?: string;
810
mappingPath?: string;
911
cmdArgs?: string[];
10-
adb?: AdbStub;
12+
adb?: ADB;
1113
verbose?: boolean;
1214
logPath?: string;
1315
disableBuildCheck?: boolean;
@@ -84,18 +86,13 @@ export interface ChromedriverStorageClientOpts {
8486
timeout?: number;
8587
}
8688

87-
/**
88-
* This is an instance of the `ADB` class from the `appium-adb` package.
89-
* Remove this definition if/when `appium-adb` gets typed properly
90-
* @see https://github.com/appium/appium-adb
91-
*/
92-
export type AdbStub = {
93-
getApiLevel: () => Promise<number>;
94-
adbPort?: number;
95-
executable: {
96-
defaultArgs: string[];
97-
};
98-
getForwardList: () => Promise<string[]>;
99-
removePortForward: (systemPort: string) => Promise<void>;
100-
getPackageInfo: (pkg: string) => Promise<{versionName: string}>;
101-
};
89+
export interface AdditionalDriverDetails {
90+
/**
91+
* Chromedriver version or `null` if it cannot be found
92+
*/
93+
version?: string | null;
94+
/**
95+
* The minimum browser version supported by chromedriver or `null` if it cannot be found
96+
*/
97+
minBrowserVersion?: string | null;
98+
}

lib/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const CD_VER =
5757

5858
/**
5959
*
60-
* @param {import('./types').AdbStub} adb
60+
* @param {import('appium-adb').ADB} adb
6161
* @param {string} bundleId
6262
* @returns
6363
*/

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
"printWidth": 100,
3232
"singleQuote": true
3333
},
34-
"main": "./build/index.js",
34+
"main": "./build/lib/index.js",
3535
"directories": {
3636
"lib": "lib"
3737
},
3838
"files": [
3939
"config",
40-
"index.js",
4140
"install-npm.js",
4241
"lib",
4342
"build",
@@ -47,12 +46,13 @@
4746
"CHANGELOG.md",
4847
"tsconfig.json"
4948
],
50-
"types": "./build/index.d.ts",
49+
"types": "./build/lib/index.d.ts",
5150
"dependencies": {
5251
"@appium/base-driver": "^9.1.0",
5352
"@appium/support": "^4.0.0",
5453
"@babel/runtime": "^7.0.0",
5554
"@xmldom/xmldom": "^0.x",
55+
"appium-adb": "^9.14.1",
5656
"asyncbox": "^2.0.2",
5757
"axios": "^1.x",
5858
"bluebird": "^3.5.1",
@@ -106,6 +106,6 @@
106106
"semantic-release": "^20.0.2",
107107
"sinon": "^15.0.0",
108108
"ts-node": "^10.9.1",
109-
"typescript": "^5.0.2"
109+
"typescript": "~5.0.4"
110110
}
111111
}

test/functional/chromedriver-e2e-specs.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
// transpile:mocha
22

3-
import Chromedriver from '../../lib/chromedriver';
4-
import { install } from '../../lib/install';
3+
import {Chromedriver} from '../../lib/chromedriver';
4+
import {install} from '../../lib/install';
55
import chai from 'chai';
66
import chaiAsPromised from 'chai-as-promised';
77
import B from 'bluebird';
8-
import { exec } from 'teen_process';
8+
import {exec} from 'teen_process';
99
import _ from 'lodash';
1010

11-
1211
let should = chai.should();
1312
chai.use(chaiAsPromised);
1413

15-
function nextState (cd) {
14+
function nextState(cd) {
1615
return new B((resolve) => {
1716
cd.on(Chromedriver.EVENT_CHANGED, (msg) => {
1817
resolve(msg.state);
1918
});
2019
});
2120
}
2221

23-
function nextError (cd) {
22+
function nextError(cd) {
2423
return new B((resolve) => {
25-
cd.on(Chromedriver.EVENT_ERROR, (err) => { // eslint-disable-line promise/prefer-await-to-callbacks
24+
cd.on(Chromedriver.EVENT_ERROR, (err) => {
25+
// eslint-disable-line promise/prefer-await-to-callbacks
2626
resolve(err);
2727
});
2828
});
2929
}
3030

31-
async function assertNoRunningChromedrivers () {
31+
async function assertNoRunningChromedrivers() {
3232
let {stdout} = await exec('ps', ['aux']);
3333
let count = 0;
3434
for (let line of stdout.split('\n')) {
@@ -40,11 +40,13 @@ async function assertNoRunningChromedrivers () {
4040
count.should.eql(0);
4141
}
4242

43-
function buildReqRes (url, method, body) {
43+
function buildReqRes(url, method, body) {
4444
let req = {originalUrl: url, method, body};
4545
let res = {};
4646
res.headers = {};
47-
res.set = (k, v) => { res[k] = v; };
47+
res.set = (k, v) => {
48+
res[k] = v;
49+
};
4850
res.status = (code) => {
4951
res.sentCode = code;
5052
return {
@@ -53,7 +55,7 @@ function buildReqRes (url, method, body) {
5355
body = JSON.parse(body);
5456
} catch (e) {}
5557
res.sentBody = body;
56-
}
58+
},
5759
};
5860
};
5961
return [req, res];
@@ -189,12 +191,15 @@ describe('chromedriver with async/await', function () {
189191
let badCd = new Chromedriver({
190192
port: 1,
191193
});
192-
await badCd.start(caps).should.eventually.be.rejectedWith('ChromeDriver crashed during startup');
194+
await badCd
195+
.start(caps)
196+
.should.eventually.be.rejectedWith('ChromeDriver crashed during startup');
193197
await assertNoRunningChromedrivers();
194198
});
195199
it('should throw an error during start if session does not work', async function () {
196200
let badCd = new Chromedriver({});
197-
await badCd.start({chromeOptions: {badCap: 'foo'}})
201+
await badCd
202+
.start({chromeOptions: {badCap: 'foo'}})
198203
.should.eventually.be.rejectedWith('cannot parse capability');
199204
await assertNoRunningChromedrivers();
200205
});

test/functional/install-e2e-specs.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
import chai from 'chai';
44
import chaiAsPromised from 'chai-as-promised';
5-
import { fs } from '@appium/support';
6-
import { install } from '../../lib/install';
7-
import {
8-
CD_BASE_DIR, getChromedriverBinaryPath, getOsName
9-
} from '../../lib/utils';
10-
import Chromedriver from '../../lib/chromedriver';
11-
5+
import {fs} from '@appium/support';
6+
import {install} from '../../lib/install';
7+
import {CD_BASE_DIR, getChromedriverBinaryPath, getOsName} from '../../lib/utils';
8+
import {Chromedriver} from '../../lib/chromedriver';
129

1310
let should = chai.should();
1411
chai.use(chaiAsPromised);
1512

16-
async function assertNoPreviousDirs () {
13+
async function assertNoPreviousDirs() {
1714
let err;
1815
try {
1916
await fs.stat(CD_BASE_DIR);

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"checkJs": true,
88
"lib": ["es2020", "dom"]
99
},
10-
"include": ["index.js", "lib"]
10+
"include": ["lib"]
1111
}

0 commit comments

Comments
 (0)