-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
97 lines (78 loc) · 2.43 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const vscode = require("vscode");
const fs = require("fs");
const {
window: { showErrorMessage }
} = vscode;
const NOTES_FORMAT = "md";
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
const CONFIG = vscode.workspace.getConfiguration("gkeep");
const defaultNotesRootDirectory = `${context.globalStoragePath}/notes`;
const notesRootDirectory =
CONFIG.notesRootDirectory || defaultNotesRootDirectory;
// Create the defaultNotesRootDirectory only once
fs.exists(context.globalStoragePath, exists => {
if (exists) {
return;
}
fs.mkdirSync(context.globalStoragePath);
fs.mkdirSync(defaultNotesRootDirectory);
});
let createNoteCommand = vscode.commands.registerCommand(
"gkeep.createNote",
async () => {
const rawTitle = await vscode.window.showInputBox({
placeHolder: "Enter a note title..."
});
const title = rawTitle.trim();
if (title === "") {
showErrorMessage("The note title cannot be empty");
return;
}
let newFilePath = `${notesRootDirectory}/${title}.${NOTES_FORMAT}`;
fs.open(newFilePath, "w", async error => {
if (error) {
showErrorMessage(`The file "${title}" couldn't be created: ${error}`);
return;
}
const doc = await vscode.workspace.openTextDocument(
vscode.Uri.file(newFilePath)
);
vscode.window.showTextDocument(doc);
});
}
);
let searchNote = vscode.commands.registerCommand(
"gkeep.searchNotes",
async () => {
const fileTuples = await vscode.workspace.fs.readDirectory(
vscode.Uri.file(notesRootDirectory)
);
const noteFiles = fileTuples
.filter(([_, type]) => type === vscode.FileType.File)
.map(([name, _]) => name)
.filter(name => name.endsWith(NOTES_FORMAT));
const selectedFileName = await vscode.window.showQuickPick(noteFiles, {
placeHolder: "Enter note title...",
canPickMany: false
});
if (!selectedFileName) {
return;
}
const doc = await vscode.workspace.openTextDocument(
vscode.Uri.file(`${notesRootDirectory}/${selectedFileName}`)
);
vscode.window.showTextDocument(doc);
}
);
context.subscriptions.push(createNoteCommand);
context.subscriptions.push(searchNote);
}
exports.activate = activate;
function deactivate() {}
module.exports = {
activate,
deactivate
};