|
1 |
| -import { join } from 'path'; |
2 |
| -import { homedir } from 'os'; |
3 |
| -import { injectable } from '@theia/core/shared/inversify'; |
4 |
| -import { FileUri } from '@theia/core/lib/node/file-uri'; |
| 1 | +import { |
| 2 | + EnvVariable, |
| 3 | + EnvVariablesServer as TheiaEnvVariablesServer, |
| 4 | +} from '@theia/core/lib/common/env-variables/env-variables-protocol'; |
| 5 | +import { isWindows } from '@theia/core/lib/common/os'; |
| 6 | +import URI from '@theia/core/lib/common/uri'; |
5 | 7 | import { BackendApplicationConfigProvider } from '@theia/core/lib/node/backend-application-config-provider';
|
6 |
| -import { EnvVariablesServerImpl as TheiaEnvVariablesServerImpl } from '@theia/core/lib/node/env-variables/env-variables-server'; |
| 8 | +import { FileUri } from '@theia/core/lib/node/file-uri'; |
| 9 | +import { |
| 10 | + inject, |
| 11 | + injectable, |
| 12 | + postConstruct, |
| 13 | +} from '@theia/core/shared/inversify'; |
| 14 | +import { list as listDrives } from 'drivelist'; |
| 15 | +import { homedir } from 'os'; |
| 16 | +import { join } from 'path'; |
| 17 | + |
| 18 | +@injectable() |
| 19 | +export class ConfigDirUriProvider { |
| 20 | + private uri: URI | undefined; |
| 21 | + |
| 22 | + configDirUri(): URI { |
| 23 | + if (!this.uri) { |
| 24 | + this.uri = FileUri.create( |
| 25 | + join(homedir(), BackendApplicationConfigProvider.get().configDirName) |
| 26 | + ); |
| 27 | + } |
| 28 | + return this.uri; |
| 29 | + } |
| 30 | +} |
7 | 31 |
|
| 32 | +// Copy-pasted from https://github.com/eclipse-theia/theia/blob/v1.31.1/packages/core/src/node/env-variables/env-variables-server.ts |
| 33 | +// to simplify the binding of the config directory location for tests. |
8 | 34 | @injectable()
|
9 |
| -export class EnvVariablesServer extends TheiaEnvVariablesServerImpl { |
10 |
| - protected override readonly configDirUri = Promise.resolve( |
11 |
| - FileUri.create( |
12 |
| - join(homedir(), BackendApplicationConfigProvider.get().configDirName) |
13 |
| - ).toString() |
14 |
| - ); |
| 35 | +export class EnvVariablesServer implements TheiaEnvVariablesServer { |
| 36 | + @inject(ConfigDirUriProvider) |
| 37 | + private readonly configDirUriProvider: ConfigDirUriProvider; |
| 38 | + |
| 39 | + private readonly envs: { [key: string]: EnvVariable } = {}; |
| 40 | + private readonly homeDirUri = FileUri.create(homedir()).toString(); |
| 41 | + |
| 42 | + constructor() { |
| 43 | + const prEnv = process.env; |
| 44 | + Object.keys(prEnv).forEach((key: string) => { |
| 45 | + let keyName = key; |
| 46 | + if (isWindows) { |
| 47 | + keyName = key.toLowerCase(); |
| 48 | + } |
| 49 | + this.envs[keyName] = { name: keyName, value: prEnv[key] }; |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + @postConstruct() |
| 54 | + protected init(): void { |
| 55 | + console.log( |
| 56 | + `Configuration directory URI: '${this.configDirUriProvider |
| 57 | + .configDirUri() |
| 58 | + .toString()}'` |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + async getExecPath(): Promise<string> { |
| 63 | + return process.execPath; |
| 64 | + } |
| 65 | + |
| 66 | + async getVariables(): Promise<EnvVariable[]> { |
| 67 | + return Object.keys(this.envs).map((key) => this.envs[key]); |
| 68 | + } |
| 69 | + |
| 70 | + async getValue(key: string): Promise<EnvVariable | undefined> { |
| 71 | + if (isWindows) { |
| 72 | + key = key.toLowerCase(); |
| 73 | + } |
| 74 | + return this.envs[key]; |
| 75 | + } |
| 76 | + |
| 77 | + async getConfigDirUri(): Promise<string> { |
| 78 | + return this.configDirUriProvider.configDirUri().toString(); |
| 79 | + } |
| 80 | + |
| 81 | + async getHomeDirUri(): Promise<string> { |
| 82 | + return this.homeDirUri; |
| 83 | + } |
| 84 | + |
| 85 | + async getDrives(): Promise<string[]> { |
| 86 | + const uris: string[] = []; |
| 87 | + const drives = await listDrives(); |
| 88 | + for (const drive of drives) { |
| 89 | + for (const mountpoint of drive.mountpoints) { |
| 90 | + if (this.filterHiddenPartitions(mountpoint.path)) { |
| 91 | + uris.push(FileUri.create(mountpoint.path).toString()); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return uris; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Filters hidden and system partitions. |
| 100 | + */ |
| 101 | + private filterHiddenPartitions(path: string): boolean { |
| 102 | + // OS X: This is your sleep-image. When your Mac goes to sleep it writes the contents of its memory to the hard disk. (https://bit.ly/2R6cztl) |
| 103 | + if (path === '/private/var/vm') { |
| 104 | + return false; |
| 105 | + } |
| 106 | + // Ubuntu: This system partition is simply the boot partition created when the computers mother board runs UEFI rather than BIOS. (https://bit.ly/2N5duHr) |
| 107 | + if (path === '/boot/efi') { |
| 108 | + return false; |
| 109 | + } |
| 110 | + return true; |
| 111 | + } |
15 | 112 | }
|
0 commit comments