|
18 | 18 |
|
19 | 19 | /* eslint-disable no-console */ |
20 | 20 |
|
| 21 | +import extract from 'extract-zip'; |
| 22 | +import fs from 'fs'; |
| 23 | +import os from 'os'; |
21 | 24 | import path from 'path'; |
| 25 | +import rimraf from 'rimraf'; |
22 | 26 | import program from 'commander'; |
23 | | -import os from 'os'; |
24 | | -import fs from 'fs'; |
25 | 27 | import { runDriver, runServer, printApiJson, launchBrowserServer, installBrowsers } from './driver'; |
26 | | -import { showTraceViewer } from '../server/trace/viewer/traceViewer'; |
| 28 | +import { TraceViewer } from '../server/trace/viewer/traceViewer'; |
27 | 29 | import * as playwright from '../..'; |
28 | 30 | import { BrowserContext } from '../client/browserContext'; |
29 | 31 | import { Browser } from '../client/browser'; |
@@ -475,3 +477,31 @@ function commandWithOpenOptions(command: string, description: string, options: a |
475 | 477 | .option('--user-agent <ua string>', 'specify user agent string') |
476 | 478 | .option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"'); |
477 | 479 | } |
| 480 | + |
| 481 | +export async function showTraceViewer(tracePath: string, browserName: string) { |
| 482 | + let stat; |
| 483 | + try { |
| 484 | + stat = fs.statSync(tracePath); |
| 485 | + } catch (e) { |
| 486 | + console.log(`No such file or directory: ${tracePath}`); |
| 487 | + return; |
| 488 | + } |
| 489 | + |
| 490 | + if (stat.isDirectory()) { |
| 491 | + const traceViewer = new TraceViewer(tracePath, browserName); |
| 492 | + await traceViewer.show(); |
| 493 | + return; |
| 494 | + } |
| 495 | + |
| 496 | + const zipFile = tracePath; |
| 497 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), `playwright-trace`)); |
| 498 | + process.on('exit', () => rimraf.sync(dir)); |
| 499 | + try { |
| 500 | + await extract(zipFile, { dir: dir }); |
| 501 | + } catch (e) { |
| 502 | + console.log(`Invalid trace file: ${zipFile}`); |
| 503 | + return; |
| 504 | + } |
| 505 | + const traceViewer = new TraceViewer(dir, browserName); |
| 506 | + await traceViewer.show(); |
| 507 | +} |
0 commit comments