18d307f52SEvan Baconimport { Device, getAttachedDevicesAsync } from './adb';
28d307f52SEvan Baconimport { listAvdsAsync } from './emulator';
3*8a424bebSJames Ideimport { CommandError } from '../../../utils/errors';
48d307f52SEvan Bacon
58d307f52SEvan Bacon/** Get a list of all devices including offline emulators. Asserts if no devices are available. */
68d307f52SEvan Baconexport async function getDevicesAsync(): Promise<Device[]> {
78d307f52SEvan Bacon  const bootedDevices = await getAttachedDevicesAsync();
88d307f52SEvan Bacon
98d307f52SEvan Bacon  const data = await listAvdsAsync();
108d307f52SEvan Bacon  const connectedNames = bootedDevices.map(({ name }) => name);
118d307f52SEvan Bacon
128d307f52SEvan Bacon  const offlineEmulators = data
138d307f52SEvan Bacon    .filter(({ name }) => !connectedNames.includes(name))
148d307f52SEvan Bacon    .map(({ name, type }) => {
158d307f52SEvan Bacon      return {
168d307f52SEvan Bacon        name,
178d307f52SEvan Bacon        type,
188d307f52SEvan Bacon        isBooted: false,
198d307f52SEvan Bacon        // TODO: Are emulators always authorized?
208d307f52SEvan Bacon        isAuthorized: true,
218d307f52SEvan Bacon      };
228d307f52SEvan Bacon    });
238d307f52SEvan Bacon
248d307f52SEvan Bacon  const allDevices = bootedDevices.concat(offlineEmulators);
258d307f52SEvan Bacon
268d307f52SEvan Bacon  if (!allDevices.length) {
278d307f52SEvan Bacon    throw new CommandError(
288d307f52SEvan Bacon      [
298d307f52SEvan Bacon        `No Android connected device found, and no emulators could be started automatically.`,
308d307f52SEvan Bacon        `Please connect a device or create an emulator (https://docs.expo.dev/workflow/android-studio-emulator).`,
318d307f52SEvan Bacon        `Then follow the instructions here to enable USB debugging:`,
328d307f52SEvan Bacon        `https://developer.android.com/studio/run/device.html#developer-device-options. If you are using Genymotion go to Settings -> ADB, select "Use custom Android SDK tools", and point it at your Android SDK directory.`,
338d307f52SEvan Bacon      ].join('\n')
348d307f52SEvan Bacon    );
358d307f52SEvan Bacon  }
368d307f52SEvan Bacon
378d307f52SEvan Bacon  return allDevices;
388d307f52SEvan Bacon}
39