// The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below var vscode = require('vscode'); var fs = require('fs'); var dateFormat = require('dateformat'); // this method is called when your extension is activated // your extension is activated the very first time the command is executed function activate(context) { console.log('next-note: activated'); context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteFindTodos', function () { // this may not require being an open TetEditor, // because it'd be nice to open it in a new buffer // that has hyperlinks to each TODO line. var editor = vscode.window.activeTextEditor; var notePaths = []; fs.readdir(getBasedir(), {encoding: "utf8"}, (err, files) => { console.log(files); // TODO match on "Task*.md" and map to TODO lines }); })); context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteInsertDate', function () { var editor = vscode.window.activeTextEditor; if (!editor) { console.error("next-note: no active editor"); return; }; //editor.document.getWordRangeAtPosition var cursorEnd = editor.selection.end; if (editor.document.lineCount > cursorEnd.line+1) { cursorEnd.line += 1; } editor.edit(editBuilder => { editBuilder.insert(cursorEnd, dateLine()); }); })); context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenPreviousWeek', function () { try { var basedir = getBasedir(); } catch(e) { vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); return; } openNote(basedir +"/"+ formatWeekFilename(previousWeek())); })); context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenCurrentWeek', function () { try { var basedir = getBasedir(); } catch(e) { vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); return; } openNote(basedir +"/"+ formatWeekFilename(currentWeek()), true); })); context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenNextWeek', function () { try { var basedir = getBasedir(); } catch(e) { vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); return; } openNote(basedir +"/"+ formatWeekFilename(nextWeek())); })); } exports.activate = activate; // this method is called when your extension is deactivated function deactivate() { console.log("next-note: deactivated") } exports.deactivate = deactivate; /** * opens filepath in the current window, creating the file if needed * * @param {String} filepath is the path of the new note file * @param {Boolean} insertDate whether to insert the date stamp on creation * @return {undefined} */ function openNote(filepath, insertDate) { fs.access(filepath, fs.constants.R_OK | fs.constants.W_OK, (err) => { if (err) { try { if (insertDate) { fs.writeFileSync(filepath, dateLine()); } else { fs.writeFileSync(filepath, ""); } } catch (e) { vscode.window.showErrorMessage("next-note: failed to create "+ filepath); console.error(e); return; } } }); vscode.workspace.openTextDocument(filepath).then(doc => { vscode.window.showTextDocument(doc); vscode.window.activeTextEditor.selection.active.line += 1; }); } /** * return a best guess at the directory path to store notes in * * @returns {String} */ function getBasedir() { var basedir = process.env["NOTEDR"]; if (!basedir) { basedir = process.env["HOME"]+"/Notes"; console.log("next-note: no basedir. Using "+basedir); console.log("next-note: set NOTEDIR to use a diferent basedir"); } try { fs.statSync(basedir); } catch(e) { try { fs.mkdirSync(basedir); } catch(e) { throw(e); } } return basedir; } /** * get the markdown style heading for the current datetime stamp * * @returns {String} (i.e. `## ...`) */ function dateLine() { var d = new Date(); return "## "+ dateFormat(d,"ddd mmm dd HH:MM:ss Z yyyy") +"\n"; } /** * get the base file name of the current week * * @param {String} output of a function like `currentWeek()` * @returns {String} (i.e. "Tasks-20170219-20170225.md") */ function formatWeekFilename(week) { return "Tasks-"+week+".md"; } /** * the string of the current week's date, sunday-saturday * * @return {String} (i.e. "20170219-20170225") */ function currentWeek() { var d = new Date(); var beginDate = new Date(); beginDate.setDate(d.getDate()-d.getDay()); // This will be sunday of current current week d.setDate(beginDate.getDate()+6); // this is a week from that sunday return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd"); } /** * the string of the previous week's date, sunday-saturday * * @return {String} (i.e. "20170219-20170225") */ function previousWeek() { var d = new Date(); var beginDate = new Date(); beginDate.setDate(d.getDate()-d.getDay()-7); // This will be sunday of current previous week d.setDate(beginDate.getDate()+6); // this is a week from that sunday return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd"); } /** * the string of the next week's date, sunday-saturday * * @return {String} (i.e. "20170219-20170225") */ function nextWeek() { var d = new Date(); var beginDate = new Date(); beginDate.setDate(d.getDate()-d.getDay()+7); // This will be sunday of current next week d.setDate(beginDate.getDate()+6); // this is a week from that sunday return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd"); }