Skip to content

Allow custom_main.js To Modify Command Line Switches #1029

@AeonSake

Description

@AeonSake

Currently, all proprietary command line switches are checked before the custom_main.js script can run, thus making it impossible to modify those specific switches.

if (app.commandLine.hasSwitch('manifest')) {
manifestJsonFileName = app.commandLine.getSwitchValue('manifest');
}
console.log('Entry!!!: ');
if (app.commandLine.hasSwitch('unpackedelectron')) {
unpackedelectron = true;
}
else if (app.commandLine.hasSwitch('unpackeddotnet')) {
unpackeddotnet = true;
}
else if (app.commandLine.hasSwitch('dotnetpacked')) {
dotnetpacked = true;
}
if (app.commandLine.hasSwitch('electronforcedport')) {
electronforcedport = app.commandLine.getSwitchValue('electronforcedport');
}
// Custom startup hook: look for custom_main.js and invoke its onStartup(host) if present.
// If the hook returns false, abort Electron startup.
try {
const fs = require('fs');
const customMainPath = path.join(__dirname, 'custom_main.js');
if (fs.existsSync(customMainPath)) {
const customMain = require(customMainPath);
if (customMain && typeof customMain.onStartup === 'function') {
const continueStartup = customMain.onStartup(globalThis);
if (continueStartup === false) {
////console.log('custom_main.js onStartup returned false. Exiting Electron host.');
// Ensure the app terminates immediately before further initialization.
// Use app.exit to allow Electron to perform its shutdown, fallback to process.exit.
try { app.exit(0); } catch (err) { process.exit(0); }
}
} else {
console.warn('custom_main.js found but no onStartup function exported.');
}
}
} catch (err) {
console.error('Error while executing custom_main.js:', err);
}

It would be great, if the checks for the individual switches were moved below the custom_main.js handling, which would allow the script to potentially modify the command line switches. This change won't break any existing application but would allow users to, e.g., force a specific startup method (e.g., by also checking environment variables as shown in the wiki article).

On that note: there might be a better approach to force a startup method at compile-time, but this is currently the only realistic way to achieve this without requiring users to run the app with CLI arguments (or create shortcuts with CLI arguments), specifically .NET-first packaged execution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions