draftify-cli 1.0.50 → 1.0.54
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.
- package/dist/index.js +31 -6
- package/dist/utils/api.js +7 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const refactor_1 = require("./commands/refactor");
|
|
|
11
11
|
const repl_1 = require("./repl");
|
|
12
12
|
const ui_1 = require("./utils/ui");
|
|
13
13
|
const config_1 = require("./utils/config");
|
|
14
|
+
const api_1 = require("./utils/api");
|
|
14
15
|
const program = new commander_1.Command();
|
|
15
16
|
program
|
|
16
17
|
.name("draftify")
|
|
@@ -87,14 +88,38 @@ async function checkVersion() {
|
|
|
87
88
|
await checkVersion();
|
|
88
89
|
// Custom logic for when `draftify` is run without any arguments
|
|
89
90
|
if (process.argv.slice(2).length === 0) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
let isValid = false;
|
|
92
|
+
let username = "Developer";
|
|
93
|
+
if ((0, config_1.getToken)()) {
|
|
94
|
+
try {
|
|
95
|
+
ui_1.ui.info("Hitelesítés ellenőrzése...");
|
|
96
|
+
const profile = await (0, api_1.getUserProfile)();
|
|
97
|
+
if (profile) {
|
|
98
|
+
isValid = true;
|
|
99
|
+
username = profile.username;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
// Network error but not 401, we allow them in
|
|
103
|
+
isValid = true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
if (e.message === "UNAUTHORIZED") {
|
|
108
|
+
(0, config_1.saveConfig)({ token: null });
|
|
109
|
+
isValid = false;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
isValid = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (!isValid) {
|
|
117
|
+
// No valid token, run interactive login flow
|
|
118
|
+
(0, login_1.loginCommand)().then((uname) => (0, repl_1.startRepl)(uname)).catch(() => process.exit(1));
|
|
94
119
|
}
|
|
95
120
|
else {
|
|
96
|
-
// Token exists, start REPL
|
|
97
|
-
(0, repl_1.startRepl)().catch((err) => {
|
|
121
|
+
// Token exists and is valid, start REPL
|
|
122
|
+
(0, repl_1.startRepl)(username).catch((err) => {
|
|
98
123
|
ui_1.ui.error(`REPL crashed: ${err.message}`);
|
|
99
124
|
process.exit(1);
|
|
100
125
|
});
|
package/dist/utils/api.js
CHANGED
|
@@ -276,20 +276,25 @@ Throughout, balance deep technical proficiency with an accessible, friendly, wel
|
|
|
276
276
|
async function getUserProfile() {
|
|
277
277
|
const token = (0, config_1.getToken)();
|
|
278
278
|
if (!token)
|
|
279
|
-
|
|
279
|
+
throw new Error("UNAUTHORIZED");
|
|
280
280
|
const apiUrl = (0, config_1.getApiUrl)();
|
|
281
281
|
try {
|
|
282
282
|
const response = await fetch(apiUrl, {
|
|
283
283
|
method: "GET",
|
|
284
284
|
headers: { Authorization: `Bearer ${token}` }
|
|
285
285
|
});
|
|
286
|
+
if (response.status === 401 || response.status === 403) {
|
|
287
|
+
throw new Error("UNAUTHORIZED");
|
|
288
|
+
}
|
|
286
289
|
if (response.ok) {
|
|
287
290
|
const data = await response.json();
|
|
288
291
|
return { username: data.username, plan: data.plan };
|
|
289
292
|
}
|
|
290
293
|
}
|
|
291
294
|
catch (e) {
|
|
292
|
-
|
|
295
|
+
if (e.message === "UNAUTHORIZED")
|
|
296
|
+
throw e;
|
|
297
|
+
// Ignore network errors
|
|
293
298
|
}
|
|
294
299
|
return null;
|
|
295
300
|
}
|