node client: autosave every 10 seconds

This commit is contained in:
quenousimporte 2023-07-12 18:07:32 +02:00
parent 477365d56e
commit 961ec84588
1 changed files with 44 additions and 5 deletions

View File

@ -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();
});
});