Skip to content

Commit 9ca7632

Browse files
chore: Add more type declarations (#886)
1 parent 4c7f168 commit 9ca7632

File tree

3 files changed

+135
-24
lines changed

3 files changed

+135
-24
lines changed

lib/check-dependencies.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import _ from 'lodash';
33
import { exec } from 'teen_process';
44
import path from 'path';
55
import XcodeBuild from './xcodebuild';
6+
import xcode from 'appium-xcode';
67
import {
78
WDA_SCHEME, SDK_SIMULATOR, WDA_RUNNER_APP
89
} from './constants';
@@ -22,23 +23,29 @@ async function buildWDASim () {
2223
}
2324

2425
// eslint-disable-next-line require-await
25-
async function checkForDependencies () {
26+
export async function checkForDependencies () {
2627
log.debug('Dependencies are up to date');
2728
return false;
2829
}
2930

30-
async function bundleWDASim (xcodebuild) {
31+
/**
32+
*
33+
* @param {XcodeBuild} xcodebuild
34+
* @returns {Promise<string>}
35+
*/
36+
export async function bundleWDASim (xcodebuild) {
3137
if (xcodebuild && !_.isFunction(xcodebuild.retrieveDerivedDataPath)) {
32-
xcodebuild = new XcodeBuild('', {});
38+
xcodebuild = new XcodeBuild(/** @type {import('appium-xcode').XcodeVersion} */ (await xcode.getVersion(true)), {});
3339
}
3440

3541
const derivedDataPath = await xcodebuild.retrieveDerivedDataPath();
42+
if (!derivedDataPath) {
43+
throw new Error('Cannot retrieve the path to the Xcode derived data folder');
44+
}
3645
const wdaBundlePath = path.join(derivedDataPath, 'Build', 'Products', 'Debug-iphonesimulator', WDA_RUNNER_APP);
3746
if (await fs.exists(wdaBundlePath)) {
3847
return wdaBundlePath;
3948
}
4049
await buildWDASim();
4150
return wdaBundlePath;
4251
}
43-
44-
export { checkForDependencies, bundleWDASim };

lib/webdriveragent.js

+72-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@ const WDA_CF_BUNDLE_NAME = 'WebDriverAgentRunner-Runner';
2727
const SHARED_RESOURCES_GUARD = new AsyncLock();
2828
const RECENT_MODULE_VERSION_ITEM_NAME = 'recentWdaModuleVersion';
2929

30-
class WebDriverAgent {
30+
export class WebDriverAgent {
31+
/** @type {string} */
32+
bootstrapPath;
33+
34+
/** @type {string} */
35+
agentPath;
36+
37+
/**
38+
* @param {import('appium-xcode').XcodeVersion} xcodeVersion
39+
* // TODO: make args typed
40+
* @param {import('@appium/types').StringRecord} [args={}]
41+
* @param {import('@appium/types').AppiumLogger?} [log=null]
42+
*/
3143
constructor (xcodeVersion, args = {}, log = null) {
3244
this.xcodeVersion = xcodeVersion;
3345

@@ -123,6 +135,10 @@ class WebDriverAgent {
123135
return `${this.updatedWDABundleId ? this.updatedWDABundleId : WDA_RUNNER_BUNDLE_ID}${this.updatedWDABundleIdSuffix}`;
124136
}
125137

138+
/**
139+
* @param {string} [bootstrapPath]
140+
* @param {string} [agentPath]
141+
*/
126142
setWDAPaths (bootstrapPath, agentPath) {
127143
// allow the user to specify a place for WDA. This is undocumented and
128144
// only here for the purposes of testing development of WDA
@@ -134,8 +150,11 @@ class WebDriverAgent {
134150
this.log.info(`Using WDA agent: '${this.agentPath}'`);
135151
}
136152

153+
/**
154+
* @returns {Promise<void>}
155+
*/
137156
async cleanupObsoleteProcesses () {
138-
const obsoletePids = await getPIDsListeningOnPort(this.url.port,
157+
const obsoletePids = await getPIDsListeningOnPort(/** @type {string} */ (this.url.port),
139158
(cmdLine) => cmdLine.includes('/WebDriverAgentRunner') &&
140159
!cmdLine.toLowerCase().includes(this.device.udid.toLowerCase()));
141160

@@ -164,6 +183,9 @@ class WebDriverAgent {
164183
return !!(await this.getStatus());
165184
}
166185

186+
/**
187+
* @returns {string}
188+
*/
167189
get basePath () {
168190
if (this.url.path === '/') {
169191
return '';
@@ -243,6 +265,8 @@ class WebDriverAgent {
243265
* Uninstall WDAs from the test device.
244266
* Over Xcode 11, multiple WDA can be in the device since Xcode 11 generates different WDA.
245267
* Appium does not expect multiple WDAs are running on a device.
268+
*
269+
* @returns {Promise<void>}
246270
*/
247271
async uninstall () {
248272
try {
@@ -476,6 +500,9 @@ class WebDriverAgent {
476500
return await this.xcodebuild.start();
477501
}
478502

503+
/**
504+
* @returns {Promise<void>}
505+
*/
479506
async startWithIDB () {
480507
this.log.info('Will launch WDA with idb instead of xcodebuild since the corresponding flag is enabled');
481508
const {wdaBundleId, testBundleId} = await this.prepareWDA();
@@ -490,6 +517,11 @@ class WebDriverAgent {
490517
return await this.idb.runXCUITest(wdaBundleId, wdaBundleId, testBundleId, {env});
491518
}
492519

520+
/**
521+
*
522+
* @param {string} wdaBundlePath
523+
* @returns {Promise<string>}
524+
*/
493525
async parseBundleId (wdaBundlePath) {
494526
const infoPlistPath = path.join(wdaBundlePath, 'Info.plist');
495527
const infoPlist = await plist.parsePlist(await fs.readFile(infoPlistPath));
@@ -499,6 +531,9 @@ class WebDriverAgent {
499531
return infoPlist.CFBundleIdentifier;
500532
}
501533

534+
/**
535+
* @returns {Promise<{wdaBundleId: string, testBundleId: string, wdaBundlePath: string}>}
536+
*/
502537
async prepareWDA () {
503538
const wdaBundlePath = this.wdaBundlePath || await this.fetchWDABundle();
504539
const wdaBundleId = await this.parseBundleId(wdaBundlePath);
@@ -509,9 +544,12 @@ class WebDriverAgent {
509544
return {wdaBundleId, testBundleId, wdaBundlePath};
510545
}
511546

547+
/**
548+
* @returns {Promise<string>}
549+
*/
512550
async fetchWDABundle () {
513551
if (!this.derivedDataPath) {
514-
return await bundleWDASim(this.xcodebuild);
552+
return await bundleWDASim(/** @type {XcodeBuild} */ (this.xcodebuild));
515553
}
516554
const wdaBundlePaths = await fs.glob(`${this.derivedDataPath}/**/*${WDA_RUNNER_APP}/`, {
517555
absolute: true,
@@ -522,14 +560,21 @@ class WebDriverAgent {
522560
return wdaBundlePaths[0];
523561
}
524562

563+
/**
564+
* @returns {Promise<boolean>}
565+
*/
525566
async isSourceFresh () {
526567
const existsPromises = [
527568
'Resources',
528569
`Resources${path.sep}WebDriverAgent.bundle`,
529-
].map((subPath) => fs.exists(path.resolve(this.bootstrapPath, subPath)));
570+
].map((subPath) => fs.exists(path.resolve(/** @type {String} */ (this.bootstrapPath), subPath)));
530571
return (await B.all(existsPromises)).some((v) => v === false);
531572
}
532573

574+
/**
575+
* @param {string} sessionId
576+
* @returns {void}
577+
*/
533578
setupProxies (sessionId) {
534579
const proxyOpts = {
535580
log: this.log,
@@ -547,6 +592,9 @@ class WebDriverAgent {
547592
this.noSessionProxy = new NoSessionProxy(proxyOpts);
548593
}
549594

595+
/**
596+
* @returns {Promise<void>}
597+
*/
550598
async quit () {
551599
if (this.usePreinstalledWDA) {
552600
this.log.info('Stopping the XCTest session');
@@ -582,6 +630,9 @@ class WebDriverAgent {
582630
}
583631
}
584632

633+
/**
634+
* @returns {import('url').UrlWithStringQuery}
635+
*/
585636
get url () {
586637
if (!this._url) {
587638
if (this.webDriverAgentUrl) {
@@ -595,30 +646,44 @@ class WebDriverAgent {
595646
return this._url;
596647
}
597648

649+
/**
650+
* @param {string} _url
651+
* @returns {void}
652+
*/
598653
set url (_url) {
599654
this._url = url.parse(_url);
600655
}
601656

657+
/**
658+
* @returns {boolean}
659+
*/
602660
get fullyStarted () {
603661
return this.started;
604662
}
605663

664+
/**
665+
* @param {boolean} started
666+
* @returns {void}s
667+
*/
606668
set fullyStarted (started) {
607669
this.started = started ?? false;
608670
}
609671

672+
/**
673+
* @returns {Promise<string|undefined>}
674+
*/
610675
async retrieveDerivedDataPath () {
611676
if (this.canSkipXcodebuild) {
612677
return;
613678
}
614-
// @ts-ignore xcodebuild should be set
615-
return await this.xcodebuild.retrieveDerivedDataPath();
679+
return await /** @type {XcodeBuild} */ (this.xcodebuild).retrieveDerivedDataPath();
616680
}
617681

618682
/**
619683
* Reuse running WDA if it has the same bundle id with updatedWDABundleId.
620684
* Or reuse it if it has the default id without updatedWDABundleId.
621685
* Uninstall it if the method faces an exception for the above situation.
686+
* @returns {Promise<void>}
622687
*/
623688
async setupCaching () {
624689
const status = await this.getStatus();
@@ -660,6 +725,7 @@ class WebDriverAgent {
660725

661726
/**
662727
* Quit and uninstall running WDA.
728+
* @returns {Promise<void>}
663729
*/
664730
async quitAndUninstall () {
665731
await this.quit();
@@ -668,4 +734,3 @@ class WebDriverAgent {
668734
}
669735

670736
export default WebDriverAgent;
671-
export { WebDriverAgent };

0 commit comments

Comments
 (0)