Skip to content

Commit

Permalink
Add window-config-file option
Browse files Browse the repository at this point in the history
  • Loading branch information
artemave authored and inukshuk committed Dec 4, 2020
1 parent 5d23ec4 commit 2509838
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ with these additional options:
--interactive Show renderer tests in persistent window [boolean]
--url, --index Load custom URL in renderer [string]
--preload Load module during renderer preload [string]
--window-config-file Supply custom electron window options [string]
-W, --warnings Print renderer warnings to console [boolean]

`window-config-file` must be a javascript file that exports an options object. Check out [electron docs](https://www.electronjs.org/docs/api/browser-window#new-browserwindowoptions) for the list of available options.

For the full list of available options, see `electron-mocha --help`.

#### Examples
Expand Down
65 changes: 53 additions & 12 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ exports.builder = yargs => {
describe: 'Print renderer warnings to console',
group: 'Electron',
type: 'boolean'
},
'window-config-file': {
describe: 'Supply custom electron window options',
group: 'Electron',
implies: 'renderer'
}
})
.check(argv => {
Expand Down Expand Up @@ -142,6 +147,30 @@ exports.builder = yargs => {
})
}

function windowOptions (argv) {
const customWindowOptions = argv['window-config-file'] ? require(join(process.cwd(), argv['window-config-file'])) : {}
const windowOptions = mergeDeep(
{
height: 700,
width: 1200,
focusable: argv.renderer || argv.inspect,
webPreferences: {
enableRemoteModule: true,
contextIsolation: false,
nodeIntegration: true,
webSecurity: false
}
},
customWindowOptions,
{
webPreferences: {
preload: join(__dirname, '..', 'renderer', 'run.js')
}
}
)
return windowOptions
}

exports.handler = async argv => {
try {
await helpers.handleRequires(argv['require-main'])
Expand All @@ -168,18 +197,7 @@ exports.handler = async argv => {
}
})
} else {
const win = createWindow({
height: 700,
width: 1200,
focusable: argv.inspect,
webPreferences: {
enableRemoteModule: true,
contextIsolation: false,
nodeIntegration: true,
webSecurity: false,
preload: join(__dirname, '..', 'renderer', 'run.js')
}
})
const win = createWindow(windowOptions(argv))

win.webContents.on('destroyed', () => app.exit(code))

Expand Down Expand Up @@ -245,3 +263,26 @@ const print = (method, args) => {
const output = util.format.apply(null, args)
console[method](output)
}

function isObject (item) {
return (item && typeof item === 'object' && !Array.isArray(item))
}

// Copied from https://stackoverflow.com/a/34749873/51209
function mergeDeep (target, ...sources) {
if (!sources.length) return target
const source = sources.shift()

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
mergeDeep(target[key], source[key])
} else {
Object.assign(target, { [key]: source[key] })
}
}
}

return mergeDeep(target, ...sources)
}

0 comments on commit 2509838

Please sign in to comment.