From 961ec84588852b0cadb034834e4928adec90e7f4 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Wed, 12 Jul 2023 18:07:32 +0200 Subject: [PATCH] node client: autosave every 10 seconds --- tools/nodeclient/app.js | 49 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/tools/nodeclient/app.js b/tools/nodeclient/app.js index a664167..c4312ba 100644 --- a/tools/nodeclient/app.js +++ b/tools/nodeclient/app.js @@ -10,6 +10,14 @@ var rl = readline.createInterface({ var settings = JSON.parse(fs.readFileSync("settings.json", { encoding: "utf8", flag: "r" })); var filter = process.argv.length > 2 ? process.argv[2] : ""; +var intervalid = null; +var notes = null; + +function filteredlist() +{ + return notes + .filter(n => n.title.toLowerCase().includes(filter.toLowerCase())); +} axios.post(`${settings.url}/handler.php`, { @@ -24,27 +32,28 @@ axios.post(`${settings.url}/handler.php`, }) .then(res => { - var notes = res.data; + notes = res.data; - notes - .filter(n => n.title.includes(filter)) + filteredlist() .every( (note, i) => { console.log(`[${i}] ${note.title}`) return i < settings.maxcount; }); + // todo: open if only one match. quit if no match rl.prompt(); rl.on("line", (line) => { - var note = notes.filter(n => n.title.includes(filter))[line]; + var note = filteredlist()[line]; + // todo: use title instead? To put in data folder? fs.writeFileSync("note.md", note.content); cp.exec(`${settings.command} note.md`, function (err, stdout, stderr) { + clearInterval(intervalid); var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" }); - if (note.content != newcontent) { note.content = newcontent; @@ -73,6 +82,36 @@ axios.post(`${settings.url}/handler.php`, console.log("no change."); } }) + + intervalid = setInterval(function() + { + //todo: refactor "save" + var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" }); + if (note.content != newcontent) + { + note.content = newcontent; + + notes.splice(notes.indexOf(note), 1); + notes.unshift(note); + + console.log("sending data file to server..."); + axios.post(`${settings.url}/handler.php`, + { + action: "push", + password: settings.password, + data: JSON.stringify(notes) + }, + { + headers: + { + "Content-type": "application/x-www-form-urlencoded" + } + }).then(res => { + console.log("done."); + }); + } + }, 10000); + rl.close(); }); });