|
7 | 7 | import type {YargsOptions} from '../third_party/index.js'; |
8 | 8 | import {yargs, hideBin} from '../third_party/index.js'; |
9 | 9 |
|
| 10 | +function parseViewportOption(arg: string | undefined) { |
| 11 | + if (arg === undefined) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + const dimensions = arg.split('x'); |
| 16 | + if (dimensions.length < 2 || dimensions.length > 3) { |
| 17 | + throw new Error( |
| 18 | + 'Invalid viewport. Expected format is `1280x720` or `1280x720x2`.', |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + const [width, height, deviceScaleFactor] = dimensions.map(Number); |
| 23 | + if ( |
| 24 | + !width || |
| 25 | + !height || |
| 26 | + Number.isNaN(width) || |
| 27 | + Number.isNaN(height) || |
| 28 | + (deviceScaleFactor !== undefined && |
| 29 | + (deviceScaleFactor <= 0 || Number.isNaN(deviceScaleFactor))) |
| 30 | + ) { |
| 31 | + throw new Error( |
| 32 | + 'Invalid viewport. Expected format is `1280x720` or `1280x720x2`.', |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + return { |
| 37 | + width, |
| 38 | + height, |
| 39 | + ...(deviceScaleFactor === undefined ? {} : {deviceScaleFactor}), |
| 40 | + }; |
| 41 | +} |
| 42 | + |
10 | 43 | export const cliOptions = { |
11 | 44 | autoConnect: { |
12 | 45 | type: 'boolean', |
@@ -124,20 +157,8 @@ export const cliOptions = { |
124 | 157 | viewport: { |
125 | 158 | type: 'string', |
126 | 159 | describe: |
127 | | - 'Initial viewport size for the Chrome instances started by the server. For example, `1280x720`. In headless mode, max size is 3840x2160px.', |
128 | | - coerce: (arg: string | undefined) => { |
129 | | - if (arg === undefined) { |
130 | | - return; |
131 | | - } |
132 | | - const [width, height] = arg.split('x').map(Number); |
133 | | - if (!width || !height || Number.isNaN(width) || Number.isNaN(height)) { |
134 | | - throw new Error('Invalid viewport. Expected format is `1280x720`.'); |
135 | | - } |
136 | | - return { |
137 | | - width, |
138 | | - height, |
139 | | - }; |
140 | | - }, |
| 160 | + 'Initial viewport size for the Chrome instances started by the server. For example, `1280x720` or `1280x720x2` to set the device scale factor. In headless mode, max size is 3840x2160px.', |
| 161 | + coerce: parseViewportOption, |
141 | 162 | }, |
142 | 163 | proxyServer: { |
143 | 164 | type: 'string', |
@@ -297,6 +318,10 @@ export function parseArguments(version: string, argv = process.argv) { |
297 | 318 | '$0 --viewport 1280x720', |
298 | 319 | 'Launch Chrome with the initial viewport size of 1280x720px', |
299 | 320 | ], |
| 321 | + [ |
| 322 | + '$0 --viewport 1280x720x2', |
| 323 | + 'Launch Chrome with the initial viewport size of 1280x720px and device scale factor 2', |
| 324 | + ], |
300 | 325 | [ |
301 | 326 | `$0 --chrome-arg='--no-sandbox' --chrome-arg='--disable-setuid-sandbox'`, |
302 | 327 | 'Launch Chrome without sandboxes. Use with caution.', |
|
0 commit comments