Skip to content

Commit

Permalink
feat: Switch babel to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Aug 21, 2023
1 parent 6c3972a commit 20b5282
Show file tree
Hide file tree
Showing 21 changed files with 174 additions and 156 deletions.
17 changes: 14 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
{
"extends": "@appium/eslint-config-appium",
"extends": ["@appium/eslint-config-appium-ts"],
"overrides": [
{
"files": "test/**/*.js",
"rules": {
"func-names": "off"
"func-names": "off",
"@typescript-eslint/no-var-requires": "off"
}
},
{
"files": "scripts/**/*",
"parserOptions": {"sourceType": "script"},
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
}
]
],
"rules": {
"require-await": "error"
}
}
2 changes: 1 addition & 1 deletion .mocharc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
require: ['@babel/register'],
require: ['ts-node/register'],
forbidOnly: Boolean(process.env.CI)
};
25 changes: 0 additions & 25 deletions babel.config.json

This file was deleted.

14 changes: 10 additions & 4 deletions lib/defaults-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import log from './logger';
* XML representation, which is ready for further usage
* with `defaults` command line tool arguments
*
* @param {*} value The value to be serialized
* @param {any} value The value to be serialized
* @param {boolean} serialize [true] Whether to serialize the resulting
* XML to string or to return raw HTMLElement instance
* @returns {HTMLElement|string} Either string or raw node representation of
* @returns {Node|string} Either string or raw node representation of
* the given value
* @throws {TypeError} If it is not known how to serialize the given value
*/
Expand All @@ -26,12 +26,14 @@ function toXmlArg (value, serialize = true) {
const keyTextEl = xmlDoc.createTextNode(subKey);
keyEl.appendChild(keyTextEl);
xmlDoc.documentElement.appendChild(keyEl);
// @ts-ignore The typecast here is fine
const subValueEl = xmlDoc.importNode(toXmlArg(subValue, false), true);
xmlDoc.documentElement.appendChild(subValueEl);
}
} else if (_.isArray(value)) {
xmlDoc = new DOMParser().parseFromString('<array></array>', 'text/xml');
for (const subValue of value) {
// @ts-ignore The typecast here is fine
const subValueEl = xmlDoc.importNode(toXmlArg(subValue, false), true);
xmlDoc.documentElement.appendChild(subValueEl);
}
Expand Down Expand Up @@ -67,26 +69,30 @@ function toXmlArg (value, serialize = true) {
* @param {Boolean} replace [false] Whether to generate arguments that replace
* complex typed values like arrays or dictionaries in the current plist or
* update them (the default settings)
* @returns {Array<Array<string>>} Each item in the array
* @returns {string[][]} Each item in the array
* is the `defaults write <plist>` command suffix
*/
function generateDefaultsCommandArgs (valuesMap, replace = false) {
/** @type {string[][]} */
const resultArgs = [];
for (const [key, value] of _.toPairs(valuesMap)) {
try {
if (!replace && _.isPlainObject(value)) {
const dictArgs = [key, '-dict-add'];
for (const [subKey, subValue] of _.toPairs(value)) {
// @ts-ignore The typecast here is fine
dictArgs.push(subKey, toXmlArg(subValue));
}
resultArgs.push(dictArgs);
} else if (!replace && _.isArray(value)) {
const arrayArgs = [key, '-array-add'];
for (const subValue of value) {
// @ts-ignore The typecast here is fine
arrayArgs.push(toXmlArg(subValue));
}
resultArgs.push(arrayArgs);
} else {
// @ts-ignore The typecast here is fine
resultArgs.push([key, toXmlArg(value)]);
}
} catch (e) {
Expand All @@ -110,7 +116,7 @@ class NSUserDefaults {
* Reads the content of the given plist file using plutil command line tool
* and serializes it to a JSON representation
*
* @returns {Object} The serialized plist content
* @returns {Promise<Record<string, any>>} The serialized plist content
* @throws {Error} If there was an error during serialization
*/
async asJson () {
Expand Down
10 changes: 3 additions & 7 deletions lib/extensions/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ extensions.installApp = async function installApp (app) {
* Returns user installed bundle ids which has 'bundleName' in their Info.Plist as 'CFBundleName'
*
* @param {string} bundleName - The bundle name of the application to be checked.
* @return {array<string>} - The list of bundle ids which have 'bundleName'
* @return {Promise<string[]>} - The list of bundle ids which have 'bundleName'
*/
extensions.getUserInstalledBundleIdsByBundleName = async function getUserInstalledBundleIdsByBundleName (bundleName) {
const appsRoot = path.resolve(this.getDir(), 'Containers', 'Bundle', 'Application');
// glob all Info.plist from simdir/data/Containers/Bundle/Application
const infoPlists = await fs.glob('*/*.app/Info.plist', {
cwd: appsRoot,
nosort: true,
strict: false,
absolute: true,
});
if (_.isEmpty(infoPlists)) {
Expand Down Expand Up @@ -62,7 +60,7 @@ extensions.getUserInstalledBundleIdsByBundleName = async function getUserInstall
* Verify whether the particular application is installed on Simulator.
*
* @param {string} bundleId - The bundle id of the application to be checked.
* @return {boolean} True if the given application is installed.
* @return {Promise<boolean>} True if the given application is installed.
*/
extensions.isAppInstalled = async function isAppInstalled (bundleId) {
try {
Expand Down Expand Up @@ -102,7 +100,7 @@ extensions.removeApp = async function removeApp (bundleId) {
* Starts the given application on Simulator
*
* @param {string} bundleId - The buindle ID of the application to be launched
* @param {LaunchAppOpts} opts
* @param {Partial<LaunchAppOpts>} opts
*/
extensions.launchApp = async function launchApp (bundleId, opts = {}) {
await this.simctl.launchApp(bundleId);
Expand Down Expand Up @@ -153,8 +151,6 @@ extensions.scrubApp = async function scrubApp (bundleId) {
const appDataRoot = await this.simctl.getAppContainer(bundleId, 'data');
const appFiles = await fs.glob('**/*', {
cwd: appDataRoot,
nosort: true,
strict: false,
nodir: true,
absolute: true,
});
Expand Down
2 changes: 1 addition & 1 deletion lib/extensions/biometric.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const extensions = {};
/**
* Get the current state of Biometric Enrollment feature.
*
* @returns {boolean} Either true or false
* @returns {Promise<boolean>} Either true or false
* @throws {Error} If Enrollment state cannot be determined
*/
extensions.isBiometricEnrolled = async function isBiometricEnrolled () {
Expand Down
4 changes: 2 additions & 2 deletions lib/extensions/geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function setLocationWithLyft (udid, latitude, longitude) {

try {
await exec(LYFT_SET_LOCATION, [
'-c', latitude, longitude,
'-c', `${latitude}`, `${longitude}`,
'-u', udid
]);
} catch (e) {
Expand Down Expand Up @@ -117,7 +117,7 @@ const extensions = {};
* into the corresponding edit field, for example '39,0006'.
* @param {string|number} longitude - The longitude value, which is going to be entered
* into the corresponding edit field, for example '19,0068'.
* @returns {boolean} True if the given parameters have correct format and were successfully accepted.
* @returns {Promise<boolean>} True if the given parameters have correct format and were successfully accepted.
* @throws {Error} If there was an error while setting the location
*/
extensions.setGeolocation = async function setGeolocation (latitude, longitude) {
Expand Down
14 changes: 10 additions & 4 deletions lib/extensions/keychain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const extensions = {};
/**
* Resolve full path to Simlator's LaunchDaemons root folder
*
* @returns {string} Full path to Simlator's LaunchDaemons root folder
* @returns {Promise<string>} Full path to Simlator's LaunchDaemons root folder
*/
extensions.getLaunchDaemonsRoot = async function getLaunchDaemonsRoot () {
const devRoot = await getDeveloperRoot();
Expand All @@ -26,7 +26,8 @@ extensions.getLaunchDaemonsRoot = async function getLaunchDaemonsRoot () {
* deleted if this method was called twice in a row without
* `restoreKeychains` being invoked.
*
* @returns {boolean} True if the backup operation was successfull.
* @this {import('../simulator-xcode-14').default}
* @returns {Promise<boolean>} True if the backup operation was successfull.
*/
extensions.backupKeychains = async function backupKeychains () {
if (!await fs.exists(this.keychainPath)) {
Expand All @@ -43,23 +44,27 @@ extensions.backupKeychains = async function backupKeychains () {
];
log.debug(`Creating keychains backup with 'zip ${zipArgs.join(' ')}' command`);
await exec('zip', zipArgs);
// @ts-ignore

Check warning on line 47 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (20)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer

Check warning on line 47 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (18)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer

Check warning on line 47 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (16)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer
if (_.isString(this._keychainsBackupPath) && await fs.exists(this._keychainsBackupPath)) {
// @ts-ignore

Check warning on line 49 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (20)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer

Check warning on line 49 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (18)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer

Check warning on line 49 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (16)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer
await fs.unlink(this._keychainsBackupPath);
}
// @ts-ignore

Check warning on line 52 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (20)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer

Check warning on line 52 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (18)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer

Check warning on line 52 in lib/extensions/keychain.js

View workflow job for this annotation

GitHub Actions / test (16)

Include a description after the "@ts-ignore" directive to explain why the @ts-ignore is necessary. The description must be 3 characters or longer
this._keychainsBackupPath = backupPath;
return true;
};

/**
* Restore the previsouly created keychains backup.
*
* @param {?string|Array<string>} excludePatterns - The list
* @param {string[]} excludePatterns - The list
* of file name patterns to be excluded from restore. The format
* of each item should be the same as '-x' option format for
* 'unzip' utility. This can also be a comma-separated string,
* which is going be transformed into a list automatically,
* for example: '*.db*,blabla.sqlite'
* @returns {boolean} If the restore opration was successful.
* @this {import('../simulator-xcode-14').default}
* @returns {Promise<boolean>} If the restore opration was successful.
* @throws {Error} If there is no keychains backup available for restore.
*/
extensions.restoreKeychains = async function restoreKeychains (excludePatterns = []) {
Expand Down Expand Up @@ -103,6 +108,7 @@ extensions.restoreKeychains = async function restoreKeychains (excludePatterns =
/**
* Clears Keychains for the particular simulator in runtime (there is no need to stop it).
*
* @this {import('../simulator-xcode-14').default}
* @throws {Error} If keychain cleanup has failed.
*/
extensions.clearKeychains = async function clearKeychains () {
Expand Down
4 changes: 2 additions & 2 deletions lib/extensions/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ extensions.shake = async function shake () {
* The simulator must be shut down in order for this method to work properly.
*
* @param {string} payload the content of the PEM certificate
* @returns {boolean} `true` if the certificate has been successfully installed
* @returns {Promise<boolean>} `true` if the certificate has been successfully installed
* or `false` if it has already been there
*/
// eslint-disable-next-line require-await
extensions.addCertificate = async function addCertificate () {
extensions.addCertificate = async function addCertificate (payload) {

Check warning on line 25 in lib/extensions/misc.js

View workflow job for this annotation

GitHub Actions / test (20)

'payload' is defined but never used

Check warning on line 25 in lib/extensions/misc.js

View workflow job for this annotation

GitHub Actions / test (18)

'payload' is defined but never used

Check warning on line 25 in lib/extensions/misc.js

View workflow job for this annotation

GitHub Actions / test (16)

'payload' is defined but never used
throw new Error(`Xcode SDK '${this.xcodeVersion}' is too old add certificates`);
};

Expand Down
4 changes: 2 additions & 2 deletions lib/extensions/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function formatStatus (status) {
*
* @param {string} db - Full path to sqlite database
* @param {string} query - The actual query string
* @returns {string} sqlite command stdout
* @returns {Promise<string>} sqlite command stdout
*/
async function execSQLiteQuery (db, query) {
log.debug(`Executing SQL query "${query}" on '${db}'`);
Expand Down Expand Up @@ -107,7 +107,7 @@ async function setAccess (udid, bundleId, permissionsMapping) {
* @param {string} serviceName - the name of the service. Should be one of
* `SERVICES` keys.
* @param {string} simDataRoot - the path to Simulator `data` root
* @returns {string} - The current status: yes/no/unset/limited
* @returns {Promise<string>} - The current status: yes/no/unset/limited
* @throws {Error} If there was an error while retrieving permissions.
*/
async function getAccess (bundleId, serviceName, simDataRoot) {
Expand Down
2 changes: 1 addition & 1 deletion lib/extensions/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extensions.setReduceTransparency = async function setReduceTransparency (reduceT
* for example, 'com.apple.Preferences' or 'com.apple.Accessibility' or
* full path to a plist file on the local file system.
* @param {object} updates Mapping of keys/values to be updated
* @returns {boolean} True if settings were actually changed
* @returns {Promise<boolean>} True if settings were actually changed
*/
extensions.updateSettings = async function updateSettings (domain, updates) {
if (_.isEmpty(updates)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/simulator-xcode-10.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ class SimulatorXcode10 extends SimulatorXcode93 {

/**
* Verify whether the particular application is installed on Simulator.
* @override
*
* @param {string} bundleId - The bundle id of the application to be checked.
* @return {boolean} True if the given application is installed.
* @return {Promise<boolean>} True if the given application is installed.
*/
async isAppInstalled (bundleId) {
try {
Expand All @@ -37,14 +36,15 @@ class SimulatorXcode10 extends SimulatorXcode93 {
}

/**
* @override
* @param {string} url
*/
async openUrl (url) {
if (!await this.isRunning()) {
throw new Error(`Tried to open '${url}', but Simulator is not in Booted state`);
}
const timer = new timing.Timer().start();
await this.simctl.openUrl(url);
/** @type {Error|undefined|null} */
let psError;
try {
await waitForCondition(async () => {
Expand Down
23 changes: 5 additions & 18 deletions lib/simulator-xcode-11.4.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import _ from 'lodash';
import SimulatorXcode11 from './simulator-xcode-11';

class SimulatorXcode11_4 extends SimulatorXcode11 {
/** @type {(bundleId: string, permissionsMapping: Record<string, any>) => Promise<void>} */
setPermissions;

constructor (udid, xcodeVersion) {
super(udid, xcodeVersion);

Expand All @@ -11,7 +14,6 @@ class SimulatorXcode11_4 extends SimulatorXcode11 {
}

/**
* @override
* Sets UI appearance style.
* This function can only be called on a booted simulator.
*
Expand All @@ -25,12 +27,11 @@ class SimulatorXcode11_4 extends SimulatorXcode11 {
}

/**
* @override
* Gets the current UI appearance style
* This function can only be called on a booted simulator.
*
* @since Xcode SDK 11.4
* @returns {string} the current UI appearance style.
* @returns {Promise<string>} the current UI appearance style.
* Possible values are:
* - dark: to switch to the Dark mode
* - light: to switch to the Light mode
Expand All @@ -47,13 +48,12 @@ class SimulatorXcode11_4 extends SimulatorXcode11 {
*/

/**
* @override
* Adds the given certificate to the booted simulator.
* The simulator could be in both running and shutdown states
* in order for this method to run as expected.
*
* @param {string} payload the content of the PEM certificate
* @param {CertificateOptions} opts
* @param {Partial<CertificateOptions>} opts
*/
async addCertificate (payload, opts = {}) {
const {
Expand All @@ -65,7 +65,6 @@ class SimulatorXcode11_4 extends SimulatorXcode11 {
}

/**
* @override
* Simulates push notification delivery to the booted simulator
*
* @since Xcode SDK 11.4
Expand All @@ -87,18 +86,6 @@ class SimulatorXcode11_4 extends SimulatorXcode11 {
}

/**
* @override
*/
async setPermissions (bundleId, permissionsMapping) {
return await super.setPermissions(bundleId, permissionsMapping);

// TODO: Switch to `simctl privacy` call after Apple
// fixes the command (https://github.com/appium/appium/issues/14355)
// Source PR: https://github.com/appium/appium-ios-simulator/pull/279
}

/**
* @override
*/
async clearKeychains () {
await this.simctl.resetKeychain();
Expand Down
Loading

0 comments on commit 20b5282

Please sign in to comment.