Skip to content

Commit

Permalink
feat: Switch babel to typescript (#128)
Browse files Browse the repository at this point in the history
* feat: Switch babel to typescript

* Fix linter
  • Loading branch information
mykola-mokhnach authored Aug 25, 2023
1 parent 584ddc7 commit e091d64
Show file tree
Hide file tree
Showing 23 changed files with 165 additions and 120 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.

15 changes: 8 additions & 7 deletions lib/afc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class AfcService extends BaseServiceSocket {
/**
* Lists a directory's contents and returns them in an array
* @param {string} path The path in unix format
* @return {Array.<string>}
* @return {Promise<string[]>}
*/
async listDirectory (path) {
const {packetNumber, response} = this._createPacketPromise(`List directory '${path}'`);
Expand All @@ -107,7 +107,7 @@ class AfcService extends BaseServiceSocket {
* Opens a file and creates a file handle
* @param {string} path The path in unix format
* @param {number} mode The file mode that will be used
* @return {number}
* @return {Promise<number>}
*/
async openFile (path, mode) {
const {packetNumber, response} = this._createPacketPromise(`Open file '${path}'`);
Expand All @@ -134,7 +134,7 @@ class AfcService extends BaseServiceSocket {
* Opens a file and creates a nodejs write stream
* @param {string} filePath The path in unix format
* @param {Object} opts The regular options that are passed to a Stream.Writable
* @return {AfcWritableFileStream}
* @return {Promise<AfcWritableFileStream>}
*/
async createWriteStream (filePath, opts) {
const fileHandle = await this.openFile(filePath, FileModes.w);
Expand All @@ -144,8 +144,8 @@ class AfcService extends BaseServiceSocket {
/**
* Opens a file and creates a nodejs read stream
* @param {string} filePath The path in unix format
* @param {Object} opts The regular options that are passed to a Stream.Readable
* @return {AfcReadableFileStream}
* @param {Object} options The regular options that are passed to a Stream.Readable
* @return {Promise<AfcReadableFileStream>}
*/
async createReadStream (filePath, options) {
const fileHandle = await this.openFile(filePath, FileModes.r);
Expand Down Expand Up @@ -198,7 +198,7 @@ class AfcService extends BaseServiceSocket {
* Read a certain length of buffer from the file handle
* @param {number} fileHandle The file handle to be used
* @param {number} length The length that wants to be read from the file handle
* @return {Buffer}
* @return {Promise<Buffer>}
*/
async readFile (fileHandle, length) {
const {packetNumber, response} = this._createPacketPromise(`Read from file handle '${fileHandle}'`);
Expand All @@ -223,7 +223,7 @@ class AfcService extends BaseServiceSocket {
/**
* Get the file info of the given path
* @param {string} path The path in unix format
* @return {FileInfo}
* @return {Promise<FileInfo>}
*/
async getFileInfo (path) {
const {packetNumber, response} = this._createPacketPromise(`Get file info '${path}'`);
Expand All @@ -238,6 +238,7 @@ class AfcService extends BaseServiceSocket {
if (res.opCode !== Operations.DATA) {
this._checkStatus(res);
}
// @ts-ignore this should be ok
return new FileInfo(this._parseObject(res.content));
}

Expand Down
4 changes: 2 additions & 2 deletions lib/house-arrest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HouseArrestService extends BaseServiceSocket {
* Vends into the application container and returns an AfcService
* @param {string} bundleId The bundle id of the app container that we will enter to
* @throws Will throw an error if house arrest fails to access the application's container
* @returns {AfcService}
* @returns {Promise<AfcService>}
*/
async vendContainer (bundleId) {
const responsePromise = this._receivePlistPromise();
Expand All @@ -55,7 +55,7 @@ class HouseArrestService extends BaseServiceSocket {
* Vends into the application documents and returns an AfcService
* @param {string} bundleId The bundle id of the app documents that we will enter to
* @throws Will throw an error if house arrest fails to access the application's documents
* @returns {AfcService}
* @returns {Promise<AfcService>}
*/
async vendDocuments (bundleId) {
const responsePromise = this._receivePlistPromise();
Expand Down
2 changes: 1 addition & 1 deletion lib/imagemounter/utils/list_developer_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async function searchAndDownloadDeveloperImageFromGithub(finalVersion, fullDownl
let fileUrl;
const fileList = await listGithubImageList(githubImageOption);
if (!fileList) {
throw new Error(`Failed to list https://github.com/${githubRepo}`);;
throw new Error(`Failed to list https://github.com/${githubRepo}`);
}
const targetFile = _.find(fileList, (item) => fileNameRegExp.test(item.path));
const splitter = subFolderList.length > 0 ? '/' : '';
Expand Down
9 changes: 5 additions & 4 deletions lib/installation-proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class InstallationProxyService extends BaseServicePlist {

/**
* Lists applications according to the opts and returns them as a map
* @param {ListApplicationOptions} opts the listing options that wants to be passed
* @returns A map of the applications which the key is the bundleId
* @param {Partial<ListApplicationOptions>} opts the listing options that wants to be passed
* @returns {Promise<Record<any, any>>} A map of the applications which the key is the bundleId
*/
async listApplications (opts = {}) {
const request = {
Expand Down Expand Up @@ -93,8 +93,8 @@ class InstallationProxyService extends BaseServicePlist {

/**
* Lists applications according to the opts and returns them as a map
* @param {LookupApplicationOptions} opts the lookup options that wants to be passed
* @returns A map of the applications which the key is the bundleId
* @param {Partial<LookupApplicationOptions>} opts the lookup options that wants to be passed
* @returns {Promise<Record<any, any>>} A map of the applications which the key is the bundleId
*/
async lookupApplications (opts = {}) {
const request = {
Expand Down Expand Up @@ -146,6 +146,7 @@ class InstallationProxyService extends BaseServicePlist {
return messages;
}
}
return messages;
}

_isFinished (response) {
Expand Down
18 changes: 10 additions & 8 deletions lib/instrument/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const AUX_TYPES = Object.freeze({

/**
* @typedef {Object} DTXMessageOptions
* @property {any} selector
* @property {number} identifier packet transmission sequence, this value is incremented for each request
* @property {number} channelCode the data transmission of the service will use this channel code.
* Each service has a separate channel
Expand All @@ -52,7 +53,7 @@ const AUX_TYPES = Object.freeze({

class DTXMessageHeader {
/**
* @param {DTXMessageOptions} data
* @param {Partial<DTXMessageOptions>} data
* @returns {Buffer} DTXMessageHeaderBuffer
*/
static build (data) {
Expand All @@ -61,11 +62,11 @@ class DTXMessageHeader {
messageHeader.writeUInt32LE(DTX_MESSAGE_HEADER_LENGTH, 4);
messageHeader.writeUInt16LE(0, 8);
messageHeader.writeUInt16LE(1, 10);
messageHeader.writeUInt32LE(data.payloadLength, 12);
messageHeader.writeUInt32LE(data.identifier, 16);
messageHeader.writeUInt32LE(data.conversationIndex, 20);
messageHeader.writeUInt32LE(data.channelCode, 24);
messageHeader.writeUInt32LE(data.expectsReply, 28);
messageHeader.writeUInt32LE(data.payloadLength ?? 0, 12);
messageHeader.writeUInt32LE(data.identifier ?? 0, 16);
messageHeader.writeUInt32LE(data.conversationIndex ?? 0, 20);
messageHeader.writeUInt32LE(data.channelCode ?? 0, 24);
messageHeader.writeUInt32LE(+(data.expectsReply ?? false), 28);
return messageHeader;
}

Expand Down Expand Up @@ -166,7 +167,7 @@ class DTXMessageAux {
* Parses nskeyed Buffer into js array
* @param {Buffer} headerBuffer
* @param {DTXMessagePayloadHeaderObject} payloadHeader
* @returns {Array}
* @returns {any[]}
*/
static parse (headerBuffer, payloadHeader) {
let cursor = 0;
Expand Down Expand Up @@ -298,7 +299,7 @@ class DTXMessageAuxBuffer {

class DTXMessage {
/**
* @param {DTXMessageOptions} opts
* @param {Partial<DTXMessageOptions>} opts
*/
constructor (opts = {}) {
const {
Expand Down Expand Up @@ -415,6 +416,7 @@ class DTXMessage {
return ret;
}
if (ret._payloadHeader.auxLength > 0) {
// @ts-ignore Not 100% sure if this ok
ret.auxiliaries = DTXMessageAux.parse(payloadBuf.slice(cursor, cursor + ret._payloadHeader.auxLength), ret._payloadHeader);
cursor += ret._payloadHeader.auxLength;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/instrument/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class InstrumentService extends BaseServiceSocket {
* @param {import('net').Socket} socketClient DTXMessage.selector
* @param {DTXCallback?} event if empty will ignore any messages
*/
constructor(socketClient, event) {
constructor(socketClient, event = null) {
super(socketClient);
this._undefinedCallback = event;
this._callbacks = new events.EventEmitter();
Expand Down Expand Up @@ -172,8 +172,8 @@ class InstrumentService extends BaseServiceSocket {
}
if (data.conversationIndex === 1) {
this._replyQueues[data.identifier].push(data);
} else if (this._channelCallbacks.listenerCount(CHANNEL_OFFSET - data.channelCode) > 0) {
this._channelCallbacks.emit(CHANNEL_OFFSET - data.channelCode, data);
} else if (this._channelCallbacks.listenerCount(`${CHANNEL_OFFSET - data.channelCode}`) > 0) {
this._channelCallbacks.emit(`${CHANNEL_OFFSET - data.channelCode}`, data);
} else {
const selector = data.selector;
if (_.isString(selector) && this._callbacks.listenerCount(selector) > 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/instrument/transformer/dtx-decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DTXDecoder extends Stream.Transform {
if (this.header.fragmentId === 0) {
// only the 0th fragment contains a message header
if (!(this.header.channel in this._dtxManager)) {
this._dtxManager[this.header.channel] = {headerBuffer, payloadBuffer: new Buffer.allocUnsafe(0)};
this._dtxManager[this.header.channel] = {headerBuffer, payloadBuffer: Buffer.allocUnsafe(0)};
}
if (this.header.fragmentCount > 1) {
// Continue to get the next message fragments
Expand Down
2 changes: 1 addition & 1 deletion lib/instrument/transformer/dtx-encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DTXEncoder extends Stream.Transform {
}

_transform (data, encoding, onData) {
this.push(this._encode(data), 'buffer');
this.push(this._encode(data), 'binary');
onData();
}

Expand Down
24 changes: 18 additions & 6 deletions lib/instrument/transformer/nskeyed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import _ from 'lodash';
import { format as stringFormat } from 'node:util';

const NSKEYED_ARCHIVE_VERSION = 100_000;
// @ts-ignore UID is not exposed to typedefs
const NULL_UID = new plistlib.UID(0);
const CYCLE_TOKEN = 'CycleToken';
const PRIMITIVE_TYPES = ['Number', 'String', 'Boolean', 'UID', 'Buffer'];
Expand Down Expand Up @@ -65,16 +66,16 @@ class BaseArchiveHandler {
/**
* @param {ArchivedObject} archive
*/
// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
decodeArchive(archive) {
throw new Error(`Did not know how to decode the object`);
}

/**
* @param {Object} obj an instance of this class
* @param {Object} obj an instance of this §class
* @param {ArchivingObject} archive
*/
// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
encodeArchive(obj, archive) {
throw new Error(`Did not know how to encode the object`);
}
Expand Down Expand Up @@ -276,7 +277,7 @@ class XCTestConfiguration extends BaseArchiveHandler {
testsToSkip: undefined,
treatMissingBaselinesAsFailures: false,
userAttachmentLifetime: 1
}
};

/**
* @param {XCTestConfigurationPlist} data
Expand All @@ -293,13 +294,19 @@ class XCTestConfiguration extends BaseArchiveHandler {
data.testBundleURL = new NSURL(undefined, data.testBundleURL);
}
if (!(data.testBundleURL instanceof NSURL)) {
throw new TypeError(`Expected testBundleURL to be a valid NSURL instance, got ${data.testBundleURL.constructor.name} instead`);
throw new TypeError(
// @ts-ignore contructor is always present
`Expected testBundleURL to be a valid NSURL instance, got ${data.testBundleURL.constructor.name} instead`
);
}
if (typeof data.sessionIdentifier === 'string') {
data.sessionIdentifier = new NSUUID(data.sessionIdentifier);
}
if (!(data.sessionIdentifier instanceof NSUUID)) {
throw new TypeError(`Expected sessionIdentifier to be a valid NSUUID instance, got ${data.sessionIdentifier.constructor.name} instead`);
throw new TypeError(
// @ts-ignore contructor is always present
`Expected sessionIdentifier to be a valid NSUUID instance, got ${data.sessionIdentifier.constructor.name} instead`
);
}
this._data = { ...XCTestConfiguration._default, ...data };
}
Expand Down Expand Up @@ -432,10 +439,12 @@ class Unarchive {
class Archive {
constructor(inputObject) {
this.input = inputObject;
/** @type {any[]} */
this.objects = ['$null']; // objects that go directly into the archive, always start with $null
}

uidForArchiver(archiver, ...addition) {
// @ts-ignore UID is not exposed to typedefs
const val = new plistlib.UID(this.objects.length);
this.objects.push({
$classes: [archiver, ...addition],
Expand All @@ -448,6 +457,7 @@ class Archive {
if (_.isUndefined(obj) || _.isNull(obj)) {
return NULL_UID;
}
// @ts-ignore UID is not exposed to typedefs
const index = new plistlib.UID(this.objects.length);
if (PRIMITIVE_TYPES.includes(obj.constructor.name)) {
this.objects.push(obj);
Expand Down Expand Up @@ -522,6 +532,7 @@ class Archive {
const d = {
$version: NSKEYED_ARCHIVE_VERSION,
$archiver: NSKEYEDARCHIVER,
// @ts-ignore UID is not exposed to typedefs
$top: { root: new plistlib.UID(1) },
$objects: this.objects
};
Expand Down Expand Up @@ -553,6 +564,7 @@ function unarchive(inputBytes) {
* @param {BaseArchiveHandler} subClass inherit from BaseArchiveHandler class
*/
function updateNSKeyedArchiveClass(name, subClass) {
// @ts-ignore prototype always exists
if (!_.isFunction(subClass.prototype?.decodeArchive) && !_.isFunction(subClass.prototype?.encodeArchive)) {
throw new Error('subClass must have decodeArchive or encodeArchive methods');
}
Expand Down
Loading

0 comments on commit e091d64

Please sign in to comment.