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
19 changes: 19 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,28 @@ import { Generator } from './generator';
options[name] = [];
}
const rootDir = path.resolve(process.cwd(), args[0] || '');
if (options.help) {
_printHelp();
process.exit(1);
}
const generator = new Generator(rootDir, options);
await generator.run();
})().catch(error => {
console.error(error);
process.exit(1);
});

function _printHelp() {
console.log(`Usage: npx create-playwright@latest [options] [rootDir]
Available options are:
--help: print this message
--browser=<name>: browsers to use in default config (default: 'chromium,firefox,webkit')
--no-browsers: do not download browsers (can be done manually via 'npx playwright install')
--next: install @next version of Playwright
--beta: install @beta version of Playwright
--ct: install Playwright Component testing
--quiet: do not ask for interactive input prompts
--gha: install GitHub Actions
--lang=<js>: language to use (default: 'TypeScript'. Potential values: 'js', 'TypeScript')
`);
}
20 changes: 15 additions & 5 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type PromptOptions = {
language: 'JavaScript' | 'TypeScript',
framework?: 'react' | 'vue' | 'svelte' | undefined,
installPlaywrightDependencies: boolean,
installPlaywrightBrowsers: boolean,
};

const assetsDir = path.join(__dirname, '..', 'assets');
Expand Down Expand Up @@ -67,6 +68,7 @@ export class Generator {
installPlaywrightDependencies: !!this.options['install-deps'],
testDir: fs.existsSync(path.join(this.rootDir, 'tests')) ? 'e2e' : 'tests',
framework: undefined,
installPlaywrightBrowsers: !this.options['no-browsers'],
};
}

Expand Down Expand Up @@ -104,12 +106,18 @@ export class Generator {
message: 'Add a GitHub Actions workflow?',
initial: false,
},
{
type: 'confirm',
name: 'installPlaywrightBrowsers',
message: 'Install Playwright browsers (can be done manually via \'npx playwright install\')?',
initial: true,
},
// Avoid installing dependencies on Windows (vast majority does not run create-playwright on Windows)
// Avoid installing dependencies on Mac (there are no dependencies)
process.platform === 'linux' && {
type: 'confirm',
name: 'installPlaywrightDependencies',
message: 'Install Playwright operating system dependencies (requires sudo / root - can be done manually via \sudo npx playwright install-deps\')?',
message: 'Install Playwright operating system dependencies (requires sudo / root - can be done manually via \'sudo npx playwright install-deps\')?',
initial: false,
},
];
Expand Down Expand Up @@ -191,10 +199,12 @@ export class Generator {
}

const browsersSuffix = this.options.browser ? ' ' + this.options.browser.join(' ') : '';
commands.push({
name: 'Downloading browsers',
command: 'npx playwright install' + (answers.installPlaywrightDependencies ? ' --with-deps' : '') + browsersSuffix,
});
if (answers.installPlaywrightBrowsers) {
commands.push({
name: 'Downloading browsers',
command: 'npx playwright install' + (answers.installPlaywrightDependencies ? ' --with-deps' : '') + browsersSuffix,
});
}

return { files, commands };
}
Expand Down
10 changes: 5 additions & 5 deletions tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import fs from 'fs';

test('should generate a project in the current directory', async ({ run, packageManager }) => {
test.slow();
const { exitCode, dir, stdout } = await run([], { installGitHubActions: true, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false });
const { exitCode, dir, stdout } = await run([], { installGitHubActions: true, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: true });
expect(exitCode).toBe(0);
expect(fs.existsSync(path.join(dir, 'tests/example.spec.ts'))).toBeTruthy();
expect(fs.existsSync(path.join(dir, 'package.json'))).toBeTruthy();
Expand Down Expand Up @@ -48,7 +48,7 @@ test('should generate a project in the current directory', async ({ run, package
});

test('should generate a project in a given directory', async ({ run, packageManager }) => {
const { exitCode, dir } = await run(['foobar'], { installGitHubActions: true, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false });
const { exitCode, dir } = await run(['foobar'], { installGitHubActions: true, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: true });
expect(exitCode).toBe(0);
expect(fs.existsSync(path.join(dir, 'foobar/tests/example.spec.ts'))).toBeTruthy();
expect(fs.existsSync(path.join(dir, 'foobar/package.json'))).toBeTruthy();
Expand All @@ -63,7 +63,7 @@ test('should generate a project in a given directory', async ({ run, packageMana
});

test('should generate a project with JavaScript and without GHA', async ({ run, packageManager }) => {
const { exitCode, dir } = await run([], { installGitHubActions: false, testDir: 'tests', language: 'JavaScript', installPlaywrightDependencies: false });
const { exitCode, dir } = await run([], { installGitHubActions: false, testDir: 'tests', language: 'JavaScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: true });
expect(exitCode).toBe(0);
expect(fs.existsSync(path.join(dir, 'tests/example.spec.js'))).toBeTruthy();
expect(fs.existsSync(path.join(dir, 'package.json'))).toBeTruthy();
Expand All @@ -79,7 +79,7 @@ test('should generate a project with JavaScript and without GHA', async ({ run,

test('should generate be able to run TS examples successfully', async ({ run, packageManager }) => {
test.slow();
const { exitCode, dir, exec } = await run([], { installGitHubActions: false, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false });
const { exitCode, dir, exec } = await run([], { installGitHubActions: false, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: true });
expect(exitCode).toBe(0);
expect(fs.existsSync(path.join(dir, 'tests/example.spec.ts'))).toBeTruthy();
expect(fs.existsSync(path.join(dir, 'package.json'))).toBeTruthy();
Expand All @@ -96,7 +96,7 @@ test('should generate be able to run TS examples successfully', async ({ run, pa

test('should generate be able to run JS examples successfully', async ({ run, packageManager }) => {
test.slow();
const { exitCode, dir, exec } = await run([], { installGitHubActions: false, testDir: 'tests', language: 'JavaScript', installPlaywrightDependencies: false });
const { exitCode, dir, exec } = await run([], { installGitHubActions: false, testDir: 'tests', language: 'JavaScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: true });
expect(exitCode).toBe(0);
expect(fs.existsSync(path.join(dir, 'tests/example.spec.js'))).toBeTruthy();
expect(fs.existsSync(path.join(dir, 'package.json'))).toBeTruthy();
Expand Down