refactored nodejs client
This commit is contained in:
		
							parent
							
								
									283db4ae6e
								
							
						
					
					
						commit
						821b9ca3e5
					
				| 
						 | 
					@ -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];
 | 
					 | 
				
			||||||
	if (currentnote)
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		// todo: use title instead? To put in data folder?
 | 
					 | 
				
			||||||
	fs.writeFileSync("note.md", currentnote.content);
 | 
						fs.writeFileSync("note.md", currentnote.content);
 | 
				
			||||||
 | 
					 | 
				
			||||||
	cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr)
 | 
						cp.exec(`${settings.command} note.md`, async function (err, stdout, stderr)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		clearInterval(intervalid);
 | 
							clearInterval(intervalid);
 | 
				
			||||||
 | 
							intervalid = null;
 | 
				
			||||||
		saveifneeded();
 | 
							saveifneeded();
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
	intervalid = setInterval(saveifneeded, 10000);
 | 
						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)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
							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();
 | 
								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 = {
 | 
									currentnote = {
 | 
				
			||||||
					title: title,
 | 
										title: title,
 | 
				
			||||||
					content: ""
 | 
										content: ""
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				notes.unshift(currentnote);
 | 
									notes.unshift(currentnote);
 | 
				
			||||||
		console.log("Creating new note: " + title);
 | 
									console.log(`Creating new note: ${title}`);
 | 
				
			||||||
		editnote(0);
 | 
									editnote();	
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	else
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		if (process.argv.length > 2)
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			filter = process.argv[2];
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		var matchcount = filteredlist().length;
 | 
							case "list":
 | 
				
			||||||
		if (matchcount == 1)
 | 
								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:");
 | 
									arg = process.argv[3];
 | 
				
			||||||
			filteredlist()
 | 
								}
 | 
				
			||||||
			.every( (note, i) =>
 | 
								if (isNaN(parseInt(arg)))
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				console.log(`[${i}] ${note.title}`)
 | 
									currentnote = notes.find(n => n.title == arg);
 | 
				
			||||||
				return Boolean(filter) || i < settings.maxcountifnofilter;
 | 
					 | 
				
			||||||
			});
 | 
					 | 
				
			||||||
			rl.prompt();
 | 
					 | 
				
			||||||
			rl.on("line", async function (line)
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				if (line == "q")
 | 
					 | 
				
			||||||
				{
 | 
					 | 
				
			||||||
					rl.close();
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			else
 | 
								else
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
					editnote(line);
 | 
									currentnote = notes[parseInt(arg)];
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			});
 | 
								if (currentnote)
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									console.log(`Editing ${currentnote.title}`);
 | 
				
			||||||
 | 
									editnote();
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								else
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									console.log(`Note ${arg} not found`);
 | 
				
			||||||
 | 
								}		
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue