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

Allow user to specify the location of the json files for the extension #17

Closed
wants to merge 4 commits into from
Closed
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: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,14 @@
"type": "boolean",
"default": true,
"description": "Show file name under annotation"
},
"saveLocation": {
"type":"string",
"default": "",
"description": "Set a save location. Default is VSCode Global path"
}
}

}
},
"scripts": {
Expand Down
9 changes: 7 additions & 2 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const getAnnotationFilePath = (): string => {

export const initializeStorageLocation = (location: string) => {
if (location) {
storageLocation = location;
storageLocation = path.resolve(location);
console.log('Save location is: ' + storageLocation);
if (!fs.existsSync(storageLocation)) {
fs.mkdirSync(storageLocation, { recursive: true });
}
Expand All @@ -26,13 +27,17 @@ export const initializeStorageLocation = (location: string) => {

export interface Configuration {
showFileName: boolean;
saveLocation: string;
}

export const getConfiguration = (): Configuration => {
const configuration = vscode.workspace.getConfiguration();
const showFileName = configuration.get('showFileName');
const saveLocation = configuration.get('saveLocation');

const config: Configuration = {
showFileName: typeof showFileName === 'boolean' ? showFileName : false
showFileName: typeof showFileName === 'boolean' ? showFileName : false,
saveLocation: typeof saveLocation === 'string' ? saveLocation : ''
};

return config;
Expand Down
9 changes: 7 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import * as fs from 'fs';
import { addNote, addPlainNote } from './note-db';
import { generateMarkdownReport } from './reporting';
import { NotesTree, TreeActions } from './notes-tree';
import { initializeStorageLocation, getAnnotationFilePath } from './configuration';
import { initializeStorageLocation, getAnnotationFilePath, getConfiguration } from './configuration';

export function activate(context: vscode.ExtensionContext) {
console.log('Extension "code-annotation" is now active!');

initializeStorageLocation(context.globalStoragePath);
const configuration = getConfiguration();

// if path is '', use global.
const storagePath = !configuration.saveLocation ? context.globalStoragePath : configuration.saveLocation;

initializeStorageLocation(storagePath);
Comment on lines -12 to +17
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might lead to some errors in the future. A better approach would be to change to have a global variable in configuration.ts that holds the global storage path. That would be initialized on activate.
With that, getAnnotationFilePath will actually query the configurations and check the configuration set by the user (in a way you already doing here).
This way, all manipulation is done only on configuration.ts, and nowhere else.
It's possible that the actual creation of the path might need to be done on getAnnotationFilePath.

What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great shout!
I'll make the changes soon.


const tree = new NotesTree();
const treeActions = new TreeActions(tree);
Expand Down