Skip to content

Commit

Permalink
feat: Switch from babel to typescript (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Aug 18, 2023
1 parent 229d312 commit bed549f
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 66 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.

16 changes: 13 additions & 3 deletions lib/simctl.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ const DEFAULT_OPTS = {


class Simctl {
/** @type {any?} */
xcrun;

/** @type {number} */
execTimeout;

/** @type {boolean} */
logErrors;

/**
* @param {SimctlOpts} opts
* @param {Partial<SimctlOpts>} opts
*/
constructor (opts = {}) {
opts = _.cloneDeep(opts);
Expand Down Expand Up @@ -111,8 +120,8 @@ class Simctl {
* @param {string} subcommand - One of available simctl subcommands.
* Execute `xcrun simctl` in Terminal to see the full list
* of available subcommands.
* @param {ExecOpts?} opts
* @return {Promise<ExecResult|SubProcess>} Either the result of teen process's `exec` or
* @param {Partial<ExecOpts>} opts
* @return {Promise<import('teen_process').TeenProcessExecResult|import('teen_process').SubProcess>} Either the result of teen process's `exec` or
* `SubProcess` instance depending of `opts.asynchronous` value.
* @throws {Error} If the simctl subcommand command returns non-zero return code.
*/
Expand Down Expand Up @@ -154,6 +163,7 @@ class Simctl {
);
execArgs = ['arch', [...archArgs, xcrun, ...args], execOpts];
}
// @ts-ignore We know what we are doing here
return asynchronous ? new SubProcess(...execArgs) : await tpExec(...execArgs);
} catch (e) {
if (!this.logErrors || !logErrors) {
Expand Down
2 changes: 1 addition & 1 deletion lib/subcommands/addmedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const commands = {};
*
* @param {string} filePath - Full path to a media file on the local
* file system.
* @return {Promise<ExecResult>} Command execution result.
* @return {Promise<import('teen_process').TeenProcessExecResult>} Command execution result.
* @throws {Error} If the corresponding simctl subcommand command
* returns non-zero return code.
* @throws {Error} If the `udid` instance property is unset
Expand Down
4 changes: 2 additions & 2 deletions lib/subcommands/bootstatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const commands = {};
* until Simulator booting is completed.
* The method is only available since Xcode8.
*
* @param {BootMonitorOptions} opts - Monitoring options.
* @returns {Promise<SubProcess>} The instance of the corresponding monitoring process.
* @param {Partial<BootMonitorOptions>} opts - Monitoring options.
* @returns {Promise<import('teen_process').SubProcess>} The instance of the corresponding monitoring process.
* @throws {Error} If the Simulator fails to finish booting within the given timeout and onFinished
* property is not set.
* @throws {Error} If the `udid` instance property is unset
Expand Down
4 changes: 2 additions & 2 deletions lib/subcommands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const commands = {};
* @param {string} name - The device name to be created.
* @param {string} deviceTypeId - Device type, for example 'iPhone 6'.
* @param {string} platformVersion - Platform version, for example '10.3'.
* @param {SimCreationOpts} opts - Simulator options for creating devices.
* @param {Partial<SimCreationOpts>} opts - Simulator options for creating devices.
* @return {Promise<string>} The UDID of the newly created device.
* @throws {Error} If the corresponding simctl subcommand command
* returns non-zero return code.
Expand Down Expand Up @@ -92,7 +92,7 @@ commands.createDevice = async function createDevice (name, deviceTypeId, platfor
}

// make sure that it gets out of the "Creating" state
const retries = parseInt(timeout / 1000, 10);
const retries = parseInt(`${timeout / 1000}`, 10);
await retryInterval(retries, 1000, async () => {
const devices = _.values(await this.getDevices());
for (const deviceArr of _.values(devices)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/subcommands/erase.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const commands = {};
*/
commands.eraseDevice = async function eraseDevice (timeout = 1000) {
// retry erase with a sleep in between because it's flakey
const retries = parseInt(timeout / 200, 10);
const retries = parseInt(`${timeout / 200}`, 10);
await retryInterval(retries, 200,
async () => await this.exec('erase', {
args: [this.requireUdid('erase')]
Expand Down
4 changes: 2 additions & 2 deletions lib/subcommands/keychain.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function handleRawPayload (payload, onPayloadStored) {
* @param {string} cert the full path to a valid .cert file containing
* the certificate content or the certificate content itself, depending on
* options
* @param {CertOptions} opts
* @param {Partial<CertOptions>} opts
* @throws {Error} if the current SDK version does not support the command
* or there was an error while adding the certificate
* @throws {Error} If the `udid` instance property is unset
Expand All @@ -62,7 +62,7 @@ commands.addRootCertificate = async function addRootCertificate (cert, opts = {}
* @param {string} cert the full path to a valid .cert file containing
* the certificate content or the certificate content itself, depending on
* options
* @param {CertOptions} opts
* @param {Partial<CertOptions>} opts
* @throws {Error} if the current SDK version does not support the command
* or there was an error while adding the certificate
* @throws {Error} If the `udid` instance property is unset
Expand Down
1 change: 1 addition & 0 deletions lib/subcommands/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const commands = {};
* @throws {Error} If the `udid` instance property is unset
*/
commands.launchApp = async function launchApp (bundleId, tries = 5) {
// @ts-ignore A string will always be returned
return await retryInterval(tries, 1000, async () => {
const {stdout} = await this.exec('launch', {
args: [this.requireUdid('launch'), bundleId],
Expand Down
12 changes: 6 additions & 6 deletions lib/subcommands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const commands = {};
* Parse the list of existing Simulator devices to represent
* it as convenient mapping.
*
* @param {?string} platform - The platform name, for example 'watchOS'.
* @return {Object} The resulting mapping. Each key is platform version,
* @param {string?} platform - The platform name, for example 'watchOS'.
* @return {Promise<Record<string, any>>} The resulting mapping. Each key is platform version,
* for example '10.3' and the corresponding value is an
* array of the matching {@link DeviceInfo} instances.
* @throws {Error} If the corresponding simctl subcommand command
Expand All @@ -39,7 +39,7 @@ commands.getDevicesByParsing = async function getDevicesByParsing (platform) {
// ...
// so, get the `-- iOS X.X --` line to find the sdk (X.X)
// and the rest of the listing in order to later find the devices
const deviceSectionRe = _.isEmpty(platform)
const deviceSectionRe = _.isEmpty(platform) || !platform
? new RegExp(`\\-\\-\\s+(\\S+)\\s+(\\S+)\\s+\\-\\-(\\n\\s{4}.+)*`, 'mgi')
: new RegExp(`\\-\\-\\s+${_.escapeRegExp(platform)}\\s+(\\S+)\\s+\\-\\-(\\n\\s{4}.+)*`, 'mgi');
const matches = [];
Expand Down Expand Up @@ -124,7 +124,7 @@ commands.getDevices = async function getDevices (forSdk, platform) {
* }
* }
*/
const versionMatchRe = _.isEmpty(platform)
const versionMatchRe = _.isEmpty(platform) || !platform
? new RegExp(`^([^\\s-]+)[\\s-](\\S+)`, 'i')
: new RegExp(`^${_.escapeRegExp(platform)}[\\s-](\\S+)`, 'i');
for (let [sdkName, entries] of _.toPairs(JSON.parse(stdout).devices)) {
Expand Down Expand Up @@ -176,7 +176,7 @@ commands.getDevices = async function getDevices (forSdk, platform) {
*
* @param {string} platformVersion - The platform version name,
* for example '10.3'.
* @param {string?} platform - The platform name, for example 'watchOS'.
* @param {string} platform - The platform name, for example 'watchOS'.
* @return {Promise<string>} The corresponding runtime name for the given
* platform version.
*/
Expand All @@ -199,7 +199,7 @@ commands.getRuntimeForPlatformVersionViaJson = async function getRuntimeForPlatf
*
* @param {string} platformVersion - The platform version name,
* for example '10.3'.
* @param {string?} platform - The platform name, for example 'watchOS'.
* @param {string} platform - The platform name, for example 'watchOS'.
* @return {Promise<string>} The corresponding runtime name for the given
* platform version.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/subcommands/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const commands = {};
* @returns {string} Formatted value, for example -73.768254
*/
function formatArg (name, value) {
const flt = parseFloat(value);
const flt = parseFloat(`${value}`);
if (isNaN(flt)) {
throw new TypeError(`${name} must be a valid number, got '${value}' instead`);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/subcommands/openurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const commands = {};
*
* @param {string} url - The URL scheme to open, for example http://appiom.io
* will be opened by the built-in mobile browser.
* @return {Promise<ExecResult>} Command execution result.
* @return {Promise<import('teen_process').TeenProcessExecResult>} Command execution result.
* @throws {Error} If the corresponding simctl subcommand command
* returns non-zero return code.
* @throws {Error} If the `udid` instance property is unset
Expand Down
4 changes: 2 additions & 2 deletions lib/subcommands/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const commands = {};
*
* @param {string|string[]} args - Spawn arguments
* @param {object} env [{}] - Additional environment variables mapping.
* @return {Promise<ExecResult>} Command execution result.
* @return {Promise<import('teen_process').TeenProcessExecResult>} Command execution result.
* @throws {Error} If the corresponding simctl subcommand command
* returns non-zero return code.
* @throws {Error} If the `udid` instance property is unset
Expand All @@ -31,7 +31,7 @@ commands.spawnProcess = async function spawnProcess (args, env = {}) {
*
* @param {string|string[]} args - Spawn arguments
* @param {object} env [{}] - Additional environment variables mapping.
* @return {Promise<SubProcess>} The instance of the process to be spawned.
* @return {Promise<import('teen_process').SubProcess>} The instance of the process to be spawned.
* @throws {Error} If the `udid` instance property is unset
*/
commands.spawnSubProcess = async function spawnSubProcess (args, env = {}) {
Expand Down
43 changes: 27 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"CHANGELOG.md"
],
"dependencies": {
"@babel/runtime": "^7.0.0",
"asyncbox": "^2.3.1",
"bluebird": "^3.5.1",
"lodash": "^4.2.1",
Expand All @@ -46,7 +45,9 @@
"which": "^3.0.1"
},
"scripts": {
"build": "rm -rf build && babel --out-dir=build/lib lib && babel --out-dir=build index.js",
"build": "tsc -b",
"clean": "npm run build -- --clean",
"rebuild": "npm run clean; npm run build",
"dev": "npm run build -- --watch",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
Expand All @@ -71,31 +72,41 @@
"singleQuote": true
},
"devDependencies": {
"@appium/eslint-config-appium": "^6.0.0",
"@babel/cli": "^7.18.10",
"@babel/core": "^7.18.10",
"@babel/eslint-parser": "^7.18.9",
"@babel/plugin-transform-runtime": "^7.18.10",
"@babel/preset-env": "^7.18.10",
"@babel/register": "^7.18.9",
"@appium/eslint-config-appium": "^8.0.4",
"@appium/eslint-config-appium-ts": "^0.3.1",
"@appium/tsconfig": "^0.3.0",
"@appium/types": "^0.13.2",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@types/bluebird": "^3.5.38",
"@types/chai": "^4.3.5",
"@types/chai-as-promised": "^7.1.5",
"@types/lodash": "^4.14.196",
"@types/mocha": "^10.0.1",
"@types/node": "^20.4.7",
"@types/sinon": "^10.0.16",
"@types/sinon-chai": "^3.2.9",
"@types/teen_process": "2.0.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"appium-xcode": "^5.0.0",
"babel-plugin-source-map-support": "^2.2.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"conventional-changelog-conventionalcommits": "^6.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-mocha": "^9.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint": "^8.46.0",
"eslint-config-prettier": "^8.9.0",
"eslint-import-resolver-typescript": "^3.5.5",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-mocha": "^10.1.0",
"eslint-plugin-promise": "^6.1.1",
"lint-staged": "^14.0.0",
"mocha": "^10.0.0",
"pre-commit": "^1.1.3",
"prettier": "^3.0.0",
"proxyquire": "^2.1.3",
"semantic-release": "^20.0.2",
"sinon": "^15.0.0"
"sinon": "^15.0.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
}
}
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@appium/tsconfig/tsconfig.json",
"compilerOptions": {
"strict": false, // TODO: make this flag true
"outDir": "build",
"types": ["node"],
"checkJs": true
},
"include": [
"index.js",
"lib"
]
}

0 comments on commit bed549f

Please sign in to comment.