aboutsummaryrefslogtreecommitdiffstats
path: root/_notes_cli.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2022-05-05 20:43:13 +0100
committerGravatar piotrruss <mail@pruss.it> 2022-05-05 20:43:13 +0100
commit6b2c9151f1e687025c6793b47ffda600d2fc089e (patch)
tree0933e94796b219ab00d07464d7442fa62adbe0af /_notes_cli.js
parent094349e061cb9b094ddba330322c7eace55a6393 (diff)
downloadnotes_cli-6b2c9151f1e687025c6793b47ffda600d2fc089e.tar.gz
notes_cli-6b2c9151f1e687025c6793b47ffda600d2fc089e.tar.bz2
notes_cli-6b2c9151f1e687025c6793b47ffda600d2fc089e.zip
Login, config file, notes list
Diffstat (limited to '_notes_cli.js')
-rwxr-xr-x_notes_cli.js167
1 files changed, 0 insertions, 167 deletions
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()