Skip to content
Closed
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
23 changes: 23 additions & 0 deletions packages/playwright-core/src/tools/cli-daemon/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,36 @@ function globalConfigFile(): string {
return path.join(process.env['PWTEST_CLI_GLOBAL_CONFIG'] ?? os.homedir(), '.playwright', 'cli.config.json');
}

const cliOutputGitignoreEntries = ['.playwright-cli/'];

async function ensureGitignore(cwd: string, entries: string[]) {
const gitignorePath = path.join(cwd, '.gitignore');
let existing = '';
try {
existing = await fs.promises.readFile(gitignorePath, 'utf8');
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A failed .gitignore write now fails the whole install. Let's make it best-effort.

It writes .gitignore unconditionally, even outside a git repo. playwright-cli install runs in any directory; this creates a .gitignore where none may be wanted. Let's only add the line to existing .gitignore.

throw error;
}
const existingLines = new Set(existing.split(/\r?\n/).map(line => line.trim()));
const missing = entries.filter(entry => !existingLines.has(entry));
if (!missing.length)
return;
const needsNewline = existing.length > 0 && !existing.endsWith('\n');
const header = '# Playwright CLI traces and snapshots (may contain credentials)\n';
const chunk = `${needsNewline ? '\n' : ''}${header}${missing.join('\n')}\n`;
await fs.promises.appendFile(gitignorePath, chunk);
console.log(`✅ Added ${missing.map(entry => `\`${entry}\``).join(', ')} to \`.gitignore\`.`);
}

export async function initWorkspace(initSkills: string | undefined, initSkillsGlobal?: string) {
const globalSkills = !!initSkillsGlobal;
if (!globalSkills) {
const cwd = process.cwd();
const playwrightDir = path.join(cwd, '.playwright');
await fs.promises.mkdir(playwrightDir, { recursive: true });
console.log(`✅ Workspace initialized at \`${cwd}\`.`);
await ensureGitignore(cwd, cliOutputGitignoreEntries);
}

const skills = initSkillsGlobal ?? initSkills;
Expand Down
22 changes: 22 additions & 0 deletions tests/mcp/cli-misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ test('install workspace', async ({ cli }, testInfo) => {
expect(fs.existsSync(playwrightDir)).toBe(true);
});

test('install workspace gitignores CLI output dir', async ({ cli }, testInfo) => {
const { output } = await cli('install');
expect(output).toContain('Added `.playwright-cli/` to `.gitignore`.');
const gitignore = await fs.promises.readFile(testInfo.outputPath('.gitignore'), 'utf8');
expect(gitignore).toContain('.playwright-cli/');

const second = await cli('install');
expect(second.output).not.toContain('Added `.playwright-cli/` to `.gitignore`.');
const gitignoreAfter = await fs.promises.readFile(testInfo.outputPath('.gitignore'), 'utf8');
expect(gitignoreAfter.split('.playwright-cli/').length - 1).toBe(1);
});

test('install workspace appends CLI output dir to an existing gitignore', async ({ cli }, testInfo) => {
await fs.promises.writeFile(testInfo.outputPath('.gitignore'), 'node_modules/\n');
await cli('install');
const gitignore = await fs.promises.readFile(testInfo.outputPath('.gitignore'), 'utf8');
expect(gitignore).toMatch(/^node_modules\/\n/);
expect(gitignore).toContain('.playwright-cli/');
});

test('install workspace w/skills', async ({ cli }, testInfo) => {
const { output } = await cli('install', '--skills');
expect(output).toContain(`Skill installed to \`.claude${path.sep}skills${path.sep}playwright-cli\`.`);
Expand All @@ -66,6 +86,8 @@ test('install w/--skills -g installs into the home directory', async ({ cli }, t
const { output } = await cli('install', '--skills', '-g', { env: { HOME: fakeHome, USERPROFILE: fakeHome } });
expect(output).toContain('Skill installed to');
expect(output).not.toContain('Workspace initialized');
expect(output).not.toContain('.gitignore');
expect(fs.existsSync(testInfo.outputPath('.gitignore'))).toBe(false);

const skillFile = path.join(fakeHome, '.claude', 'skills', 'playwright-cli', 'SKILL.md');
expect(fs.existsSync(skillFile)).toBe(true);
Expand Down