@learnpack/learnpack 2.1.39 → 2.1.40
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/README.md +370 -35
- package/bin/run +17 -17
- package/bin/run.cmd +3 -3
- package/lib/commands/audit.d.ts +6 -6
- package/lib/commands/audit.js +342 -342
- package/lib/commands/clean.d.ts +8 -8
- package/lib/commands/clean.js +25 -25
- package/lib/commands/download.d.ts +13 -13
- package/lib/commands/download.js +55 -55
- package/lib/commands/init.d.ts +9 -9
- package/lib/commands/init.js +123 -123
- package/lib/commands/login.d.ts +14 -14
- package/lib/commands/login.js +37 -37
- package/lib/commands/logout.d.ts +14 -14
- package/lib/commands/logout.js +37 -37
- package/lib/commands/publish.d.ts +14 -14
- package/lib/commands/publish.js +82 -82
- package/lib/commands/start.d.ts +7 -7
- package/lib/commands/start.js +239 -208
- package/lib/commands/test.d.ts +6 -6
- package/lib/commands/test.js +62 -62
- package/lib/index.d.ts +1 -1
- package/lib/index.js +4 -4
- package/lib/managers/config/allowed_files.d.ts +5 -5
- package/lib/managers/config/allowed_files.js +30 -30
- package/lib/managers/config/defaults.d.ts +41 -41
- package/lib/managers/config/defaults.js +44 -44
- package/lib/managers/config/exercise.d.ts +36 -36
- package/lib/managers/config/exercise.js +236 -236
- package/lib/managers/config/index.d.ts +3 -3
- package/lib/managers/config/index.js +337 -337
- package/lib/managers/file.d.ts +14 -14
- package/lib/managers/file.js +153 -153
- package/lib/managers/gitpod.d.ts +3 -3
- package/lib/managers/gitpod.js +67 -67
- package/lib/managers/server/index.d.ts +6 -6
- package/lib/managers/server/index.js +58 -58
- package/lib/managers/server/routes.d.ts +4 -4
- package/lib/managers/server/routes.js +219 -219
- package/lib/managers/session.d.ts +3 -3
- package/lib/managers/session.js +125 -125
- package/lib/managers/socket.d.ts +3 -3
- package/lib/managers/socket.js +176 -176
- package/lib/managers/telemetry.d.ts +74 -74
- package/lib/managers/telemetry.js +206 -206
- package/lib/managers/test.js +84 -84
- package/lib/models/action.d.ts +2 -2
- package/lib/models/action.js +2 -2
- package/lib/models/audit.d.ts +15 -15
- package/lib/models/audit.js +2 -2
- package/lib/models/config-manager.d.ts +21 -21
- package/lib/models/config-manager.js +2 -2
- package/lib/models/config.d.ts +68 -67
- package/lib/models/config.js +2 -2
- package/lib/models/counter.d.ts +11 -11
- package/lib/models/counter.js +2 -2
- package/lib/models/errors.d.ts +15 -15
- package/lib/models/errors.js +2 -2
- package/lib/models/exercise-obj.d.ts +30 -30
- package/lib/models/exercise-obj.js +2 -2
- package/lib/models/file.d.ts +5 -5
- package/lib/models/file.js +2 -2
- package/lib/models/findings.d.ts +17 -17
- package/lib/models/findings.js +2 -2
- package/lib/models/flags.d.ts +10 -10
- package/lib/models/flags.js +2 -2
- package/lib/models/front-matter.d.ts +11 -11
- package/lib/models/front-matter.js +2 -2
- package/lib/models/gitpod-data.d.ts +16 -16
- package/lib/models/gitpod-data.js +2 -2
- package/lib/models/language.d.ts +4 -4
- package/lib/models/language.js +2 -2
- package/lib/models/package.d.ts +7 -7
- package/lib/models/package.js +2 -2
- package/lib/models/plugin-config.d.ts +16 -16
- package/lib/models/plugin-config.js +2 -2
- package/lib/models/session.d.ts +31 -31
- package/lib/models/session.js +2 -2
- package/lib/models/socket.d.ts +36 -32
- package/lib/models/socket.js +2 -2
- package/lib/models/status.d.ts +1 -1
- package/lib/models/status.js +2 -2
- package/lib/models/success-types.d.ts +1 -1
- package/lib/models/success-types.js +2 -2
- package/lib/plugin/command/compile.d.ts +6 -6
- package/lib/plugin/command/compile.js +18 -18
- package/lib/plugin/command/test.d.ts +6 -6
- package/lib/plugin/command/test.js +25 -25
- package/lib/plugin/index.d.ts +27 -27
- package/lib/plugin/index.js +7 -7
- package/lib/plugin/plugin.d.ts +8 -8
- package/lib/plugin/plugin.js +68 -68
- package/lib/plugin/utils.d.ts +16 -16
- package/lib/plugin/utils.js +58 -58
- package/lib/ui/download.d.ts +5 -5
- package/lib/ui/download.js +61 -61
- package/lib/utils/BaseCommand.d.ts +8 -8
- package/lib/utils/BaseCommand.js +41 -41
- package/lib/utils/SessionCommand.d.ts +10 -10
- package/lib/utils/SessionCommand.js +43 -43
- package/lib/utils/api.d.ts +14 -14
- package/lib/utils/api.js +255 -255
- package/lib/utils/audit.d.ts +16 -16
- package/lib/utils/audit.js +303 -303
- package/lib/utils/checkNotInstalled.d.ts +2 -0
- package/lib/utils/checkNotInstalled.js +36 -0
- package/lib/utils/console.d.ts +12 -12
- package/lib/utils/console.js +19 -19
- package/lib/utils/errors.d.ts +17 -17
- package/lib/utils/errors.js +100 -100
- package/lib/utils/exercisesQueue.d.ts +9 -9
- package/lib/utils/exercisesQueue.js +38 -38
- package/lib/utils/fileQueue.d.ts +40 -40
- package/lib/utils/fileQueue.js +168 -168
- package/lib/utils/misc.d.ts +1 -1
- package/lib/utils/misc.js +23 -23
- package/lib/utils/osOperations.d.ts +5 -5
- package/lib/utils/osOperations.js +72 -72
- package/lib/utils/validators.d.ts +5 -5
- package/lib/utils/validators.js +17 -17
- package/lib/utils/watcher.d.ts +2 -2
- package/lib/utils/watcher.js +25 -25
- package/oclif.manifest.json +1 -1
- package/package.json +139 -139
- package/src/commands/audit.ts +443 -443
- package/src/commands/clean.ts +29 -29
- package/src/commands/download.ts +61 -61
- package/src/commands/init.ts +170 -170
- package/src/commands/login.ts +42 -42
- package/src/commands/logout.ts +43 -43
- package/src/commands/publish.ts +107 -107
- package/src/commands/start.ts +53 -23
- package/src/commands/test.ts +85 -85
- package/src/index.ts +1 -1
- package/src/managers/config/allowed_files.ts +29 -29
- package/src/managers/config/defaults.ts +42 -42
- package/src/managers/config/exercise.ts +311 -311
- package/src/managers/config/index.ts +455 -455
- package/src/managers/file.ts +196 -196
- package/src/managers/gitpod.ts +84 -84
- package/src/managers/server/index.ts +78 -78
- package/src/managers/server/routes.ts +330 -330
- package/src/managers/session.ts +145 -145
- package/src/managers/socket.ts +250 -250
- package/src/managers/telemetry.ts +346 -346
- package/src/managers/test.ts +83 -83
- package/src/models/action.ts +10 -10
- package/src/models/audit.ts +16 -16
- package/src/models/config-manager.ts +23 -23
- package/src/models/config.ts +5 -3
- package/src/models/counter.ts +11 -11
- package/src/models/errors.ts +22 -22
- package/src/models/exercise-obj.ts +29 -29
- package/src/models/file.ts +5 -5
- package/src/models/findings.ts +18 -18
- package/src/models/flags.ts +10 -10
- package/src/models/front-matter.ts +11 -11
- package/src/models/gitpod-data.ts +19 -19
- package/src/models/language.ts +4 -4
- package/src/models/package.ts +7 -7
- package/src/models/plugin-config.ts +17 -17
- package/src/models/session.ts +34 -34
- package/src/models/socket.ts +5 -0
- package/src/models/status.ts +16 -16
- package/src/models/success-types.ts +1 -1
- package/src/plugin/command/compile.ts +17 -17
- package/src/plugin/command/test.ts +30 -30
- package/src/plugin/index.ts +6 -6
- package/src/plugin/plugin.ts +94 -94
- package/src/plugin/utils.ts +87 -87
- package/src/types/node-fetch.d.ts +1 -1
- package/src/ui/download.ts +71 -71
- package/src/utils/BaseCommand.ts +48 -48
- package/src/utils/SessionCommand.ts +43 -43
- package/src/utils/api.ts +303 -303
- package/src/utils/audit.ts +393 -393
- package/src/utils/checkNotInstalled.ts +46 -0
- package/src/utils/console.ts +24 -24
- package/src/utils/errors.ts +117 -117
- package/src/utils/exercisesQueue.ts +51 -51
- package/src/utils/fileQueue.ts +198 -198
- package/src/utils/misc.ts +23 -23
- package/src/utils/osOperations.ts +79 -79
- package/src/utils/templates/gitignore.txt +19 -19
- package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.es.md +24 -24
- package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.md +24 -24
- package/src/utils/templates/incremental/README.ejs +4 -4
- package/src/utils/templates/incremental/README.es.ejs +4 -4
- package/src/utils/templates/isolated/01-hello-world/README.es.md +26 -26
- package/src/utils/templates/isolated/01-hello-world/README.md +26 -26
- package/src/utils/templates/isolated/README.ejs +4 -4
- package/src/utils/templates/isolated/README.es.ejs +4 -4
- package/src/utils/templates/no-grading/README.ejs +4 -4
- package/src/utils/templates/no-grading/README.es.ejs +4 -4
- package/src/utils/validators.ts +18 -18
- package/src/utils/watcher.ts +27 -27
package/lib/utils/api.js
CHANGED
|
@@ -1,255 +1,255 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const console_1 = require("../utils/console");
|
|
4
|
-
const storage = require("node-persist");
|
|
5
|
-
const cli_ux_1 = require("cli-ux");
|
|
6
|
-
const HOST = "https://breathecode.herokuapp.com";
|
|
7
|
-
const RIGOBOT_HOST = "https://rigobot.herokuapp.com";
|
|
8
|
-
// eslint-disable-next-line
|
|
9
|
-
const _fetch = require("node-fetch");
|
|
10
|
-
const fetch = async (url, options = {}, returnAsJson = true) => {
|
|
11
|
-
const headers = { "Content-Type": "application/json" };
|
|
12
|
-
console_1.default.debug(`Fetching ${url}`);
|
|
13
|
-
let session = null;
|
|
14
|
-
try {
|
|
15
|
-
session = await storage.getItem("bc-payload");
|
|
16
|
-
if (session.token && session.token !== "" && !url.includes("/token"))
|
|
17
|
-
headers.Authorization = "Token " + session.token;
|
|
18
|
-
}
|
|
19
|
-
catch (_a) { }
|
|
20
|
-
try {
|
|
21
|
-
const resp = await _fetch(url, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, headers), options.headers) }));
|
|
22
|
-
if (resp.status >= 200 && resp.status < 300) {
|
|
23
|
-
return returnAsJson ? await resp.json() : await resp.text();
|
|
24
|
-
}
|
|
25
|
-
if (resp.status === 401)
|
|
26
|
-
console_1.default.debug("Invalid authentication credentials", `Code: 401`);
|
|
27
|
-
// throw APIError("Invalid authentication credentials", 401)
|
|
28
|
-
else if (resp.status === 404)
|
|
29
|
-
throw APIError("Package not found", 404);
|
|
30
|
-
else if (resp.status >= 500)
|
|
31
|
-
throw APIError("Impossible to connect with the server", 500);
|
|
32
|
-
else if (resp.status >= 400) {
|
|
33
|
-
const error = await resp.json();
|
|
34
|
-
if (error.detail || error.error) {
|
|
35
|
-
throw APIError(error.detail || error.error);
|
|
36
|
-
}
|
|
37
|
-
else if (error.nonFieldErrors) {
|
|
38
|
-
throw APIError(error.nonFieldErrors[0], error);
|
|
39
|
-
}
|
|
40
|
-
else if (typeof error === "object") {
|
|
41
|
-
if (Object.keys(error).length > 0) {
|
|
42
|
-
const key = error[Object.keys(error)[0]];
|
|
43
|
-
throw APIError(`${key}: ${error[key][0]}`, error);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
throw APIError("Uknown error");
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else
|
|
51
|
-
throw APIError("Uknown error");
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
console_1.default.error(error.message);
|
|
55
|
-
throw error;
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
const login = async (identification, password) => {
|
|
59
|
-
try {
|
|
60
|
-
cli_ux_1.default.action.start(`Looking for credentials with ${identification}`);
|
|
61
|
-
await cli_ux_1.default.wait(1000);
|
|
62
|
-
const url = `${HOST}/v1/auth/login/`;
|
|
63
|
-
const data = await fetch(url, {
|
|
64
|
-
body: JSON.stringify({
|
|
65
|
-
email: identification,
|
|
66
|
-
password: password,
|
|
67
|
-
}),
|
|
68
|
-
method: "post",
|
|
69
|
-
});
|
|
70
|
-
cli_ux_1.default.action.stop("ready");
|
|
71
|
-
let rigoPayload = null;
|
|
72
|
-
try {
|
|
73
|
-
rigoPayload = await loginRigo(data.token);
|
|
74
|
-
}
|
|
75
|
-
catch (_a) {
|
|
76
|
-
return Object.assign(Object.assign({}, data), { rigobot: null });
|
|
77
|
-
}
|
|
78
|
-
return Object.assign(Object.assign({}, data), { rigobot: rigoPayload });
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
cli_ux_1.default.action.stop("error");
|
|
82
|
-
console_1.default.error(error.message);
|
|
83
|
-
console_1.default.debug(error);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
const loginRigo = async (token) => {
|
|
87
|
-
try {
|
|
88
|
-
const rigoUrl = `${RIGOBOT_HOST}/v1/auth/me/token?breathecode_token=${token}`;
|
|
89
|
-
const rigoResp = await _fetch(rigoUrl);
|
|
90
|
-
const rigobotJson = await rigoResp.json();
|
|
91
|
-
return rigobotJson;
|
|
92
|
-
}
|
|
93
|
-
catch (error) {
|
|
94
|
-
// Handle the error as needed, for example log it or return a custom error message
|
|
95
|
-
console_1.default.error("Error logging in to Rigo, did you already accepted Rigobot?:", error);
|
|
96
|
-
throw new Error("Failed to log in to Rigo");
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
const publish = async (config) => {
|
|
100
|
-
const keys = [
|
|
101
|
-
"difficulty",
|
|
102
|
-
"language",
|
|
103
|
-
"skills",
|
|
104
|
-
"technologies",
|
|
105
|
-
"slug",
|
|
106
|
-
"repository",
|
|
107
|
-
"author",
|
|
108
|
-
"title",
|
|
109
|
-
];
|
|
110
|
-
const payload = {};
|
|
111
|
-
for (const k of keys)
|
|
112
|
-
config[k] ? (payload[k] = config[k]) : null;
|
|
113
|
-
try {
|
|
114
|
-
console.log("Package to publish:", payload);
|
|
115
|
-
cli_ux_1.default.action.start("Updating package information...");
|
|
116
|
-
await cli_ux_1.default.wait(1000);
|
|
117
|
-
const data = await fetch(`${HOST}/v1/package/${config.slug}`, {
|
|
118
|
-
method: "PUT",
|
|
119
|
-
body: JSON.stringify(payload),
|
|
120
|
-
});
|
|
121
|
-
cli_ux_1.default.action.stop("ready");
|
|
122
|
-
return data;
|
|
123
|
-
}
|
|
124
|
-
catch (error) {
|
|
125
|
-
console_1.default.error(error.message);
|
|
126
|
-
console_1.default.debug(error);
|
|
127
|
-
throw error;
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const update = async (config) => {
|
|
131
|
-
try {
|
|
132
|
-
cli_ux_1.default.action.start("Updating package information...");
|
|
133
|
-
await cli_ux_1.default.wait(1000);
|
|
134
|
-
const data = await fetch(`${HOST}/v1/package/`, {
|
|
135
|
-
method: "POST",
|
|
136
|
-
body: JSON.stringify(config),
|
|
137
|
-
});
|
|
138
|
-
cli_ux_1.default.action.stop("ready");
|
|
139
|
-
return data;
|
|
140
|
-
}
|
|
141
|
-
catch (error) {
|
|
142
|
-
console_1.default.error(error.message);
|
|
143
|
-
console_1.default.debug(error);
|
|
144
|
-
throw error;
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
const getPackage = async (slug) => {
|
|
148
|
-
try {
|
|
149
|
-
cli_ux_1.default.action.start("Downloading package information...");
|
|
150
|
-
await cli_ux_1.default.wait(1000);
|
|
151
|
-
const data = await fetch(`${HOST}/v1/package/${slug}`);
|
|
152
|
-
cli_ux_1.default.action.stop("ready");
|
|
153
|
-
return data;
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
if (error.status === 404)
|
|
157
|
-
console_1.default.error(`Package ${slug} does not exist`);
|
|
158
|
-
else
|
|
159
|
-
console_1.default.error(`Package ${slug} does not exist`);
|
|
160
|
-
console_1.default.debug(error);
|
|
161
|
-
throw error;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
const getLangs = async () => {
|
|
165
|
-
try {
|
|
166
|
-
cli_ux_1.default.action.start("Downloading language options...");
|
|
167
|
-
await cli_ux_1.default.wait(1000);
|
|
168
|
-
const data = await fetch(`${HOST}/v1/package/language`);
|
|
169
|
-
cli_ux_1.default.action.stop("ready");
|
|
170
|
-
return data;
|
|
171
|
-
}
|
|
172
|
-
catch (error) {
|
|
173
|
-
if (error.status === 404)
|
|
174
|
-
console_1.default.error("Package slug does not exist");
|
|
175
|
-
else
|
|
176
|
-
console_1.default.error("Package slug does not exist");
|
|
177
|
-
console_1.default.debug(error);
|
|
178
|
-
throw error;
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
const getAllPackages = async ({ lang = "", slug = "", }) => {
|
|
182
|
-
try {
|
|
183
|
-
cli_ux_1.default.action.start("Downloading packages...");
|
|
184
|
-
await cli_ux_1.default.wait(1000);
|
|
185
|
-
const data = await fetch(`${HOST}/v1/package/all?limit=100&language=${lang}&slug=${slug}`);
|
|
186
|
-
cli_ux_1.default.action.stop("ready");
|
|
187
|
-
return data;
|
|
188
|
-
}
|
|
189
|
-
catch (error) {
|
|
190
|
-
console_1.default.error(`Package ${slug} does not exist`);
|
|
191
|
-
console_1.default.debug(error);
|
|
192
|
-
throw error;
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
const APIError = (error, code) => {
|
|
196
|
-
const message = error.message || error;
|
|
197
|
-
const _err = new Error(message);
|
|
198
|
-
_err.status = code || 400;
|
|
199
|
-
return _err;
|
|
200
|
-
};
|
|
201
|
-
const sendBatchTelemetry = async function (url, body) {
|
|
202
|
-
if (!url) {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
const session = await storage.getItem("bc-payload");
|
|
206
|
-
if (!session ||
|
|
207
|
-
!Object.prototype.hasOwnProperty.call(session, "token") ||
|
|
208
|
-
session.token === "") {
|
|
209
|
-
console_1.default.debug("No token found, skipping stream telemetry delivery");
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
fetch(url, {
|
|
213
|
-
method: "POST",
|
|
214
|
-
body: JSON.stringify(body),
|
|
215
|
-
}, false)
|
|
216
|
-
.then(response => {
|
|
217
|
-
console_1.default.debug("Telemetry sent successfully");
|
|
218
|
-
return response.text();
|
|
219
|
-
})
|
|
220
|
-
.catch(error => {
|
|
221
|
-
console_1.default.debug("Error while sending batch Telemetry", error);
|
|
222
|
-
});
|
|
223
|
-
};
|
|
224
|
-
const sendStreamTelemetry = async function (url, body) {
|
|
225
|
-
if (!url) {
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
const session = await storage.getItem("bc-payload");
|
|
229
|
-
if (!session ||
|
|
230
|
-
!Object.prototype.hasOwnProperty.call(session, "token") ||
|
|
231
|
-
session.token === "") {
|
|
232
|
-
console_1.default.debug("No token found, skipping stream telemetry delivery");
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
fetch(url, {
|
|
236
|
-
method: "POST",
|
|
237
|
-
body: JSON.stringify(body),
|
|
238
|
-
}, false)
|
|
239
|
-
.then(response => {
|
|
240
|
-
return response;
|
|
241
|
-
})
|
|
242
|
-
.catch(error => {
|
|
243
|
-
console_1.default.debug("Error while sending stream Telemetry", error);
|
|
244
|
-
});
|
|
245
|
-
};
|
|
246
|
-
exports.default = {
|
|
247
|
-
login,
|
|
248
|
-
publish,
|
|
249
|
-
update,
|
|
250
|
-
getPackage,
|
|
251
|
-
getLangs,
|
|
252
|
-
getAllPackages,
|
|
253
|
-
sendBatchTelemetry,
|
|
254
|
-
sendStreamTelemetry,
|
|
255
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const console_1 = require("../utils/console");
|
|
4
|
+
const storage = require("node-persist");
|
|
5
|
+
const cli_ux_1 = require("cli-ux");
|
|
6
|
+
const HOST = "https://breathecode.herokuapp.com";
|
|
7
|
+
const RIGOBOT_HOST = "https://rigobot.herokuapp.com";
|
|
8
|
+
// eslint-disable-next-line
|
|
9
|
+
const _fetch = require("node-fetch");
|
|
10
|
+
const fetch = async (url, options = {}, returnAsJson = true) => {
|
|
11
|
+
const headers = { "Content-Type": "application/json" };
|
|
12
|
+
console_1.default.debug(`Fetching ${url}`);
|
|
13
|
+
let session = null;
|
|
14
|
+
try {
|
|
15
|
+
session = await storage.getItem("bc-payload");
|
|
16
|
+
if (session.token && session.token !== "" && !url.includes("/token"))
|
|
17
|
+
headers.Authorization = "Token " + session.token;
|
|
18
|
+
}
|
|
19
|
+
catch (_a) { }
|
|
20
|
+
try {
|
|
21
|
+
const resp = await _fetch(url, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, headers), options.headers) }));
|
|
22
|
+
if (resp.status >= 200 && resp.status < 300) {
|
|
23
|
+
return returnAsJson ? await resp.json() : await resp.text();
|
|
24
|
+
}
|
|
25
|
+
if (resp.status === 401)
|
|
26
|
+
console_1.default.debug("Invalid authentication credentials", `Code: 401`);
|
|
27
|
+
// throw APIError("Invalid authentication credentials", 401)
|
|
28
|
+
else if (resp.status === 404)
|
|
29
|
+
throw APIError("Package not found", 404);
|
|
30
|
+
else if (resp.status >= 500)
|
|
31
|
+
throw APIError("Impossible to connect with the server", 500);
|
|
32
|
+
else if (resp.status >= 400) {
|
|
33
|
+
const error = await resp.json();
|
|
34
|
+
if (error.detail || error.error) {
|
|
35
|
+
throw APIError(error.detail || error.error);
|
|
36
|
+
}
|
|
37
|
+
else if (error.nonFieldErrors) {
|
|
38
|
+
throw APIError(error.nonFieldErrors[0], error);
|
|
39
|
+
}
|
|
40
|
+
else if (typeof error === "object") {
|
|
41
|
+
if (Object.keys(error).length > 0) {
|
|
42
|
+
const key = error[Object.keys(error)[0]];
|
|
43
|
+
throw APIError(`${key}: ${error[key][0]}`, error);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
throw APIError("Uknown error");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else
|
|
51
|
+
throw APIError("Uknown error");
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console_1.default.error(error.message);
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const login = async (identification, password) => {
|
|
59
|
+
try {
|
|
60
|
+
cli_ux_1.default.action.start(`Looking for credentials with ${identification}`);
|
|
61
|
+
await cli_ux_1.default.wait(1000);
|
|
62
|
+
const url = `${HOST}/v1/auth/login/`;
|
|
63
|
+
const data = await fetch(url, {
|
|
64
|
+
body: JSON.stringify({
|
|
65
|
+
email: identification,
|
|
66
|
+
password: password,
|
|
67
|
+
}),
|
|
68
|
+
method: "post",
|
|
69
|
+
});
|
|
70
|
+
cli_ux_1.default.action.stop("ready");
|
|
71
|
+
let rigoPayload = null;
|
|
72
|
+
try {
|
|
73
|
+
rigoPayload = await loginRigo(data.token);
|
|
74
|
+
}
|
|
75
|
+
catch (_a) {
|
|
76
|
+
return Object.assign(Object.assign({}, data), { rigobot: null });
|
|
77
|
+
}
|
|
78
|
+
return Object.assign(Object.assign({}, data), { rigobot: rigoPayload });
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
cli_ux_1.default.action.stop("error");
|
|
82
|
+
console_1.default.error(error.message);
|
|
83
|
+
console_1.default.debug(error);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const loginRigo = async (token) => {
|
|
87
|
+
try {
|
|
88
|
+
const rigoUrl = `${RIGOBOT_HOST}/v1/auth/me/token?breathecode_token=${token}`;
|
|
89
|
+
const rigoResp = await _fetch(rigoUrl);
|
|
90
|
+
const rigobotJson = await rigoResp.json();
|
|
91
|
+
return rigobotJson;
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
// Handle the error as needed, for example log it or return a custom error message
|
|
95
|
+
console_1.default.error("Error logging in to Rigo, did you already accepted Rigobot?:", error);
|
|
96
|
+
throw new Error("Failed to log in to Rigo");
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const publish = async (config) => {
|
|
100
|
+
const keys = [
|
|
101
|
+
"difficulty",
|
|
102
|
+
"language",
|
|
103
|
+
"skills",
|
|
104
|
+
"technologies",
|
|
105
|
+
"slug",
|
|
106
|
+
"repository",
|
|
107
|
+
"author",
|
|
108
|
+
"title",
|
|
109
|
+
];
|
|
110
|
+
const payload = {};
|
|
111
|
+
for (const k of keys)
|
|
112
|
+
config[k] ? (payload[k] = config[k]) : null;
|
|
113
|
+
try {
|
|
114
|
+
console.log("Package to publish:", payload);
|
|
115
|
+
cli_ux_1.default.action.start("Updating package information...");
|
|
116
|
+
await cli_ux_1.default.wait(1000);
|
|
117
|
+
const data = await fetch(`${HOST}/v1/package/${config.slug}`, {
|
|
118
|
+
method: "PUT",
|
|
119
|
+
body: JSON.stringify(payload),
|
|
120
|
+
});
|
|
121
|
+
cli_ux_1.default.action.stop("ready");
|
|
122
|
+
return data;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
console_1.default.error(error.message);
|
|
126
|
+
console_1.default.debug(error);
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const update = async (config) => {
|
|
131
|
+
try {
|
|
132
|
+
cli_ux_1.default.action.start("Updating package information...");
|
|
133
|
+
await cli_ux_1.default.wait(1000);
|
|
134
|
+
const data = await fetch(`${HOST}/v1/package/`, {
|
|
135
|
+
method: "POST",
|
|
136
|
+
body: JSON.stringify(config),
|
|
137
|
+
});
|
|
138
|
+
cli_ux_1.default.action.stop("ready");
|
|
139
|
+
return data;
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
console_1.default.error(error.message);
|
|
143
|
+
console_1.default.debug(error);
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const getPackage = async (slug) => {
|
|
148
|
+
try {
|
|
149
|
+
cli_ux_1.default.action.start("Downloading package information...");
|
|
150
|
+
await cli_ux_1.default.wait(1000);
|
|
151
|
+
const data = await fetch(`${HOST}/v1/package/${slug}`);
|
|
152
|
+
cli_ux_1.default.action.stop("ready");
|
|
153
|
+
return data;
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
if (error.status === 404)
|
|
157
|
+
console_1.default.error(`Package ${slug} does not exist`);
|
|
158
|
+
else
|
|
159
|
+
console_1.default.error(`Package ${slug} does not exist`);
|
|
160
|
+
console_1.default.debug(error);
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const getLangs = async () => {
|
|
165
|
+
try {
|
|
166
|
+
cli_ux_1.default.action.start("Downloading language options...");
|
|
167
|
+
await cli_ux_1.default.wait(1000);
|
|
168
|
+
const data = await fetch(`${HOST}/v1/package/language`);
|
|
169
|
+
cli_ux_1.default.action.stop("ready");
|
|
170
|
+
return data;
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
if (error.status === 404)
|
|
174
|
+
console_1.default.error("Package slug does not exist");
|
|
175
|
+
else
|
|
176
|
+
console_1.default.error("Package slug does not exist");
|
|
177
|
+
console_1.default.debug(error);
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
const getAllPackages = async ({ lang = "", slug = "", }) => {
|
|
182
|
+
try {
|
|
183
|
+
cli_ux_1.default.action.start("Downloading packages...");
|
|
184
|
+
await cli_ux_1.default.wait(1000);
|
|
185
|
+
const data = await fetch(`${HOST}/v1/package/all?limit=100&language=${lang}&slug=${slug}`);
|
|
186
|
+
cli_ux_1.default.action.stop("ready");
|
|
187
|
+
return data;
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
console_1.default.error(`Package ${slug} does not exist`);
|
|
191
|
+
console_1.default.debug(error);
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const APIError = (error, code) => {
|
|
196
|
+
const message = error.message || error;
|
|
197
|
+
const _err = new Error(message);
|
|
198
|
+
_err.status = code || 400;
|
|
199
|
+
return _err;
|
|
200
|
+
};
|
|
201
|
+
const sendBatchTelemetry = async function (url, body) {
|
|
202
|
+
if (!url) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const session = await storage.getItem("bc-payload");
|
|
206
|
+
if (!session ||
|
|
207
|
+
!Object.prototype.hasOwnProperty.call(session, "token") ||
|
|
208
|
+
session.token === "") {
|
|
209
|
+
console_1.default.debug("No token found, skipping stream telemetry delivery");
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
fetch(url, {
|
|
213
|
+
method: "POST",
|
|
214
|
+
body: JSON.stringify(body),
|
|
215
|
+
}, false)
|
|
216
|
+
.then(response => {
|
|
217
|
+
console_1.default.debug("Telemetry sent successfully");
|
|
218
|
+
return response.text();
|
|
219
|
+
})
|
|
220
|
+
.catch(error => {
|
|
221
|
+
console_1.default.debug("Error while sending batch Telemetry", error);
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
const sendStreamTelemetry = async function (url, body) {
|
|
225
|
+
if (!url) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const session = await storage.getItem("bc-payload");
|
|
229
|
+
if (!session ||
|
|
230
|
+
!Object.prototype.hasOwnProperty.call(session, "token") ||
|
|
231
|
+
session.token === "") {
|
|
232
|
+
console_1.default.debug("No token found, skipping stream telemetry delivery");
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
fetch(url, {
|
|
236
|
+
method: "POST",
|
|
237
|
+
body: JSON.stringify(body),
|
|
238
|
+
}, false)
|
|
239
|
+
.then(response => {
|
|
240
|
+
return response;
|
|
241
|
+
})
|
|
242
|
+
.catch(error => {
|
|
243
|
+
console_1.default.debug("Error while sending stream Telemetry", error);
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
exports.default = {
|
|
247
|
+
login,
|
|
248
|
+
publish,
|
|
249
|
+
update,
|
|
250
|
+
getPackage,
|
|
251
|
+
getLangs,
|
|
252
|
+
getAllPackages,
|
|
253
|
+
sendBatchTelemetry,
|
|
254
|
+
sendStreamTelemetry,
|
|
255
|
+
};
|
package/lib/utils/audit.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { IAuditErrors } from "../models/audit";
|
|
2
|
-
import { IConfigObj } from "../models/config";
|
|
3
|
-
import { ICounter } from "../models/counter";
|
|
4
|
-
import { IFindings } from "../models/findings";
|
|
5
|
-
import { IExercise } from "../models/exercise-obj";
|
|
6
|
-
declare const _default: {
|
|
7
|
-
isUrl: (url: string, errors: IAuditErrors[], counter: ICounter) => Promise<boolean>;
|
|
8
|
-
checkForEmptySpaces: (str: string) => boolean;
|
|
9
|
-
checkLearnpackClean: (configObj: IConfigObj, errors: IAuditErrors[]) => void;
|
|
10
|
-
findInFile: (types: string[], content: string) => IFindings;
|
|
11
|
-
checkUrl: (config: IConfigObj, filePath: string, fileName: string, exercise: IExercise | undefined, errors: IAuditErrors[], warnings: IAuditErrors[], counter: ICounter | undefined) => Promise<boolean>;
|
|
12
|
-
writeFile: (content: string, filePath: string) => Promise<void>;
|
|
13
|
-
showErrors: (errors: IAuditErrors[], counter: ICounter | undefined) => Promise<unknown>;
|
|
14
|
-
showWarnings: (warnings: IAuditErrors[]) => Promise<unknown>;
|
|
15
|
-
};
|
|
16
|
-
export default _default;
|
|
1
|
+
import { IAuditErrors } from "../models/audit";
|
|
2
|
+
import { IConfigObj } from "../models/config";
|
|
3
|
+
import { ICounter } from "../models/counter";
|
|
4
|
+
import { IFindings } from "../models/findings";
|
|
5
|
+
import { IExercise } from "../models/exercise-obj";
|
|
6
|
+
declare const _default: {
|
|
7
|
+
isUrl: (url: string, errors: IAuditErrors[], counter: ICounter) => Promise<boolean>;
|
|
8
|
+
checkForEmptySpaces: (str: string) => boolean;
|
|
9
|
+
checkLearnpackClean: (configObj: IConfigObj, errors: IAuditErrors[]) => void;
|
|
10
|
+
findInFile: (types: string[], content: string) => IFindings;
|
|
11
|
+
checkUrl: (config: IConfigObj, filePath: string, fileName: string, exercise: IExercise | undefined, errors: IAuditErrors[], warnings: IAuditErrors[], counter: ICounter | undefined) => Promise<boolean>;
|
|
12
|
+
writeFile: (content: string, filePath: string) => Promise<void>;
|
|
13
|
+
showErrors: (errors: IAuditErrors[], counter: ICounter | undefined) => Promise<unknown>;
|
|
14
|
+
showWarnings: (warnings: IAuditErrors[]) => Promise<unknown>;
|
|
15
|
+
};
|
|
16
|
+
export default _default;
|