93 lines
3.5 KiB
TypeScript
93 lines
3.5 KiB
TypeScript
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);
|
|
|
|
const rawOptions = CommandLine._program.opts();
|
|
|
|
// Convert the string log level back to LoggingLevel enum
|
|
const logLevel = rawOptions.logLevel ? LoggingLevelMapping.convertLoggingLevelTypeToLoggingLevel(rawOptions.logLevel) : defaultLogLevel;
|
|
|
|
return {
|
|
...rawOptions,
|
|
logLevel
|
|
} as CommandLineOptions;
|
|
}
|
|
|
|
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 <applicationId>', 'The application ID to use');
|
|
CommandLine._program.requiredOption('--secret <secret>', 'The secret to use');
|
|
CommandLine._program.requiredOption('--pcast-uri <pcastUri>', 'The pcast URI to use');
|
|
CommandLine._program.requiredOption('--channel-uri <channelUri>', 'The channel URI to use');
|
|
|
|
CommandLine._program.option('--ingest-uri <ingestUri>', 'The ingest URI to use');
|
|
CommandLine._program.option('--publisher-uri <publisherUri>', 'The publisher URI to use');
|
|
CommandLine._program.option(
|
|
'--viewer <browser@version:OS@OSVersion...>',
|
|
'The browser and OS for simulating a viewer to use',
|
|
handleArrayOption,
|
|
defaultViewers
|
|
);
|
|
CommandLine._program.option(
|
|
'--publisher <browser@version:OS@OSVersion...>',
|
|
'The browser and OS for simulating a publisher to use',
|
|
handleArrayOption,
|
|
defaultPublishers
|
|
);
|
|
CommandLine._program.option('-t, --test <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 <username>', 'The BrowserStack username to use', process.env.BROWSERSTACK_USER || '');
|
|
CommandLine._program.option('--browserstack-key <key>', 'The BrowserStack key to use', process.env.BROWSERSTACK_KEY || '');
|
|
CommandLine._program.option('--log-level <logLevel>', 'The log level to use', LoggingLevelMapping.convertLoggingLevelToLoggingLevelType(defaultLogLevel));
|
|
};
|
|
|
|
setupProgramOptions();
|
|
}
|
|
|
|
private constructor() {
|
|
throw new Error('[CommandLine] is a static class that may not be instantiated');
|
|
}
|
|
}
|