Skip to content

Commit

Permalink
Provide safer logic when dealing with restore process.
Browse files Browse the repository at this point in the history
  • Loading branch information
tahabasri committed Jul 27, 2021
1 parent b2d8370 commit 116a23e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "snippets",
"displayName": "Snippets",
"description": "Manage your code snippets without quitting your editor.",
"version": "2.0.1",
"preview": true,
"version": "2.0.2",
"preview": false,
"license": "SEE LICENSE IN LICENSE.txt",
"publisher": "tahabasri",
"author": {
Expand Down
2 changes: 2 additions & 0 deletions src/config/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ export const enum Labels {
snippetsInvalidNewPath = "Snippets path [{0}] is not a valid JSON file, will revert back to old location [{1}].",
snippetsNoNewPath = "Snippets will be saved to old location [{0}].",

snippetsBackupRequest = "Please keep a copy of your snippets file before proceeding with the restore. Yours is located in [{0}]",
snippetsMigrateRequest = "You have some old snippets saved in [{0}], do you want to restore them ? (Original file will be saved in case of error).",
snippetsDataRestored = "Snippets restored. Kept original file as [{0}].",
snippetsDataNotRestored = "Snippets were not restored. We kept original file in [{0}]. Please reload window and try again !",
snippetsDataRestoredButFileNotRenamed = "Snippets restored. But couldn't rename file [{0}], please rename it manually.",
snippetsNoDataRestored = "No data was provided from file to restore.",

Expand Down
33 changes: 21 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export function activate(context: vscode.ExtensionContext) {
if(dataAccess.hasNoChild() && !noData) {
const migrateData = Labels.migrateData;
const discardData = Labels.discardData;
vscode.window.showWarningMessage(
StringUtility.formatString(Labels.snippetsBackupRequest, oldSnippetsPath)
);
vscode.window.showInformationMessage(
StringUtility.formatString(Labels.snippetsMigrateRequest, oldSnippetsPath),
...[migrateData, discardData])
Expand All @@ -72,18 +75,24 @@ export function activate(context: vscode.ExtensionContext) {
newSnippets.lastId = oldSnippets.lastId;
dataAccess.save(newSnippets);
snippetsProvider.sync();
fs.rename(oldSnippetsPath, `${oldSnippetsPath}_bak`, (err) => {
if (err) {
vscode.window.showInformationMessage(
StringUtility.formatString(Labels.snippetsDataRestoredButFileNotRenamed, `${oldSnippetsPath}_bak`)
);
}else {
//file removed
vscode.window.showInformationMessage(
StringUtility.formatString(Labels.snippetsDataRestored, `${oldSnippetsPath}_bak`)
);
}
});
if (dataAccess.hasNoChild() || !newSnippets.children || newSnippets.children.length !== oldSnippets.children.length) {
vscode.window.showErrorMessage(
StringUtility.formatString(Labels.snippetsDataNotRestored, oldSnippetsPath)
);
}else {
fs.rename(oldSnippetsPath, `${oldSnippetsPath}_bak`, (err) => {
if (err) {
vscode.window.showInformationMessage(
StringUtility.formatString(Labels.snippetsDataRestoredButFileNotRenamed, `${oldSnippetsPath}_bak`)
);
}else {
//file removed
vscode.window.showInformationMessage(
StringUtility.formatString(Labels.snippetsDataRestored, `${oldSnippetsPath}_bak`)
);
}
});
}
}else{
vscode.window.showInformationMessage(Labels.snippetsNoDataRestored);
}
Expand Down

0 comments on commit 116a23e

Please sign in to comment.