1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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()
|