From 89bdfb52069069c13388e731beb4e527e1ca2fe9 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Wed, 8 Mar 2023 13:36:31 +0100 Subject: [PATCH] chore: remove duplication in parameter definitions (#9805) --- packages/browsers/src/CLI.ts | 78 +++++++++++++++++------------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/packages/browsers/src/CLI.ts b/packages/browsers/src/CLI.ts index 6b2c2f5e..710d48bc 100644 --- a/packages/browsers/src/CLI.ts +++ b/packages/browsers/src/CLI.ts @@ -50,33 +50,45 @@ export class CLI { this.#cachePath = cachePath; } + #defineBrowserParameter(yargs: yargs.Argv): 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): 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): 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 { await yargs(hideBin(argv)) .command( 'install ', 'Download and install the specified browser', yargs => { - yargs.positional('browser', { - description: 'The browser version', - type: 'string', - 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(), - }); + this.#defineBrowserParameter(yargs); + this.#definePlatformParameter(yargs); + this.#definePathParameter(yargs); }, async argv => { const args = argv as unknown as InstallArgs; @@ -115,32 +127,14 @@ export class CLI { 'launch ', 'Launch the specified browser', yargs => { - yargs.positional('browser', { - description: 'The browser version', - type: 'string', - coerce: (opt): LaunchArgs['browser'] => { - return { - name: this.#parseBrowser(opt), - buildId: this.#parseBuildId(opt), - }; - }, - }); + this.#defineBrowserParameter(yargs); + this.#definePlatformParameter(yargs); + this.#definePathParameter(yargs); yargs.option('detached', { type: 'boolean', desc: 'Whether to detach the child process.', 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 => { const args = argv as unknown as LaunchArgs;