diff --git a/tools/nodeclient/app.js b/tools/nodeclient/app.js index 5cca625..02a46bd 100644 --- a/tools/nodeclient/app.js +++ b/tools/nodeclient/app.js @@ -1,20 +1,14 @@ const axios = require("axios"); -const readline = require("readline"); const fs = require("fs"); const openpgp = require("openpgp"); var cp = require("child_process"); -var rl = readline.createInterface({ - input: process.stdin, - output: process.stdout -}); - var settings = JSON.parse(fs.readFileSync("settings.json", { encoding: "utf8", flag: "r" })); var pgpkey = fs.readFileSync("key.acs", { encoding: "utf8", flag: "r" }); -var filter = ""; var intervalid = null; var notes = null; var currentnote = null; +var command = null; function timestamp() { @@ -52,12 +46,6 @@ async function encrypt(str) encryptionKeys: publicKey }); } -function filteredlist() -{ - return notes - .filter(n => simplifystring(n.title).includes(simplifystring(filter))); -} - async function saveifneeded() { var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" }); @@ -85,34 +73,34 @@ async function saveifneeded() console.log("...done."); }); } - else + else if (!intervalid) { console.log("no change."); } } -function editnote(index) +function editnote() { - currentnote = filteredlist()[index]; - if (currentnote) + fs.writeFileSync("note.md", currentnote.content); + cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr) { - // todo: use title instead? To put in data folder? - fs.writeFileSync("note.md", currentnote.content); - - cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr) - { - clearInterval(intervalid); - saveifneeded(); - }); - intervalid = setInterval(saveifneeded, 10000); - } - else - { - console.log("No note found."); - } + clearInterval(intervalid); + intervalid = null; + saveifneeded(); + }); + intervalid = setInterval(saveifneeded, 10000); } // Run part +if (process.argv.length <= 2) +{ + command = "list"; +} +else +{ + command = process.argv[2]; +} + axios.post(`${settings.url}/handler.php`, { action: "fetch", @@ -128,50 +116,72 @@ axios.post(`${settings.url}/handler.php`, { notes = JSON.parse(await decrypt(res.data)); - if (process.argv.length > 2 && process.argv[2] === "new") + switch (command) { - var title = timestamp(); - currentnote = { - title: title, - content: "" - } - notes.unshift(currentnote); - console.log("Creating new note: " + title); - editnote(0); - } - else - { - if (process.argv.length > 2) - { - filter = process.argv[2]; - } + case "help": + case "-h": + case "--help": + var appcmd = "notes"; + console.log(`list notes: ${appcmd} [list]`); + console.log(`edit a note: ${appcmd} [open|edit]