import {Command} from 'commander'; import PackageJson from '../../package.json' assert {type: 'json'}; import {LoggingLevel, LoggingLevelMapping} from '../logger/LoggingLevel'; export interface CommandLineOptions { applicationId: string; secret: string; logLevel: LoggingLevel; pcastUri: string; ingestUri: string; channelUri: string; publisherUri: string; viewers: string[]; publishers: string[]; tests: string[]; useBrowserstack: boolean; useBrowserstackLocal: boolean; browserstackUser: string; browserstackKey: string; } interface ConfigurationObject { viewers: string[]; publishers: string[]; tests: string[]; useBrowserstack: boolean; useBrowserstackLocal: boolean; browserstackUser: string; browserstackKey: string; logLevel: LoggingLevel; applicationId: string; secret: string; pcastUri: string; ingestUri: string; channelUri: string; publisherUri?: string; } const defaultLogLevel = LoggingLevel.Info; const defaultViewers: string[] = []; const defaultPublishers: string[] = []; const defaultTests: string[] = []; const defaultUseBrowserstack = false; const defaultUseBrowserstackLocal = false; export default class CommandLine { private static readonly _program: Command = new Command(); public static parse(args: string[]): CommandLineOptions { CommandLine._program.parse(args); return CommandLine._program.opts(); } /** * Converts a configuration object to command line arguments array */ public static configToArgs(config: ConfigurationObject): string[] { const args: string[] = []; // Required options args.push('--application-id', config.applicationId); args.push('--secret', config.secret); args.push('--pcast-uri', config.pcastUri); args.push('--channel-uri', config.channelUri); // Optional URI options if (config.publisherUri) { args.push('--publisher-uri', config.publisherUri); } if (config.ingestUri) { args.push('--ingest-uri', config.ingestUri); } // Browser and OS options (singular option names, multiple values) config.viewers.forEach(viewer => { args.push('--viewer', viewer); }); config.publishers.forEach(publisher => { args.push('--publisher', publisher); }); // Test options (singular option name, multiple values) config.tests.forEach(test => { args.push('--test', test); }); // BrowserStack options if (config.useBrowserstack) { args.push('--use-browserstack'); } if (config.useBrowserstackLocal) { args.push('--use-browserstack-local'); } if (config.browserstackUser) { args.push('--browserstack-user', config.browserstackUser); } if (config.browserstackKey) { args.push('--browserstack-key', config.browserstackKey); } // Logging options if (config.logLevel) { args.push('--log-level', LoggingLevelMapping.convertLoggingLevelToLoggingLevelType(config.logLevel)); } return args; } static { CommandLine._program.version(PackageJson.version); CommandLine._setupProgramOptions(); } private static _setupProgramOptions(): void { const handleArrayOption = (value: string, previousValue: string[]) => { if (previousValue) { return previousValue.concat(value); } return [value]; }; // Required options CommandLine._program.requiredOption('--application-id ', 'The application ID to use'); CommandLine._program.requiredOption('--secret ', 'The secret to use'); CommandLine._program.requiredOption('--pcast-uri ', 'The pcast URI to use'); CommandLine._program.requiredOption('--channel-uri ', 'The channel URI to use'); CommandLine._program.option('--ingest-uri ', 'The ingest URI to use'); CommandLine._program.option('--publisher-uri ', 'The publisher URI to use'); CommandLine._program.option('--viewer ', 'The browser and OS for simulating a viewer to use', handleArrayOption, defaultViewers); CommandLine._program.option('--publisher ', 'The browser and OS for simulating a publisher to use', handleArrayOption, defaultPublishers); CommandLine._program.option('-t, --test ', 'The test to run', handleArrayOption, defaultTests); CommandLine._program.option('--use-browserstack', 'Run tests using BrowserStack', defaultUseBrowserstack); CommandLine._program.option('--use-browserstack-local', 'Run tests using BrowserStack Local', defaultUseBrowserstackLocal); CommandLine._program.option('--browserstack-user ', 'The BrowserStack username to use', process.env.BROWSERSTACK_USER || ''); CommandLine._program.option('--browserstack-key ', 'The BrowserStack key to use', process.env.BROWSERSTACK_KEY || ''); CommandLine._program.option('--log-level ', 'The log level to use', LoggingLevelMapping.convertLoggingLevelToLoggingLevelType(defaultLogLevel)); } private constructor() { throw new Error('[CommandLine] is a static class that may not be instantiated'); } }