|
| 1 | +import os from 'os' |
| 2 | +import debugModule from 'debug' |
| 3 | +import { DEFAULT_ELECTRON_FLAGS } from './util/chromium_flags' |
| 4 | + |
| 5 | +const debug = debugModule('cypress:server:append_electron_switches') |
| 6 | + |
| 7 | +export const appendElectronSwitches = (app: Electron.App) => { |
| 8 | + // NOTE: errors are printed in development mode only |
| 9 | + try { |
| 10 | + // when running inside the electron process, we need to append the default switches immediately |
| 11 | + // before the electron browser is launched. Otherwise, there may be some odd behavior. |
| 12 | + debug('appending default switches for electron: %o', DEFAULT_ELECTRON_FLAGS) |
| 13 | + DEFAULT_ELECTRON_FLAGS.forEach(({ name, value }) => { |
| 14 | + value ? app.commandLine.appendSwitch(name, value) : app.commandLine.appendSwitch(name) |
| 15 | + }) |
| 16 | + |
| 17 | + if (os.platform() === 'linux') { |
| 18 | + app.disableHardwareAcceleration() |
| 19 | + } |
| 20 | + |
| 21 | + if (process.env.ELECTRON_EXTRA_LAUNCH_ARGS) { |
| 22 | + // regex will be used to convert ELECTRON_EXTRA_LAUNCH_ARGS into an array, for example |
| 23 | + // input: 'foo --ipsum=0 --bar=--baz=quux --lorem="--ipsum=dolor --sit=amet"' |
| 24 | + // output: ['foo', '--ipsum=0', '--bar=--baz=quux', '--lorem="--ipsum=dolor --sit=amet"'] |
| 25 | + const regex = /(?:[^\s"']+|"[^"]*"|'[^']*')+/g |
| 26 | + const electronLaunchArguments = process.env.ELECTRON_EXTRA_LAUNCH_ARGS.match(regex) || [] |
| 27 | + |
| 28 | + electronLaunchArguments.forEach((arg) => { |
| 29 | + // arg can be just key --disable-http-cache |
| 30 | + // or key value --remote-debugging-port=8315 |
| 31 | + // or key value with another value --foo=--bar=4196 |
| 32 | + // or key value with another multiple value --foo='--bar=4196 --baz=quux' |
| 33 | + const [key, ...value] = arg.split('=') |
| 34 | + |
| 35 | + // because this is an environment variable, everything is a string |
| 36 | + // thus we don't have to worry about casting |
| 37 | + // --foo=false for example will be "--foo", "false" |
| 38 | + if (value.length) { |
| 39 | + let joinedValues = value.join('=') |
| 40 | + |
| 41 | + // check if the arg is wrapped in " or ' (unicode) |
| 42 | + const isWrappedInQuotes = !!['\u0022', '\u0027'].find(((charAsUnicode) => joinedValues.startsWith(charAsUnicode) && joinedValues.endsWith(charAsUnicode))) |
| 43 | + |
| 44 | + if (isWrappedInQuotes) { |
| 45 | + joinedValues = joinedValues.slice(1, -1) |
| 46 | + } |
| 47 | + |
| 48 | + app.commandLine.appendSwitch(key, joinedValues) |
| 49 | + } else { |
| 50 | + app.commandLine.appendSwitch(key) |
| 51 | + } |
| 52 | + }) |
| 53 | + } |
| 54 | + } catch (e) { |
| 55 | + debug('environment error %s', e.message) |
| 56 | + } |
| 57 | +} |
0 commit comments