Skip to content

Commit

Permalink
core: enable portable-mode (#12690)
Browse files Browse the repository at this point in the history
Signed-off-by: Vlad Arama <vlad.arama@ericsson.com>
  • Loading branch information
vladarama authored Nov 17, 2023
1 parent 13a7708 commit bf93b29
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/core/src/electron-main/electron-main-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as path from 'path';
import { Argv } from 'yargs';
import { AddressInfo } from 'net';
import { promises as fs } from 'fs';
import { existsSync, mkdirSync } from 'fs-extra';
import { fork, ForkOptions } from 'child_process';
import { DefaultTheme, FrontendApplicationConfig } from '@theia/application-package/lib/application-props';
import URI from '../common/uri';
Expand Down Expand Up @@ -171,6 +172,8 @@ export class ElectronMainApplication {
@inject(TheiaElectronWindowFactory)
protected readonly windowFactory: TheiaElectronWindowFactory;

protected isPortable = this.makePortable();

protected readonly electronStore = new Storage<{
windowstate?: TheiaBrowserWindowOptions
}>();
Expand All @@ -194,6 +197,20 @@ export class ElectronMainApplication {
return this._config;
}

protected makePortable(): boolean {
const dataFolderPath = path.join(app.getAppPath(), 'data');
const appDataPath = path.join(dataFolderPath, 'app-data');
if (existsSync(dataFolderPath)) {
if (!existsSync(appDataPath)) {
mkdirSync(appDataPath);
}
app.setPath('userData', appDataPath);
return true;
} else {
return false;
}
}

async start(config: FrontendApplicationConfig): Promise<void> {
this.useNativeWindowFrame = this.getTitleBarStyle(config) === 'native';
this._config = config;
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/node/env-variables/env-variables-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { join } from 'path';
import { homedir } from 'os';
import { injectable } from 'inversify';
import * as drivelist from 'drivelist';
import { pathExists, mkdir } from 'fs-extra';
import { EnvVariable, EnvVariablesServer } from '../../common/env-variables';
import { isWindows } from '../../common/os';
import { FileUri } from '../file-uri';
Expand All @@ -28,6 +29,7 @@ export class EnvVariablesServerImpl implements EnvVariablesServer {
protected readonly envs: { [key: string]: EnvVariable } = {};
protected readonly homeDirUri = FileUri.create(homedir()).toString();
protected readonly configDirUri: Promise<string>;
protected readonly pathExistenceCache: { [key: string]: boolean } = {};

constructor() {
this.configDirUri = this.createConfigDirUri();
Expand All @@ -43,7 +45,25 @@ export class EnvVariablesServerImpl implements EnvVariablesServer {
}

protected async createConfigDirUri(): Promise<string> {
return FileUri.create(process.env.THEIA_CONFIG_DIR || join(homedir(), '.theia')).toString();
let dataFolderPath: string = '';
if (process.env.THEIA_APP_PROJECT_PATH) {
dataFolderPath = join(process.env.THEIA_APP_PROJECT_PATH, 'data');
}
const userDataPath = join(dataFolderPath, 'user-data');
const dataFolderExists = this.pathExistenceCache[dataFolderPath] ??= await pathExists(dataFolderPath);
if (dataFolderExists) {
const userDataExists = this.pathExistenceCache[userDataPath] ??= await pathExists(userDataPath);
if (userDataExists) {
process.env.THEIA_CONFIG_DIR = userDataPath;
} else {
await mkdir(userDataPath);
process.env.THEIA_CONFIG_DIR = userDataPath;
this.pathExistenceCache[userDataPath] = true;
}
} else {
process.env.THEIA_CONFIG_DIR = join(homedir(), '.theia');
}
return FileUri.create(process.env.THEIA_CONFIG_DIR).toString();
}

async getExecPath(): Promise<string> {
Expand Down

0 comments on commit bf93b29

Please sign in to comment.