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; } 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(); } static { CommandLine._program.version(PackageJson.version); const 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)); } setupProgramOptions(); } private constructor() { throw new Error('[CommandLine] is a static class that may not be instantiated'); } }