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
25 changes: 20 additions & 5 deletions src/packageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ class NPM implements PackageManager {
class Yarn implements PackageManager {
name = 'Yarn'
cli = 'yarn'
private workspace: boolean

constructor(rootDir: string) {
this.workspace = this.isWorkspace(rootDir);
}

private isWorkspace(rootDir: string) {
try {
const packageJSON = JSON.parse(fs.readFileSync(path.join(rootDir, 'package.json'), 'utf-8'));
return !!packageJSON.workspaces;
} catch (e) {
return false;
}
}

init(): string {
return 'yarn init -y'
Expand All @@ -67,7 +81,7 @@ class Yarn implements PackageManager {
}

installDevDependency(name: string): string {
return `yarn add --dev ${name}`
return `yarn add --dev ${this.workspace ? '-W ' : ''}${name}`
}

runPlaywrightTest(args: string): string {
Expand All @@ -82,8 +96,10 @@ class Yarn implements PackageManager {
class PNPM implements PackageManager {
name = 'pnpm'
cli = 'pnpm'
private workspace: boolean;

constructor(private workspace: boolean) {
constructor(rootDir: string) {
this.workspace = fs.existsSync(path.resolve(rootDir, 'pnpm-workspace.yaml'));
}

init(): string {
Expand Down Expand Up @@ -118,10 +134,9 @@ class PNPM implements PackageManager {
export function determinePackageManager(rootDir: string): PackageManager {
if (process.env.npm_config_user_agent) {
if (process.env.npm_config_user_agent.includes('yarn'))
return new Yarn();
return new Yarn(rootDir);
if (process.env.npm_config_user_agent.includes('pnpm'))
return new PNPM(fs.existsSync(path.resolve(rootDir, 'pnpm-workspace.yaml')));
return new NPM();
return new PNPM(rootDir);
}
return new NPM();
}
25 changes: 25 additions & 0 deletions tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ test('should generate in the root of pnpm workspace', async ({ run, packageManag
expect(fs.existsSync(path.join(dir, 'playwright.config.ts'))).toBeTruthy();
});

test('should generate in the root of yarn workspaces', async ({ run, packageManager }) => {
test.skip(packageManager !== 'yarn');

const dir = test.info().outputDir;
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, 'package.json'), `{
"name": "yarn-monorepo",
"version": "1.0.0",
"private": true,
"workspaces": ["packages/*"]
}`);
for (const pkg of ['foo', 'bar']) {
const packageDir = path.join(dir, 'packages', pkg);
fs.mkdirSync(packageDir, { recursive: true });
childProcess.execSync('yarn init -y', { cwd: packageDir });
}
childProcess.execSync('yarn install', { cwd: dir });

await run([], { installGitHubActions: false, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: false });
assertLockFilesExist(dir, packageManager);
expect(fs.existsSync(path.join(dir, 'tests/example.spec.ts'))).toBeTruthy();
expect(fs.existsSync(path.join(dir, 'node_modules/playwright'))).toBeTruthy();
expect(fs.existsSync(path.join(dir, 'playwright.config.ts'))).toBeTruthy();
});

test('should not duplicate gitignore entries', async ({ run, dir }) => {
fs.writeFileSync(path.join(dir, '.gitignore'), validGitignore);

Expand Down