-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #581 from mre/backup-system
Backup system
- Loading branch information
Showing
17 changed files
with
572 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { randomUUID } from "crypto"; | ||
import * as vscode from "vscode"; | ||
import { getConfig } from "./config"; | ||
import { formatUnixTime } from "./date"; | ||
import SnippetsStorage, { | ||
StorageOperation, | ||
TreeElement, | ||
} from "./snippetsStorage"; | ||
|
||
export interface Backup { | ||
id: string; | ||
dateUnix: number; | ||
elements: TreeElement[]; | ||
beforeOperation?: string; | ||
} | ||
|
||
export interface BackupItem extends vscode.QuickPickItem { | ||
item: Backup; | ||
} | ||
|
||
const STORAGE_KEY = "snippet.snippetBackupsStorageKey"; | ||
const MAX_BACKUPS = 10; | ||
|
||
export class BackupManager { | ||
private backups: Backup[] = []; | ||
private elementsBeforeRestore: TreeElement[] | null = null; | ||
|
||
constructor( | ||
private readonly context: vscode.ExtensionContext, | ||
private readonly snippets: SnippetsStorage | ||
) { | ||
this.load(); | ||
snippets.onBeforeSave = (elements, operation) => | ||
this.makeBackup(elements, operation); | ||
} | ||
|
||
getBackupItems(): BackupItem[] { | ||
const items = this.backups.map((backup) => { | ||
const time = `${formatUnixTime(backup.dateUnix)}`; | ||
const detail = backup.beforeOperation | ||
? `before "${backup.beforeOperation}"` | ||
: undefined; | ||
const description = `${this.snippets.getSnippetCount( | ||
backup.elements | ||
)} snippet${ | ||
this.snippets.getSnippetCount(backup.elements) === 1 ? "" : "s" | ||
}`; | ||
|
||
return { | ||
label: time, | ||
item: backup, | ||
description, | ||
detail, | ||
}; | ||
}); | ||
|
||
items.sort((a, b) => b.item.dateUnix - a.item.dateUnix); | ||
|
||
return items; | ||
} | ||
|
||
async restoreBackup(id: string) { | ||
const backup = this.backups.find((backup) => backup.id === id); | ||
|
||
if (!backup) { | ||
console.error(`Backup with id ${id} not found.`); | ||
return; | ||
} | ||
|
||
this.elementsBeforeRestore = this.snippets.getElements(); | ||
await this.snippets.replaceElements(backup.elements); | ||
} | ||
|
||
async undoLastRestore() { | ||
if (this.elementsBeforeRestore === null) { | ||
return; | ||
} | ||
|
||
await this.snippets.replaceElements(this.elementsBeforeRestore); | ||
this.elementsBeforeRestore = null; | ||
} | ||
|
||
private load(): void { | ||
this.backups = JSON.parse( | ||
this.context.globalState.get(STORAGE_KEY) || "[]" | ||
) as Backup[]; | ||
} | ||
|
||
private async makeBackup( | ||
elements: TreeElement[], | ||
operation?: StorageOperation | ||
) { | ||
if (!getConfig("saveBackups")) { | ||
return; | ||
} | ||
|
||
const backup: Backup = { | ||
id: randomUUID(), | ||
dateUnix: Date.now(), | ||
elements, | ||
beforeOperation: operation, | ||
}; | ||
|
||
this.backups.push(backup); | ||
|
||
if (this.backups.length > MAX_BACKUPS) { | ||
this.backups.shift(); | ||
} | ||
|
||
await this.save(); | ||
} | ||
|
||
private async save() { | ||
await this.context.globalState.update( | ||
STORAGE_KEY, | ||
JSON.stringify(this.backups) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function formatUnixTime(ms: number): string { | ||
const date = new Date(ms); | ||
return `${date.toDateString()}, ${date.toLocaleTimeString([], { | ||
hour: "2-digit", | ||
minute: "2-digit", | ||
})}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.