#496 - Move to file storage for larger states

This commit is contained in:
Elio Struyf
2023-02-02 18:46:50 +01:00
parent 089f0a643d
commit 2b7f124582
6 changed files with 18731 additions and 18 deletions
+4 -4
View File
@@ -12,13 +12,13 @@
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "pnpm: build:ext"
"preLaunchTask": "npm: build:ext"
},
{
"name": "Attach Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
}
+10 -10
View File
@@ -15,6 +15,8 @@
### ⚡️ Optimizations
- [#496](https://github.com/estruyf/vscode-front-matter/issues/496): Make use of the `storageUri` and `globalStorageUri` for storing larger states
### 🐞 Fixes
- [#469](https://github.com/estruyf/vscode-front-matter/issues/469): Fix for using the root folder as content folder
@@ -22,7 +24,7 @@
- [#482](https://github.com/estruyf/vscode-front-matter/issues/482): Update the description when you want to overwrite the default content type description
- [#493](https://github.com/estruyf/vscode-front-matter/issues/493): Fix an issue where a custom placeholder value is replaced by an `array` instead of a `string`
## [8.2.0] - 2022-12-08 - [Release notes](https://beta.frontmatter.codes/updates/v8.2.0)
## [8.2.0] - 2022-12-08 - [Release notes](https://beta.frontmatter.codes/updates/v8.2.0)
### ✨ New features
@@ -69,7 +71,7 @@
- [#422](https://github.com/estruyf/vscode-front-matter/issues/422): Fix in panel initialization logic
## [8.1.0] - 2022-09-22 - [Release notes](https://beta.frontmatter.codes/updates/v8.1.0)
## [8.1.0] - 2022-09-22 - [Release notes](https://beta.frontmatter.codes/updates/v8.1.0)
### ✨ New features
@@ -121,7 +123,7 @@
- Fix missing clipboard icon for the media card action
- Fix in tags rendering on content cards
## [8.0.0] - 2022-07-11 - [Release notes](https://beta.frontmatter.codes/updates/v8.0.0)
## [8.0.0] - 2022-07-11 - [Release notes](https://beta.frontmatter.codes/updates/v8.0.0)
### ✨ New Features
@@ -165,7 +167,6 @@
- [#346](https://github.com/estruyf/vscode-front-matter/issues/346): Fix media dashboard refresh action
## [7.3.1] - 2022-05-26
### 🐞 Fixes
@@ -192,7 +193,6 @@
- [#334](https://github.com/estruyf/vscode-front-matter/issues/334): Fix for locked content folders retrieval
- [#339](https://github.com/estruyf/vscode-front-matter/issues/339): Fix for content folders without a title
## [7.2.0] - 2022-05-02 - [Release notes](https://beta.frontmatter.codes/updates/v7.2.0)
### 🎨 Enhancements
@@ -234,7 +234,7 @@
## [7.1.0] - 2022-04-07 - [Release notes](https://beta.frontmatter.codes/updates/v7.1.0)
### 🎨 Enhancements
- [#240](https://github.com/estruyf/vscode-front-matter/issues/240): Capability added to define display modes
- [#246](https://github.com/estruyf/vscode-front-matter/issues/246): Support to add multiple tags/keywords/taxonomy via comma separated values
- [#293](https://github.com/estruyf/vscode-front-matter/issues/293): Support added for setting preview images in block fields
@@ -254,7 +254,7 @@
- [#304](https://github.com/estruyf/vscode-front-matter/issues/304): Fix yaml stringify which caused additional fields to be added
- [#305](https://github.com/estruyf/vscode-front-matter/issues/305): Fix for overflow issue in taxonomy picker
- [#306](https://github.com/estruyf/vscode-front-matter/issues/306): Fix for default value of content type fields
- [#311](https://github.com/estruyf/vscode-front-matter/issues/311): Fix for updating snippets
- [#311](https://github.com/estruyf/vscode-front-matter/issues/311): Fix for updating snippets
## [7.0.0] - 2022-03-21 - [Release notes](https://beta.frontmatter.codes/updates/v7.0.0)
@@ -430,7 +430,7 @@ As from this version onwards, the extension will be published to [open-vsx.org](
### 🎨 Enhancements
- [#173](https://github.com/estruyf/vscode-front-matter/issues/173): Allow to specify your own sorting for the content dashboard
- [#174](https://github.com/estruyf/vscode-front-matter/issues/174): Added option to exclude sub-directories from page/markdown content retrieval
- [#174](https://github.com/estruyf/vscode-front-matter/issues/174): Added option to exclude sub-directories from page/markdown content retrieval
## [5.4.0] - 2021-11-05
@@ -576,10 +576,10 @@ As from this version onwards, the extension will be published to [open-vsx.org](
- Fix typo in the `package.json` file for the preview command
## [2.5.0] - 2020-08-19
## [2.5.0] - 2020-08-19
- Moved the center layout button to the other actions section
- [#60](https://github.com/estruyf/vscode-front-matter/issues/60): Added the ability to open a site preview in VS Code
- [#60](https://github.com/estruyf/vscode-front-matter/issues/60): Added the ability to open a site preview in VS Code
## [2.4.1] - 2020-08-16
+18608
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -23,9 +23,9 @@ export class Cache {
private static async clear() {
const ext = Extension.getInstance();
await ext.setState(ExtensionState.Dashboard.Pages.Cache, undefined, 'workspace');
await ext.setState(ExtensionState.Dashboard.Pages.Index, undefined, 'workspace');
await ext.setState(ExtensionState.Settings.Extends, undefined, 'workspace');
await ext.setState(ExtensionState.Dashboard.Pages.Cache, undefined, 'workspace', true);
await ext.setState(ExtensionState.Dashboard.Pages.Index, undefined, 'workspace', true);
await ext.setState(ExtensionState.Settings.Extends, undefined, 'workspace', true);
Notifications.info('Cache cleared');
}
+2
View File
@@ -368,6 +368,8 @@ export async function activate(context: vscode.ExtensionContext) {
collapseAll,
createFolder
);
console.log(`FRONT MATTER CMS activated!`)
}
export function deactivate() {
+104 -1
View File
@@ -302,8 +302,31 @@ export class Extension {
public async setState<T>(
propKey: string,
propValue: T,
type: 'workspace' | 'global' = 'global'
type: 'workspace' | 'global' = 'global',
setState: boolean = false
): Promise<void> {
if (this.isFileStorageNeeded(propKey)) {
let storageUri: Uri | undefined = undefined;
if (type === 'global') {
storageUri = await this.createGlobalStorageIfNotExists();
} else {
storageUri = await this.createLocalStorageIfNotExists();
}
if (storageUri) {
const workspaceData = Uri.joinPath(
storageUri,
`${this.formatStorageFileName(propKey)}.json`
);
const writeData = new TextEncoder().encode(JSON.stringify(propValue));
await workspace.fs.writeFile(workspaceData, writeData);
if (!setState) {
return;
}
}
}
if (type === 'global') {
await this.ctx.globalState.update(propKey, propValue);
} else {
@@ -315,6 +338,32 @@ export class Extension {
propKey: string,
type: 'workspace' | 'global' = 'global'
): Promise<T | undefined> {
if (this.isFileStorageNeeded(propKey)) {
let storageUri: Uri | undefined = undefined;
if (type === 'global') {
storageUri = await this.createGlobalStorageIfNotExists();
} else {
storageUri = await this.createLocalStorageIfNotExists();
}
if (storageUri) {
try {
const workspaceData = Uri.joinPath(
storageUri,
`${this.formatStorageFileName(propKey)}.json`
);
const fileData = await workspace.fs.readFile(workspaceData);
if (fileData) {
const jsonData = new TextDecoder().decode(fileData);
return JSON.parse(jsonData);
}
} catch (e) {
// File doesn't exist, go and check if available on normal state
}
}
}
if (type === 'global') {
return await this.ctx.globalState.get(propKey);
} else {
@@ -351,4 +400,58 @@ export class Extension {
)!;
return frontMatter.packageJSON;
}
/**
* Checks if the storage key needs to be stored in a file
* @param propKey
* @returns
*/
private isFileStorageNeeded(propKey: string) {
return propKey === ExtensionState.Dashboard.Pages.Cache;
}
/**
* Replace special characters in the storage file name
* @param propKey
* @returns
*/
private formatStorageFileName(propKey: string) {
return propKey.replace(/:/g, '.');
}
/**
* Validates if the global storage path exists
*/
private async createGlobalStorageIfNotExists() {
if (!this.ctx.globalStorageUri) {
return;
}
try {
// When folder doesn't exist, and error gets thrown
await workspace.fs.stat(this.ctx.globalStorageUri);
} catch {
await workspace.fs.createDirectory(this.ctx.globalStorageUri);
}
return this.ctx.globalStorageUri;
}
/**
* Validates if the storage path exists
*/
private async createLocalStorageIfNotExists() {
if (!this.ctx.storageUri) {
return;
}
try {
// When folder doesn't exist, and error gets thrown
await workspace.fs.stat(this.ctx.storageUri);
} catch {
await workspace.fs.createDirectory(this.ctx.storageUri);
}
return this.ctx.storageUri;
}
}