From 47cc7c4ae8454f566f98c2cb3d607de4171b7bbe Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Wed, 16 Feb 2022 09:54:12 -0800 Subject: [PATCH] chore: refactor internal cli commands to use commander (#12146) --- packages/playwright-core/src/cli/innerCli.ts | 39 ++++++++++++++------ tests/config/default.playwright.config.ts | 2 +- tests/port-forwarding-server.spec.ts | 2 +- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/packages/playwright-core/src/cli/innerCli.ts b/packages/playwright-core/src/cli/innerCli.ts index 37237c8d38..0783fcbf8c 100644 --- a/packages/playwright-core/src/cli/innerCli.ts +++ b/packages/playwright-core/src/cli/innerCli.ts @@ -218,6 +218,33 @@ program launchGridAgent(options.agentId, options.gridUrl); }); +program + .command('run-driver', { hidden: true }) + .action(function(options) { + runDriver(); + }); + +program + .command('run-server', { hidden: true }) + .option('--port ', 'Server port') + .action(function(options) { + runServer(options.port ? +options.port : undefined).catch(logErrorAndExit); + }); + +program + .command('print-api-json', { hidden: true }) + .action(function(options) { + printApiJson(); + }); + +program + .command('launch-server', { hidden: true }) + .requiredOption('--browser ', 'Browser name, one of "chromium", "firefox" or "webkit"') + .option('--config ', 'JSON file with launchServer options') + .action(function(options) { + launchBrowserServer(options.browserName, options.config); + }); + program .command('show-trace [trace...]') .option('-b, --browser ', 'browser to use, one of cr, chromium, ff, firefox, wk, webkit', 'chromium') @@ -270,17 +297,7 @@ if (!process.env.PW_LANG_NAME) { } } -if (process.argv[2] === 'run-driver') - runDriver(); -else if (process.argv[2] === 'run-server') - runServer(process.argv[3] ? +process.argv[3] : undefined).catch(logErrorAndExit); -else if (process.argv[2] === 'print-api-json') - printApiJson(); -else if (process.argv[2] === 'launch-server') - launchBrowserServer(process.argv[3], process.argv[4]).catch(logErrorAndExit); -else - program.parse(process.argv); - +program.parse(process.argv); type Options = { browser: string; diff --git a/tests/config/default.playwright.config.ts b/tests/config/default.playwright.config.ts index 0c3fc3d4fe..8cd90d9bcf 100644 --- a/tests/config/default.playwright.config.ts +++ b/tests/config/default.playwright.config.ts @@ -71,7 +71,7 @@ if (mode === 'service') { if (mode === 'service2') { config.webServer = { - command: 'npx playwright run-server 3333', + command: 'npx playwright run-server --port=3333', port: 3333, reuseExistingServer: true, }; diff --git a/tests/port-forwarding-server.spec.ts b/tests/port-forwarding-server.spec.ts index 23ca2c33e6..5b3d8faa3e 100644 --- a/tests/port-forwarding-server.spec.ts +++ b/tests/port-forwarding-server.spec.ts @@ -27,7 +27,7 @@ class OutOfProcessPlaywrightServer { private _receivedPortPromise: Promise; constructor(port: number, proxyPort: number) { - this._driverProcess = childProcess.fork(path.join(__dirname, '..', 'packages', 'playwright-core', 'lib', 'cli', 'cli.js'), ['run-server', port.toString()], { + this._driverProcess = childProcess.fork(path.join(__dirname, '..', 'packages', 'playwright-core', 'lib', 'cli', 'cli.js'), ['run-server', '--port', port.toString()], { stdio: 'pipe', detached: true, env: {