refactored nodejs client

This commit is contained in:
quenousimporte 2023-09-10 11:24:06 +02:00
parent 283db4ae6e
commit 821b9ca3e5
2 changed files with 81 additions and 72 deletions

View File

@ -1,20 +1,14 @@
const axios = require("axios"); const axios = require("axios");
const readline = require("readline");
const fs = require("fs"); const fs = require("fs");
const openpgp = require("openpgp"); const openpgp = require("openpgp");
var cp = require("child_process"); 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 settings = JSON.parse(fs.readFileSync("settings.json", { encoding: "utf8", flag: "r" }));
var pgpkey = fs.readFileSync("key.acs", { encoding: "utf8", flag: "r" }); var pgpkey = fs.readFileSync("key.acs", { encoding: "utf8", flag: "r" });
var filter = "";
var intervalid = null; var intervalid = null;
var notes = null; var notes = null;
var currentnote = null; var currentnote = null;
var command = null;
function timestamp() function timestamp()
{ {
@ -52,12 +46,6 @@ async function encrypt(str)
encryptionKeys: publicKey }); encryptionKeys: publicKey });
} }
function filteredlist()
{
return notes
.filter(n => simplifystring(n.title).includes(simplifystring(filter)));
}
async function saveifneeded() async function saveifneeded()
{ {
var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" }); var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" });
@ -85,34 +73,34 @@ async function saveifneeded()
console.log("...done."); console.log("...done.");
}); });
} }
else else if (!intervalid)
{ {
console.log("no change."); console.log("no change.");
} }
} }
function editnote(index) function editnote()
{ {
currentnote = filteredlist()[index]; fs.writeFileSync("note.md", currentnote.content);
if (currentnote) cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr)
{ {
// todo: use title instead? To put in data folder? clearInterval(intervalid);
fs.writeFileSync("note.md", currentnote.content); intervalid = null;
saveifneeded();
cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr) });
{ intervalid = setInterval(saveifneeded, 10000);
clearInterval(intervalid);
saveifneeded();
});
intervalid = setInterval(saveifneeded, 10000);
}
else
{
console.log("No note found.");
}
} }
// Run part // Run part
if (process.argv.length <= 2)
{
command = "list";
}
else
{
command = process.argv[2];
}
axios.post(`${settings.url}/handler.php`, axios.post(`${settings.url}/handler.php`,
{ {
action: "fetch", action: "fetch",
@ -128,50 +116,72 @@ axios.post(`${settings.url}/handler.php`,
{ {
notes = JSON.parse(await decrypt(res.data)); notes = JSON.parse(await decrypt(res.data));
if (process.argv.length > 2 && process.argv[2] === "new") switch (command)
{ {
var title = timestamp(); case "help":
currentnote = { case "-h":
title: title, case "--help":
content: "" var appcmd = "notes";
} console.log(`list notes: ${appcmd} [list]`);
notes.unshift(currentnote); console.log(`edit a note: ${appcmd} [open|edit] <title|index>`);
console.log("Creating new note: " + title); console.log(`create a note: ${appcmd} new|create|add [<title>]`);
editnote(0); console.log(`display help: ${appcmd} help|-h|--help`);
} break;
else
{
if (process.argv.length > 2)
{
filter = process.argv[2];
}
var matchcount = filteredlist().length; case "new":
if (matchcount == 1) case "create":
{ case "add":
editnote(0); var title = timestamp();
} if (process.argv.length > 3)
else if (matchcount > 1)
{
console.log("Select a note or type 'q' to quit:");
filteredlist()
.every( (note, i) =>
{ {
console.log(`[${i}] ${note.title}`) title = process.argv[3];
return Boolean(filter) || i < settings.maxcountifnofilter; }
}); if (notes.find(n => n.title == title))
rl.prompt();
rl.on("line", async function (line)
{ {
if (line == "q") console.log(`${title}: already exists`);
{ }
rl.close(); else
{
currentnote = {
title: title,
content: ""
} }
else notes.unshift(currentnote);
{ console.log(`Creating new note: ${title}`);
editnote(line); editnote();
} }
}); break;
}
case "list":
for (var i = notes.length - 1; i >= 0; i--)
{
console.log(`[${i}] ${notes[i].title}`);
}
break;
default:
var arg = command;
if (arg === "open" || arg === "edit")
{
arg = process.argv[3];
}
if (isNaN(parseInt(arg)))
{
currentnote = notes.find(n => n.title == arg);
}
else
{
currentnote = notes[parseInt(arg)];
}
if (currentnote)
{
console.log(`Editing ${currentnote.title}`);
editnote();
}
else
{
console.log(`Note ${arg} not found`);
}
break;
} }
}); });

View File

@ -1,6 +1,5 @@
{ {
"password": "", "password": "",
"url": "http://localhost:8000", "url": "http://localhost:8000",
"maxcountifnofilter": 10,
"command": "sublime_text.exe -w" "command": "sublime_text.exe -w"
} }