diff --git a/extension.js b/extension.js index 6a9bbda..7242506 100644 --- a/extension.js +++ b/extension.js @@ -27,53 +27,100 @@ function activate(context) { context.subscriptions.push(vscode.commands.registerTextEditorCommand('extension.nextNoteOpenCurrentWeek', function () { - var basedir = process.env["NOTEDIR"]; - - 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); + var basedir = getBasedir(); } catch(e) { - try { - fs.mkdirSync(basedir); - } catch(e) { - vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); - return - } + vscode.window.showErrorMessage("next-note: failed to make directory: "+basedir); + return; } var notepath = basedir +"/"+ currentWeekFilename(); - fs.access(notepath, fs.constants.R_OK | fs.constants.W_OK, (err) => { - if (err) { - try { - fs.writeFileSync(notepath, dateLine()); - } catch (e) { - vscode.window.showErrorMessage("next-note: failed to create "+ notepath); - console.error(e); - return; - } - } - }); - vscode.workspace.openTextDocument(notepath).then(doc => { - vscode.window.showTextDocument(doc); - vscode.window.activeTextEditor.selection.active.line += 1; - }); + + openNote(notepath); })); } 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 + * @return {undefined} + */ +function openNote(filepath) { + fs.access(filepath, fs.constants.R_OK | fs.constants.W_OK, (err) => { + if (err) { + try { + fs.writeFileSync(filepath, dateLine()); + } 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["NOTEDIR"]; + + 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 + * + * @returns {String} (i.e. "Tasks-20170219-20170225.md") + */ function currentWeekFilename() { return "Tasks-"+currentWeek()+".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(); @@ -82,10 +129,4 @@ function currentWeek() { return dateFormat(beginDate, "yyyymmdd")+"-"+dateFormat(d,"yyyymmdd"); //return String(beginDate.getFullYear())+String(beginDate.getMonth())+String(beginDate.getDate()) +"-"+ String(d.getFullYear())+String(d.getMonth())+String(d.getDate()); -} - -// this method is called when your extension is deactivated -function deactivate() { - console.log("next-note: deactivated") -} -exports.deactivate = deactivate; \ No newline at end of file +} \ No newline at end of file diff --git a/package.json b/package.json index 3a1698b..45890cc 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "next-note", "displayName": "next-note", "description": "Helpers for daily notes", - "version": "0.0.1", + "version": "0.0.2", "publisher": "vbatts", "repository": { "type": "git",