Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ drivers/
nohup.out
.trace
.tmp
.playwright-cli

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add the generated help file here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since help.json is now auto-generated during build, it probably shouldn't be checked into version control.

allure*
blob-report
playwright-report
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
"roll": "node utils/roll_browser.js",
"check-deps": "node utils/check_deps.js",
"build-android-driver": "./utils/build_android_driver.sh",
"innerloop": "playwright run-server --reuse-browser"
"innerloop": "playwright run-server --reuse-browser",
"playwright-cli": "node packages/playwright/lib/mcp/terminal/cli.js",
"test-playwright-cli": "playwright test --config=tests/mcp/playwright.config.ts --project=chrome cli.spec.ts",
"playwright-cli-readme": "node utils/generate_cli_help.js --readme"
},
"workspaces": [
"packages/*"
Expand Down
47 changes: 0 additions & 47 deletions packages/playwright/src/mcp/terminal/help.json

This file was deleted.

20 changes: 4 additions & 16 deletions packages/playwright/src/mcp/terminal/helpGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

import fs from 'fs';
import path from 'path';
import { commands } from './commands';

import type zodType from 'zod';
Expand Down Expand Up @@ -77,7 +75,7 @@ const categories: { name: Category, title: string }[] = [
{ name: 'session', title: 'Sessions' },
] as const;

function generateHelp() {
export function generateHelp() {
const lines: string[] = [];
lines.push('Usage: playwright-cli <command> [args] [options]');

Expand All @@ -99,7 +97,7 @@ function generateHelp() {
}


function generateReadme() {
export function generateReadme() {
const lines: string[] = [];
lines.push('\n## Commands');

Expand Down Expand Up @@ -136,27 +134,17 @@ function generateReadmeEntry(command: AnyCommandSchema): string {
return formatWithGap(prefix, suffix, 40);
}

async function main() {
export function generateHelpJSON() {
const help = {
global: generateHelp(),
commands: Object.fromEntries(
Object.entries(commands).map(([name, command]) => [name, generateCommandHelp(command)])
),
};
const readme = generateReadme();
const fileName = path.resolve(__dirname, 'help.json').replace('lib', 'src');
// eslint-disable-next-line no-console
console.log('Writing ', path.relative(process.cwd(), fileName));
await fs.promises.writeFile(fileName, JSON.stringify(help, null, 2));
// eslint-disable-next-line no-console
console.log(help.global);
// eslint-disable-next-line no-console
console.log(readme);
return help;
}

function formatWithGap(prefix: string, text: string, threshold: number = 30) {
const indent = Math.max(1, threshold - prefix.length);
return prefix + ' '.repeat(indent) + text;
}

void main();
15 changes: 9 additions & 6 deletions utils/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,15 @@ for (const webPackage of ['html-reporter', 'recorder', 'trace-viewer']) {
}));
}

// Generate CLI help.
onChanges.push({
inputs: [
'packages/playwright/src/mcp/terminal/commands.ts',
'utils/generate_cli_help.js',
],
script: 'utils/generate_cli_help.js',
});

// Generate injected.
onChanges.push({
inputs: [
Expand Down Expand Up @@ -636,12 +645,6 @@ copyFiles.push({
to: 'packages/playwright/lib',
});

copyFiles.push({
files: 'packages/playwright/src/mcp/terminal/*.json',
from: 'packages/playwright/src',
to: 'packages/playwright/lib',
});

copyFiles.push({
files: 'packages/playwright/src/mcp/terminal/*.md',
from: 'packages/playwright/src',
Expand Down
86 changes: 0 additions & 86 deletions utils/generate_cli_commands.js

This file was deleted.

36 changes: 36 additions & 0 deletions utils/generate_cli_help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @ts-check

const fs = require('fs')
const path = require('path')

const { generateHelp, generateReadme, generateHelpJSON } = require('../packages/playwright/lib/mcp/terminal/helpGenerator.js');

if (process.argv[2] === '--readme') {
console.log(generateReadme());
process.exit(0);
}

if (process.argv[2] === '--print') {
console.log(generateHelp());
process.exit(0);
}

const fileName = path.resolve(__dirname, '../packages/playwright/lib/mcp/terminal/help.json');
console.log('Writing ', path.relative(process.cwd(), fileName));
fs.writeFileSync(fileName, JSON.stringify(generateHelpJSON(), null, 2));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a recommendation: Consider adding error handling here to make build failures more obvious:

try {
  fs.writeFileSync(fileName, JSON.stringify(generateHelpJSON(), null, 2));
  console.log('✓ Successfully wrote', path.relative(process.cwd(), fileName));
} catch (error) {
  console.error('✗ Failed to write help file:', error.message);
  process.exit(1);
}

This will make it clearer if the file write fails during the build process.

Loading