| 1 |
const { spawn, exec } = require( 'child_process' ); |
| 2 |
const packageJson = require( './package.json' ); |
| 3 |
|
| 4 |
function isDockerExist() { |
| 5 |
return new Promise( ( resolve ) => { |
| 6 |
exec( 'docker -v', ( error ) => { |
| 7 |
resolve( ! error ); |
| 8 |
} ); |
| 9 |
} ); |
| 10 |
} |
| 11 |
|
| 12 |
async function run( grep ) { |
| 13 |
const playwrightVersion = packageJson.devDependencies[ '@playwright/test' ]; |
| 14 |
const workingDir = process.cwd(); |
| 15 |
const browsers = process.env.BROWSERS || 'chromium'; |
| 16 |
|
| 17 |
const command = 'docker run'; |
| 18 |
const options = [ |
| 19 |
'--rm', |
| 20 |
'--network host', |
| 21 |
`--volume ${ workingDir }:/work`, |
| 22 |
'--workdir /work/', |
| 23 |
`--env BROWSERS=${ browsers }`, |
| 24 |
'--interactive', |
| 25 |
process.env.CI ? '' : '--tty', |
| 26 |
]; |
| 27 |
const image = `mcr.microsoft.com/playwright:v${ playwrightVersion.replace( '^', '' ) }-jammy`; |
| 28 |
const grepFlag = grep.length ? `--grep="${ grep }"` : ''; |
| 29 |
const commandToRun = `/bin/bash -c "npm run test:playwright -- --update-snapshots ${ grepFlag }"`; |
| 30 |
|
| 31 |
await new Promise( ( resolve, reject ) => { |
| 32 |
const child = spawn( `${ command } ${ options.join( ' ' ) } ${ image } ${ commandToRun }`, { |
| 33 |
stdio: 'inherit', |
| 34 |
shell: true, |
| 35 |
} ); |
| 36 |
|
| 37 |
child.on( 'close', ( code ) => { |
| 38 |
if ( code !== 0 ) { |
| 39 |
reject( new Error( `Docker process exited with code ${ code }` ) ); |
| 40 |
} else { |
| 41 |
resolve(); |
| 42 |
} |
| 43 |
} ); |
| 44 |
} ); |
| 45 |
} |
| 46 |
|
| 47 |
( async () => { |
| 48 |
if ( ! await isDockerExist() ) { |
| 49 |
// eslint-disable-next-line no-console |
| 50 |
console.error( 'Docker is not installed, please install it first.' ); |
| 51 |
|
| 52 |
process.exit( 1 ); |
| 53 |
} |
| 54 |
|
| 55 |
await run( process.argv.slice( 2 ) ); |
| 56 |
} )(); |
| 57 |
|