Skip to content

Commit

Permalink
Add a cli parameter '--userDataArea` to set the Electron 'userData' path
Browse files Browse the repository at this point in the history
Part of #13107

Contributed on behalf of STMicroelectronics

Signed-off-by: Thomas Mäder <[email protected]>
  • Loading branch information
tsmaeder committed Dec 5, 2023
1 parent 273c7e2 commit 1fd73b7
Showing 1 changed file with 37 additions and 41 deletions.
78 changes: 37 additions & 41 deletions packages/core/src/electron-main/electron-main-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,8 @@ export interface ElectronMainCommandOptions {
*/
readonly file?: string;

}

/**
* Fields related to a launch event.
*
* This kind of event is triggered in two different contexts:
* 1. The app is launched for the first time, `secondInstance` is false.
* 2. The app is already running but user relaunches it, `secondInstance` is true.
*/
export interface ElectronMainExecutionParams {
readonly secondInstance: boolean;
readonly argv: string[];
readonly cwd: string;
readonly secondInstance: boolean;
}

/**
Expand Down Expand Up @@ -212,21 +201,38 @@ export class ElectronMainApplication {
}

async start(config: FrontendApplicationConfig): Promise<void> {
const args = this.processArgv.getProcessArgvWithoutBin(process.argv);
this.useNativeWindowFrame = this.getTitleBarStyle(config) === 'native';
this._config = config;
this.hookApplicationEvents();
this.showInitialWindow();
const port = await this.startBackend();
this._backendPort.resolve(port);
await app.whenReady();
await this.attachElectronSecurityToken(port);
await this.startContributions();
await this.launch({
secondInstance: false,
argv: args,
cwd: process.cwd()
});
const argv = this.processArgv.getProcessArgvWithoutBin(process.argv);
createYargs(argv, process.cwd())
.command('$0 [file]', false,
cmd => cmd
.option('userDataArea', {
type: 'string',
describe: 'The area where the electron main process puts its data'
})
.positional('file', { type: 'string' }),
async args => {
if (args.userDataArea) {
console.info(`using electron user data area : '${args.userDataArea}'`);
await fs.mkdir(args.userDataArea, { recursive: true });
app.setPath('userData', args.userDataArea);
}
this.useNativeWindowFrame = this.getTitleBarStyle(config) === 'native';
this._config = config;
this.hookApplicationEvents();
this.showInitialWindow();
const port = await this.startBackend();
this._backendPort.resolve(port);
await app.whenReady();
await this.attachElectronSecurityToken(port);
await this.startContributions();

this.handleMainCommand({
file: args.file,
cwd: process.cwd(),
secondInstance: false
});
},
).parse();
}

protected getTitleBarStyle(config: FrontendApplicationConfig): 'native' | 'custom' {
Expand Down Expand Up @@ -288,15 +294,6 @@ export class ElectronMainApplication {
}
}

protected async launch(params: ElectronMainExecutionParams): Promise<void> {
createYargs(params.argv, params.cwd)
.command('$0 [file]', false,
cmd => cmd
.positional('file', { type: 'string' }),
args => this.handleMainCommand(params, { file: args.file }),
).parse();
}

/**
* Use this rather than creating `BrowserWindow` instances from scratch, since some security parameters need to be set, this method will do it.
*
Expand Down Expand Up @@ -422,15 +419,15 @@ export class ElectronMainApplication {
app.quit();
}

protected async handleMainCommand(params: ElectronMainExecutionParams, options: ElectronMainCommandOptions): Promise<void> {
if (params.secondInstance === false) {
protected async handleMainCommand(options: ElectronMainCommandOptions): Promise<void> {
if (options.secondInstance === false) {
await this.openWindowWithWorkspace(''); // restore previous workspace.
} else if (options.file === undefined) {
await this.openDefaultWindow();
} else {
let workspacePath: string | undefined;
try {
workspacePath = await fs.realpath(path.resolve(params.cwd, options.file));
workspacePath = await fs.realpath(path.resolve(options.cwd, options.file));
} catch {
console.error(`Could not resolve the workspace path. "${options.file}" is not a valid 'file' option. Falling back to the default workspace location.`);
}
Expand Down Expand Up @@ -645,9 +642,8 @@ export class ElectronMainApplication {
if (wrapper) {
const listener = wrapper.onDidClose(async () => {
listener.dispose();
await this.launch({
await this.handleMainCommand({
secondInstance: false,
argv: this.processArgv.getProcessArgvWithoutBin(process.argv),
cwd: process.cwd()
});
this.restarting = false;
Expand Down

0 comments on commit 1fd73b7

Please sign in to comment.