diff --git a/package-lock.json b/package-lock.json index eb294f8ebd..0c803a6e44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,7 @@ "@babel/plugin-transform-modules-commonjs": "^7.14.5", "@babel/preset-typescript": "^7.14.5", "colors": "^1.4.0", - "commander": "^6.1.0", + "commander": "^8.2.0", "debug": "^4.1.1", "expect": "^26.4.2", "extract-zip": "^2.0.1", @@ -3318,11 +3318,11 @@ } }, "node_modules/commander": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", - "integrity": "sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==", + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.2.0.tgz", + "integrity": "sha512-LLKxDvHeL91/8MIyTAD5BFMNtoIwztGPMiM/7Bl8rIPmHCZXRxmSWr91h57dpOpnQ6jIUqEWdXE/uBYMfiVZDA==", "engines": { - "node": ">= 6" + "node": ">= 12" } }, "node_modules/commondir": { @@ -13479,9 +13479,9 @@ "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==" }, "commander": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", - "integrity": "sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==" + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.2.0.tgz", + "integrity": "sha512-LLKxDvHeL91/8MIyTAD5BFMNtoIwztGPMiM/7Bl8rIPmHCZXRxmSWr91h57dpOpnQ6jIUqEWdXE/uBYMfiVZDA==" }, "commondir": { "version": "1.0.1", diff --git a/package.json b/package.json index aad5538e55..c1fba18861 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "@babel/plugin-transform-modules-commonjs": "^7.14.5", "@babel/preset-typescript": "^7.14.5", "colors": "^1.4.0", - "commander": "^6.1.0", + "commander": "^8.2.0", "debug": "^4.1.1", "expect": "^26.4.2", "extract-zip": "^2.0.1", diff --git a/src/cli/cli.ts b/src/cli/cli.ts index 840b538c50..761e8144d6 100755 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -21,7 +21,7 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; -import program from 'commander'; +import { program, Command } from 'commander'; import { runDriver, runServer, printApiJson, launchBrowserServer } from './driver'; import { showTraceViewer } from '../server/trace/viewer/traceViewer'; import * as playwright from '../..'; @@ -42,48 +42,41 @@ program .name(process.env.PW_CLI_NAME || 'npx playwright'); commandWithOpenOptions('open [url]', 'open page in browser specified via -b, --browser', []) - .action(function(url, command) { - open(command, url, language()).catch(logErrorAndExit); + .action(function(url, options) { + open(options, url, language()).catch(logErrorAndExit); }) - .on('--help', function() { - console.log(''); - console.log('Examples:'); - console.log(''); - console.log(' $ open'); - console.log(' $ open -b webkit https://example.com'); - }); + .addHelpText('afterAll', ` +Examples: + + $ open $ open -b webkit https://example.com`); commandWithOpenOptions('codegen [url]', 'open page and generate code for user actions', [ ['-o, --output ', 'saves the generated script to a file'], ['--target ', `language to generate, one of javascript, test, python, python-async, csharp`, language()], - ]).action(function(url, command) { - codegen(command, url, command.target, command.output).catch(logErrorAndExit); -}).on('--help', function() { - console.log(''); - console.log('Examples:'); - console.log(''); - console.log(' $ codegen'); - console.log(' $ codegen --target=python'); - console.log(' $ codegen -b webkit https://example.com'); -}); + ]).action(function(url, options) { + codegen(options, url, options.target, options.output).catch(logErrorAndExit); +}).addHelpText('afterAll', ` +Examples: + + $ codegen + $ codegen --target=python + $ codegen -b webkit https://example.com`); program .command('debug [args...]', { hidden: true }) .description('run command in debug mode: disable timeout, open inspector') .allowUnknownOption(true) - .action(function(app, args) { - spawn(app, args, { + .action(function(app, options) { + spawn(app, options, { env: { ...process.env, PWDEBUG: '1' }, stdio: 'inherit' }); - }).on('--help', function() { - console.log(''); - console.log('Examples:'); - console.log(''); - console.log(' $ debug node test.js'); - console.log(' $ debug npm run test'); - }); + }).addHelpText('afterAll', ` +Examples: + + $ debug node test.js + $ debug npm run test`); function suggestedBrowsersToInstall() { return registry.executables().filter(e => e.installType !== 'none' && e.type !== 'tool').map(e => e.name).join(', '); @@ -110,16 +103,16 @@ program .command('install [browser...]') .description('ensure browsers necessary for this version of Playwright are installed') .option('--with-deps', 'install system dependencies for browsers') - .action(async function(args: string[], command: program.Command) { + .action(async function(args: string[], options: { withDeps?: boolean }) { try { if (!args.length) { const executables = registry.defaultExecutables(); - if (command.opts().withDeps) + if (options.withDeps) await registry.installDeps(executables); await registry.install(executables); } else { const executables = checkBrowsersToInstall(args); - if (command.opts().withDeps) + if (options.withDeps) await registry.installDeps(executables); await registry.install(executables); } @@ -127,15 +120,14 @@ program console.log(`Failed to install browsers\n${e}`); process.exit(1); } - }).on('--help', function() { - console.log(``); - console.log(`Examples:`); - console.log(` - $ install`); - console.log(` Install default browsers.`); - console.log(``); - console.log(` - $ install chrome firefox`); - console.log(` Install custom browsers, supports ${suggestedBrowsersToInstall()}.`); - }); + }).addHelpText('afterAll', ` + +Examples: + - $ install + Install default browsers. + + - $ install chrome firefox + Install custom browsers, supports ${suggestedBrowsersToInstall()}.`); program @@ -151,15 +143,13 @@ program console.log(`Failed to install browser dependencies\n${e}`); process.exit(1); } - }).on('--help', function() { - console.log(``); - console.log(`Examples:`); - console.log(` - $ install-deps`); - console.log(` Install dependencies for default browsers.`); - console.log(``); - console.log(` - $ install-deps chrome firefox`); - console.log(` Install dependencies for specific browsers, supports ${suggestedBrowsersToInstall()}.`); - }); + }).addHelpText('afterAll', ` +Examples: + - $ install-deps + Install dependencies for default browsers. + + - $ install-deps chrome firefox + Install dependencies for specific browsers, supports ${suggestedBrowsersToInstall()}.`); const browsers = [ { alias: 'cr', name: 'Chromium', type: 'chromium' }, @@ -169,14 +159,12 @@ const browsers = [ for (const { alias, name, type } of browsers) { commandWithOpenOptions(`${alias} [url]`, `open page in ${name}`, []) - .action(function(url, command) { - open({ ...command, browser: type }, url, command.target).catch(logErrorAndExit); - }).on('--help', function() { - console.log(''); - console.log('Examples:'); - console.log(''); - console.log(` $ ${alias} https://example.com`); - }); + .action(function(url, options) { + open({ ...options, browser: type }, url, options.target).catch(logErrorAndExit); + }).addHelpText('afterAll', ` +Examples: + + $ ${alias} https://example.com`); } commandWithOpenOptions('screenshot ', 'capture a page screenshot', @@ -186,25 +174,21 @@ commandWithOpenOptions('screenshot ', 'capture a page screenshot ['--full-page', 'whether to take a full page screenshot (entire scrollable area)'], ]).action(function(url, filename, command) { screenshot(command, command, url, filename).catch(logErrorAndExit); -}).on('--help', function() { - console.log(''); - console.log('Examples:'); - console.log(''); - console.log(' $ screenshot -b webkit https://example.com example.png'); -}); +}).addHelpText('afterAll', ` +Examples: + + $ screenshot -b webkit https://example.com example.png`); commandWithOpenOptions('pdf ', 'save page as pdf', [ ['--wait-for-selector ', 'wait for given selector before saving as pdf'], ['--wait-for-timeout ', 'wait for given timeout in milliseconds before saving as pdf'], - ]).action(function(url, filename, command) { - pdf(command, command, url, filename).catch(logErrorAndExit); -}).on('--help', function() { - console.log(''); - console.log('Examples:'); - console.log(''); - console.log(' $ pdf https://example.com example.pdf'); -}); + ]).action(function(url, filename, options) { + pdf(options, options, url, filename).catch(logErrorAndExit); +}).addHelpText('afterAll', ` +Examples: + + $ pdf https://example.com example.pdf`); program .command('experimental-grid-server', { hidden: true }) @@ -227,20 +211,18 @@ program .command('show-trace [trace]') .option('-b, --browser ', 'browser to use, one of cr, chromium, ff, firefox, wk, webkit', 'chromium') .description('Show trace viewer') - .action(function(trace, command) { - if (command.browser === 'cr') - command.browser = 'chromium'; - if (command.browser === 'ff') - command.browser = 'firefox'; - if (command.browser === 'wk') - command.browser = 'webkit'; - showTraceViewer(trace, command.browser).catch(logErrorAndExit); - }).on('--help', function() { - console.log(''); - console.log('Examples:'); - console.log(''); - console.log(' $ show-trace trace/directory'); - }); + .action(function(trace, options) { + if (options.browser === 'cr') + options.browser = 'chromium'; + if (options.browser === 'ff') + options.browser = 'firefox'; + if (options.browser === 'wk') + options.browser = 'webkit'; + showTraceViewer(trace, options.browser).catch(logErrorAndExit); + }).addHelpText('afterAll', ` +Examples: + + $ show-trace trace/directory`); if (!process.env.PW_CLI_TARGET_LANG) { let playwrightTestPackagePath = null; @@ -260,7 +242,7 @@ if (!process.env.PW_CLI_TARGET_LANG) { } else { const command = program.command('test').allowUnknownOption(true); command.description('Run tests with Playwright Test. Available in @playwright/test package.'); - command.action(async (args, opts) => { + command.action(async () => { console.error('Please install @playwright/test package to use Playwright Test.'); console.error(' npm install -D @playwright/test'); process.exit(1); @@ -560,7 +542,7 @@ function language(): string { return process.env.PW_CLI_TARGET_LANG || 'test'; } -function commandWithOpenOptions(command: string, description: string, options: any[][]): program.Command { +function commandWithOpenOptions(command: string, description: string, options: any[][]): Command { let result = program.command(command).description(description); for (const option of options) result = result.option(option[0], ...option.slice(1)); diff --git a/src/test/cli.ts b/src/test/cli.ts index f4cacdb5db..52741bda7b 100644 --- a/src/test/cli.ts +++ b/src/test/cli.ts @@ -16,7 +16,7 @@ /* eslint-disable no-console */ -import commander from 'commander'; +import { Command } from 'commander'; import fs from 'fs'; import path from 'path'; import type { Config } from './types'; @@ -39,7 +39,7 @@ const defaultConfig: Config = { workers: Math.ceil(require('os').cpus().length / 2), }; -export function addTestCommand(program: commander.CommanderStatic) { +export function addTestCommand(program: Command) { const command = program.command('test [test-filter...]'); command.description('Run tests with Playwright Test'); command.option('--browser ', `Browser to use for tests, one of "all", "chromium", "firefox" or "webkit" (default: "chromium")`); @@ -71,16 +71,14 @@ export function addTestCommand(program: commander.CommanderStatic) { process.exit(1); } }); - command.on('--help', () => { - console.log(''); - console.log('Arguments [test-filter...]:'); - console.log(' Pass arguments to filter test files. Each argument is treated as a regular expression.'); - console.log(''); - console.log('Examples:'); - console.log(' $ test my.spec.ts'); - console.log(' $ test --headed'); - console.log(' $ test --browser=webkit'); - }); + command.addHelpText('afterAll', ` +Arguments [test-filter...]: + Pass arguments to filter test files. Each argument is treated as a regular expression. + +Examples: + $ test my.spec.ts + $ test --headed + $ test --browser=webkit`); } async function createLoader(opts: { [key: string]: any }): Promise {