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

use globalStorageUri for user settings #1295

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions src/core/extension-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {extensions, workspace, window, Uri, ExtensionContext} from 'vscode';
import {extensions, workspace, window, Uri, ExtensionContext, FileSystemError} from 'vscode';
import {posix} from 'path';
import {CONFIG_FILE_NAME, USER_CONFIG_FILE_NAME, MATERIAL_THEME_EXT_ID} from '../env';

Expand Down Expand Up @@ -44,7 +44,12 @@ class ExtensionManager implements IExtensionManager {
constructor() {
const extensionFolderUri = Uri.file(extensions.getExtension(MATERIAL_THEME_EXT_ID).extensionPath);
this.configFileUri = extensionFolderUri.with({path: posix.join(extensionFolderUri.path, CONFIG_FILE_NAME)});
this.userConfigFileUri = extensionFolderUri.with({path: posix.join(extensionFolderUri.path, USER_CONFIG_FILE_NAME)});
this.userConfigFileUri = extensionFolderUri.with({
path: this.migrateFileLocation(
posix.join(extensionFolderUri.path, USER_CONFIG_FILE_NAME),
posix.join(ExtensionContext.globalStorageUri.fsPath, USER_CONFIG_FILE_NAME)
)
});
}

getPackageJSON(): PackageJSON {
Expand Down Expand Up @@ -123,6 +128,22 @@ class ExtensionManager implements IExtensionManager {
return JSON.parse(configContent) as MaterialThemeConfig;
} catch {}
}

private async migrateFileLocation(oldLocation: string, newLocation: string): Promise<string> {
try {
// Check if the old location exists
await workspace.fs.stat(oldLocation);

await workspace.fs.rename(oldLocation, newLocation, { overwrite: true });
} catch (error) {
if (error instanceof FileSystemError) {
console.error('Error moving location:', error.message);
} else {
throw error;
}
}
return newLocation;
}
}

export const extensionManager = new ExtensionManager();