Skip to content

Commit 2fb6aa3

Browse files
authored
chore: package manager cleanups (#38)
1 parent 0686b4f commit 2fb6aa3

File tree

8 files changed

+140
-87
lines changed

8 files changed

+140
-87
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ jobs:
2525
- run: npm ci
2626
- run: npx playwright install-deps
2727
- run: npm run build
28+
- run: npx tsc --noEmit
2829
- run: npm run test

src/generator.ts

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,23 @@ import fs from 'fs';
1919
import { prompt } from 'enquirer';
2020
import colors from 'ansi-colors';
2121

22-
import { executeCommands, createFiles, determinePackageManager, executeTemplate, Command, languageToFileExtension } from './utils';
23-
import { PackageManager } from './types';
22+
import { executeCommands, createFiles, executeTemplate, Command, languageToFileExtension } from './utils';
23+
import { packageManager } from './packageManager';
2424

2525
export type PromptOptions = {
2626
testDir: string,
2727
installGitHubActions: boolean,
2828
language: 'JavaScript' | 'TypeScript',
29-
framework: 'react' | 'vue' | 'svelte' | undefined,
29+
framework?: 'react' | 'vue' | 'svelte' | undefined,
3030
installPlaywrightDependencies: boolean,
3131
};
3232

3333
const assetsDir = path.join(__dirname, '..', 'assets');
3434

3535
export class Generator {
36-
packageManager: PackageManager;
3736
constructor(private readonly rootDir: string, private readonly options: { [key: string]: string[] }) {
3837
if (!fs.existsSync(rootDir))
3938
fs.mkdirSync(rootDir);
40-
this.packageManager = determinePackageManager(this.rootDir);
4139
}
4240

4341
async run() {
@@ -146,14 +144,9 @@ export class Generator {
146144
}
147145

148146
if (answers.installGitHubActions) {
149-
const pmInstallCommand: Record<PackageManager, string> = {
150-
npm: 'npm ci',
151-
pnpm: 'pnpm install',
152-
yarn: 'yarn',
153-
}
154147
const githubActionsScript = executeTemplate(this._readAsset('github-actions.yml'), {
155-
installDepsCommand: pmInstallCommand[this.packageManager],
156-
runTestsCommand: commandToRunTests(this.packageManager),
148+
installDepsCommand: packageManager.ci(),
149+
runTestsCommand: packageManager.runPlaywrightTest(),
157150
}, new Map());
158151
files.set('.github/workflows/playwright.yml', githubActionsScript);
159152
}
@@ -164,45 +157,29 @@ export class Generator {
164157
}
165158

166159
if (!fs.existsSync(path.join(this.rootDir, 'package.json'))) {
167-
const pmInitializeCommand: Record<PackageManager, string> = {
168-
npm: 'npm init -y',
169-
pnpm: 'pnpm init',
170-
yarn: 'yarn init -y'
171-
}
172-
const pmOfficialName: Record<PackageManager, string> = {
173-
npm: 'NPM',
174-
pnpm: 'Pnpm',
175-
yarn: 'Yarn',
176-
}
177160
commands.push({
178-
name: `Initializing ${pmOfficialName[this.packageManager]} project`,
179-
command: pmInitializeCommand[this.packageManager],
161+
name: `Initializing ${packageManager.name} project`,
162+
command: packageManager.init(),
180163
});
181164
}
182165

183-
let packageLine = '';
184-
const packageName = '@playwright/test';
166+
let packageTag = '';
185167
if (this.options.beta)
186-
packageLine = '@beta';
168+
packageTag = '@beta';
187169
if (this.options.next)
188-
packageLine = '@next';
170+
packageTag = '@next';
189171

190-
const pmInstallDevDepCommand: Record<PackageManager, string> = {
191-
npm: 'npm install --save-dev',
192-
pnpm: 'pnpm add --save-dev',
193-
yarn: 'yarn add --dev'
194-
}
195172
if (!this.options.ct) {
196173
commands.push({
197174
name: 'Installing Playwright Test',
198-
command: `${pmInstallDevDepCommand[this.packageManager]} ${packageName}${packageLine}`,
175+
command: packageManager.installDevDependency(`@playwright/test${packageTag}`),
199176
});
200177
}
201178

202179
if (this.options.ct) {
203180
commands.push({
204181
name: 'Installing Playwright Component Testing',
205-
command: `${pmInstallDevDepCommand[this.packageManager]} ${ctPackageName}${packageLine}`,
182+
command: packageManager.installDevDependency(`${ctPackageName}${packageTag}`),
206183
});
207184

208185
const extension = languageToFileExtension(answers.language);
@@ -265,24 +242,24 @@ export class Generator {
265242
console.log(`
266243
Inside that directory, you can run several commands:
267244
268-
${colors.cyan(commandToRunTests(this.packageManager))}
245+
${colors.cyan(packageManager.runPlaywrightTest())}
269246
Runs the end-to-end tests.
270247
271-
${colors.cyan(commandToRunTests(this.packageManager, '--project=chromium'))}
248+
${colors.cyan(packageManager.runPlaywrightTest('--project=chromium'))}
272249
Runs the tests only on Desktop Chrome.
273250
274-
${colors.cyan(commandToRunTests(this.packageManager, 'example'))}
251+
${colors.cyan(packageManager.runPlaywrightTest('example'))}
275252
Runs the tests in a specific file.
276253
277-
${colors.cyan(`${commandToRunTests(this.packageManager, '--debug')}`)}
254+
${colors.cyan(packageManager.runPlaywrightTest('--debug'))}
278255
Runs the tests in debug mode.
279256
280-
${colors.cyan(`${commandToRunCodegen(this.packageManager)}`)}
257+
${colors.cyan(packageManager.npx('playwright', 'codegen'))}
281258
Auto generate tests with Codegen.
282259
283260
We suggest that you begin by typing:
284261
285-
${colors.cyan(prefix + ' ' + commandToRunTests(this.packageManager))}
262+
${colors.cyan(prefix + ' ' + packageManager.runPlaywrightTest())}
286263
287264
And check out the following files:
288265
- .${path.sep}${pathToNavigate ? path.join(pathToNavigate, exampleSpecPath) : exampleSpecPath} - Example end-to-end test
@@ -299,40 +276,24 @@ Happy hacking! 🎭`);
299276
console.log(`
300277
Inside that directory, you can run several commands:
301278
302-
${colors.cyan(`${this.packageManager} run test-ct`)}
279+
${colors.cyan(`${packageManager.cli} run test-ct`)}
303280
Runs the component tests.
304281
305-
${colors.cyan(`${this.packageManager} run test-ct -- --project=chromium`)}
282+
${colors.cyan(`${packageManager.cli} run test-ct -- --project=chromium`)}
306283
Runs the tests only on Desktop Chrome.
307284
308-
${colors.cyan(`${this.packageManager} run test-ct App.test.ts`)}
285+
${colors.cyan(`${packageManager.cli} run test-ct App.test.ts`)}
309286
Runs the tests in the specific file.
310287
311-
${colors.cyan(`${this.packageManager} run test-ct -- --debug`)}
288+
${colors.cyan(`${packageManager.cli} run test-ct -- --debug`)}
312289
Runs the tests in debug mode.
313290
314291
We suggest that you begin by typing:
315292
316-
${colors.cyan(`${this.packageManager} run test-ct`)}
293+
${colors.cyan(`${packageManager.cli} run test-ct`)}
317294
318295
Visit https://playwright.dev/docs/intro for more information. ✨
319296
320297
Happy hacking! 🎭`);
321298
}
322299
}
323-
324-
export function commandToRunTests(packageManager: PackageManager, args?: string) {
325-
if (packageManager === 'pnpm')
326-
return `pnpm playwright test${args ? (' ' + args) : ''}`;
327-
if (packageManager === 'yarn')
328-
return `yarn playwright test${args ? (' ' + args) : ''}`;
329-
return `npx playwright test${args ? (' ' + args) : ''}`;
330-
}
331-
332-
export function commandToRunCodegen(packageManager: PackageManager) {
333-
if (packageManager === 'pnpm')
334-
return `pnpm playwright codegen`;
335-
if (packageManager === 'yarn')
336-
return `yarn playwright codegen`;
337-
return `npx playwright codegen`;
338-
}

src/packageManager.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
interface PackageManager {
2+
cli: string;
3+
name: string
4+
init(): string
5+
npx(command: string, args: string): string
6+
ci(): string
7+
installDevDependency(name: string): string
8+
runPlaywrightTest(args?: string): string
9+
}
10+
11+
class NPM implements PackageManager {
12+
name = 'NPM'
13+
cli = 'npm'
14+
15+
init(): string {
16+
return 'npm init -y'
17+
}
18+
19+
npx(command: string, args: string): string {
20+
return `npx ${command} ${args}`
21+
}
22+
23+
ci(): string {
24+
return 'npm ci'
25+
}
26+
27+
installDevDependency(name: string): string {
28+
return `npm install --save-dev ${name}`
29+
}
30+
31+
runPlaywrightTest(args: string): string {
32+
return this.npx('playwright', `test${args ? (' ' + args) : ''}`);
33+
}
34+
}
35+
36+
class Yarn implements PackageManager {
37+
name = 'Yarn'
38+
cli = 'yarn'
39+
40+
init(): string {
41+
return 'yarn init -y'
42+
}
43+
44+
npx(command: string, args: string): string {
45+
return `yarn ${command} ${args}`
46+
}
47+
48+
ci(): string {
49+
return 'yarn'
50+
}
51+
52+
installDevDependency(name: string): string {
53+
return `yarn add --dev ${name}`
54+
}
55+
56+
runPlaywrightTest(args: string): string {
57+
return this.npx('playwright', `test${args ? (' ' + args) : ''}`);
58+
}
59+
}
60+
61+
class PNPM implements PackageManager {
62+
name = 'pnpm'
63+
cli = 'pnpm'
64+
65+
init(): string {
66+
return 'pnpm init'
67+
}
68+
69+
npx(command: string, args: string): string {
70+
return `pnpm dlx ${command} ${args}`
71+
}
72+
73+
ci(): string {
74+
return 'pnpm install'
75+
}
76+
77+
installDevDependency(name: string): string {
78+
return `pnpm add --save-dev ${name}`
79+
}
80+
81+
runPlaywrightTest(args: string): string {
82+
return this.npx('playwright', `test${args ? (' ' + args) : ''}`);
83+
}
84+
}
85+
86+
function determinePackageManager(): PackageManager {
87+
if (process.env.npm_config_user_agent) {
88+
if (process.env.npm_config_user_agent.includes('yarn'))
89+
return new Yarn()
90+
if (process.env.npm_config_user_agent.includes('pnpm'))
91+
return new PNPM()
92+
return new NPM()
93+
}
94+
return new NPM()
95+
}
96+
97+
export const packageManager = determinePackageManager();

src/types.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/utils.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import path from 'path';
2020

2121
import { prompt } from 'enquirer';
2222
import colors from 'ansi-colors';
23-
import { PackageManager } from './types';
2423

2524
export type Command = {
2625
command: string;
@@ -56,21 +55,6 @@ export async function createFiles(rootDir: string, files: Map<string, string>, f
5655
}
5756
}
5857

59-
export function determinePackageManager(rootDir: string): PackageManager {
60-
if (fs.existsSync(path.join(rootDir, 'yarn.lock')))
61-
return 'yarn';
62-
if (fs.existsSync(path.join(rootDir, 'pnpm-lock.yaml')))
63-
return 'pnpm';
64-
if (process.env.npm_config_user_agent) {
65-
if (process.env.npm_config_user_agent.includes('yarn'))
66-
return 'yarn'
67-
if (process.env.npm_config_user_agent.includes('pnpm'))
68-
return 'pnpm'
69-
return 'npm';
70-
}
71-
return 'npm';
72-
}
73-
7458
export function executeTemplate(input: string, args: Record<string, string>, sections: Map<string, 'show' | 'hide' | 'comment'>): string {
7559
for (const key in args)
7660
input = input.replace(new RegExp('{{' + key + '}}', 'g'), args[key]);

tests/baseFixtures.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { spawn, SpawnOptionsWithoutStdio } from 'child_process';
1919
import path from 'path';
2020
import fs from 'fs';
2121
import { PromptOptions } from '../src/generator';
22-
import { PackageManager } from '../src/types';
22+
23+
export type PackageManager = 'npm' | 'pnpm' | 'yarn'
2324

2425
type TestFixtures = {
2526
packageManager: PackageManager;

tests/integration.spec.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { test, expect } from './baseFixtures';
16+
import { test, expect, PackageManager } from './baseFixtures';
1717
import path from 'path';
1818
import fs from 'fs';
19-
import { PackageManager } from '../src/types';
2019

2120
for (const packageManager of ['npm', 'pnpm', 'yarn'] as PackageManager[]) {
2221
test.describe(`Package manager: ${packageManager}`, () => {
@@ -91,11 +90,11 @@ for (const packageManager of ['npm', 'pnpm', 'yarn'] as PackageManager[]) {
9190
expect(fs.existsSync(path.join(dir, 'playwright.config.ts'))).toBeTruthy();
9291

9392
{
94-
const { code } = await exec(packageManager === 'npm' ? 'npx' : packageManager === 'pnpm' ? 'pnpm dlx' : 'yarn', ['playwright', 'install-deps']);
93+
const { code } = await exec(packageManagerToNpxCommand(packageManager), ['playwright', 'install-deps']);
9594
expect(code).toBe(0);
9695
}
9796

98-
const { code } = await exec(packageManager === 'npm' ? 'npx' : packageManager === 'pnpm' ? 'pnpm dlx' : 'yarn', ['playwright', 'test']);
97+
const { code } = await exec(packageManagerToNpxCommand(packageManager), ['playwright', 'test']);
9998
expect(code).toBe(0);
10099
});
101100

@@ -108,12 +107,23 @@ for (const packageManager of ['npm', 'pnpm', 'yarn'] as PackageManager[]) {
108107
expect(fs.existsSync(path.join(dir, 'playwright.config.js'))).toBeTruthy();
109108

110109
{
111-
const { code } = await exec(packageManager === 'npm' ? 'npx' : packageManager === 'pnpm' ? 'pnpm dlx' : 'yarn', ['playwright', 'install-deps']);
110+
const { code } = await exec(packageManagerToNpxCommand(packageManager), ['playwright', 'install-deps']);
112111
expect(code).toBe(0);
113112
}
114113

115-
const { code } = await exec(packageManager === 'npm' ? 'npx' : packageManager === 'pnpm' ? 'pnpm dlx' : 'yarn', ['playwright', 'test']);
114+
const { code } = await exec(packageManagerToNpxCommand(packageManager), ['playwright', 'test']);
116115
expect(code).toBe(0);
117116
});
118117
});
119118
}
119+
120+
function packageManagerToNpxCommand(packageManager: PackageManager): string {
121+
switch (packageManager) {
122+
case 'npm':
123+
return 'npx';
124+
case 'yarn':
125+
return 'yarn';
126+
case 'pnpm':
127+
return 'pnpm dlx';
128+
}
129+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"useUnknownInCatchVariables": false,
1313
},
1414
"compileOnSave": true,
15-
"include": ["src"],
15+
"include": ["src", "tests"],
1616
}

0 commit comments

Comments
 (0)