Skip to content

Commit 76e0979

Browse files
authored
fix: format (#290)
1 parent f075330 commit 76e0979

37 files changed

Lines changed: 1091 additions & 1043 deletions

.github/workflows/unit-test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ jobs:
3838
name: Install dev dependencies
3939
- run: npm run lint
4040
name: Run linter
41+
- run: npm run format:check
42+
name: Run Prettier check
4143
- run: npm run test
4244
name: Run unit tests

lib/helpers.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as semver from 'semver';
2-
import { spawn } from 'node:child_process';
3-
import { Readable } from 'node:stream';
2+
import {spawn} from 'node:child_process';
3+
import {Readable} from 'node:stream';
44

55
export const DEFAULT_EXEC_TIMEOUT = 10 * 60 * 1000; // ms
66
export const SIM_RUNTIME_NAME = 'com.apple.CoreSimulator.SimRuntime.';
@@ -13,7 +13,7 @@ export const SIM_RUNTIME_NAME = 'com.apple.CoreSimulator.SimRuntime.';
1313
* @return The version in 'major.minor' form
1414
* @throws {Error} If the version not parseable by the `semver` package
1515
*/
16-
export function normalizeVersion (version: string): string {
16+
export function normalizeVersion(version: string): string {
1717
const semverVersion = semver.coerce(version);
1818
if (!semverVersion) {
1919
throw new Error(`Unable to parse version '${version}'`);
@@ -24,7 +24,7 @@ export function normalizeVersion (version: string): string {
2424
/**
2525
* @returns The xcrun binary name
2626
*/
27-
export function getXcrunBinary (): string {
27+
export function getXcrunBinary(): string {
2828
return process.env.XCRUN_BINARY || 'xcrun';
2929
}
3030

@@ -33,7 +33,7 @@ export function getXcrunBinary (): string {
3333
*
3434
* @returns Promise resolving to UUID string
3535
*/
36-
export async function uuidV4 (): Promise<string> {
36+
export async function uuidV4(): Promise<string> {
3737
const uuidLib = await import('uuid');
3838
return uuidLib.v4();
3939
}
@@ -45,7 +45,7 @@ export async function uuidV4 (): Promise<string> {
4545
* @return Promise resolving to parsed JSON object
4646
* @throws {Error} If plutil fails to convert the input
4747
*/
48-
export async function convertPlistToJson (plistInput: string): Promise<any> {
48+
export async function convertPlistToJson(plistInput: string): Promise<any> {
4949
const plutilProcess = spawn('plutil', ['-convert', 'json', '-o', '-', '-']);
5050
let jsonOutput = '';
5151
plutilProcess.stdout.on('data', (chunk) => {
@@ -71,11 +71,12 @@ export async function convertPlistToJson (plistInput: string): Promise<any> {
7171
});
7272
} catch (err) {
7373
plutilProcess.kill(9);
74-
throw new Error(`Failed to convert plist to JSON: ${err instanceof Error ? err.message : String(err)}`);
74+
throw new Error(
75+
`Failed to convert plist to JSON: ${err instanceof Error ? err.message : String(err)}`,
76+
);
7577
} finally {
7678
plutilProcess.removeAllListeners();
7779
inputStream.removeAllListeners();
7880
}
7981
return JSON.parse(jsonOutput);
8082
}
81-

lib/logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import appiumLogger from '@appium/logger';
22

33
export const LOG_PREFIX = 'simctl';
44

5-
function getLogger () {
5+
function getLogger() {
66
const logger = global._global_npmlog || appiumLogger;
77
if (!logger.debug) {
8-
logger.addLevel('debug', 1000, { fg: 'blue', bg: 'black' }, 'dbug');
8+
logger.addLevel('debug', 1000, {fg: 'blue', bg: 'black'}, 'dbug');
99
}
1010
return logger;
1111
}

lib/simctl.ts

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import _ from 'lodash';
22
import which from 'which';
3-
import { log, LOG_PREFIX } from './logger';
4-
import {
5-
DEFAULT_EXEC_TIMEOUT, getXcrunBinary,
6-
} from './helpers';
7-
import { exec as tpExec, SubProcess } from 'teen_process';
3+
import {log, LOG_PREFIX} from './logger';
4+
import {DEFAULT_EXEC_TIMEOUT, getXcrunBinary} from './helpers';
5+
import {exec as tpExec, SubProcess} from 'teen_process';
86
import * as addmediaCommands from './subcommands/addmedia';
97
import * as appinfoCommands from './subcommands/appinfo';
108
import * as bootCommands from './subcommands/boot';
@@ -30,9 +28,7 @@ import * as terminateCommands from './subcommands/terminate';
3028
import * as uiCommands from './subcommands/ui';
3129
import * as uninstallCommands from './subcommands/uninstall';
3230
import * as locationCommands from './subcommands/location';
33-
import type {
34-
XCRun, ExecOpts, SimctlOpts, ExecResult,
35-
} from './types';
31+
import type {XCRun, ExecOpts, SimctlOpts, ExecResult} from './types';
3632

3733
const SIMCTL_ENV_PREFIX = 'SIMCTL_CHILD_';
3834

@@ -43,27 +39,27 @@ export class Simctl {
4339
private _udid: string | null;
4440
private _devicesSetPath: string | null;
4541

46-
constructor (opts: SimctlOpts = {}) {
47-
this.xcrun = _.cloneDeep(opts.xcrun ?? { path: null });
42+
constructor(opts: SimctlOpts = {}) {
43+
this.xcrun = _.cloneDeep(opts.xcrun ?? {path: null});
4844
this.execTimeout = opts.execTimeout ?? DEFAULT_EXEC_TIMEOUT;
4945
this.logErrors = opts.logErrors ?? true;
5046
this._udid = opts.udid ?? null;
5147
this._devicesSetPath = opts.devicesSetPath ?? null;
5248
}
5349

54-
set udid (value: string | null) {
50+
set udid(value: string | null) {
5551
this._udid = value;
5652
}
5753

58-
get udid (): string | null {
54+
get udid(): string | null {
5955
return this._udid;
6056
}
6157

62-
set devicesSetPath (value: string | null) {
58+
set devicesSetPath(value: string | null) {
6359
this._devicesSetPath = value;
6460
}
6561

66-
get devicesSetPath (): string | null {
62+
get devicesSetPath(): string | null {
6763
return this._devicesSetPath;
6864
}
6965

@@ -72,26 +68,30 @@ export class Simctl {
7268
* @returns The UDID string
7369
* @throws {Error} If UDID is not set
7470
*/
75-
requireUdid (commandName: string | null = null): string {
71+
requireUdid(commandName: string | null = null): string {
7672
if (!this.udid) {
77-
throw new Error(`udid is required to be set for ` +
78-
(commandName ? `the '${commandName}' command` : 'this simctl command'));
73+
throw new Error(
74+
`udid is required to be set for ` +
75+
(commandName ? `the '${commandName}' command` : 'this simctl command'),
76+
);
7977
}
8078
return this.udid;
8179
}
8280

8381
/**
8482
* @returns Promise resolving to the xcrun binary path
8583
*/
86-
async requireXcrun (): Promise<string> {
84+
async requireXcrun(): Promise<string> {
8785
const xcrunBinary = getXcrunBinary();
8886

8987
if (!this.xcrun.path) {
9088
try {
9189
this.xcrun.path = await which(xcrunBinary);
9290
} catch {
93-
throw new Error(`${xcrunBinary} tool has not been found in PATH. ` +
94-
`Are Xcode developers tools installed?`);
91+
throw new Error(
92+
`${xcrunBinary} tool has not been found in PATH. ` +
93+
`Are Xcode developers tools installed?`,
94+
);
9595
}
9696
}
9797
if (!this.xcrun.path) {
@@ -110,10 +110,7 @@ export class Simctl {
110110
* `SubProcess` instance depending of `opts.asynchronous` value.
111111
* @throws {Error} If the simctl subcommand command returns non-zero return code.
112112
*/
113-
async exec<T extends ExecOpts> (
114-
subcommand: string,
115-
opts?: T
116-
): Promise<ExecResult<T>> {
113+
async exec<T extends ExecOpts>(subcommand: string, opts?: T): Promise<ExecResult<T>> {
117114
const {
118115
args: initialArgs = [],
119116
env: initialEnv = {},
@@ -122,20 +119,21 @@ export class Simctl {
122119
logErrors = true,
123120
architectures,
124121
timeout,
125-
} = opts ?? {} as T;
122+
} = opts ?? ({} as T);
126123
// run a particular simctl command
127124
const args = [
128125
'simctl',
129126
...(this.devicesSetPath ? ['--set', this.devicesSetPath] : []),
130127
subcommand,
131-
...initialArgs
128+
...initialArgs,
132129
];
133130
// Prefix all passed in environment variables with 'SIMCTL_CHILD_', simctl
134131
// will then pass these to the child (spawned) process.
135132
const env = _.defaults(
136-
_.mapKeys(initialEnv,
137-
(value, key) => _.startsWith(key, SIMCTL_ENV_PREFIX) ? key : `${SIMCTL_ENV_PREFIX}${key}`),
138-
process.env
133+
_.mapKeys(initialEnv, (value, key) =>
134+
_.startsWith(key, SIMCTL_ENV_PREFIX) ? key : `${SIMCTL_ENV_PREFIX}${key}`,
135+
),
136+
process.env,
139137
);
140138

141139
const execOpts: any = {
@@ -150,14 +148,19 @@ export class Simctl {
150148
let execArgs: [string, string[], any];
151149
if (architectures?.length) {
152150
const archArgs = _.flatMap(
153-
(_.isArray(architectures) ? architectures : [architectures]).map((arch) => ['-arch', arch])
151+
(_.isArray(architectures) ? architectures : [architectures]).map((arch) => [
152+
'-arch',
153+
arch,
154+
]),
154155
);
155156
execArgs = ['arch', [...archArgs, xcrun, ...args], execOpts];
156157
} else {
157158
execArgs = [xcrun, args, execOpts];
158159
}
159160
// We know what we are doing here - the type system can't handle the dynamic nature
160-
return (asynchronous ? new SubProcess(...execArgs) : await tpExec(...execArgs)) as ExecResult<T>;
161+
return (
162+
asynchronous ? new SubProcess(...execArgs) : await tpExec(...execArgs)
163+
) as ExecResult<T>;
161164
} catch (e: any) {
162165
if (!this.logErrors || !logErrors) {
163166
// if we don't want to see the errors, just throw and allow the calling
@@ -218,4 +221,3 @@ export class Simctl {
218221
}
219222

220223
export default Simctl;
221-

lib/subcommands/addmedia.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Simctl } from '../simctl';
2-
import type { TeenProcessExecResult } from 'teen_process';
1+
import type {Simctl} from '../simctl';
2+
import type {TeenProcessExecResult} from 'teen_process';
33

44
/**
55
* Add the particular media file to Simulator's library.
@@ -12,7 +12,10 @@ import type { TeenProcessExecResult } from 'teen_process';
1212
* returns non-zero return code.
1313
* @throws {Error} If the `udid` instance property is unset
1414
*/
15-
export async function addMedia (this: Simctl, filePath: string): Promise<TeenProcessExecResult<string>> {
15+
export async function addMedia(
16+
this: Simctl,
17+
filePath: string,
18+
): Promise<TeenProcessExecResult<string>> {
1619
return await this.exec('addmedia', {
1720
args: [this.requireUdid('addmedia'), filePath],
1821
});

lib/subcommands/appinfo.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Simctl } from '../simctl';
2-
import type { AppInfo } from '../types';
3-
import { convertPlistToJson } from '../helpers';
1+
import type {Simctl} from '../simctl';
2+
import type {AppInfo} from '../types';
3+
import {convertPlistToJson} from '../helpers';
44
import _ from 'lodash';
55

66
/**
@@ -13,7 +13,7 @@ import _ from 'lodash';
1313
* returns non-zero return code.
1414
* @throws {Error} If the `udid` instance property is unset
1515
*/
16-
export async function appInfo (this: Simctl, bundleId: string): Promise<AppInfo> {
16+
export async function appInfo(this: Simctl, bundleId: string): Promise<AppInfo> {
1717
const {stdout} = await this.exec('appinfo', {
1818
args: [this.requireUdid('appinfo'), bundleId],
1919
});
@@ -26,7 +26,7 @@ export async function appInfo (this: Simctl, bundleId: string): Promise<AppInfo>
2626
result = await convertPlistToJson(stdout);
2727
} catch (err) {
2828
throw new Error(
29-
`Cannot retrieve app info for ${bundleId}: ${err instanceof Error ? err.message : String(err)}`
29+
`Cannot retrieve app info for ${bundleId}: ${err instanceof Error ? err.message : String(err)}`,
3030
);
3131
}
3232
}

lib/subcommands/boot.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
2-
import { log, LOG_PREFIX } from '../logger';
3-
import type { Simctl } from '../simctl';
2+
import {log, LOG_PREFIX} from '../logger';
3+
import type {Simctl} from '../simctl';
44

55
/**
66
* Boot the particular Simulator if it is not running.
@@ -9,10 +9,10 @@ import type { Simctl } from '../simctl';
99
* returns non-zero return code.
1010
* @throws {Error} If the `udid` instance property is unset
1111
*/
12-
export async function bootDevice (this: Simctl): Promise<void> {
12+
export async function bootDevice(this: Simctl): Promise<void> {
1313
try {
1414
await this.exec('boot', {
15-
args: [this.requireUdid('boot')]
15+
args: [this.requireUdid('boot')],
1616
});
1717
} catch (e: any) {
1818
if (_.includes(e.message, 'Unable to boot device in current state: Booted')) {

0 commit comments

Comments
 (0)