polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
323 lines • 15.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadAuthAndServiceConfig = loadAuthAndServiceConfig;
exports.validateOutputFormat = validateOutputFormat;
exports.validateYn = validateYn;
exports.validateImageType = validateImageType;
exports.validateShowCondition = validateShowCondition;
exports.validateDuration = validateDuration;
exports.registerCardPushCommands = registerCardPushCommands;
const card_push_handler_1 = require("../handlers/card-push.handler");
const auth_adapter_1 = require("../config/auth-adapter");
const manager_1 = require("../config/manager");
const errors_1 = require("../utils/errors");
const DEFAULT_TIMEOUT_MS = 30000;
async function loadAuthAndServiceConfig(parentOptions) {
const authResult = auth_adapter_1.authAdapter.tryGetAuthConfig(parentOptions);
if (!authResult) {
throw new Error(auth_adapter_1.authAdapter.getStatusMessage(parentOptions));
}
let configResult;
try {
configResult = await manager_1.configManager.load({
cliOptions: parentOptions,
});
}
catch (error) {
if (error instanceof Error && error.message.includes('Auth configuration is incomplete')) {
configResult = {
config: {
baseUrl: 'https://api.polyv.net',
timeout: DEFAULT_TIMEOUT_MS,
debug: false,
},
};
}
else {
throw error;
}
}
const serviceConfig = {
baseUrl: configResult.config.baseUrl,
timeout: configResult.config.timeout,
debug: configResult.config.debug,
};
const isVerbose = !!parentOptions['verbose'];
if (isVerbose) {
console.log('Using authentication from:', authResult.source);
}
return { authConfig: authResult.config, serviceConfig, isVerbose };
}
function validateOutputFormat(format) {
if (format !== 'table' && format !== 'json') {
throw new Error('Invalid output format. Must be table or json');
}
return format;
}
function validateYn(value) {
if (value !== 'Y' && value !== 'N') {
throw new Error('Value must be Y or N');
}
return value;
}
function validateImageType(imageType) {
const validTypes = ['giftbox', 'redpack', 'custom', 'weixinWork'];
if (!validTypes.includes(imageType)) {
throw new Error(`Invalid imageType. Must be one of: ${validTypes.join(', ')}`);
}
return imageType;
}
function validateShowCondition(showCondition) {
const validConditions = ['PUSH', 'WATCH'];
if (!validConditions.includes(showCondition)) {
throw new Error(`Invalid showCondition. Must be one of: ${validConditions.join(', ')}`);
}
return showCondition;
}
function validateDuration(duration) {
const durationNum = parseInt(duration, 10);
const validDurations = [0, 5, 10, 20, 30];
if (!validDurations.includes(durationNum)) {
throw new Error(`Invalid duration. Must be one of: ${validDurations.join(', ')}`);
}
return durationNum;
}
function registerCardPushCommands(program) {
const cardPushCmd = program
.command('card-push')
.description('Manage card push for live streaming (管理直播卡片推送)');
cardPushCmd
.command('list')
.description('List all card-pushes (列出所有卡片推送)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new card_push_handler_1.CardPushHandler(authConfig, serviceConfig);
await handler.listCardPushes({
channelId: options.channelId,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
cardPushCmd
.command('create')
.description('Create a new card-push (创建新的卡片推送)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.requiredOption('--imageType <type>', 'Image type (giftbox|redpack|custom|weixinWork)', validateImageType)
.requiredOption('--title <title>', 'Card title (卡片标题,最多16个字符)')
.requiredOption('--link <url>', 'Card link URL (卡片链接地址)')
.requiredOption('--duration <seconds>', 'Card countdown duration (0,5,10,20,30秒)', validateDuration)
.requiredOption('--showCondition <condition>', 'Show condition (PUSH|WATCH)', validateShowCondition)
.option('--cardType <type>', 'Card type (common|qrCode)', 'common')
.option('--durationPosition <position>', 'Duration position (bottom|top)')
.option('--conditionValue <value>', 'Watch duration value (观看时长)', (val) => parseInt(val, 10))
.option('--conditionUnit <unit>', 'Watch duration unit (SECONDS|MINUTES)')
.option('--countdownMsg <message>', 'Countdown message (倒计时文案,最多8个字符)')
.option('--enterEnabled <enabled>', 'Card entry enabled (Y|N)')
.option('--linkEnabled <enabled>', 'Card link enabled (Y|N)')
.option('--redirectType <type>', 'Redirect type (iframe|tab)')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new card_push_handler_1.CardPushHandler(authConfig, serviceConfig);
await handler.createCardPush({
channelId: options.channelId,
cardType: options.cardType,
imageType: options.imageType,
title: options.title,
link: options.link,
duration: options.duration,
durationPosition: options.durationPosition,
showCondition: options.showCondition,
conditionValue: options.conditionValue,
conditionUnit: options.conditionUnit,
countdownMsg: options.countdownMsg,
enterEnabled: options.enterEnabled,
linkEnabled: options.linkEnabled,
redirectType: options.redirectType,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
cardPushCmd
.command('update')
.description('Update an existing card-push (更新现有卡片推送)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.requiredOption('--cardPushId <id>', 'Card-push ID (卡片推送ID)')
.option('--cardType <type>', 'Card type (common|qrCode)')
.option('--imageType <type>', 'Image type (giftbox|redpack|custom|weixinWork)', validateImageType)
.option('--title <title>', 'Card title (卡片标题,最多16个字符)')
.option('--link <url>', 'Card link URL (卡片链接地址)')
.option('--duration <seconds>', 'Card countdown duration (0,5,10,20,30秒)', validateDuration)
.option('--durationPosition <position>', 'Duration position (bottom|top)')
.option('--showCondition <condition>', 'Show condition (PUSH|WATCH)', validateShowCondition)
.option('--conditionValue <value>', 'Watch duration value (观看时长)', (val) => parseInt(val, 10))
.option('--conditionUnit <unit>', 'Watch duration unit (SECONDS|MINUTES)')
.option('--countdownMsg <message>', 'Countdown message (倒计时文案,最多8个字符)')
.option('--enterEnabled <enabled>', 'Card entry enabled (Y|N)')
.option('--linkEnabled <enabled>', 'Card link enabled (Y|N)')
.option('--redirectType <type>', 'Redirect type (iframe|tab)')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new card_push_handler_1.CardPushHandler(authConfig, serviceConfig);
await handler.updateCardPush({
channelId: options.channelId,
cardPushId: options.cardPushId,
cardType: options.cardType,
imageType: options.imageType,
title: options.title,
link: options.link,
duration: options.duration,
durationPosition: options.durationPosition,
showCondition: options.showCondition,
conditionValue: options.conditionValue,
conditionUnit: options.conditionUnit,
countdownMsg: options.countdownMsg,
enterEnabled: options.enterEnabled,
linkEnabled: options.linkEnabled,
redirectType: options.redirectType,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
cardPushCmd
.command('push')
.description('Push a card to viewers (推送卡片到观众)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.requiredOption('--cardPushId <id>', 'Card-push ID (卡片推送ID)')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new card_push_handler_1.CardPushHandler(authConfig, serviceConfig);
await handler.pushCard({
channelId: options.channelId,
cardPushId: options.cardPushId,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
cardPushCmd
.command('cancel')
.description('Cancel a pushing card (取消正在推送的卡片)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.requiredOption('--cardPushId <id>', 'Card-push ID (卡片推送ID)')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new card_push_handler_1.CardPushHandler(authConfig, serviceConfig);
await handler.cancelPush({
channelId: options.channelId,
cardPushId: options.cardPushId,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
cardPushCmd
.command('delete')
.description('Delete a card-push (删除卡片推送)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.requiredOption('--cardPushId <id>', 'Card-push ID (卡片推送ID)')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new card_push_handler_1.CardPushHandler(authConfig, serviceConfig);
await handler.deleteCardPush({
channelId: options.channelId,
cardPushId: options.cardPushId,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
const shareCmd = cardPushCmd
.command('share')
.description('Manage channel share settings (管理频道分享设置)');
shareCmd
.command('get')
.description('Get channel share settings (查询频道分享设置)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new card_push_handler_1.CardPushHandler(authConfig, serviceConfig);
await handler.getShare({
channelId: options.channelId,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
shareCmd
.command('update')
.description('Update channel share settings (更新频道分享设置)')
.requiredOption('--channelId <id>', 'Channel ID (频道ID)')
.requiredOption('--share-btn-enable <yn>', 'Share button enabled (Y|N)', validateYn)
.requiredOption('--title-type <type>', 'Share title type (follow|custom)')
.option('--weixin-share-title <title>', 'WeChat share title')
.option('--weixin-share-desc <desc>', 'WeChat share description')
.option('--weixin-share-custom-url <url>', 'Custom WeChat share URL')
.option('--web-share-custom-url <url>', 'Custom web share URL')
.option('--weixin-share-custom-url-with-param-enabled <yn>', 'Append params to custom WeChat share URL (Y|N)', validateYn)
.option('--web-share-custom-url-with-param-enabled <yn>', 'Append params to custom web share URL (Y|N)', validateYn)
.option('-f, --force', 'Skip confirmation prompt')
.option('-o, --output <format>', 'Output format (table|json)', validateOutputFormat, 'table')
.action(async (options) => {
try {
const { authConfig, serviceConfig } = await loadAuthAndServiceConfig(options);
const handler = new card_push_handler_1.CardPushHandler(authConfig, serviceConfig);
await handler.updateShare({
channelId: options.channelId,
shareBtnEnable: options.shareBtnEnable,
titleType: options.titleType,
weixinShareTitle: options.weixinShareTitle,
weixinShareDesc: options.weixinShareDesc,
weixinShareCustomUrl: options.weixinShareCustomUrl,
webShareCustomUrl: options.webShareCustomUrl,
weixinShareCustomUrlWithParamEnabled: options.weixinShareCustomUrlWithParamEnabled,
webShareCustomUrlWithParamEnabled: options.webShareCustomUrlWithParamEnabled,
force: options.force,
output: options.output,
});
}
catch (error) {
(0, errors_1.logError)(error instanceof Error ? error : new Error(String(error)));
process.exit(1);
}
});
}
//# sourceMappingURL=card-push.commands.js.map