chore: remove duplication in parameter definitions (#9805)

This commit is contained in:
Alex Rudenko 2023-03-08 13:36:31 +01:00 committed by GitHub
parent c1a7c736ff
commit 89bdfb5206
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -50,33 +50,45 @@ export class CLI {
this.#cachePath = cachePath; this.#cachePath = cachePath;
} }
#defineBrowserParameter(yargs: yargs.Argv<unknown>): void {
yargs.positional('browser', {
description: 'The browser version',
type: 'string',
coerce: (opt): InstallArgs['browser'] => {
return {
name: this.#parseBrowser(opt),
buildId: this.#parseBuildId(opt),
};
},
});
}
#definePlatformParameter(yargs: yargs.Argv<unknown>): void {
yargs.option('platform', {
type: 'string',
desc: 'Platform that the binary needs to be compatible with.',
choices: Object.values(BrowserPlatform),
defaultDescription: 'Auto-detected by default.',
});
}
#definePathParameter(yargs: yargs.Argv<unknown>): void {
yargs.option('path', {
type: 'string',
desc: 'Path to the root folder for the browser downloads and installation',
default: process.cwd(),
});
}
async run(argv: string[]): Promise<void> { async run(argv: string[]): Promise<void> {
await yargs(hideBin(argv)) await yargs(hideBin(argv))
.command( .command(
'install <browser>', 'install <browser>',
'Download and install the specified browser', 'Download and install the specified browser',
yargs => { yargs => {
yargs.positional('browser', { this.#defineBrowserParameter(yargs);
description: 'The browser version', this.#definePlatformParameter(yargs);
type: 'string', this.#definePathParameter(yargs);
coerce: (opt): InstallArgs['browser'] => {
return {
name: this.#parseBrowser(opt),
buildId: this.#parseBuildId(opt),
};
},
});
yargs.option('platform', {
type: 'string',
desc: 'Platform that the binary needs to be compatible with.',
choices: Object.values(BrowserPlatform),
defaultDescription: 'Auto-detected by default.',
});
yargs.option('path', {
type: 'string',
desc: 'Path where the browsers will be downloaded to and installed from',
default: process.cwd(),
});
}, },
async argv => { async argv => {
const args = argv as unknown as InstallArgs; const args = argv as unknown as InstallArgs;
@ -115,32 +127,14 @@ export class CLI {
'launch <browser>', 'launch <browser>',
'Launch the specified browser', 'Launch the specified browser',
yargs => { yargs => {
yargs.positional('browser', { this.#defineBrowserParameter(yargs);
description: 'The browser version', this.#definePlatformParameter(yargs);
type: 'string', this.#definePathParameter(yargs);
coerce: (opt): LaunchArgs['browser'] => {
return {
name: this.#parseBrowser(opt),
buildId: this.#parseBuildId(opt),
};
},
});
yargs.option('detached', { yargs.option('detached', {
type: 'boolean', type: 'boolean',
desc: 'Whether to detach the child process.', desc: 'Whether to detach the child process.',
default: false, default: false,
}); });
yargs.option('platform', {
type: 'string',
desc: 'Platform that the binary needs to be compatible with.',
choices: Object.values(BrowserPlatform),
defaultDescription: 'Auto-detected by default.',
});
yargs.option('path', {
type: 'string',
desc: 'Path where the browsers will be downloaded to and installed from',
default: process.cwd(),
});
}, },
async argv => { async argv => {
const args = argv as unknown as LaunchArgs; const args = argv as unknown as LaunchArgs;