@@ -19,25 +19,23 @@ import fs from 'fs';
19
19
import { prompt } from 'enquirer' ;
20
20
import colors from 'ansi-colors' ;
21
21
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 ' ;
24
24
25
25
export type PromptOptions = {
26
26
testDir : string ,
27
27
installGitHubActions : boolean ,
28
28
language : 'JavaScript' | 'TypeScript' ,
29
- framework : 'react' | 'vue' | 'svelte' | undefined ,
29
+ framework ? : 'react' | 'vue' | 'svelte' | undefined ,
30
30
installPlaywrightDependencies : boolean ,
31
31
} ;
32
32
33
33
const assetsDir = path . join ( __dirname , '..' , 'assets' ) ;
34
34
35
35
export class Generator {
36
- packageManager : PackageManager ;
37
36
constructor ( private readonly rootDir : string , private readonly options : { [ key : string ] : string [ ] } ) {
38
37
if ( ! fs . existsSync ( rootDir ) )
39
38
fs . mkdirSync ( rootDir ) ;
40
- this . packageManager = determinePackageManager ( this . rootDir ) ;
41
39
}
42
40
43
41
async run ( ) {
@@ -146,14 +144,9 @@ export class Generator {
146
144
}
147
145
148
146
if ( answers . installGitHubActions ) {
149
- const pmInstallCommand : Record < PackageManager , string > = {
150
- npm : 'npm ci' ,
151
- pnpm : 'pnpm install' ,
152
- yarn : 'yarn' ,
153
- }
154
147
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 ( ) ,
157
150
} , new Map ( ) ) ;
158
151
files . set ( '.github/workflows/playwright.yml' , githubActionsScript ) ;
159
152
}
@@ -164,45 +157,29 @@ export class Generator {
164
157
}
165
158
166
159
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
- }
177
160
commands . push ( {
178
- name : `Initializing ${ pmOfficialName [ this . packageManager ] } project` ,
179
- command : pmInitializeCommand [ this . packageManager ] ,
161
+ name : `Initializing ${ packageManager . name } project` ,
162
+ command : packageManager . init ( ) ,
180
163
} ) ;
181
164
}
182
165
183
- let packageLine = '' ;
184
- const packageName = '@playwright/test' ;
166
+ let packageTag = '' ;
185
167
if ( this . options . beta )
186
- packageLine = '@beta' ;
168
+ packageTag = '@beta' ;
187
169
if ( this . options . next )
188
- packageLine = '@next' ;
170
+ packageTag = '@next' ;
189
171
190
- const pmInstallDevDepCommand : Record < PackageManager , string > = {
191
- npm : 'npm install --save-dev' ,
192
- pnpm : 'pnpm add --save-dev' ,
193
- yarn : 'yarn add --dev'
194
- }
195
172
if ( ! this . options . ct ) {
196
173
commands . push ( {
197
174
name : 'Installing Playwright Test' ,
198
- command : ` ${ pmInstallDevDepCommand [ this . packageManager ] } ${ packageName } ${ packageLine } ` ,
175
+ command : packageManager . installDevDependency ( `@playwright/test ${ packageTag } ` ) ,
199
176
} ) ;
200
177
}
201
178
202
179
if ( this . options . ct ) {
203
180
commands . push ( {
204
181
name : 'Installing Playwright Component Testing' ,
205
- command : `${ pmInstallDevDepCommand [ this . packageManager ] } ${ ctPackageName } ${ packageLine } ` ,
182
+ command : packageManager . installDevDependency ( `${ ctPackageName } ${ packageTag } ` ) ,
206
183
} ) ;
207
184
208
185
const extension = languageToFileExtension ( answers . language ) ;
@@ -265,24 +242,24 @@ export class Generator {
265
242
console . log ( `
266
243
Inside that directory, you can run several commands:
267
244
268
- ${ colors . cyan ( commandToRunTests ( this . packageManager ) ) }
245
+ ${ colors . cyan ( packageManager . runPlaywrightTest ( ) ) }
269
246
Runs the end-to-end tests.
270
247
271
- ${ colors . cyan ( commandToRunTests ( this . packageManager , '--project=chromium' ) ) }
248
+ ${ colors . cyan ( packageManager . runPlaywrightTest ( '--project=chromium' ) ) }
272
249
Runs the tests only on Desktop Chrome.
273
250
274
- ${ colors . cyan ( commandToRunTests ( this . packageManager , 'example' ) ) }
251
+ ${ colors . cyan ( packageManager . runPlaywrightTest ( 'example' ) ) }
275
252
Runs the tests in a specific file.
276
253
277
- ${ colors . cyan ( ` ${ commandToRunTests ( this . packageManager , '--debug' ) } ` ) }
254
+ ${ colors . cyan ( packageManager . runPlaywrightTest ( '--debug' ) ) }
278
255
Runs the tests in debug mode.
279
256
280
- ${ colors . cyan ( ` ${ commandToRunCodegen ( this . packageManager ) } ` ) }
257
+ ${ colors . cyan ( packageManager . npx ( 'playwright' , 'codegen' ) ) }
281
258
Auto generate tests with Codegen.
282
259
283
260
We suggest that you begin by typing:
284
261
285
- ${ colors . cyan ( prefix + ' ' + commandToRunTests ( this . packageManager ) ) }
262
+ ${ colors . cyan ( prefix + ' ' + packageManager . runPlaywrightTest ( ) ) }
286
263
287
264
And check out the following files:
288
265
- .${ path . sep } ${ pathToNavigate ? path . join ( pathToNavigate , exampleSpecPath ) : exampleSpecPath } - Example end-to-end test
@@ -299,40 +276,24 @@ Happy hacking! 🎭`);
299
276
console . log ( `
300
277
Inside that directory, you can run several commands:
301
278
302
- ${ colors . cyan ( `${ this . packageManager } run test-ct` ) }
279
+ ${ colors . cyan ( `${ packageManager . cli } run test-ct` ) }
303
280
Runs the component tests.
304
281
305
- ${ colors . cyan ( `${ this . packageManager } run test-ct -- --project=chromium` ) }
282
+ ${ colors . cyan ( `${ packageManager . cli } run test-ct -- --project=chromium` ) }
306
283
Runs the tests only on Desktop Chrome.
307
284
308
- ${ colors . cyan ( `${ this . packageManager } run test-ct App.test.ts` ) }
285
+ ${ colors . cyan ( `${ packageManager . cli } run test-ct App.test.ts` ) }
309
286
Runs the tests in the specific file.
310
287
311
- ${ colors . cyan ( `${ this . packageManager } run test-ct -- --debug` ) }
288
+ ${ colors . cyan ( `${ packageManager . cli } run test-ct -- --debug` ) }
312
289
Runs the tests in debug mode.
313
290
314
291
We suggest that you begin by typing:
315
292
316
- ${ colors . cyan ( `${ this . packageManager } run test-ct` ) }
293
+ ${ colors . cyan ( `${ packageManager . cli } run test-ct` ) }
317
294
318
295
Visit https://playwright.dev/docs/intro for more information. ✨
319
296
320
297
Happy hacking! 🎭` ) ;
321
298
}
322
299
}
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
- }
0 commit comments