umpordez 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/cli.mjs +95 -55
  2. package/package.json +4 -1
package/cli.mjs CHANGED
@@ -2,7 +2,8 @@ import inquirer from 'inquirer';
2
2
  import chalk from 'chalk';
3
3
  import figlet from 'figlet';
4
4
 
5
- // everything that has a beginning has an end
5
+ import fs from 'node:fs';
6
+
6
7
  console.log(chalk.green(figlet.textSync('umpordez')));
7
8
  console.log('');
8
9
 
@@ -10,58 +11,92 @@ function sample(arr) {
10
11
  return arr[Math.floor(Math.random() * arr.length)];
11
12
  }
12
13
 
14
+ async function saveUser(user) {
15
+ await fs.promises.writeFile('_user.json', JSON.stringify(user));
16
+
17
+ }
18
+
13
19
  async function getUser() {
14
- // try get from process.env
15
- // try get from json file
20
+ try {
21
+ const data = await fs.promises.readFile('_user.json');
22
+ return JSON.parse(data);
23
+ } catch (ex) {
24
+ console.error(ex);
25
+ }
16
26
  }
17
27
 
18
- const user = await getUser();
28
+ let user = await getUser();
19
29
 
20
- if (user) {
30
+ if (user?.id) {
21
31
  console.log(`Logged in as /${user.login} \o/`);
22
32
  console.log('');
23
33
  }
24
34
 
25
- const choices = {
26
- 'I want to authenticate myself': async () => {
27
- await inquirer.prompt([
28
- {
29
- type: 'password',
30
- name: 'key',
31
- message: `So, please tell me... what's your key?`
32
- }
33
- ]);
34
-
35
- console.log('');
36
- console.log(chalk.green('Cool!! 😎 trying to login'));
37
- console.log(chalk.green('...'));
38
- console.log(chalk.green('You are in! /Deividy ;)'));
39
- console.log('');
40
-
41
- const opt = await inquirer.prompt([
42
- {
43
- type: 'rawlist',
44
- name: 'task',
45
- message: `What's next?`,
46
- choices: Object.keys(choices).slice(1)
35
+ const API_URL = `http://localhost:9000/rest`;
36
+
37
+ async function tryAuthenticate(token) {
38
+ const res = await fetch(API_URL, {
39
+ headers: {
40
+ Authorization: token
41
+ }
42
+ });
43
+
44
+ return res.json();
45
+ }
46
+
47
+ function getChoices() {
48
+ const choices = {
49
+ 'I want to authenticate myself': async () => {
50
+ const res = await inquirer.prompt([
51
+ {
52
+ type: 'password',
53
+ name: 'key',
54
+ message: `So, please tell me... what's your key?`
55
+ }
56
+ ]);
57
+
58
+ await saveUser({ id: null, token: res.key });
59
+
60
+ console.log('');
61
+ console.log(chalk.green('Cool!! 😎 trying to login'));
62
+ console.log(chalk.green('...'));
63
+
64
+ const authUser = await tryAuthenticate(res.key);
65
+
66
+ if (!authUser || !authUser.id) {
67
+ console.error('OH NO, invalid credentials or something with your network dude');
68
+ return promptMain();
47
69
  }
48
- ]);
49
70
 
50
- console.log('');
51
- console.log('You have choose:');
52
- console.log(chalk.cyan(opt.task));
53
- console.log('');
71
+ await saveUser({ ...authUser, token: res.key });
72
+
73
+ user = authUser;
74
+ console.log(`Hello hello ${user.login}!!!`);
75
+
76
+ return promptMain();
77
+ },
78
+ 'I want to logout': async () => {
79
+ await saveUser({});
80
+ user = {};
81
+
82
+ console.log(chalk.green('ciao'));
83
+ return promptMain();
84
+ },
85
+ 'I want to leave': () => {
86
+ console.log(chalk.green('So, my friend...'));
87
+ console.log(chalk.green('farewell o/'));
88
+ },
89
+ };
90
+
91
+ if (user?.id) {
92
+ delete choices['I want to authenticate myself'];
93
+ } else {
94
+ delete choices['I want to logout'];
95
+ }
54
96
 
55
- await choices[opt.task]();
56
- },
57
- 'I want to logout': () => {
97
+ return choices;
98
+ }
58
99
 
59
- },
60
- 'I want to leave': () => {
61
- console.log(chalk.green('So, my friend...'));
62
- console.log(chalk.green('farewell o/'));
63
- },
64
- };
65
100
 
66
101
  const welcomeMessages = [
67
102
  'hello, my young padawan',
@@ -70,21 +105,26 @@ const welcomeMessages = [
70
105
  'heeeey yooo'
71
106
  ];
72
107
 
73
- const option = await inquirer.prompt([
74
- {
75
- type: 'rawlist',
76
- name: 'task',
77
- message: `${sample(welcomeMessages)}\n Please, choose one:`,
78
- choices: Object.keys(choices)
79
- }
80
- ]);
81
108
 
109
+ async function promptMain() {
110
+ const choices = getChoices();
82
111
 
83
- console.log('');
84
- console.log('You have choose:');
85
- console.log(chalk.cyan(option.task));
86
- console.log('');
112
+ const option = await inquirer.prompt([
113
+ {
114
+ type: 'rawlist',
115
+ name: 'task',
116
+ message: `${sample(welcomeMessages)}\n Please, choose one:`,
117
+ choices: Object.keys(choices)
118
+ }
119
+ ]);
120
+
121
+
122
+ console.log('');
123
+ console.log('You have choose:');
124
+ console.log(chalk.cyan(option.task));
125
+ console.log('');
87
126
 
88
- await choices[option.task]();
127
+ await choices[option.task]();
128
+ }
89
129
 
90
- process.exit(0);
130
+ await promptMain();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "umpordez",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "description": "umpordez cli, interact, create, build your own meme",
6
6
  "main": "cli.mjs",
7
7
  "imports": {
@@ -24,6 +24,9 @@
24
24
  "bugs": {
25
25
  "url": "https://github.com/umpordez/cli/issues"
26
26
  },
27
+ "bin": {
28
+ "umpordez": "cli.mjs"
29
+ },
27
30
  "homepage": "https://github.com/umpordez/cli#readme",
28
31
  "dependencies": {
29
32
  "chalk": "^5.4.1",