From 1fd73b75e41acd7f891bebafcbac6affa80b035a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=A4der?= Date: Tue, 5 Dec 2023 17:21:11 +0100 Subject: [PATCH 1/3] Add a cli parameter '--userDataArea` to set the Electron 'userData' path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Part of #13107 Contributed on behalf of STMicroelectronics Signed-off-by: Thomas Mäder --- .../electron-main-application.ts | 78 +++++++++---------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/packages/core/src/electron-main/electron-main-application.ts b/packages/core/src/electron-main/electron-main-application.ts index 3a67acd71ff1b..97c06729b3fea 100644 --- a/packages/core/src/electron-main/electron-main-application.ts +++ b/packages/core/src/electron-main/electron-main-application.ts @@ -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; } /** @@ -212,21 +201,38 @@ export class ElectronMainApplication { } async start(config: FrontendApplicationConfig): Promise { - 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' { @@ -288,15 +294,6 @@ export class ElectronMainApplication { } } - protected async launch(params: ElectronMainExecutionParams): Promise { - 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. * @@ -422,15 +419,15 @@ export class ElectronMainApplication { app.quit(); } - protected async handleMainCommand(params: ElectronMainExecutionParams, options: ElectronMainCommandOptions): Promise { - if (params.secondInstance === false) { + protected async handleMainCommand(options: ElectronMainCommandOptions): Promise { + 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.`); } @@ -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; From 25cf41218a54edc18d765e2692ab5c529b134d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=A4der?= Date: Wed, 20 Dec 2023 10:53:01 +0100 Subject: [PATCH 2/3] Address review comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Mäder --- .../electron-main/electron-main-application.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/core/src/electron-main/electron-main-application.ts b/packages/core/src/electron-main/electron-main-application.ts index 97c06729b3fea..4388b4632132f 100644 --- a/packages/core/src/electron-main/electron-main-application.ts +++ b/packages/core/src/electron-main/electron-main-application.ts @@ -55,6 +55,11 @@ export interface ElectronMainCommandOptions { readonly file?: string; readonly cwd: string; + + /** + * 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; } @@ -205,16 +210,16 @@ export class ElectronMainApplication { createYargs(argv, process.cwd()) .command('$0 [file]', false, cmd => cmd - .option('userDataArea', { + .option('electronUserData', { 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); + 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; From 930d02320f4b369b91c415596a5ae5cbe987b3ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=A4der?= Date: Wed, 20 Dec 2023 11:18:32 +0100 Subject: [PATCH 3/3] Make the linter happy. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Mäder --- packages/core/src/electron-main/electron-main-application.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/electron-main/electron-main-application.ts b/packages/core/src/electron-main/electron-main-application.ts index 4388b4632132f..645048d78012c 100644 --- a/packages/core/src/electron-main/electron-main-application.ts +++ b/packages/core/src/electron-main/electron-main-application.ts @@ -57,7 +57,7 @@ export interface ElectronMainCommandOptions { readonly cwd: string; /** - * If the app is launched for the first time, `secondInstance` is false. + * 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;