Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cli parameter '--userDataArea` to set the Electron 'userData' path #13155

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 41 additions & 40 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,13 @@ export interface ElectronMainCommandOptions {
*/
readonly file?: string;

}
readonly cwd: 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 {
/**
* If the app is launched for the first time, `secondInstance` is false.
* If the app is already running but user relaunches it, `secondInstance` is true.
*/
readonly secondInstance: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should transfer the type doc comments for secondInstance, as it may not be obvious what that means.

readonly argv: string[];
readonly cwd: string;
}

/**
Expand Down Expand Up @@ -212,21 +206,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('electronUserData', {
type: 'string',
describe: 'The area where the electron main process puts its data'
})
.positional('file', { type: 'string' }),
async args => {
if (args.electronUserData) {
console.info(`using electron user data area : '${args.electronUserData}'`);
await fs.mkdir(args.electronUserData, { recursive: true });
app.setPath('userData', args.electronUserData);
}
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 +299,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 +424,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 +647,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
Loading