|
| 1 | +import { copyFileSync, existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { spawnSync } from 'node:child_process'; |
| 4 | +import net from 'node:net'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | + |
| 7 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | +const root = path.resolve(__dirname, '..'); |
| 9 | +const outDir = path.resolve(root, process.env.MEDIA_OUT_DIR || 'docs/media/generated/git-cms'); |
| 10 | +const resultsDir = path.resolve(root, 'test-results/media'); |
| 11 | +const mediaProject = process.env.MEDIA_PROJECT || 'git-cms-media'; |
| 12 | +async function findOpenPort(start = 47639) { |
| 13 | + for (let port = start; port < start + 50; port += 1) { |
| 14 | + const isOpen = await new Promise((resolve) => { |
| 15 | + const server = net.createServer(); |
| 16 | + server.once('error', () => resolve(false)); |
| 17 | + server.once('listening', () => { |
| 18 | + server.close(() => resolve(true)); |
| 19 | + }); |
| 20 | + server.listen(port, '127.0.0.1'); |
| 21 | + }); |
| 22 | + if (isOpen) return String(port); |
| 23 | + } |
| 24 | + throw new Error(`Could not find an open media port starting at ${start}`); |
| 25 | +} |
| 26 | + |
| 27 | +const mediaPort = process.env.MEDIA_PORT || await findOpenPort(); |
| 28 | + |
| 29 | +function walk(dir) { |
| 30 | + const entries = readdirSync(dir, { withFileTypes: true }); |
| 31 | + for (const entry of entries) { |
| 32 | + const full = path.join(dir, entry.name); |
| 33 | + if (entry.isDirectory()) { |
| 34 | + const nested = walk(full); |
| 35 | + if (nested) return nested; |
| 36 | + continue; |
| 37 | + } |
| 38 | + if (entry.isFile() && entry.name === 'video.webm') { |
| 39 | + return full; |
| 40 | + } |
| 41 | + } |
| 42 | + return null; |
| 43 | +} |
| 44 | + |
| 45 | +rmSync(resultsDir, { recursive: true, force: true }); |
| 46 | +mkdirSync(outDir, { recursive: true }); |
| 47 | + |
| 48 | +spawnSync('docker', ['compose', '-p', mediaProject, 'down', '-v', '--remove-orphans'], { |
| 49 | + cwd: root, |
| 50 | + stdio: 'ignore', |
| 51 | +}); |
| 52 | + |
| 53 | +const env = { |
| 54 | + ...process.env, |
| 55 | + MEDIA_OUT_DIR: outDir, |
| 56 | + MEDIA_PORT: mediaPort, |
| 57 | + MEDIA_PROJECT: mediaProject, |
| 58 | +}; |
| 59 | +delete env.NO_COLOR; |
| 60 | + |
| 61 | +const args = [ |
| 62 | + path.join(root, 'node_modules/@playwright/test/cli.js'), |
| 63 | + 'test', |
| 64 | + '--config', |
| 65 | + 'playwright.media.config.js', |
| 66 | +]; |
| 67 | + |
| 68 | +const result = spawnSync(process.execPath, args, { |
| 69 | + cwd: root, |
| 70 | + env, |
| 71 | + stdio: 'inherit', |
| 72 | +}); |
| 73 | + |
| 74 | +if (result.status !== 0) { |
| 75 | + process.exit(result.status ?? 1); |
| 76 | +} |
| 77 | + |
| 78 | +if (!existsSync(resultsDir)) { |
| 79 | + console.error(`Expected Playwright output directory at ${resultsDir}`); |
| 80 | + process.exit(1); |
| 81 | +} |
| 82 | + |
| 83 | +const videoPath = walk(resultsDir); |
| 84 | +if (!videoPath) { |
| 85 | + console.error(`Could not find recorded video under ${resultsDir}`); |
| 86 | + process.exit(1); |
| 87 | +} |
| 88 | + |
| 89 | +const finalVideoPath = path.join(outDir, 'git-cms-walkthrough.webm'); |
| 90 | +copyFileSync(videoPath, finalVideoPath); |
| 91 | + |
| 92 | +console.log(`Saved browser footage to ${finalVideoPath}`); |
| 93 | +console.log(`Saved poster image to ${path.join(outDir, 'git-cms-poster.png')}`); |
0 commit comments