@react-native-harness/cli 1.0.0-alpha.17 → 1.0.0-alpha.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,87 +0,0 @@
1
- import { type ChildProcess } from 'node:child_process';
2
- import {
3
- assertAndroidRunnerConfig,
4
- TestRunnerConfig,
5
- } from '@react-native-harness/config';
6
- import { logger } from '@react-native-harness/tools';
7
-
8
- import { type PlatformAdapter } from '../platform-adapter.js';
9
- import {
10
- runEmulator,
11
- getEmulatorDeviceId,
12
- reversePort,
13
- isAppInstalled,
14
- getEmulatorStatus,
15
- } from './emulator.js';
16
- import { runApp, killApp } from './build.js';
17
- import { killWithAwait } from '../../process.js';
18
- import { runMetro } from '../../bundlers/metro.js';
19
- import { AppNotInstalledError } from '../../errors/errors.js';
20
-
21
- const androidPlatformAdapter: PlatformAdapter = {
22
- name: 'android',
23
- getEnvironment: async (runner: TestRunnerConfig) => {
24
- assertAndroidRunnerConfig(runner);
25
-
26
- let emulator: ChildProcess | null = null;
27
- const emulatorStatus = await getEmulatorStatus(runner.deviceId);
28
- logger.debug(`Emulator status: ${emulatorStatus}`);
29
-
30
- const metroPromise = runMetro();
31
-
32
- if (emulatorStatus === 'stopped') {
33
- logger.debug(`Emulator ${runner.deviceId} is stopped, starting it`);
34
- emulator = await runEmulator(runner.deviceId);
35
- }
36
-
37
- const deviceId = await getEmulatorDeviceId(runner.deviceId);
38
- logger.debug(`Device ID: ${deviceId}`);
39
-
40
- if (!deviceId) {
41
- throw new Error('Emulator not found');
42
- }
43
-
44
- await Promise.all([
45
- reversePort(8081),
46
- reversePort(8080),
47
- reversePort(3001),
48
- ]);
49
- logger.debug('Ports reversed');
50
-
51
- const isInstalled = await isAppInstalled(deviceId, runner.bundleId);
52
- logger.debug(`App is installed: ${isInstalled}`);
53
-
54
- if (!isInstalled) {
55
- throw new AppNotInstalledError(
56
- runner.deviceId,
57
- runner.bundleId,
58
- 'android'
59
- );
60
- }
61
-
62
- logger.debug('Waiting for Metro to start');
63
- const metro = await metroPromise;
64
- logger.debug('Metro started');
65
-
66
- logger.debug('Running app');
67
- await runApp(deviceId, runner.bundleId, runner.activityName);
68
- logger.debug('App running');
69
-
70
- return {
71
- restart: async () => {
72
- await runApp(deviceId, runner.bundleId, runner.activityName);
73
- },
74
- dispose: async () => {
75
- await killApp(deviceId, runner.bundleId);
76
-
77
- if (emulator) {
78
- await killWithAwait(emulator);
79
- }
80
-
81
- await killWithAwait(metro);
82
- },
83
- };
84
- },
85
- };
86
-
87
- export default androidPlatformAdapter;
@@ -1,71 +0,0 @@
1
- import { spawn, spawnAndForget } from '@react-native-harness/tools';
2
-
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- export const listDevices = async (): Promise<any> => {
5
- const { stdout } = await spawn('xcrun', [
6
- 'simctl',
7
- 'list',
8
- 'devices',
9
- '--json',
10
- ]);
11
- return JSON.parse(stdout);
12
- };
13
-
14
- export const getDeviceByName = async (
15
- simulatorName: string,
16
- systemVersion: string
17
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
- ): Promise<any | null> => {
19
- const devices = await listDevices();
20
- const expectedRuntimeId = `com.apple.CoreSimulator.SimRuntime.iOS-${systemVersion.replace(
21
- /\./,
22
- '-'
23
- )}`;
24
-
25
- const runtime = devices.devices[expectedRuntimeId];
26
-
27
- if (!runtime) {
28
- return null;
29
- }
30
-
31
- const runtimeDevices = devices.devices[runtime];
32
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
- const device = runtimeDevices.find((d: any) => d.name === simulatorName);
34
-
35
- if (device) {
36
- return device;
37
- }
38
-
39
- return null;
40
- };
41
-
42
- export const listApps = async (udid: string): Promise<string[]> => {
43
- const { stdout: plistOutput } = await spawn('xcrun', [
44
- 'simctl',
45
- 'listapps',
46
- udid,
47
- ]);
48
- const { stdout: jsonOutput } = await spawn(
49
- 'plutil',
50
- ['-convert', 'json', '-o', '-', '-'],
51
- { stdin: { string: plistOutput } }
52
- );
53
- return Object.keys(JSON.parse(jsonOutput));
54
- };
55
-
56
- export const isAppInstalled = async (
57
- udid: string,
58
- bundleId: string
59
- ): Promise<boolean> => {
60
- const appList = await listApps(udid);
61
- return appList.includes(bundleId);
62
- };
63
-
64
- export const runApp = async (udid: string, appName: string): Promise<void> => {
65
- await killApp(udid, appName);
66
- await spawn('xcrun', ['simctl', 'launch', udid, appName]);
67
- };
68
-
69
- export const killApp = async (udid: string, appName: string): Promise<void> => {
70
- await spawnAndForget('xcrun', ['simctl', 'terminate', udid, appName]);
71
- };
@@ -1,79 +0,0 @@
1
- import { spawn, spawnAndForget } from '@react-native-harness/tools';
2
-
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- type Device = any;
5
-
6
- export const listDevices = async (): Promise<{ devices: Device[] }> => {
7
- const { stdout } = await spawn('xcrun', [
8
- 'simctl',
9
- 'list',
10
- 'devices',
11
- '--json',
12
- ]);
13
- return JSON.parse(stdout);
14
- };
15
-
16
- export const getDeviceByName = async (
17
- simulatorName: string
18
- ): Promise<Device | null> => {
19
- const devices = await listDevices();
20
-
21
- for (const runtime in devices.devices) {
22
- const runtimeDevices = devices.devices[runtime];
23
- for (const device of runtimeDevices) {
24
- if (device.name === simulatorName && device.isAvailable) {
25
- return device;
26
- }
27
- }
28
- }
29
-
30
- return null;
31
- };
32
-
33
- export const listApps = async (udid: string): Promise<string[]> => {
34
- const { stdout: plistOutput } = await spawn('xcrun', [
35
- 'simctl',
36
- 'listapps',
37
- udid,
38
- ]);
39
- const { stdout: jsonOutput } = await spawn(
40
- 'plutil',
41
- ['-convert', 'json', '-o', '-', '-'],
42
- { stdin: { string: plistOutput } }
43
- );
44
- return Object.keys(JSON.parse(jsonOutput));
45
- };
46
-
47
- export const isAppInstalled = async (
48
- simulatorName: string,
49
- bundleId: string
50
- ): Promise<boolean> => {
51
- const device = await getDeviceByName(simulatorName);
52
-
53
- if (!device) {
54
- throw new Error(`Simulator ${simulatorName} not found`);
55
- }
56
-
57
- const appList = await listApps(device.udid);
58
- return appList.includes(bundleId);
59
- };
60
-
61
- export const runApp = async (
62
- simulatorName: string,
63
- appName: string
64
- ): Promise<void> => {
65
- await killApp(simulatorName, appName);
66
- await spawn('xcrun', ['simctl', 'launch', simulatorName, appName]);
67
- };
68
-
69
- export const killApp = async (
70
- simulatorName: string,
71
- appName: string
72
- ): Promise<void> => {
73
- await spawnAndForget('xcrun', [
74
- 'simctl',
75
- 'terminate',
76
- simulatorName,
77
- appName,
78
- ]);
79
- };
@@ -1,66 +0,0 @@
1
- import {
2
- assertIOSRunnerConfig,
3
- TestRunnerConfig,
4
- } from '@react-native-harness/config';
5
- import { type PlatformAdapter } from '../platform-adapter.js';
6
- import {
7
- getSimulatorDeviceId,
8
- getSimulatorStatus,
9
- runSimulator,
10
- stopSimulator,
11
- } from './simulator.js';
12
- import { isAppInstalled, runApp, killApp } from './build.js';
13
- import { killWithAwait } from '../../process.js';
14
- import { runMetro } from '../../bundlers/metro.js';
15
- import { AppNotInstalledError } from '../../errors/errors.js';
16
- import { assert } from '../../utils.js';
17
-
18
- const iosPlatformAdapter: PlatformAdapter = {
19
- name: 'ios',
20
- getEnvironment: async (runner: TestRunnerConfig) => {
21
- assertIOSRunnerConfig(runner);
22
- // TODO: system version is also important as there may be two emulators with the same name
23
- // but different system versions
24
-
25
- let shouldStopSimulator = false;
26
- const udid = await getSimulatorDeviceId(
27
- runner.deviceId,
28
- runner.systemVersion
29
- );
30
-
31
- assert(!!udid, 'Simulator not found');
32
-
33
- const simulatorStatus = await getSimulatorStatus(udid);
34
- const metroPromise = runMetro();
35
-
36
- if (simulatorStatus === 'stopped') {
37
- await runSimulator(udid);
38
- shouldStopSimulator = true;
39
- }
40
-
41
- const isInstalled = await isAppInstalled(udid, runner.bundleId);
42
-
43
- if (!isInstalled) {
44
- throw new AppNotInstalledError(runner.deviceId, runner.bundleId, 'ios');
45
- }
46
-
47
- const metro = await metroPromise;
48
- await runApp(udid, runner.bundleId);
49
-
50
- return {
51
- restart: async () => {
52
- await runApp(udid, runner.bundleId);
53
- },
54
- dispose: async () => {
55
- await killApp(udid, runner.bundleId);
56
- if (shouldStopSimulator) {
57
- await stopSimulator(udid);
58
- }
59
-
60
- await killWithAwait(metro);
61
- },
62
- };
63
- },
64
- };
65
-
66
- export default iosPlatformAdapter;
@@ -1,171 +0,0 @@
1
- import { spawn, SubprocessError } from '@react-native-harness/tools';
2
-
3
- export type IOSSimulatorStatus = 'stopped' | 'loading' | 'running';
4
-
5
- export const getSimulatorDeviceId = async (
6
- simulatorName: string,
7
- systemVersion: string
8
- ): Promise<string | null> => {
9
- try {
10
- const { stdout } = await spawn('xcrun', [
11
- 'simctl',
12
- 'list',
13
- 'devices',
14
- '--json',
15
- ]);
16
- const devices = JSON.parse(stdout);
17
- const expectedRuntimeId = `com.apple.CoreSimulator.SimRuntime.iOS-${systemVersion.replace(
18
- /\./,
19
- '-'
20
- )}`;
21
-
22
- const runtime = devices.devices[expectedRuntimeId];
23
-
24
- if (!runtime) {
25
- return null;
26
- }
27
-
28
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
- const device = runtime.find((d: any) => d.name === simulatorName);
30
-
31
- if (device) {
32
- return device.udid;
33
- }
34
-
35
- return null;
36
- } catch {
37
- return null;
38
- }
39
- };
40
-
41
- export const getAvailableSimulators = async (): Promise<
42
- Array<{ name: string; udid: string; runtime: string }>
43
- > => {
44
- try {
45
- const { stdout } = await spawn('xcrun', [
46
- 'simctl',
47
- 'list',
48
- 'devices',
49
- '--json',
50
- ]);
51
- const devices = JSON.parse(stdout);
52
- const simulators: Array<{
53
- name: string;
54
- udid: string;
55
- runtime: string;
56
- }> = [];
57
-
58
- for (const runtime in devices.devices) {
59
- if (runtime.includes('iOS')) {
60
- const runtimeDevices = devices.devices[runtime];
61
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
62
- runtimeDevices.forEach((device: any) => {
63
- if (device.isAvailable) {
64
- simulators.push({
65
- name: device.name,
66
- udid: device.udid,
67
- runtime: runtime,
68
- });
69
- }
70
- });
71
- }
72
- }
73
-
74
- return simulators;
75
- } catch {
76
- return [];
77
- }
78
- };
79
-
80
- export const getSimulatorStatus = async (
81
- udid: string
82
- ): Promise<IOSSimulatorStatus> => {
83
- try {
84
- const { stdout } = await spawn('xcrun', [
85
- 'simctl',
86
- 'list',
87
- 'devices',
88
- '--json',
89
- ]);
90
- const devices = JSON.parse(stdout);
91
-
92
- for (const runtime in devices.devices) {
93
- if (runtime.includes('iOS')) {
94
- const runtimeDevices = devices.devices[runtime];
95
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
96
- const device = runtimeDevices.find((d: any) => d.udid === udid);
97
-
98
- if (device) {
99
- switch (device.state) {
100
- case 'Booted':
101
- return 'running';
102
- case 'Booting':
103
- return 'loading';
104
- default:
105
- return 'stopped';
106
- }
107
- }
108
- }
109
- }
110
-
111
- return 'stopped';
112
- } catch {
113
- return 'stopped';
114
- }
115
- };
116
-
117
- export const runSimulator = async (udid: string): Promise<void> => {
118
- try {
119
- await spawn('xcrun', ['simctl', 'boot', udid]);
120
- } catch (bootError) {
121
- // Ignore if simulator is already booted
122
- if (
123
- bootError instanceof SubprocessError &&
124
- !bootError.stderr?.includes(
125
- 'Unable to boot device in current state: Booted'
126
- )
127
- ) {
128
- throw bootError;
129
- }
130
- }
131
-
132
- await spawn('open', ['-a', 'Simulator']);
133
-
134
- let attempts = 0;
135
-
136
- while (true) {
137
- attempts++;
138
-
139
- const status = await getSimulatorStatus(udid);
140
-
141
- if (status === 'running') {
142
- break;
143
- }
144
-
145
- if (attempts > 10) {
146
- throw new Error('Simulator not running');
147
- }
148
-
149
- await new Promise((resolve) => setTimeout(resolve, 1000));
150
- }
151
- };
152
-
153
- export const stopSimulator = async (udid: string): Promise<void> => {
154
- await stopSimulatorById(udid);
155
- };
156
-
157
- const stopSimulatorById = async (udid: string): Promise<void> => {
158
- try {
159
- await spawn('xcrun', ['simctl', 'shutdown', udid]);
160
- } catch (shutdownError) {
161
- // Ignore if simulator is already shut down
162
- if (
163
- shutdownError instanceof SubprocessError &&
164
- !shutdownError.stderr?.includes(
165
- 'Unable to shutdown device in current state: Shutdown'
166
- )
167
- ) {
168
- throw shutdownError;
169
- }
170
- }
171
- };
@@ -1,11 +0,0 @@
1
- import { TestRunnerConfig } from '@react-native-harness/config';
2
-
3
- export type Environment = {
4
- restart: () => Promise<void>;
5
- dispose: () => Promise<void>;
6
- };
7
-
8
- export type PlatformAdapter = {
9
- name: string;
10
- getEnvironment: (runner: TestRunnerConfig) => Promise<Environment>;
11
- };
@@ -1,26 +0,0 @@
1
- import { PlatformAdapter } from './platform-adapter.js';
2
- import androidPlatformAdapter from './android/index.js';
3
- import iosPlatformAdapter from './ios/index.js';
4
- import webPlatformAdapter from './web/index.js';
5
- import vegaPlatformAdapter from './vega/index.js';
6
-
7
- const platformAdapters = {
8
- android: androidPlatformAdapter,
9
- ios: iosPlatformAdapter,
10
- web: webPlatformAdapter,
11
- vega: vegaPlatformAdapter,
12
- };
13
-
14
- export const getPlatformAdapter = async (
15
- platformName: string
16
- ): Promise<PlatformAdapter> => {
17
- if (!(platformName in platformAdapters)) {
18
- throw new Error(`Platform adapter for ${platformName} not found`);
19
- }
20
-
21
- try {
22
- return platformAdapters[platformName as keyof typeof platformAdapters];
23
- } catch {
24
- throw new Error(`Platform adapter for ${platformName} not found`);
25
- }
26
- };
@@ -1,85 +0,0 @@
1
- import path from 'node:path';
2
- import { spawn } from '@react-native-harness/tools';
3
-
4
- export type VegaBuildTarget = 'sim_tv_x86_64' | 'sim_tv_aarch64';
5
- export type VegaBuildType = 'Debug' | 'Release';
6
-
7
- /**
8
- * Build Vega app and produce .vpkg file
9
- */
10
- export const buildVegaApp = async (
11
- buildType: VegaBuildType = 'Release',
12
- target?: VegaBuildTarget
13
- ): Promise<void> => {
14
- const args = ['run', 'build:app'];
15
-
16
- if (buildType) {
17
- args.push('-b', buildType);
18
- }
19
-
20
- if (target) {
21
- args.push('-t', target);
22
- }
23
-
24
- await spawn('npm', args);
25
- };
26
-
27
- /**
28
- * Clean build artifacts
29
- */
30
- export const cleanBuild = async (): Promise<void> => {
31
- await spawn('kepler', ['clean']);
32
- };
33
-
34
- /**
35
- * Get the expected .vpkg file path based on build configuration
36
- */
37
- export const getVpkgPath = (
38
- appName: string,
39
- buildType: VegaBuildType = 'Release',
40
- target: VegaBuildTarget = 'sim_tv_x86_64'
41
- ): string => {
42
- const buildTypeStr = buildType.toLowerCase();
43
- const vpkgFileName = `${appName}_${target}.vpkg`;
44
-
45
- return path.join(
46
- process.cwd(),
47
- 'build',
48
- `${target}-${buildTypeStr}`,
49
- vpkgFileName
50
- );
51
- };
52
-
53
- /**
54
- * Launch an already installed app on specified Vega virtual device
55
- */
56
- export const runApp = async (
57
- deviceId: string,
58
- bundleId: string
59
- ): Promise<void> => {
60
- await spawn('kepler', [
61
- 'device',
62
- 'launch-app',
63
- '--device',
64
- deviceId,
65
- '--appName',
66
- bundleId,
67
- ]);
68
- };
69
-
70
- /**
71
- * Kill/terminate app on specified Vega virtual device
72
- */
73
- export const killApp = async (
74
- deviceId: string,
75
- bundleId: string
76
- ): Promise<void> => {
77
- await spawn('kepler', [
78
- 'device',
79
- 'terminate-app',
80
- '--device',
81
- deviceId,
82
- '--appName',
83
- bundleId,
84
- ]);
85
- };