Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
{
"command": "security-notes.setNoteStatusToDo",
"title": "Mark as To-Do"
},
{
"command": "security-notes.saveNotesToFile",
"title": "Security-Notes: Save Notes to Local Database"
}
],
"configuration": {
Expand Down Expand Up @@ -266,4 +270,4 @@
"rethinkdb": "^2.4.2",
"uuid": "^9.0.0"
}
}
}
14 changes: 11 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { Resource } from './reactions/resource';
import { ImportToolResultsWebview } from './webviews/importToolResultsWebview';
import { commentController } from './controllers/comments';
import { reactionHandler } from './handlers/reaction';
import { saveNotesToFileHandler } from './handlers/saveNotesToFile';
import {
getSetting,
saveNoteComment,
setNoteStatus,
syncNoteMapWithRemote,
} from './helpers';
import { RemoteDb } from './persistence/remote-db';
import { loadCommentsFromFile, saveCommentsToFile } from './persistence/local-db';
import { loadNotesFromFile, saveNotesToFile } from './persistence/local-db';

const noteMap = new Map<string, vscode.CommentThread>();
let remoteDb: RemoteDb | undefined;
Expand Down Expand Up @@ -53,6 +54,13 @@ export function activate(context: vscode.ExtensionContext) {
// reaction handler
commentController.reactionHandler = reactionHandler;

// save notes to file handler
context.subscriptions.push(
vscode.commands.registerCommand('security-notes.saveNotesToFile', () =>
saveNotesToFileHandler(noteMap),
),
);

// create note button
context.subscriptions.push(
vscode.commands.registerCommand(
Expand Down Expand Up @@ -222,7 +230,7 @@ export function activate(context: vscode.ExtensionContext) {
);

// load persisted comments from file
const persistedThreads = loadCommentsFromFile();
const persistedThreads = loadNotesFromFile();
persistedThreads.forEach((thread) => {
noteMap.set(thread.contextValue ? thread.contextValue : '', thread);
});
Expand All @@ -239,5 +247,5 @@ export function activate(context: vscode.ExtensionContext) {

export function deactivate(context: vscode.ExtensionContext) {
// persist comments in file
saveCommentsToFile(noteMap);
saveNotesToFile(noteMap);
}
7 changes: 7 additions & 0 deletions src/handlers/saveNotesToFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

import { CommentThread } from 'vscode';
import { saveNotesToFile } from '../persistence/local-db';

export const saveNotesToFileHandler = (noteMap: Map<string, CommentThread>) =>
saveNotesToFile(noteMap);
7 changes: 3 additions & 4 deletions src/persistence/local-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import * as fs from 'fs';
import { Serializer } from '../serialization/serializer';
import { Deserializer } from '../serialization/deserializer';
import { CommentThread } from 'vscode';
import { getSetting } from '../../helpers';
import { getLocalDbFilePath } from '../../utils';

const persistenceFile = getLocalDbFilePath();

export const saveCommentsToFile = (noteList: Map<string, CommentThread>) => {
fs.writeFileSync(persistenceFile, JSON.stringify(Serializer.serialize(noteList)));
export const saveNotesToFile = (noteMap: Map<string, CommentThread>) => {
fs.writeFileSync(persistenceFile, JSON.stringify(Serializer.serialize(noteMap)));
};

export const loadCommentsFromFile = (): CommentThread[] => {
export const loadNotesFromFile = (): CommentThread[] => {
// Check if persistence file exists and load comments
if (fs.existsSync(persistenceFile)) {
const jsonFile = fs.readFileSync(persistenceFile).toString();
Expand Down