chore: upgrade commander.js to version 8 (#9230)

This commit is contained in:
Max Schmitt 2021-09-30 12:24:24 +02:00 committed by GitHub
parent 2d428c8a4e
commit cf12a1a97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 109 deletions

16
package-lock.json generated
View File

@ -27,7 +27,7 @@
"@babel/plugin-transform-modules-commonjs": "^7.14.5", "@babel/plugin-transform-modules-commonjs": "^7.14.5",
"@babel/preset-typescript": "^7.14.5", "@babel/preset-typescript": "^7.14.5",
"colors": "^1.4.0", "colors": "^1.4.0",
"commander": "^6.1.0", "commander": "^8.2.0",
"debug": "^4.1.1", "debug": "^4.1.1",
"expect": "^26.4.2", "expect": "^26.4.2",
"extract-zip": "^2.0.1", "extract-zip": "^2.0.1",
@ -3318,11 +3318,11 @@
} }
}, },
"node_modules/commander": { "node_modules/commander": {
"version": "6.2.1", "version": "8.2.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", "resolved": "https://registry.npmjs.org/commander/-/commander-8.2.0.tgz",
"integrity": "sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==", "integrity": "sha512-LLKxDvHeL91/8MIyTAD5BFMNtoIwztGPMiM/7Bl8rIPmHCZXRxmSWr91h57dpOpnQ6jIUqEWdXE/uBYMfiVZDA==",
"engines": { "engines": {
"node": ">= 6" "node": ">= 12"
} }
}, },
"node_modules/commondir": { "node_modules/commondir": {
@ -13479,9 +13479,9 @@
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==" "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
}, },
"commander": { "commander": {
"version": "6.2.1", "version": "8.2.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", "resolved": "https://registry.npmjs.org/commander/-/commander-8.2.0.tgz",
"integrity": "sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==" "integrity": "sha512-LLKxDvHeL91/8MIyTAD5BFMNtoIwztGPMiM/7Bl8rIPmHCZXRxmSWr91h57dpOpnQ6jIUqEWdXE/uBYMfiVZDA=="
}, },
"commondir": { "commondir": {
"version": "1.0.1", "version": "1.0.1",

View File

@ -59,7 +59,7 @@
"@babel/plugin-transform-modules-commonjs": "^7.14.5", "@babel/plugin-transform-modules-commonjs": "^7.14.5",
"@babel/preset-typescript": "^7.14.5", "@babel/preset-typescript": "^7.14.5",
"colors": "^1.4.0", "colors": "^1.4.0",
"commander": "^6.1.0", "commander": "^8.2.0",
"debug": "^4.1.1", "debug": "^4.1.1",
"expect": "^26.4.2", "expect": "^26.4.2",
"extract-zip": "^2.0.1", "extract-zip": "^2.0.1",

View File

@ -21,7 +21,7 @@
import fs from 'fs'; import fs from 'fs';
import os from 'os'; import os from 'os';
import path from 'path'; import path from 'path';
import program from 'commander'; import { program, Command } from 'commander';
import { runDriver, runServer, printApiJson, launchBrowserServer } from './driver'; import { runDriver, runServer, printApiJson, launchBrowserServer } from './driver';
import { showTraceViewer } from '../server/trace/viewer/traceViewer'; import { showTraceViewer } from '../server/trace/viewer/traceViewer';
import * as playwright from '../..'; import * as playwright from '../..';
@ -42,48 +42,41 @@ program
.name(process.env.PW_CLI_NAME || 'npx playwright'); .name(process.env.PW_CLI_NAME || 'npx playwright');
commandWithOpenOptions('open [url]', 'open page in browser specified via -b, --browser', []) commandWithOpenOptions('open [url]', 'open page in browser specified via -b, --browser', [])
.action(function(url, command) { .action(function(url, options) {
open(command, url, language()).catch(logErrorAndExit); open(options, url, language()).catch(logErrorAndExit);
}) })
.on('--help', function() { .addHelpText('afterAll', `
console.log(''); Examples:
console.log('Examples:');
console.log(''); $ open $ open -b webkit https://example.com`);
console.log(' $ open');
console.log(' $ open -b webkit https://example.com');
});
commandWithOpenOptions('codegen [url]', 'open page and generate code for user actions', commandWithOpenOptions('codegen [url]', 'open page and generate code for user actions',
[ [
['-o, --output <file name>', 'saves the generated script to a file'], ['-o, --output <file name>', 'saves the generated script to a file'],
['--target <language>', `language to generate, one of javascript, test, python, python-async, csharp`, language()], ['--target <language>', `language to generate, one of javascript, test, python, python-async, csharp`, language()],
]).action(function(url, command) { ]).action(function(url, options) {
codegen(command, url, command.target, command.output).catch(logErrorAndExit); codegen(options, url, options.target, options.output).catch(logErrorAndExit);
}).on('--help', function() { }).addHelpText('afterAll', `
console.log(''); Examples:
console.log('Examples:');
console.log(''); $ codegen
console.log(' $ codegen'); $ codegen --target=python
console.log(' $ codegen --target=python'); $ codegen -b webkit https://example.com`);
console.log(' $ codegen -b webkit https://example.com');
});
program program
.command('debug <app> [args...]', { hidden: true }) .command('debug <app> [args...]', { hidden: true })
.description('run command in debug mode: disable timeout, open inspector') .description('run command in debug mode: disable timeout, open inspector')
.allowUnknownOption(true) .allowUnknownOption(true)
.action(function(app, args) { .action(function(app, options) {
spawn(app, args, { spawn(app, options, {
env: { ...process.env, PWDEBUG: '1' }, env: { ...process.env, PWDEBUG: '1' },
stdio: 'inherit' stdio: 'inherit'
}); });
}).on('--help', function() { }).addHelpText('afterAll', `
console.log(''); Examples:
console.log('Examples:');
console.log(''); $ debug node test.js
console.log(' $ debug node test.js'); $ debug npm run test`);
console.log(' $ debug npm run test');
});
function suggestedBrowsersToInstall() { function suggestedBrowsersToInstall() {
return registry.executables().filter(e => e.installType !== 'none' && e.type !== 'tool').map(e => e.name).join(', '); return registry.executables().filter(e => e.installType !== 'none' && e.type !== 'tool').map(e => e.name).join(', ');
@ -110,16 +103,16 @@ program
.command('install [browser...]') .command('install [browser...]')
.description('ensure browsers necessary for this version of Playwright are installed') .description('ensure browsers necessary for this version of Playwright are installed')
.option('--with-deps', 'install system dependencies for browsers') .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 { try {
if (!args.length) { if (!args.length) {
const executables = registry.defaultExecutables(); const executables = registry.defaultExecutables();
if (command.opts().withDeps) if (options.withDeps)
await registry.installDeps(executables); await registry.installDeps(executables);
await registry.install(executables); await registry.install(executables);
} else { } else {
const executables = checkBrowsersToInstall(args); const executables = checkBrowsersToInstall(args);
if (command.opts().withDeps) if (options.withDeps)
await registry.installDeps(executables); await registry.installDeps(executables);
await registry.install(executables); await registry.install(executables);
} }
@ -127,15 +120,14 @@ program
console.log(`Failed to install browsers\n${e}`); console.log(`Failed to install browsers\n${e}`);
process.exit(1); process.exit(1);
} }
}).on('--help', function() { }).addHelpText('afterAll', `
console.log(``);
console.log(`Examples:`); Examples:
console.log(` - $ install`); - $ install
console.log(` Install default browsers.`); Install default browsers.
console.log(``);
console.log(` - $ install chrome firefox`); - $ install chrome firefox
console.log(` Install custom browsers, supports ${suggestedBrowsersToInstall()}.`); Install custom browsers, supports ${suggestedBrowsersToInstall()}.`);
});
program program
@ -151,15 +143,13 @@ program
console.log(`Failed to install browser dependencies\n${e}`); console.log(`Failed to install browser dependencies\n${e}`);
process.exit(1); process.exit(1);
} }
}).on('--help', function() { }).addHelpText('afterAll', `
console.log(``); Examples:
console.log(`Examples:`); - $ install-deps
console.log(` - $ install-deps`); Install dependencies for default browsers.
console.log(` Install dependencies for default browsers.`);
console.log(``); - $ install-deps chrome firefox
console.log(` - $ install-deps chrome firefox`); Install dependencies for specific browsers, supports ${suggestedBrowsersToInstall()}.`);
console.log(` Install dependencies for specific browsers, supports ${suggestedBrowsersToInstall()}.`);
});
const browsers = [ const browsers = [
{ alias: 'cr', name: 'Chromium', type: 'chromium' }, { alias: 'cr', name: 'Chromium', type: 'chromium' },
@ -169,14 +159,12 @@ const browsers = [
for (const { alias, name, type } of browsers) { for (const { alias, name, type } of browsers) {
commandWithOpenOptions(`${alias} [url]`, `open page in ${name}`, []) commandWithOpenOptions(`${alias} [url]`, `open page in ${name}`, [])
.action(function(url, command) { .action(function(url, options) {
open({ ...command, browser: type }, url, command.target).catch(logErrorAndExit); open({ ...options, browser: type }, url, options.target).catch(logErrorAndExit);
}).on('--help', function() { }).addHelpText('afterAll', `
console.log(''); Examples:
console.log('Examples:');
console.log(''); $ ${alias} https://example.com`);
console.log(` $ ${alias} https://example.com`);
});
} }
commandWithOpenOptions('screenshot <url> <filename>', 'capture a page screenshot', commandWithOpenOptions('screenshot <url> <filename>', 'capture a page screenshot',
@ -186,25 +174,21 @@ commandWithOpenOptions('screenshot <url> <filename>', 'capture a page screenshot
['--full-page', 'whether to take a full page screenshot (entire scrollable area)'], ['--full-page', 'whether to take a full page screenshot (entire scrollable area)'],
]).action(function(url, filename, command) { ]).action(function(url, filename, command) {
screenshot(command, command, url, filename).catch(logErrorAndExit); screenshot(command, command, url, filename).catch(logErrorAndExit);
}).on('--help', function() { }).addHelpText('afterAll', `
console.log(''); Examples:
console.log('Examples:');
console.log(''); $ screenshot -b webkit https://example.com example.png`);
console.log(' $ screenshot -b webkit https://example.com example.png');
});
commandWithOpenOptions('pdf <url> <filename>', 'save page as pdf', commandWithOpenOptions('pdf <url> <filename>', 'save page as pdf',
[ [
['--wait-for-selector <selector>', 'wait for given selector before saving as pdf'], ['--wait-for-selector <selector>', 'wait for given selector before saving as pdf'],
['--wait-for-timeout <timeout>', 'wait for given timeout in milliseconds before saving as pdf'], ['--wait-for-timeout <timeout>', 'wait for given timeout in milliseconds before saving as pdf'],
]).action(function(url, filename, command) { ]).action(function(url, filename, options) {
pdf(command, command, url, filename).catch(logErrorAndExit); pdf(options, options, url, filename).catch(logErrorAndExit);
}).on('--help', function() { }).addHelpText('afterAll', `
console.log(''); Examples:
console.log('Examples:');
console.log(''); $ pdf https://example.com example.pdf`);
console.log(' $ pdf https://example.com example.pdf');
});
program program
.command('experimental-grid-server', { hidden: true }) .command('experimental-grid-server', { hidden: true })
@ -227,20 +211,18 @@ program
.command('show-trace [trace]') .command('show-trace [trace]')
.option('-b, --browser <browserType>', 'browser to use, one of cr, chromium, ff, firefox, wk, webkit', 'chromium') .option('-b, --browser <browserType>', 'browser to use, one of cr, chromium, ff, firefox, wk, webkit', 'chromium')
.description('Show trace viewer') .description('Show trace viewer')
.action(function(trace, command) { .action(function(trace, options) {
if (command.browser === 'cr') if (options.browser === 'cr')
command.browser = 'chromium'; options.browser = 'chromium';
if (command.browser === 'ff') if (options.browser === 'ff')
command.browser = 'firefox'; options.browser = 'firefox';
if (command.browser === 'wk') if (options.browser === 'wk')
command.browser = 'webkit'; options.browser = 'webkit';
showTraceViewer(trace, command.browser).catch(logErrorAndExit); showTraceViewer(trace, options.browser).catch(logErrorAndExit);
}).on('--help', function() { }).addHelpText('afterAll', `
console.log(''); Examples:
console.log('Examples:');
console.log(''); $ show-trace trace/directory`);
console.log(' $ show-trace trace/directory');
});
if (!process.env.PW_CLI_TARGET_LANG) { if (!process.env.PW_CLI_TARGET_LANG) {
let playwrightTestPackagePath = null; let playwrightTestPackagePath = null;
@ -260,7 +242,7 @@ if (!process.env.PW_CLI_TARGET_LANG) {
} else { } else {
const command = program.command('test').allowUnknownOption(true); const command = program.command('test').allowUnknownOption(true);
command.description('Run tests with Playwright Test. Available in @playwright/test package.'); 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('Please install @playwright/test package to use Playwright Test.');
console.error(' npm install -D @playwright/test'); console.error(' npm install -D @playwright/test');
process.exit(1); process.exit(1);
@ -560,7 +542,7 @@ function language(): string {
return process.env.PW_CLI_TARGET_LANG || 'test'; 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); let result = program.command(command).description(description);
for (const option of options) for (const option of options)
result = result.option(option[0], ...option.slice(1)); result = result.option(option[0], ...option.slice(1));

View File

@ -16,7 +16,7 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
import commander from 'commander'; import { Command } from 'commander';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import type { Config } from './types'; import type { Config } from './types';
@ -39,7 +39,7 @@ const defaultConfig: Config = {
workers: Math.ceil(require('os').cpus().length / 2), 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...]'); const command = program.command('test [test-filter...]');
command.description('Run tests with Playwright Test'); command.description('Run tests with Playwright Test');
command.option('--browser <browser>', `Browser to use for tests, one of "all", "chromium", "firefox" or "webkit" (default: "chromium")`); command.option('--browser <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); process.exit(1);
} }
}); });
command.on('--help', () => { command.addHelpText('afterAll', `
console.log(''); Arguments [test-filter...]:
console.log('Arguments [test-filter...]:'); Pass arguments to filter test files. Each argument is treated as a regular expression.
console.log(' Pass arguments to filter test files. Each argument is treated as a regular expression.');
console.log(''); Examples:
console.log('Examples:'); $ test my.spec.ts
console.log(' $ test my.spec.ts'); $ test --headed
console.log(' $ test --headed'); $ test --browser=webkit`);
console.log(' $ test --browser=webkit');
});
} }
async function createLoader(opts: { [key: string]: any }): Promise<Loader> { async function createLoader(opts: { [key: string]: any }): Promise<Loader> {