notes/tools/nodeclient/app.js

79 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-07-11 14:48:03 +02:00
const axios = require("axios");
const readline = require("readline");
const fs = require("fs");
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 filter = process.argv.length > 2 ? process.argv[2] : "";
2023-07-11 14:48:03 +02:00
axios.post(`${settings.url}/handler.php`,
{
action: "fetch",
password: settings.password
},
{
headers:
{
"Content-type": "application/x-www-form-urlencoded"
}
})
.then(res =>
{
var notes = res.data;
notes
.filter(n => n.title.includes(filter))
.every( (note, i) =>
2023-07-11 14:48:03 +02:00
{
console.log(`[${i}] ${note.title}`)
return i < settings.maxcount;
});
rl.prompt();
rl.on("line", (line) =>
{
var note = notes.filter(n => n.title.includes(filter))[line];
2023-07-11 14:48:03 +02:00
fs.writeFileSync("note.md", note.content);
2023-07-11 18:24:30 +02:00
cp.exec(`${settings.command} note.md`, function (err, stdout, stderr)
2023-07-11 14:48:03 +02:00
{
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);
2023-07-11 18:16:51 +02:00
console.log("sending data file to server...");
2023-07-11 14:48:03 +02:00
axios.post(`${settings.url}/handler.php`,
{
action: "push",
password: settings.password,
data: JSON.stringify(notes)
},
{
headers:
{
"Content-type": "application/x-www-form-urlencoded"
}
2023-07-11 18:16:51 +02:00
}).then(res => {
console.log("done.");
2023-07-11 14:48:03 +02:00
});
}
2023-07-11 18:16:51 +02:00
else
{
console.log("no change.");
}
2023-07-11 14:48:03 +02:00
})
rl.close();
});
});