@capawesome/cli
Version:
The Capawesome Cloud Command Line Interface (CLI) to manage Live Updates and more.
69 lines (68 loc) • 3.05 kB
JavaScript
import appChannelsService from '../../../services/app-channels.js';
import appDevicesService from '../../../services/app-devices.js';
import { withAuth } from '../../../utils/auth.js';
import { isInteractive } from '../../../utils/environment.js';
import { prompt, promptAppSelection, promptOrganizationSelection } from '../../../utils/prompt.js';
import { defineCommand, defineOptions } from '@robingenz/zli';
import consola from 'consola';
import { z } from 'zod';
export default defineCommand({
description: 'Force a device to use a specific channel.',
options: defineOptions(z.object({
appId: z.string().uuid({ message: 'App ID must be a UUID.' }).optional().describe('ID of the app.'),
deviceId: z.array(z.string()).optional().describe('ID of the device. Can be specified multiple times.'),
channel: z.string().optional().describe('Name of the channel to force.'),
})),
action: withAuth(async (options, args) => {
let { appId, deviceId: deviceIds, channel } = options;
if (!appId) {
if (!isInteractive()) {
consola.error('You must provide an app ID when running in non-interactive environment.');
process.exit(1);
}
const organizationId = await promptOrganizationSelection();
appId = await promptAppSelection(organizationId);
}
if (!deviceIds || deviceIds.length === 0) {
if (!isInteractive()) {
consola.error('You must provide the device ID when running in non-interactive environment.');
process.exit(1);
}
const deviceId = await prompt('Enter the device ID:', {
type: 'text',
});
deviceIds = [deviceId];
}
if (!channel) {
if (!isInteractive()) {
consola.error('You must provide a channel when running in non-interactive environment.');
process.exit(1);
}
// @ts-ignore wait till https://github.com/unjs/consola/pull/280 is merged
channel = await prompt('Enter the name of the channel to force:', {
type: 'text',
});
if (!channel) {
consola.error('You must provide a channel name.');
process.exit(1);
}
}
const channels = await appChannelsService.findAll({ appId, name: channel });
if (channels.length === 0) {
consola.error('Channel not found.');
process.exit(1);
}
const channelId = channels[0]?.id;
if (!channelId) {
consola.error('Channel ID not found.');
process.exit(1);
}
await appDevicesService.updateMany({
appId,
deviceIds,
forcedAppChannelId: channelId,
});
const deviceCount = deviceIds.length;
consola.success(`${deviceCount === 1 ? 'Device' : `${deviceCount} devices`} forced to channel successfully.`);
}),
});