From 6b2c9151f1e687025c6793b47ffda600d2fc089e Mon Sep 17 00:00:00 2001 From: piotrruss Date: Thu, 5 May 2022 20:43:13 +0100 Subject: Login, config file, notes list --- _notes_cli.js | 167 ---------------------------------------------------------- 1 file changed, 167 deletions(-) delete mode 100755 _notes_cli.js (limited to '_notes_cli.js') diff --git a/_notes_cli.js b/_notes_cli.js deleted file mode 100755 index 904f493..0000000 --- a/_notes_cli.js +++ /dev/null @@ -1,167 +0,0 @@ -const fs = require('fs') -const { homedir } = require('os') - -let conf - -const https = require('https') -const readline = require("readline") -// const spawn = require('child_process').spawn - -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout -}) - -const cl = (m, c) => console.log(c ? `\x1b[${c}m${m}\x1b[0m` : m) -const cls = () => console.clear() - -const configPath = `${homedir}/.local/share/notes_cli` -console.log(configPath) - -const setConf = (c, callback) => { - fs.writeFile(configPath, JSON.stringify(c), function(err) { - if(err) { - cl(err, 32) - cl('Error writting configuration file', 32) - return null - } - - conf = c - return callback() - }) -} - -const getConf = () => { - fs.readFile(configPath, function(err, f){ - if (err) { - cl('Configuration file not found', 33) - return login() - } - - try { - const r = JSON.parse(f.toString()) - - console.log(r) - - if (!r.session || !r.userId || !r.email || !r.list) { - cl('Error parsing configuration', 31) - process.exit(1) - } - - conf = r - return console.log('here handle config') - - // - - } catch (e) { - cl('Error reading configuration', 31) - process.exit(1) - } - }) -} - -const post = (path, data, callback) => { - const dataString = JSON.stringify(data) - const options = { - hostname: 'apps.pruss.it', - port: 443, - path, - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Content-Length': dataString.length - }, - timeout: 1000 - }; - - const req = https.request(options, (res) => { - let data = ''; - - res.on('data', (chunk) => { - data += chunk; - }); - - res.on('end', () => { - return callback(JSON.parse(data), res.headers, res.statusCode); - }); - - }).on("error", () => { - callback(null) - }); - - req.write(dataString); - req.end(); -} - -const login = () => { - cl('\nLogin to My Apps\n', 32) - cl('+-------------------------------------------------------+\n'+ - '|Account can be created through apps.pruss.it website.|\n'+ - '|To quit type "q" and press enter. |\n'+ - '+-------------------------------------------------------+\n', 32) - rl.resume() - rl.question('Email: ', e => { - if (e === 'q') { - cl('\nExiting My Apps', 31) - rl.close() - process.exit(1) - } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)) { - cls() - cl('\nNot a valid email address, try again.', 31) - rl.pause() - return login() - } - - rl.stdoutMuted = true - - rl.question('Password: ', p => { - if (p === 'q') { - cl('\nExiting My Apps', 31) - rl.close() - process.exit(1) - } - - rl.pause() - - post( - '/api/login', - {"email": e, "password": p}, - (o, h, s) => { - if (!o || s !== 201) { - cl('Could not log in, try again', 31) - return login() - } - - const {_id, email, isVerified, noteList} = o - const session = h['set-cookie'] - - if (!_id || !email || !noteList) { - cl('Could not log in', 31) - process.exit(1) - } else if (!isVerified) { - cl('User not verified.\nPlease first verify the user using apps.pruss.it', 32) - return 1 - } - - const c = {userId: _id, email, list: noteList, session} - - setConf(c, () => { - cl('Successfully saved configuration', 32) - cl('saved config: ', conf) - }) - }) - }) - - rl._writeToOutput = function _writeToOutput(stringToWrite) { - if (rl.stdoutMuted) - rl.output.write("*") - else - rl.output.write(stringToWrite) - }; - - rl.history = rl.history.slice(1) - }) -} - -cls() -getConf() -- cgit v1.2.3