eas-cli
Version:
EAS command line tool
126 lines (125 loc) • 6.19 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const apple_utils_1 = require("@expo/apple-utils");
const core_1 = require("@oclif/core");
const assert_1 = tslib_1.__importDefault(require("assert"));
const EasCommand_1 = tslib_1.__importDefault(require("../../commandUtils/EasCommand"));
const flags_1 = require("../../commandUtils/flags");
const pagination_1 = require("../../commandUtils/pagination");
const AppleDeviceMutation_1 = require("../../credentials/ios/api/graphql/mutations/AppleDeviceMutation");
const AppleDeviceQuery_1 = require("../../credentials/ios/api/graphql/queries/AppleDeviceQuery");
const authenticate_1 = require("../../credentials/ios/appstore/authenticate");
const queries_1 = require("../../devices/queries");
const formatDevice_1 = tslib_1.__importDefault(require("../../devices/utils/formatDevice"));
const log_1 = tslib_1.__importDefault(require("../../log"));
const ora_1 = require("../../ora");
const projectUtils_1 = require("../../project/projectUtils");
const prompts_1 = require("../../prompts");
const json_1 = require("../../utils/json");
class DeviceRename extends EasCommand_1.default {
static description = 'rename a registered device';
static flags = {
'apple-team-id': core_1.Flags.string({ description: 'The Apple team ID on which to find the device' }),
udid: core_1.Flags.string({ description: 'The Apple device ID to rename' }),
name: core_1.Flags.string({ description: 'The new name for the device' }),
...flags_1.EasNonInteractiveAndJsonFlags,
};
static contextDefinition = {
...this.ContextOptions.ProjectId,
...this.ContextOptions.LoggedIn,
};
async runAsync() {
const { flags } = await this.parse(DeviceRename);
const paginatedQueryOptions = (0, pagination_1.getPaginatedQueryOptions)(flags);
let { 'apple-team-id': appleTeamIdentifier, udid, name } = flags;
const { projectId, loggedIn: { graphqlClient }, } = await this.getContextAsync(DeviceRename, {
nonInteractive: paginatedQueryOptions.nonInteractive,
});
const account = await (0, projectUtils_1.getOwnerAccountForProjectIdAsync)(graphqlClient, projectId);
let appleTeamName;
if (paginatedQueryOptions.json) {
(0, json_1.enableJsonOutput)();
}
let appleTeam;
if (!appleTeamIdentifier) {
appleTeam = await (0, queries_1.selectAppleTeamOnAccountAsync)(graphqlClient, {
accountName: account.name,
selectionPromptTitle: `What Apple team would you like to list devices for?`,
paginatedQueryOptions,
});
appleTeamIdentifier = appleTeam.appleTeamIdentifier;
appleTeamName = appleTeam.appleTeamName;
}
(0, assert_1.default)(appleTeamIdentifier, 'No team identifier is specified');
const chosenDevice = udid
? await AppleDeviceQuery_1.AppleDeviceQuery.getByDeviceIdentifierAsync(graphqlClient, account.name, udid)
: await (0, queries_1.selectAppleDeviceOnAppleTeamAsync)(graphqlClient, {
accountName: account.name,
appleTeamIdentifier,
selectionPromptTitle: `Which device would you like to rename?`,
paginatedQueryOptions,
});
const newDeviceName = name ? name : await this.promptForNewDeviceNameAsync(chosenDevice.name);
this.logChosenDevice(chosenDevice, appleTeamName, appleTeamIdentifier, paginatedQueryOptions);
await this.renameDeviceOnExpoAsync(graphqlClient, chosenDevice, newDeviceName);
await this.renameDeviceOnAppleAsync(chosenDevice, appleTeamIdentifier, newDeviceName);
}
async promptForNewDeviceNameAsync(initial) {
const { name } = await (0, prompts_1.promptAsync)({
type: 'text',
name: 'name',
message: 'New device name:',
initial: initial ?? undefined,
});
return name;
}
async renameDeviceOnExpoAsync(graphqlClient, chosenDevice, newDeviceName) {
const removalSpinner = (0, ora_1.ora)(`Renaming Apple device on EAS`).start();
try {
await AppleDeviceMutation_1.AppleDeviceMutation.updateAppleDeviceAsync(graphqlClient, chosenDevice.id, {
name: newDeviceName,
});
removalSpinner.succeed('Renamed Apple device on EAS');
}
catch (err) {
removalSpinner.fail();
throw err;
}
}
async renameDeviceOnAppleAsync(device, appleTeamIdentifier, newDeviceName) {
const ctx = await (0, authenticate_1.authenticateAsync)({ teamId: appleTeamIdentifier });
const context = (0, authenticate_1.getRequestContext)(ctx);
log_1.default.addNewLineIfNone();
const removeAppleSpinner = (0, ora_1.ora)('Renaming device on Apple').start();
try {
const appleValidatedDevices = await apple_utils_1.Device.getAsync(context);
const appleValidatedDevice = appleValidatedDevices.find(d => d.attributes.udid === device.identifier);
if (appleValidatedDevice) {
await appleValidatedDevice.updateAsync({ name: newDeviceName });
removeAppleSpinner.succeed('Renamed device on Apple');
}
else {
removeAppleSpinner.warn('Device not found on Apple Developer Portal. Expo-registered devices will not appear there until they are chosen for an internal distribution build.');
}
}
catch (err) {
removeAppleSpinner.fail();
throw err;
}
}
logChosenDevice(device, appleTeamName, appleTeamIdentifier, { json }) {
if (json) {
(0, json_1.printJsonOnlyOutput)(device);
}
else {
log_1.default.addNewLineIfNone();
log_1.default.log((0, formatDevice_1.default)(device, {
appleTeamName,
appleTeamIdentifier,
}));
log_1.default.newLine();
}
}
}
exports.default = DeviceRename;
;