Skip to content

Commit

Permalink
[plugin-ext] Fix undefined path on windows
Browse files Browse the repository at this point in the history
Some path returned by the filesystem was forced as non-undefined in the
code, while at runtime the value actually ended up undefined.

Hopefully improved filesystem access.

Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
  • Loading branch information
paul-marechal committed Jan 11, 2019
1 parent 26f677d commit 5da05ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
25 changes: 9 additions & 16 deletions packages/plugin-ext/src/main/node/paths/plugin-paths-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,11 @@ export class PluginPathsServiceImpl implements PluginPathsService {
const parentLogsDir = await this.getLogsDirPath();

if (!parentLogsDir) {
return Promise.reject(new Error('Unable to get parent log directory'));
}

if (parentLogsDir && !await this.fileSystem.exists(parentLogsDir)) {
await this.fileSystem.createFolder(parentLogsDir);
throw new Error('Unable to get parent log directory');
}

const pluginDirPath = path.join(parentLogsDir, this.gererateTimeFolderName(), 'host');
if (!await this.fileSystem.exists(pluginDirPath)) {
await this.fileSystem.createFolder(pluginDirPath);
}
await this.fileSystem.createFolder(pluginDirPath);

return new URI(pluginDirPath).path.toString();
}
Expand All @@ -67,16 +61,15 @@ export class PluginPathsServiceImpl implements PluginPathsService {
const parentStorageDir = await this.getWorkspaceStorageDirPath();

if (!parentStorageDir) {
return Promise.reject(new Error('Unable to get parent storage directory'));
throw new Error('Unable to get parent storage directory');
}

if (!workspace) {
if (!this.storagePathInitialized) {
this.deferredStoragePath.resolve(undefined);
this.storagePathInitialized = true;
}
this.cachedStoragePath = undefined;
return Promise.resolve(undefined);
return this.cachedStoragePath = undefined;
}

if (!await this.fileSystem.exists(parentStorageDir)) {
Expand All @@ -94,14 +87,13 @@ export class PluginPathsServiceImpl implements PluginPathsService {
this.deferredStoragePath.resolve(storagePathString);
this.storagePathInitialized = true;
}
this.cachedStoragePath = storagePathString;

return this.cachedStoragePath;
return this.cachedStoragePath = storagePathString;
}

async getLastStoragePath(): Promise<string | undefined> {
if (this.storagePathInitialized) {
return Promise.resolve(this.cachedStoragePath);
return this.cachedStoragePath;
} else {
return this.deferredStoragePath.promise;
}
Expand Down Expand Up @@ -157,9 +149,10 @@ export class PluginPathsServiceImpl implements PluginPathsService {
private async getUserHomeDir(): Promise<string> {
const homeDirStat = await this.fileSystem.getCurrentUserHome();
if (!homeDirStat) {
return Promise.reject(new Error('Unable to get user home directory'));
throw new Error('Unable to get user home directory');
}
return homeDirStat.uri;
const homeDirPath = await this.fileSystem.getFsPath(homeDirStat.uri);
return homeDirPath!;
}

}
46 changes: 15 additions & 31 deletions packages/plugin-ext/src/main/node/plugins-key-value-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,30 @@ export class PluginsKeyValueStorage {
private theiaDirPath: string | undefined;
private globalDataPath: string | undefined;

private deferredTheiaDirPath: Deferred<string>;
private deferredTheiaDirPath = new Deferred<string>();

constructor(
@inject(PluginPathsService) private readonly pluginPathsService: PluginPathsService,
@inject(FileSystem) protected readonly fileSystem: FileSystem
) {
this.deferredTheiaDirPath = new Deferred<string>();

this.pluginPathsService.getTheiaDirPath().then((theiaDirPathUri: string) =>
this.fileSystem.getFsPath(theiaDirPathUri)
).then((theiaDirPath: string | undefined) => {
this.theiaDirPath = theiaDirPath!;
this.globalDataPath = path.join(this.theiaDirPath, PluginPaths.PLUGINS_GLOBAL_STORAGE_DIR, 'global-state.json');

if (!fs.existsSync(path.dirname(this.globalDataPath))) {
this.createDirectories(path.dirname(this.globalDataPath));
}

this.deferredTheiaDirPath.resolve(this.theiaDirPath);
});
this.setupDirectories();
}

private createDirectories(pathString: string): void {
const toCreate: string[] = [];
private async setupDirectories() {
const theiaDirPath = await this.pluginPathsService.getTheiaDirPath();
await this.fileSystem.createFolder(theiaDirPath);
this.theiaDirPath = theiaDirPath;

while (!fs.existsSync(pathString)) {
toCreate.push(path.basename(pathString));
pathString = path.dirname(pathString);
}
this.globalDataPath = path.join(this.theiaDirPath, PluginPaths.PLUGINS_GLOBAL_STORAGE_DIR, 'global-state.json');
await this.fileSystem.createFolder(path.dirname(this.globalDataPath));

while (toCreate.length > 0) {
pathString = path.join(pathString, toCreate.pop()!);
fs.mkdirSync(pathString);
}
this.deferredTheiaDirPath.resolve(this.theiaDirPath);
}

async set(key: string, value: KeysToAnyValues, isGlobal: boolean): Promise<boolean> {
const dataPath = await this.getDataPath(isGlobal);
if (!dataPath) {
return Promise.reject('Cannot save data: no opened workspace');
throw new Error('Cannot save data: no opened workspace');
}

const data = this.readFromFile(dataPath);
Expand All @@ -79,27 +63,27 @@ export class PluginsKeyValueStorage {
}

this.writeToFile(dataPath, data);
return Promise.resolve(true);
return true;
}

async get(key: string, isGlobal: boolean): Promise<KeysToAnyValues> {
const dataPath = await this.getDataPath(isGlobal);
if (!dataPath) {
return Promise.resolve({});
return {};
}

const data = this.readFromFile(dataPath);
return Promise.resolve(data[key]);
return data[key];
}

async getAll(isGlobal: boolean): Promise<KeysToKeysToAnyValue> {
const dataPath = await this.getDataPath(isGlobal);
if (!dataPath) {
return Promise.resolve({});
return {};
}

const data = this.readFromFile(dataPath);
return Promise.resolve(data);
return data;
}

private async getDataPath(isGlobal: boolean): Promise<string | undefined> {
Expand Down

0 comments on commit 5da05ed

Please sign in to comment.