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 settings = JSON.parse(fs.readFileSync("settings.json", { encoding: "utf8", flag: "r" }));
var filter = process.argv.length > 2 ? process.argv[2] : ""; 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`, axios.post(`${settings.url}/handler.php`,
{ {
@ -24,27 +32,28 @@ axios.post(`${settings.url}/handler.php`,
}) })
.then(res => .then(res =>
{ {
var notes = res.data; notes = res.data;
notes filteredlist()
.filter(n => n.title.includes(filter))
.every( (note, i) => .every( (note, i) =>
{ {
console.log(`[${i}] ${note.title}`) console.log(`[${i}] ${note.title}`)
return i < settings.maxcount; return i < settings.maxcount;
}); });
// todo: open if only one match. quit if no match
rl.prompt(); rl.prompt();
rl.on("line", (line) => 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); fs.writeFileSync("note.md", note.content);
cp.exec(`${settings.command} note.md`, function (err, stdout, stderr) cp.exec(`${settings.command} note.md`, function (err, stdout, stderr)
{ {
clearInterval(intervalid);
var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" }); var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" });
if (note.content != newcontent) if (note.content != newcontent)
{ {
note.content = newcontent; note.content = newcontent;
@ -73,6 +82,36 @@ axios.post(`${settings.url}/handler.php`,
console.log("no change."); 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(); rl.close();
}); });
}); });