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