@learnpack/learnpack 4.0.12 → 4.0.14
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +16 -35
- package/lib/commands/publish.d.ts +6 -9
- package/lib/commands/publish.js +137 -59
- package/lib/managers/server/routes.js +23 -0
- package/lib/managers/session.js +7 -5
- package/lib/models/session.d.ts +1 -0
- package/lib/utils/api.js +1 -0
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
- package/src/commands/publish.ts +181 -107
- package/src/managers/server/routes.ts +27 -0
- package/src/managers/session.ts +150 -145
- package/src/models/session.ts +35 -34
- package/src/utils/api.ts +304 -303
- package/lib/commands/build.d.ts +0 -11
- package/lib/commands/build.js +0 -160
- package/src/commands/build.ts +0 -181
package/src/utils/api.ts
CHANGED
@@ -1,303 +1,304 @@
|
|
1
|
-
import Console from "../utils/console"
|
2
|
-
import * as storage from "node-persist"
|
3
|
-
import cli from "cli-ux"
|
4
|
-
const HOST = "https://breathecode.herokuapp.com"
|
5
|
-
const RIGOBOT_HOST = "https://rigobot.herokuapp.com"
|
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
|
-
Console.
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
const
|
107
|
-
const
|
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
|
-
Console.
|
147
|
-
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
Console.
|
164
|
-
|
165
|
-
|
166
|
-
}
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
}
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
}
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
Console.
|
220
|
-
|
221
|
-
|
222
|
-
}
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
const
|
227
|
-
_err
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
!
|
240
|
-
session
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
!
|
272
|
-
session
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
}
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
1
|
+
import Console from "../utils/console"
|
2
|
+
import * as storage from "node-persist"
|
3
|
+
import cli from "cli-ux"
|
4
|
+
const HOST = "https://breathecode.herokuapp.com"
|
5
|
+
const RIGOBOT_HOST = "https://rigobot.herokuapp.com"
|
6
|
+
// const RIGOBOT_HOST = "https://8000-charlytoc-rigobot-bmwdeam7cev.ws-us116.gitpod.io"
|
7
|
+
|
8
|
+
// eslint-disable-next-line
|
9
|
+
const _fetch = require("node-fetch")
|
10
|
+
|
11
|
+
interface IHeaders {
|
12
|
+
"Content-Type"?: string
|
13
|
+
Authorization?: string
|
14
|
+
}
|
15
|
+
|
16
|
+
interface IOptions {
|
17
|
+
headers?: IHeaders
|
18
|
+
method?: string
|
19
|
+
body?: string
|
20
|
+
}
|
21
|
+
|
22
|
+
const fetch = async (
|
23
|
+
url: string,
|
24
|
+
options: IOptions = {},
|
25
|
+
returnAsJson = true
|
26
|
+
) => {
|
27
|
+
const headers: IHeaders = { "Content-Type": "application/json" }
|
28
|
+
Console.debug(`Fetching ${url}`)
|
29
|
+
let session = null
|
30
|
+
try {
|
31
|
+
session = await storage.getItem("bc-payload")
|
32
|
+
if (session.token && session.token !== "" && !url.includes("/token"))
|
33
|
+
headers.Authorization = "Token " + session.token
|
34
|
+
} catch {}
|
35
|
+
|
36
|
+
try {
|
37
|
+
const resp = await _fetch(url, {
|
38
|
+
...options,
|
39
|
+
headers: { ...headers, ...options.headers },
|
40
|
+
} as any)
|
41
|
+
|
42
|
+
if (resp.status >= 200 && resp.status < 300) {
|
43
|
+
return returnAsJson ? await resp.json() : await resp.text()
|
44
|
+
}
|
45
|
+
|
46
|
+
if (resp.status === 401)
|
47
|
+
Console.debug("Invalid authentication credentials", `Code: 401`)
|
48
|
+
// throw APIError("Invalid authentication credentials", 401)
|
49
|
+
else if (resp.status === 404)
|
50
|
+
throw APIError("Package not found", 404)
|
51
|
+
else if (resp.status >= 500)
|
52
|
+
throw APIError("Impossible to connect with the server", 500)
|
53
|
+
else if (resp.status >= 400) {
|
54
|
+
const error = await resp.json()
|
55
|
+
if (error.detail || error.error) {
|
56
|
+
throw APIError(error.detail || error.error)
|
57
|
+
} else if (error.nonFieldErrors) {
|
58
|
+
throw APIError(error.nonFieldErrors[0], error)
|
59
|
+
} else if (typeof error === "object") {
|
60
|
+
if (Object.keys(error).length > 0) {
|
61
|
+
const key = error[Object.keys(error)[0]]
|
62
|
+
throw APIError(`${key}: ${error[key][0]}`, error)
|
63
|
+
}
|
64
|
+
} else {
|
65
|
+
throw APIError("Uknown error")
|
66
|
+
}
|
67
|
+
} else
|
68
|
+
throw APIError("Uknown error")
|
69
|
+
} catch (error) {
|
70
|
+
Console.error((error as TypeError).message)
|
71
|
+
throw error
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
const login = async (identification: string, password: string) => {
|
76
|
+
try {
|
77
|
+
cli.action.start(`Looking for credentials with ${identification}`)
|
78
|
+
await cli.wait(1000)
|
79
|
+
const url = `${HOST}/v1/auth/login/`
|
80
|
+
|
81
|
+
const data = await fetch(url, {
|
82
|
+
body: JSON.stringify({
|
83
|
+
email: identification,
|
84
|
+
password: password,
|
85
|
+
}),
|
86
|
+
method: "post",
|
87
|
+
})
|
88
|
+
cli.action.stop("ready")
|
89
|
+
let rigoPayload = null
|
90
|
+
try {
|
91
|
+
rigoPayload = await loginRigo(data.token)
|
92
|
+
} catch {
|
93
|
+
return { ...data, rigobot: null }
|
94
|
+
}
|
95
|
+
|
96
|
+
return { ...data, rigobot: rigoPayload }
|
97
|
+
} catch (error) {
|
98
|
+
cli.action.stop("error")
|
99
|
+
Console.error((error as TypeError).message)
|
100
|
+
Console.debug(error)
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
const loginRigo = async (token: string) => {
|
105
|
+
try {
|
106
|
+
const rigoUrl = `${RIGOBOT_HOST}/v1/auth/me/token?breathecode_token=${token}`
|
107
|
+
const rigoResp = await _fetch(rigoUrl)
|
108
|
+
const rigobotJson = await rigoResp.json()
|
109
|
+
return rigobotJson
|
110
|
+
} catch (error) {
|
111
|
+
// Handle the error as needed, for example log it or return a custom error message
|
112
|
+
Console.error(
|
113
|
+
"Error logging in to Rigo, did you already accepted Rigobot?:",
|
114
|
+
error
|
115
|
+
)
|
116
|
+
throw new Error("Failed to log in to Rigo")
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
const publish = async (config: any) => {
|
121
|
+
const keys = [
|
122
|
+
"difficulty",
|
123
|
+
"language",
|
124
|
+
"skills",
|
125
|
+
"technologies",
|
126
|
+
"slug",
|
127
|
+
"repository",
|
128
|
+
"author",
|
129
|
+
"title",
|
130
|
+
]
|
131
|
+
|
132
|
+
const payload: { [key: string]: string } = {}
|
133
|
+
for (const k of keys)
|
134
|
+
config[k] ? (payload[k] = config[k]) : null
|
135
|
+
try {
|
136
|
+
console.log("Package to publish:", payload)
|
137
|
+
cli.action.start("Updating package information...")
|
138
|
+
await cli.wait(1000)
|
139
|
+
const data = await fetch(`${HOST}/v1/package/${config.slug}`, {
|
140
|
+
method: "PUT",
|
141
|
+
body: JSON.stringify(payload),
|
142
|
+
})
|
143
|
+
cli.action.stop("ready")
|
144
|
+
return data
|
145
|
+
} catch (error) {
|
146
|
+
Console.error((error as TypeError).message)
|
147
|
+
Console.debug(error)
|
148
|
+
throw error
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
const update = async (config: any) => {
|
153
|
+
try {
|
154
|
+
cli.action.start("Updating package information...")
|
155
|
+
await cli.wait(1000)
|
156
|
+
const data = await fetch(`${HOST}/v1/package/`, {
|
157
|
+
method: "POST",
|
158
|
+
body: JSON.stringify(config),
|
159
|
+
})
|
160
|
+
cli.action.stop("ready")
|
161
|
+
return data
|
162
|
+
} catch (error) {
|
163
|
+
Console.error((error as any).message)
|
164
|
+
Console.debug(error)
|
165
|
+
throw error
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
const getPackage = async (slug: string) => {
|
170
|
+
try {
|
171
|
+
cli.action.start("Downloading package information...")
|
172
|
+
await cli.wait(1000)
|
173
|
+
const data = await fetch(`${HOST}/v1/package/${slug}`)
|
174
|
+
cli.action.stop("ready")
|
175
|
+
return data
|
176
|
+
} catch (error) {
|
177
|
+
if ((error as any).status === 404)
|
178
|
+
Console.error(`Package ${slug} does not exist`)
|
179
|
+
else
|
180
|
+
Console.error(`Package ${slug} does not exist`)
|
181
|
+
Console.debug(error)
|
182
|
+
throw error
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
const getLangs = async () => {
|
187
|
+
try {
|
188
|
+
cli.action.start("Downloading language options...")
|
189
|
+
await cli.wait(1000)
|
190
|
+
const data = await fetch(`${HOST}/v1/package/language`)
|
191
|
+
cli.action.stop("ready")
|
192
|
+
return data
|
193
|
+
} catch (error) {
|
194
|
+
if ((error as any).status === 404)
|
195
|
+
Console.error("Package slug does not exist")
|
196
|
+
else
|
197
|
+
Console.error("Package slug does not exist")
|
198
|
+
Console.debug(error)
|
199
|
+
throw error
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
203
|
+
const getAllPackages = async ({
|
204
|
+
lang = "",
|
205
|
+
slug = "",
|
206
|
+
}: {
|
207
|
+
lang?: string
|
208
|
+
slug?: string
|
209
|
+
}) => {
|
210
|
+
try {
|
211
|
+
cli.action.start("Downloading packages...")
|
212
|
+
await cli.wait(1000)
|
213
|
+
const data = await fetch(
|
214
|
+
`${HOST}/v1/package/all?limit=100&language=${lang}&slug=${slug}`
|
215
|
+
)
|
216
|
+
cli.action.stop("ready")
|
217
|
+
return data
|
218
|
+
} catch (error) {
|
219
|
+
Console.error(`Package ${slug} does not exist`)
|
220
|
+
Console.debug(error)
|
221
|
+
throw error
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
const APIError = (error: TypeError | string, code?: number) => {
|
226
|
+
const message: string = (error as TypeError).message || (error as string)
|
227
|
+
const _err = new Error(message) as any
|
228
|
+
_err.status = code || 400
|
229
|
+
return _err
|
230
|
+
}
|
231
|
+
|
232
|
+
const sendBatchTelemetry = async function (url: string, body: object) {
|
233
|
+
if (!url) {
|
234
|
+
return
|
235
|
+
}
|
236
|
+
|
237
|
+
const session = await storage.getItem("bc-payload")
|
238
|
+
if (
|
239
|
+
!session ||
|
240
|
+
!Object.prototype.hasOwnProperty.call(session, "token") ||
|
241
|
+
session.token === ""
|
242
|
+
) {
|
243
|
+
Console.debug("No token found, skipping stream telemetry delivery")
|
244
|
+
return
|
245
|
+
}
|
246
|
+
|
247
|
+
fetch(
|
248
|
+
url,
|
249
|
+
{
|
250
|
+
method: "POST",
|
251
|
+
body: JSON.stringify(body),
|
252
|
+
},
|
253
|
+
false
|
254
|
+
)
|
255
|
+
.then(response => {
|
256
|
+
Console.debug("Telemetry sent successfully")
|
257
|
+
return response.text()
|
258
|
+
})
|
259
|
+
.catch(error => {
|
260
|
+
Console.debug("Error while sending batch Telemetry", error)
|
261
|
+
})
|
262
|
+
}
|
263
|
+
|
264
|
+
const sendStreamTelemetry = async function (url: string, body: object) {
|
265
|
+
if (!url) {
|
266
|
+
return
|
267
|
+
}
|
268
|
+
|
269
|
+
const session = await storage.getItem("bc-payload")
|
270
|
+
if (
|
271
|
+
!session ||
|
272
|
+
!Object.prototype.hasOwnProperty.call(session, "token") ||
|
273
|
+
session.token === ""
|
274
|
+
) {
|
275
|
+
Console.debug("No token found, skipping stream telemetry delivery")
|
276
|
+
return
|
277
|
+
}
|
278
|
+
|
279
|
+
fetch(
|
280
|
+
url,
|
281
|
+
{
|
282
|
+
method: "POST",
|
283
|
+
body: JSON.stringify(body),
|
284
|
+
},
|
285
|
+
false
|
286
|
+
)
|
287
|
+
.then(response => {
|
288
|
+
return response
|
289
|
+
})
|
290
|
+
.catch(error => {
|
291
|
+
Console.debug("Error while sending stream Telemetry", error)
|
292
|
+
})
|
293
|
+
}
|
294
|
+
|
295
|
+
export default {
|
296
|
+
login,
|
297
|
+
publish,
|
298
|
+
update,
|
299
|
+
getPackage,
|
300
|
+
getLangs,
|
301
|
+
getAllPackages,
|
302
|
+
sendBatchTelemetry,
|
303
|
+
sendStreamTelemetry,
|
304
|
+
}
|
package/lib/commands/build.d.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
import SessionCommand from "../utils/SessionCommand";
|
2
|
-
export default class BuildCommand extends SessionCommand {
|
3
|
-
static description: string;
|
4
|
-
static flags: {
|
5
|
-
help: import("@oclif/parser/lib/flags").IBooleanFlag<void>;
|
6
|
-
};
|
7
|
-
init(): Promise<void>;
|
8
|
-
run(): Promise<void>;
|
9
|
-
copyDirectory(src: string, dest: string): void;
|
10
|
-
removeDirectory(dir: string): void;
|
11
|
-
}
|