Skip to content

Commit 92f2d98

Browse files
authored
fix: install in the root of yarn workspaces (#135)
fix: install in the root of yarm workspaces Fixes microsoft/playwright#34370
1 parent d5436f4 commit 92f2d98

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/packageManager.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ class NPM implements PackageManager {
4949
class Yarn implements PackageManager {
5050
name = 'Yarn'
5151
cli = 'yarn'
52+
private workspace: boolean
53+
54+
constructor(rootDir: string) {
55+
this.workspace = this.isWorkspace(rootDir);
56+
}
57+
58+
private isWorkspace(rootDir: string) {
59+
try {
60+
const packageJSON = JSON.parse(fs.readFileSync(path.join(rootDir, 'package.json'), 'utf-8'));
61+
return !!packageJSON.workspaces;
62+
} catch (e) {
63+
return false;
64+
}
65+
}
5266

5367
init(): string {
5468
return 'yarn init -y'
@@ -67,7 +81,7 @@ class Yarn implements PackageManager {
6781
}
6882

6983
installDevDependency(name: string): string {
70-
return `yarn add --dev ${name}`
84+
return `yarn add --dev ${this.workspace ? '-W ' : ''}${name}`
7185
}
7286

7387
runPlaywrightTest(args: string): string {
@@ -82,8 +96,10 @@ class Yarn implements PackageManager {
8296
class PNPM implements PackageManager {
8397
name = 'pnpm'
8498
cli = 'pnpm'
99+
private workspace: boolean;
85100

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

89105
init(): string {
@@ -118,10 +134,9 @@ class PNPM implements PackageManager {
118134
export function determinePackageManager(rootDir: string): PackageManager {
119135
if (process.env.npm_config_user_agent) {
120136
if (process.env.npm_config_user_agent.includes('yarn'))
121-
return new Yarn();
137+
return new Yarn(rootDir);
122138
if (process.env.npm_config_user_agent.includes('pnpm'))
123-
return new PNPM(fs.existsSync(path.resolve(rootDir, 'pnpm-workspace.yaml')));
124-
return new NPM();
139+
return new PNPM(rootDir);
125140
}
126141
return new NPM();
127142
}

tests/integration.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@ test('should generate in the root of pnpm workspace', async ({ run, packageManag
112112
expect(fs.existsSync(path.join(dir, 'playwright.config.ts'))).toBeTruthy();
113113
});
114114

115+
test('should generate in the root of yarn workspaces', async ({ run, packageManager }) => {
116+
test.skip(packageManager !== 'yarn');
117+
118+
const dir = test.info().outputDir;
119+
fs.mkdirSync(dir, { recursive: true });
120+
fs.writeFileSync(path.join(dir, 'package.json'), `{
121+
"name": "yarn-monorepo",
122+
"version": "1.0.0",
123+
"private": true,
124+
"workspaces": ["packages/*"]
125+
}`);
126+
for (const pkg of ['foo', 'bar']) {
127+
const packageDir = path.join(dir, 'packages', pkg);
128+
fs.mkdirSync(packageDir, { recursive: true });
129+
childProcess.execSync('yarn init -y', { cwd: packageDir });
130+
}
131+
childProcess.execSync('yarn install', { cwd: dir });
132+
133+
await run([], { installGitHubActions: false, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: false });
134+
assertLockFilesExist(dir, packageManager);
135+
expect(fs.existsSync(path.join(dir, 'tests/example.spec.ts'))).toBeTruthy();
136+
expect(fs.existsSync(path.join(dir, 'node_modules/playwright'))).toBeTruthy();
137+
expect(fs.existsSync(path.join(dir, 'playwright.config.ts'))).toBeTruthy();
138+
});
139+
115140
test('should not duplicate gitignore entries', async ({ run, dir }) => {
116141
fs.writeFileSync(path.join(dir, '.gitignore'), validGitignore);
117142

0 commit comments

Comments
 (0)