aboutsummaryrefslogtreecommitdiffstats
path: root/_notes_cli.js
diff options
context:
space:
mode:
Diffstat (limited to '_notes_cli.js')
-rwxr-xr-x_notes_cli.js167
1 files changed, 167 insertions, 0 deletions
diff --git a/_notes_cli.js b/_notes_cli.js
new file mode 100755
index 0000000..904f493
--- /dev/null
+++ b/_notes_cli.js
@@ -0,0 +1,167 @@
+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()