1 | import { readJson, writeFileSync } from 'fs-extra';
|
2 | import { resolve, join } from 'path';
|
3 | import {
|
4 | recordHARClient,
|
5 | getBrowserArgs,
|
6 | IConditions,
|
7 | } from '@tracerbench/core';
|
8 |
|
9 | import { TBBaseCommand, getConfig } from '../command-config';
|
10 | import {
|
11 | dest,
|
12 | url,
|
13 | cookiespath,
|
14 | filename,
|
15 | marker,
|
16 | config,
|
17 | } from '../helpers/flags';
|
18 |
|
19 | export default class RecordHAR extends TBBaseCommand {
|
20 | public static description = 'Generates a HAR file from a URL.';
|
21 | public static flags = {
|
22 | url: url({ required: true, default: undefined }),
|
23 | dest: dest({ required: true }),
|
24 | cookiespath: cookiespath({ required: true }),
|
25 | filename: filename({ required: true, default: 'tracerbench' }),
|
26 | marker: marker({ required: true }),
|
27 | config: config(),
|
28 | };
|
29 | public async init() {
|
30 | const { flags } = this.parse(RecordHAR);
|
31 | this.parsedConfig = getConfig(flags.config, flags, this.explicitFlags);
|
32 | }
|
33 |
|
34 | public async run() {
|
35 | const { flags } = this.parse(RecordHAR);
|
36 | const { url, dest, cookiespath, filename, marker } = flags;
|
37 | const { network, cpuThrottleRate, browserArgs } = this.parsedConfig;
|
38 | const conditions: IConditions = {
|
39 | network: network ? network : 'none',
|
40 | cpu: cpuThrottleRate ? parseInt(cpuThrottleRate as string, 10) : 1,
|
41 | };
|
42 |
|
43 | const cookies = await readJson(resolve(cookiespath));
|
44 |
|
45 |
|
46 | const harArchive = await recordHARClient(
|
47 | url,
|
48 | cookies,
|
49 | marker,
|
50 | conditions,
|
51 | getBrowserArgs(browserArgs)
|
52 | );
|
53 |
|
54 | const harPath = join(dest, `${filename}.har`);
|
55 |
|
56 | writeFileSync(harPath, JSON.stringify(harArchive));
|
57 |
|
58 | this.log(`HAR recorded and available here: ${harPath}`);
|
59 | }
|
60 | }
|