UNPKG

eas-cli

Version:
100 lines (99 loc) 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runCurrentMachineMethodAsync = void 0; const tslib_1 = require("tslib"); const spawn_async_1 = tslib_1.__importDefault(require("@expo/spawn-async")); const chalk_1 = tslib_1.__importDefault(require("chalk")); const os_1 = tslib_1.__importDefault(require("os")); const utils_1 = require("./utils"); const AppleDeviceMutation_1 = require("../../../credentials/ios/api/graphql/mutations/AppleDeviceMutation"); const generated_1 = require("../../../graphql/generated"); const log_1 = tslib_1.__importDefault(require("../../../log")); const ora_1 = require("../../../ora"); const prompts_1 = require("../../../prompts"); const errors_1 = require("../../utils/errors"); async function runCurrentMachineMethodAsync(graphqlClient, accountId, appleTeam) { log_1.default.newLine(); log_1.default.log(chalk_1.default.white('Checking if current machine is an Apple Silicon one.')); if (!isMachineAppleSilicon()) { throw new errors_1.DeviceCreateError("Current machine is not of Apple Silicon type - provisioning UDID can't be added automatically."); } log_1.default.log(chalk_1.default.green('Check successful.')); await collectDataAndRegisterDeviceAsync(graphqlClient, { accountId, appleTeam }); } exports.runCurrentMachineMethodAsync = runCurrentMachineMethodAsync; function isMachineAppleSilicon() { return os_1.default.cpus()[0].model.includes('Apple M') && process.platform === 'darwin'; } async function collectDataAndRegisterDeviceAsync(graphqlClient, { accountId, appleTeam, }) { const deviceData = await collectDeviceDataAsync(appleTeam); if (!deviceData) { return; } const { udid, deviceClass, name } = deviceData; const spinner = (0, ora_1.ora)(`Registering Apple device on EAS`).start(); try { await AppleDeviceMutation_1.AppleDeviceMutation.createAppleDeviceAsync(graphqlClient, { appleTeamId: appleTeam.id, identifier: udid, name, deviceClass: deviceClass ?? undefined, }, accountId); } catch (err) { spinner.fail(); throw err; } spinner.succeed(); } async function collectDeviceDataAsync(appleTeam, initialValues = {}) { log_1.default.log(chalk_1.default.white('Fetching the provisioning UDID.')); const [udid, defaultMachineName] = await fetchCurrentMachineUDIDAsync(); log_1.default.log(chalk_1.default.green(`Fetched the provisioning UDID - ${udid}`)); const name = await (0, utils_1.promptForNameAsync)(defaultMachineName ?? initialValues.name); const deviceClass = generated_1.AppleDeviceClass.Mac; const deviceData = { udid, name, deviceClass, }; (0, utils_1.printDeviceData)(deviceData, appleTeam); const registrationConfirmed = await (0, prompts_1.confirmAsync)({ message: 'Is this what you want to register?', }); if (!registrationConfirmed) { log_1.default.log('No worries, just try again.'); log_1.default.newLine(); return null; } else { return deviceData; } } async function fetchCurrentMachineUDIDAsync() { try { const profilerData = (await (0, spawn_async_1.default)('system_profiler', ['-json', 'SPHardwareDataType'])).stdout.trim(); if (!profilerData) { const message = 'Failed to fetch the provisioning UDID from system profiler'; log_1.default.error(message); throw new errors_1.DeviceCreateError(message); } const profilerDataJSON = JSON.parse(profilerData); const provisioningUDID = profilerDataJSON?.SPHardwareDataType[0]?.provisioning_UDID; if (!provisioningUDID) { const message = 'System profiler data did not contain the provisioning UDID'; log_1.default.error(message); throw new errors_1.DeviceCreateError(message); } const defaultMachineName = profilerDataJSON?.SPHardwareDataType[0]?.machine_name; return [provisioningUDID, defaultMachineName]; } catch (err) { if (!(err instanceof errors_1.DeviceCreateError)) { const message = `Failed to fetch the provisioning UDID of the current machine - ${err.message}`; log_1.default.error(message); throw new errors_1.DeviceCreateError(message); } throw err; } }