@learnpack/learnpack 5.0.108 → 5.0.112
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 +13 -13
- package/lib/commands/publish.js +1 -1
- package/lib/creatorDist/assets/{index-naiPvmWT.js → index-Bw3XbAoy.js} +4840 -4905
- package/lib/creatorDist/assets/{index-Cvdi97GX.css → index-VqUlNIHR.css} +0 -3
- package/lib/creatorDist/index.html +2 -2
- package/lib/utils/configBuilder.js +1 -1
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
- package/src/commands/publish.ts +495 -495
- package/src/creator/src/App.tsx +2 -5
- package/src/creator/src/components/LessonItem.tsx +21 -20
- package/src/creator/src/components/Login.tsx +1 -1
- package/src/creator/src/components/PreviewGenerator.tsx +6 -6
- package/src/creator/src/components/syllabus/ContentIndex.tsx +19 -19
- package/src/creator/src/components/syllabus/SyllabusEditor.tsx +2 -2
- package/src/creator/src/utils/creatorUtils.ts +3 -0
- package/src/creator/src/utils/store.ts +1 -0
- package/src/creatorDist/assets/{index-naiPvmWT.js → index-Bw3XbAoy.js} +4840 -4905
- package/src/creatorDist/assets/{index-Cvdi97GX.css → index-VqUlNIHR.css} +0 -3
- package/src/creatorDist/index.html +2 -2
- package/src/ui/_app/app.css +1 -1
- package/src/ui/_app/app.js +267 -267
- package/src/ui/app.tar.gz +0 -0
- package/src/utils/configBuilder.ts +1 -1
package/src/commands/publish.ts
CHANGED
@@ -1,495 +1,495 @@
|
|
1
|
-
/* eslint-disable arrow-parens */
|
2
|
-
/* eslint-disable unicorn/no-array-for-each */
|
3
|
-
import { execSync } from "child_process"
|
4
|
-
import { flags } from "@oclif/command"
|
5
|
-
import SessionCommand from "../utils/SessionCommand"
|
6
|
-
import SessionManager from "../managers/session"
|
7
|
-
import * as fs from "fs"
|
8
|
-
import * as path from "path"
|
9
|
-
import * as archiver from "archiver"
|
10
|
-
import axios from "axios"
|
11
|
-
import FormData = require("form-data")
|
12
|
-
import Console from "../utils/console"
|
13
|
-
import {
|
14
|
-
decompress,
|
15
|
-
downloadEditor,
|
16
|
-
checkIfDirectoryExists,
|
17
|
-
} from "../managers/file"
|
18
|
-
import api, { getConsumable, RIGOBOT_HOST, TAcademy } from "../utils/api"
|
19
|
-
import * as prompts from "prompts"
|
20
|
-
import { generateCourseShortName, isValidRigoToken } from "../utils/rigoActions"
|
21
|
-
import { minutesToISO8601Duration } from "../utils/misc"
|
22
|
-
|
23
|
-
const uploadZipEndpont = RIGOBOT_HOST + "/v1/learnpack/upload"
|
24
|
-
|
25
|
-
export const handleAssetCreation = async (
|
26
|
-
sessionPayload: any,
|
27
|
-
academy: TAcademy,
|
28
|
-
learnJson: any,
|
29
|
-
learnpackDeployUrl: string,
|
30
|
-
category: number
|
31
|
-
) => {
|
32
|
-
try {
|
33
|
-
const { exists, academyId } = await api.doesAssetExists(
|
34
|
-
sessionPayload.token,
|
35
|
-
learnJson.slug
|
36
|
-
)
|
37
|
-
|
38
|
-
if (!exists) {
|
39
|
-
Console.info("Asset does not exist in this academy, creating it")
|
40
|
-
const asset = await api.createAsset(sessionPayload.token, academy.id, {
|
41
|
-
slug: learnJson.slug,
|
42
|
-
title: learnJson.title.us,
|
43
|
-
lang: "us",
|
44
|
-
description: learnJson.description.us,
|
45
|
-
learnpack_deploy_url: learnpackDeployUrl,
|
46
|
-
technologies: ["node", "bash"],
|
47
|
-
url: "https://4geeksacademy.com",
|
48
|
-
category: category,
|
49
|
-
owner: sessionPayload.id,
|
50
|
-
author: sessionPayload.id,
|
51
|
-
})
|
52
|
-
Console.info("Asset created with id", asset.id)
|
53
|
-
} else {
|
54
|
-
Console.info("Asset exists, updating it")
|
55
|
-
const asset = await api.updateAsset(
|
56
|
-
sessionPayload.token,
|
57
|
-
academyId as number,
|
58
|
-
learnJson.slug,
|
59
|
-
{
|
60
|
-
learnpack_deploy_url: learnpackDeployUrl,
|
61
|
-
title: learnJson.title.us,
|
62
|
-
description: learnJson.description.us,
|
63
|
-
}
|
64
|
-
)
|
65
|
-
Console.info("Asset updated with id", asset.id)
|
66
|
-
}
|
67
|
-
} catch (error) {
|
68
|
-
Console.error("Error updating or creating asset:", error)
|
69
|
-
}
|
70
|
-
}
|
71
|
-
|
72
|
-
const runAudit = (strict: boolean) => {
|
73
|
-
try {
|
74
|
-
Console.info("Running learnpack audit before publishing...")
|
75
|
-
execSync(`learnpack audit ${strict ? "--strict" : ""}`, {
|
76
|
-
stdio: "inherit",
|
77
|
-
})
|
78
|
-
} catch (error) {
|
79
|
-
Console.error("Failed to audit with learnpack")
|
80
|
-
|
81
|
-
// Si `execSync` lanza un error, capturamos el mensaje de error
|
82
|
-
if (error instanceof Error) {
|
83
|
-
Console.error(error.message)
|
84
|
-
} else {
|
85
|
-
Console.error("Unknown error occurred")
|
86
|
-
}
|
87
|
-
|
88
|
-
// Detener la ejecución del comando si `learnpack publish` falla
|
89
|
-
process.exit(1)
|
90
|
-
}
|
91
|
-
|
92
|
-
// Continuar con el proceso de build solo si `learnpack publish` fue exitoso
|
93
|
-
Console.info(
|
94
|
-
"Learnpack publish completed successfully. Proceeding with build..."
|
95
|
-
)
|
96
|
-
}
|
97
|
-
|
98
|
-
type Academy = {
|
99
|
-
id: number
|
100
|
-
name: string
|
101
|
-
slug?: string
|
102
|
-
timezone?: string
|
103
|
-
}
|
104
|
-
|
105
|
-
type Category = {
|
106
|
-
id: number
|
107
|
-
slug: string
|
108
|
-
title: string
|
109
|
-
lang: string
|
110
|
-
academy: Academy
|
111
|
-
}
|
112
|
-
|
113
|
-
function getCategoriesByAcademy(
|
114
|
-
categories: Category[],
|
115
|
-
academy: Academy
|
116
|
-
): Category[] {
|
117
|
-
return categories.filter((cat) => cat.academy.id === academy.id)
|
118
|
-
}
|
119
|
-
|
120
|
-
const selectAcademy = async (
|
121
|
-
academies: TAcademy[],
|
122
|
-
bcToken: string
|
123
|
-
): Promise<{ academy: TAcademy | null; category: number }> => {
|
124
|
-
if (academies.length === 0) {
|
125
|
-
return { academy: null, category: 0 }
|
126
|
-
}
|
127
|
-
|
128
|
-
if (academies.length === 1) {
|
129
|
-
return { academy: academies[0], category: 0 }
|
130
|
-
}
|
131
|
-
|
132
|
-
// prompts the user to select an academy to upload the assets
|
133
|
-
Console.info("In which academy do you want to publish the asset?")
|
134
|
-
const response = await prompts({
|
135
|
-
type: "select",
|
136
|
-
name: "academy",
|
137
|
-
message: "Select an academy",
|
138
|
-
choices: academies.map((academy) => ({
|
139
|
-
title: academy.name,
|
140
|
-
value: academy,
|
141
|
-
})),
|
142
|
-
})
|
143
|
-
|
144
|
-
const categories: Category[] = await api.getCategories(bcToken)
|
145
|
-
const categoriesByAcademy = getCategoriesByAcademy(
|
146
|
-
categories,
|
147
|
-
response.academy
|
148
|
-
)
|
149
|
-
|
150
|
-
const categoriesPrompt = await prompts({
|
151
|
-
type: "select",
|
152
|
-
name: "category",
|
153
|
-
message: "Select a category",
|
154
|
-
choices: categoriesByAcademy.map((category) => ({
|
155
|
-
title: category.title,
|
156
|
-
value: category.id,
|
157
|
-
})),
|
158
|
-
})
|
159
|
-
|
160
|
-
return {
|
161
|
-
academy: response.academy,
|
162
|
-
category: categoriesPrompt.category,
|
163
|
-
}
|
164
|
-
}
|
165
|
-
|
166
|
-
class BuildCommand extends SessionCommand {
|
167
|
-
static description =
|
168
|
-
"Builds the project by copying necessary files and directories into a zip file"
|
169
|
-
|
170
|
-
static flags = {
|
171
|
-
help: flags.help({ char: "h" }),
|
172
|
-
strict: flags.boolean({
|
173
|
-
char: "s",
|
174
|
-
description: "strict mode",
|
175
|
-
default: false,
|
176
|
-
}),
|
177
|
-
}
|
178
|
-
|
179
|
-
async init() {
|
180
|
-
const { flags } = this.parse(BuildCommand)
|
181
|
-
await this.initSession(flags)
|
182
|
-
}
|
183
|
-
|
184
|
-
async run() {
|
185
|
-
const buildDir = path.join(process.cwd(), "build")
|
186
|
-
|
187
|
-
const { flags }: { flags: { strict: boolean } } = this.parse(BuildCommand)
|
188
|
-
const strict = flags.strict
|
189
|
-
Console.debug("Strict mode: ", strict)
|
190
|
-
// this.configManager?.clean()
|
191
|
-
|
192
|
-
const configObject = this.configManager?.get()
|
193
|
-
|
194
|
-
let sessionPayload = await SessionManager.getPayload()
|
195
|
-
|
196
|
-
const sessionExists = sessionPayload && sessionPayload.rigobot
|
197
|
-
|
198
|
-
const isValidToken =
|
199
|
-
sessionExists && sessionPayload.rigobot.key ?
|
200
|
-
await isValidRigoToken(sessionPayload.rigobot.key) :
|
201
|
-
false
|
202
|
-
|
203
|
-
const isValidBreathecodeToken =
|
204
|
-
sessionExists && sessionPayload.token ?
|
205
|
-
await api.validateToken(sessionPayload.token) :
|
206
|
-
false
|
207
|
-
|
208
|
-
if (!sessionExists || !isValidBreathecodeToken || !isValidToken) {
|
209
|
-
Console.info(
|
210
|
-
"Almost there! First you need to login with 4Geeks.com to use AI Generation tool for creators. You can create a new account here: https://4geeks.com/checkout?plan=
|
211
|
-
)
|
212
|
-
try {
|
213
|
-
sessionPayload = await SessionManager.login()
|
214
|
-
} catch (error) {
|
215
|
-
Console.error("Error trying to authenticate")
|
216
|
-
Console.error((error as TypeError).message || (error as string))
|
217
|
-
}
|
218
|
-
}
|
219
|
-
|
220
|
-
const rigoToken = sessionPayload.rigobot.key
|
221
|
-
|
222
|
-
const consumable = await getConsumable(
|
223
|
-
sessionPayload.token,
|
224
|
-
"learnpack-publish"
|
225
|
-
)
|
226
|
-
|
227
|
-
if (consumable.count === 0) {
|
228
|
-
Console.error(
|
229
|
-
"It seems you cannot publish tutorials. Make sure you creator subscription is up to date here: https://4geeks.com/profile/subscriptions. If you believe there is an issue you can always contact support@4geeks.com"
|
230
|
-
)
|
231
|
-
process.exit(1)
|
232
|
-
}
|
233
|
-
|
234
|
-
if (configObject) {
|
235
|
-
Console.info("Cleaning configuration files")
|
236
|
-
this.configManager?.clean()
|
237
|
-
// build exerises
|
238
|
-
runAudit(strict)
|
239
|
-
|
240
|
-
Console.debug("Building exercises")
|
241
|
-
this.configManager?.buildIndex()
|
242
|
-
}
|
243
|
-
|
244
|
-
const academies = await api.listUserAcademies(sessionPayload.token)
|
245
|
-
|
246
|
-
if (academies.length === 0) {
|
247
|
-
Console.error(
|
248
|
-
"It seems you cannot publish tutorials. Make sure you creator subscription is up to date here: https://4geeks.com/profile/subscriptions. If you believe there is an issue you can always contact support@4geeks.com"
|
249
|
-
)
|
250
|
-
process.exit(1)
|
251
|
-
}
|
252
|
-
|
253
|
-
const { academy, category } = await selectAcademy(
|
254
|
-
academies,
|
255
|
-
sessionPayload.token
|
256
|
-
)
|
257
|
-
|
258
|
-
const learnJsonPath = path.join(process.cwd(), "learn.json")
|
259
|
-
if (!fs.existsSync(learnJsonPath)) {
|
260
|
-
this.error("learn.json not found")
|
261
|
-
}
|
262
|
-
|
263
|
-
const learnJson = JSON.parse(fs.readFileSync(learnJsonPath, "utf-8"))
|
264
|
-
|
265
|
-
const zipFilePath = path.join(process.cwd(), `${learnJson.slug}.zip`)
|
266
|
-
|
267
|
-
// Ensure build directory exists
|
268
|
-
if (!fs.existsSync(buildDir)) {
|
269
|
-
fs.mkdirSync(buildDir)
|
270
|
-
}
|
271
|
-
|
272
|
-
if (configObject) {
|
273
|
-
const { config } = configObject
|
274
|
-
const appAlreadyExists = checkIfDirectoryExists(`${config?.dirPath}/_app`)
|
275
|
-
|
276
|
-
if (!appAlreadyExists) {
|
277
|
-
// download app and decompress
|
278
|
-
await downloadEditor(
|
279
|
-
config?.editor.version,
|
280
|
-
`${config?.dirPath}/app.tar.gz`
|
281
|
-
)
|
282
|
-
|
283
|
-
Console.info("Decompressing LearnPack UI, this may take a minute...")
|
284
|
-
await decompress(
|
285
|
-
`${config?.dirPath}/app.tar.gz`,
|
286
|
-
`${config?.dirPath}/_app/`
|
287
|
-
)
|
288
|
-
}
|
289
|
-
}
|
290
|
-
|
291
|
-
// Copy config.json
|
292
|
-
const configPath = path.join(process.cwd(), ".learn", "config.json")
|
293
|
-
if (fs.existsSync(configPath)) {
|
294
|
-
fs.copyFileSync(configPath, path.join(buildDir, "config.json"))
|
295
|
-
} else {
|
296
|
-
this.error("config.json not found")
|
297
|
-
}
|
298
|
-
|
299
|
-
// Copy .learn/assets directory, if it exists else create it
|
300
|
-
const assetsDir = path.join(process.cwd(), ".learn", "assets")
|
301
|
-
if (fs.existsSync(assetsDir)) {
|
302
|
-
this.copyDirectory(assetsDir, path.join(buildDir, ".learn", "assets"))
|
303
|
-
} else {
|
304
|
-
fs.mkdirSync(path.join(buildDir, ".learn", "assets"), { recursive: true })
|
305
|
-
}
|
306
|
-
|
307
|
-
// Copy .learn/_app directory files to the same level as config.json
|
308
|
-
const appDir = path.join(process.cwd(), ".learn", "_app")
|
309
|
-
if (fs.existsSync(appDir)) {
|
310
|
-
this.copyDirectory(appDir, buildDir)
|
311
|
-
} else {
|
312
|
-
this.error(".learn/_app directory not found")
|
313
|
-
}
|
314
|
-
|
315
|
-
// After copying the _app directory
|
316
|
-
const indexHtmlPath = path.join(appDir, "index.html")
|
317
|
-
const buildIndexHtmlPath = path.join(buildDir, "index.html")
|
318
|
-
const manifestPWA = path.join(appDir, "manifest.webmanifest")
|
319
|
-
const buildManifestPWA = path.join(buildDir, "manifest.webmanifest")
|
320
|
-
|
321
|
-
if (fs.existsSync(indexHtmlPath)) {
|
322
|
-
let indexHtmlContent = fs.readFileSync(indexHtmlPath, "utf-8")
|
323
|
-
|
324
|
-
const description = learnJson.description.us || "LearnPack is awesome!"
|
325
|
-
const title =
|
326
|
-
learnJson.title.us || "LearnPack: Interactive Learning as a Service"
|
327
|
-
|
328
|
-
const previewUrl =
|
329
|
-
learnJson.preview ||
|
330
|
-
"https://raw.githubusercontent.com/learnpack/ide/refs/heads/master/public/learnpack.svg"
|
331
|
-
// Replace placeholders and the <title>Old title </title> tag for a new tag with the title
|
332
|
-
indexHtmlContent = indexHtmlContent
|
333
|
-
.replace(/{{description}}/g, description)
|
334
|
-
.replace(/<title>.*<\/title>/, `<title>${title}</title>`)
|
335
|
-
.replace(/{{title}}/g, title)
|
336
|
-
.replace(/{{preview}}/g, previewUrl)
|
337
|
-
.replace(/{{slug}}/g, learnJson.slug)
|
338
|
-
.replace(/{{duration}}/g, minutesToISO8601Duration(learnJson.duration))
|
339
|
-
|
340
|
-
// Write the modified content to the build directory
|
341
|
-
fs.writeFileSync(buildIndexHtmlPath, indexHtmlContent)
|
342
|
-
} else {
|
343
|
-
this.error("index.html not found in _app directory")
|
344
|
-
}
|
345
|
-
|
346
|
-
if (fs.existsSync(manifestPWA)) {
|
347
|
-
let manifestPWAContent = fs.readFileSync(manifestPWA, "utf-8")
|
348
|
-
manifestPWAContent = manifestPWAContent.replace(
|
349
|
-
"{{course_title}}",
|
350
|
-
learnJson.title.us
|
351
|
-
)
|
352
|
-
|
353
|
-
const courseShortName = { answer: "testing-tutorial" }
|
354
|
-
// const courseShortName = await generateCourseShortName(rigoToken, {
|
355
|
-
// learnJSON: JSON.stringify(learnJson),
|
356
|
-
// })
|
357
|
-
|
358
|
-
manifestPWAContent = manifestPWAContent.replace(
|
359
|
-
"{{course_app_name}}",
|
360
|
-
courseShortName.answer
|
361
|
-
)
|
362
|
-
fs.writeFileSync(buildManifestPWA, manifestPWAContent)
|
363
|
-
} else {
|
364
|
-
this.error("manifest.webmanifest not found in _app directory")
|
365
|
-
}
|
366
|
-
|
367
|
-
// Copy exercises directory
|
368
|
-
const exercisesDir = path.join(process.cwd(), "exercises")
|
369
|
-
const learnExercisesDir = path.join(process.cwd(), ".learn", "exercises")
|
370
|
-
|
371
|
-
if (fs.existsSync(exercisesDir)) {
|
372
|
-
this.copyDirectory(exercisesDir, path.join(buildDir, "exercises"))
|
373
|
-
} else if (fs.existsSync(learnExercisesDir)) {
|
374
|
-
this.copyDirectory(learnExercisesDir, path.join(buildDir, "exercises"))
|
375
|
-
} else {
|
376
|
-
this.error("exercises directory not found in either location")
|
377
|
-
}
|
378
|
-
|
379
|
-
fs.copyFileSync(learnJsonPath, path.join(buildDir, "learn.json"))
|
380
|
-
const sidebarPath = path.join(process.cwd(), ".learn", "sidebar.json")
|
381
|
-
if (fs.existsSync(sidebarPath)) {
|
382
|
-
fs.copyFileSync(sidebarPath, path.join(buildDir, "sidebar.json"))
|
383
|
-
} else {
|
384
|
-
this.error("sidebar.json not found in .learn directory")
|
385
|
-
}
|
386
|
-
|
387
|
-
const output = fs.createWriteStream(zipFilePath)
|
388
|
-
const archive = archiver("zip", {
|
389
|
-
zlib: { level: 9 },
|
390
|
-
})
|
391
|
-
|
392
|
-
output.on("close", async () => {
|
393
|
-
this.log(
|
394
|
-
`Build completed: ${zipFilePath} (${archive.pointer()} total bytes)`
|
395
|
-
)
|
396
|
-
// Remove build directory after zip is created
|
397
|
-
|
398
|
-
Console.debug("Zip file saved in project root")
|
399
|
-
|
400
|
-
const formData = new FormData()
|
401
|
-
formData.append("file", fs.createReadStream(zipFilePath))
|
402
|
-
formData.append("config", JSON.stringify(learnJson))
|
403
|
-
|
404
|
-
try {
|
405
|
-
const res = await axios.post(uploadZipEndpont, formData, {
|
406
|
-
headers: {
|
407
|
-
...formData.getHeaders(),
|
408
|
-
Authorization: `Token ${rigoToken}`,
|
409
|
-
},
|
410
|
-
})
|
411
|
-
console.log(res.data)
|
412
|
-
|
413
|
-
fs.unlinkSync(zipFilePath)
|
414
|
-
this.removeDirectory(buildDir)
|
415
|
-
|
416
|
-
if (academy) {
|
417
|
-
await handleAssetCreation(
|
418
|
-
sessionPayload,
|
419
|
-
academy,
|
420
|
-
learnJson,
|
421
|
-
res.data.url,
|
422
|
-
category
|
423
|
-
)
|
424
|
-
}
|
425
|
-
} catch (error) {
|
426
|
-
if (axios.isAxiosError(error)) {
|
427
|
-
if (error.response && error.response.status === 403) {
|
428
|
-
console.error("Error 403:", error.response.data.error)
|
429
|
-
} else if (error.response && error.response.status === 400) {
|
430
|
-
console.error(error.response.data.error)
|
431
|
-
} else {
|
432
|
-
console.error("Error uploading file:", error)
|
433
|
-
}
|
434
|
-
} else {
|
435
|
-
console.error("Error uploading file:", error)
|
436
|
-
}
|
437
|
-
|
438
|
-
// fs.unlinkSync(zipFilePath)
|
439
|
-
// this.removeDirectory(buildDir)
|
440
|
-
}
|
441
|
-
})
|
442
|
-
|
443
|
-
archive.on("error", (err: any) => {
|
444
|
-
throw err
|
445
|
-
})
|
446
|
-
|
447
|
-
archive.pipe(output)
|
448
|
-
archive.directory(buildDir, false)
|
449
|
-
await archive.finalize()
|
450
|
-
}
|
451
|
-
|
452
|
-
copyDirectory(src: string, dest: string) {
|
453
|
-
if (!fs.existsSync(dest)) {
|
454
|
-
fs.mkdirSync(dest, { recursive: true })
|
455
|
-
}
|
456
|
-
|
457
|
-
const entries = fs.readdirSync(src, { withFileTypes: true })
|
458
|
-
|
459
|
-
for (const entry of entries) {
|
460
|
-
const srcPath = path.join(src, entry.name)
|
461
|
-
const destPath = path.join(dest, entry.name)
|
462
|
-
|
463
|
-
if (entry.isDirectory()) {
|
464
|
-
this.copyDirectory(srcPath, destPath)
|
465
|
-
} else {
|
466
|
-
fs.copyFileSync(srcPath, destPath)
|
467
|
-
}
|
468
|
-
}
|
469
|
-
}
|
470
|
-
|
471
|
-
removeDirectory(dir: string) {
|
472
|
-
if (fs.existsSync(dir)) {
|
473
|
-
fs.readdirSync(dir).forEach((file) => {
|
474
|
-
const currentPath = path.join(dir, file)
|
475
|
-
if (fs.lstatSync(currentPath).isDirectory()) {
|
476
|
-
this.removeDirectory(currentPath)
|
477
|
-
} else {
|
478
|
-
fs.unlinkSync(currentPath)
|
479
|
-
}
|
480
|
-
})
|
481
|
-
fs.rmdirSync(dir)
|
482
|
-
}
|
483
|
-
}
|
484
|
-
}
|
485
|
-
|
486
|
-
BuildCommand.flags = {
|
487
|
-
strict: flags.boolean({
|
488
|
-
char: "s",
|
489
|
-
description: "strict mode",
|
490
|
-
default: false,
|
491
|
-
}),
|
492
|
-
help: flags.help({ char: "h" }),
|
493
|
-
}
|
494
|
-
|
495
|
-
export default BuildCommand
|
1
|
+
/* eslint-disable arrow-parens */
|
2
|
+
/* eslint-disable unicorn/no-array-for-each */
|
3
|
+
import { execSync } from "child_process"
|
4
|
+
import { flags } from "@oclif/command"
|
5
|
+
import SessionCommand from "../utils/SessionCommand"
|
6
|
+
import SessionManager from "../managers/session"
|
7
|
+
import * as fs from "fs"
|
8
|
+
import * as path from "path"
|
9
|
+
import * as archiver from "archiver"
|
10
|
+
import axios from "axios"
|
11
|
+
import FormData = require("form-data")
|
12
|
+
import Console from "../utils/console"
|
13
|
+
import {
|
14
|
+
decompress,
|
15
|
+
downloadEditor,
|
16
|
+
checkIfDirectoryExists,
|
17
|
+
} from "../managers/file"
|
18
|
+
import api, { getConsumable, RIGOBOT_HOST, TAcademy } from "../utils/api"
|
19
|
+
import * as prompts from "prompts"
|
20
|
+
import { generateCourseShortName, isValidRigoToken } from "../utils/rigoActions"
|
21
|
+
import { minutesToISO8601Duration } from "../utils/misc"
|
22
|
+
|
23
|
+
const uploadZipEndpont = RIGOBOT_HOST + "/v1/learnpack/upload"
|
24
|
+
|
25
|
+
export const handleAssetCreation = async (
|
26
|
+
sessionPayload: any,
|
27
|
+
academy: TAcademy,
|
28
|
+
learnJson: any,
|
29
|
+
learnpackDeployUrl: string,
|
30
|
+
category: number
|
31
|
+
) => {
|
32
|
+
try {
|
33
|
+
const { exists, academyId } = await api.doesAssetExists(
|
34
|
+
sessionPayload.token,
|
35
|
+
learnJson.slug
|
36
|
+
)
|
37
|
+
|
38
|
+
if (!exists) {
|
39
|
+
Console.info("Asset does not exist in this academy, creating it")
|
40
|
+
const asset = await api.createAsset(sessionPayload.token, academy.id, {
|
41
|
+
slug: learnJson.slug,
|
42
|
+
title: learnJson.title.us,
|
43
|
+
lang: "us",
|
44
|
+
description: learnJson.description.us,
|
45
|
+
learnpack_deploy_url: learnpackDeployUrl,
|
46
|
+
technologies: ["node", "bash"],
|
47
|
+
url: "https://4geeksacademy.com",
|
48
|
+
category: category,
|
49
|
+
owner: sessionPayload.id,
|
50
|
+
author: sessionPayload.id,
|
51
|
+
})
|
52
|
+
Console.info("Asset created with id", asset.id)
|
53
|
+
} else {
|
54
|
+
Console.info("Asset exists, updating it")
|
55
|
+
const asset = await api.updateAsset(
|
56
|
+
sessionPayload.token,
|
57
|
+
academyId as number,
|
58
|
+
learnJson.slug,
|
59
|
+
{
|
60
|
+
learnpack_deploy_url: learnpackDeployUrl,
|
61
|
+
title: learnJson.title.us,
|
62
|
+
description: learnJson.description.us,
|
63
|
+
}
|
64
|
+
)
|
65
|
+
Console.info("Asset updated with id", asset.id)
|
66
|
+
}
|
67
|
+
} catch (error) {
|
68
|
+
Console.error("Error updating or creating asset:", error)
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
const runAudit = (strict: boolean) => {
|
73
|
+
try {
|
74
|
+
Console.info("Running learnpack audit before publishing...")
|
75
|
+
execSync(`learnpack audit ${strict ? "--strict" : ""}`, {
|
76
|
+
stdio: "inherit",
|
77
|
+
})
|
78
|
+
} catch (error) {
|
79
|
+
Console.error("Failed to audit with learnpack")
|
80
|
+
|
81
|
+
// Si `execSync` lanza un error, capturamos el mensaje de error
|
82
|
+
if (error instanceof Error) {
|
83
|
+
Console.error(error.message)
|
84
|
+
} else {
|
85
|
+
Console.error("Unknown error occurred")
|
86
|
+
}
|
87
|
+
|
88
|
+
// Detener la ejecución del comando si `learnpack publish` falla
|
89
|
+
process.exit(1)
|
90
|
+
}
|
91
|
+
|
92
|
+
// Continuar con el proceso de build solo si `learnpack publish` fue exitoso
|
93
|
+
Console.info(
|
94
|
+
"Learnpack publish completed successfully. Proceeding with build..."
|
95
|
+
)
|
96
|
+
}
|
97
|
+
|
98
|
+
type Academy = {
|
99
|
+
id: number
|
100
|
+
name: string
|
101
|
+
slug?: string
|
102
|
+
timezone?: string
|
103
|
+
}
|
104
|
+
|
105
|
+
type Category = {
|
106
|
+
id: number
|
107
|
+
slug: string
|
108
|
+
title: string
|
109
|
+
lang: string
|
110
|
+
academy: Academy
|
111
|
+
}
|
112
|
+
|
113
|
+
function getCategoriesByAcademy(
|
114
|
+
categories: Category[],
|
115
|
+
academy: Academy
|
116
|
+
): Category[] {
|
117
|
+
return categories.filter((cat) => cat.academy.id === academy.id)
|
118
|
+
}
|
119
|
+
|
120
|
+
const selectAcademy = async (
|
121
|
+
academies: TAcademy[],
|
122
|
+
bcToken: string
|
123
|
+
): Promise<{ academy: TAcademy | null; category: number }> => {
|
124
|
+
if (academies.length === 0) {
|
125
|
+
return { academy: null, category: 0 }
|
126
|
+
}
|
127
|
+
|
128
|
+
if (academies.length === 1) {
|
129
|
+
return { academy: academies[0], category: 0 }
|
130
|
+
}
|
131
|
+
|
132
|
+
// prompts the user to select an academy to upload the assets
|
133
|
+
Console.info("In which academy do you want to publish the asset?")
|
134
|
+
const response = await prompts({
|
135
|
+
type: "select",
|
136
|
+
name: "academy",
|
137
|
+
message: "Select an academy",
|
138
|
+
choices: academies.map((academy) => ({
|
139
|
+
title: academy.name,
|
140
|
+
value: academy,
|
141
|
+
})),
|
142
|
+
})
|
143
|
+
|
144
|
+
const categories: Category[] = await api.getCategories(bcToken)
|
145
|
+
const categoriesByAcademy = getCategoriesByAcademy(
|
146
|
+
categories,
|
147
|
+
response.academy
|
148
|
+
)
|
149
|
+
|
150
|
+
const categoriesPrompt = await prompts({
|
151
|
+
type: "select",
|
152
|
+
name: "category",
|
153
|
+
message: "Select a category",
|
154
|
+
choices: categoriesByAcademy.map((category) => ({
|
155
|
+
title: category.title,
|
156
|
+
value: category.id,
|
157
|
+
})),
|
158
|
+
})
|
159
|
+
|
160
|
+
return {
|
161
|
+
academy: response.academy,
|
162
|
+
category: categoriesPrompt.category,
|
163
|
+
}
|
164
|
+
}
|
165
|
+
|
166
|
+
class BuildCommand extends SessionCommand {
|
167
|
+
static description =
|
168
|
+
"Builds the project by copying necessary files and directories into a zip file"
|
169
|
+
|
170
|
+
static flags = {
|
171
|
+
help: flags.help({ char: "h" }),
|
172
|
+
strict: flags.boolean({
|
173
|
+
char: "s",
|
174
|
+
description: "strict mode",
|
175
|
+
default: false,
|
176
|
+
}),
|
177
|
+
}
|
178
|
+
|
179
|
+
async init() {
|
180
|
+
const { flags } = this.parse(BuildCommand)
|
181
|
+
await this.initSession(flags)
|
182
|
+
}
|
183
|
+
|
184
|
+
async run() {
|
185
|
+
const buildDir = path.join(process.cwd(), "build")
|
186
|
+
|
187
|
+
const { flags }: { flags: { strict: boolean } } = this.parse(BuildCommand)
|
188
|
+
const strict = flags.strict
|
189
|
+
Console.debug("Strict mode: ", strict)
|
190
|
+
// this.configManager?.clean()
|
191
|
+
|
192
|
+
const configObject = this.configManager?.get()
|
193
|
+
|
194
|
+
let sessionPayload = await SessionManager.getPayload()
|
195
|
+
|
196
|
+
const sessionExists = sessionPayload && sessionPayload.rigobot
|
197
|
+
|
198
|
+
const isValidToken =
|
199
|
+
sessionExists && sessionPayload.rigobot.key ?
|
200
|
+
await isValidRigoToken(sessionPayload.rigobot.key) :
|
201
|
+
false
|
202
|
+
|
203
|
+
const isValidBreathecodeToken =
|
204
|
+
sessionExists && sessionPayload.token ?
|
205
|
+
await api.validateToken(sessionPayload.token) :
|
206
|
+
false
|
207
|
+
|
208
|
+
if (!sessionExists || !isValidBreathecodeToken || !isValidToken) {
|
209
|
+
Console.info(
|
210
|
+
"Almost there! First you need to login with 4Geeks.com to use AI Generation tool for creators. You can create a new account here: https://4geeks.com/checkout?plan=learnpack-creator"
|
211
|
+
)
|
212
|
+
try {
|
213
|
+
sessionPayload = await SessionManager.login()
|
214
|
+
} catch (error) {
|
215
|
+
Console.error("Error trying to authenticate")
|
216
|
+
Console.error((error as TypeError).message || (error as string))
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
const rigoToken = sessionPayload.rigobot.key
|
221
|
+
|
222
|
+
const consumable = await getConsumable(
|
223
|
+
sessionPayload.token,
|
224
|
+
"learnpack-publish"
|
225
|
+
)
|
226
|
+
|
227
|
+
if (consumable.count === 0) {
|
228
|
+
Console.error(
|
229
|
+
"It seems you cannot publish tutorials. Make sure you creator subscription is up to date here: https://4geeks.com/profile/subscriptions. If you believe there is an issue you can always contact support@4geeks.com"
|
230
|
+
)
|
231
|
+
process.exit(1)
|
232
|
+
}
|
233
|
+
|
234
|
+
if (configObject) {
|
235
|
+
Console.info("Cleaning configuration files")
|
236
|
+
this.configManager?.clean()
|
237
|
+
// build exerises
|
238
|
+
runAudit(strict)
|
239
|
+
|
240
|
+
Console.debug("Building exercises")
|
241
|
+
this.configManager?.buildIndex()
|
242
|
+
}
|
243
|
+
|
244
|
+
const academies = await api.listUserAcademies(sessionPayload.token)
|
245
|
+
|
246
|
+
if (academies.length === 0) {
|
247
|
+
Console.error(
|
248
|
+
"It seems you cannot publish tutorials. Make sure you creator subscription is up to date here: https://4geeks.com/profile/subscriptions. If you believe there is an issue you can always contact support@4geeks.com"
|
249
|
+
)
|
250
|
+
process.exit(1)
|
251
|
+
}
|
252
|
+
|
253
|
+
const { academy, category } = await selectAcademy(
|
254
|
+
academies,
|
255
|
+
sessionPayload.token
|
256
|
+
)
|
257
|
+
|
258
|
+
const learnJsonPath = path.join(process.cwd(), "learn.json")
|
259
|
+
if (!fs.existsSync(learnJsonPath)) {
|
260
|
+
this.error("learn.json not found")
|
261
|
+
}
|
262
|
+
|
263
|
+
const learnJson = JSON.parse(fs.readFileSync(learnJsonPath, "utf-8"))
|
264
|
+
|
265
|
+
const zipFilePath = path.join(process.cwd(), `${learnJson.slug}.zip`)
|
266
|
+
|
267
|
+
// Ensure build directory exists
|
268
|
+
if (!fs.existsSync(buildDir)) {
|
269
|
+
fs.mkdirSync(buildDir)
|
270
|
+
}
|
271
|
+
|
272
|
+
if (configObject) {
|
273
|
+
const { config } = configObject
|
274
|
+
const appAlreadyExists = checkIfDirectoryExists(`${config?.dirPath}/_app`)
|
275
|
+
|
276
|
+
if (!appAlreadyExists) {
|
277
|
+
// download app and decompress
|
278
|
+
await downloadEditor(
|
279
|
+
config?.editor.version,
|
280
|
+
`${config?.dirPath}/app.tar.gz`
|
281
|
+
)
|
282
|
+
|
283
|
+
Console.info("Decompressing LearnPack UI, this may take a minute...")
|
284
|
+
await decompress(
|
285
|
+
`${config?.dirPath}/app.tar.gz`,
|
286
|
+
`${config?.dirPath}/_app/`
|
287
|
+
)
|
288
|
+
}
|
289
|
+
}
|
290
|
+
|
291
|
+
// Copy config.json
|
292
|
+
const configPath = path.join(process.cwd(), ".learn", "config.json")
|
293
|
+
if (fs.existsSync(configPath)) {
|
294
|
+
fs.copyFileSync(configPath, path.join(buildDir, "config.json"))
|
295
|
+
} else {
|
296
|
+
this.error("config.json not found")
|
297
|
+
}
|
298
|
+
|
299
|
+
// Copy .learn/assets directory, if it exists else create it
|
300
|
+
const assetsDir = path.join(process.cwd(), ".learn", "assets")
|
301
|
+
if (fs.existsSync(assetsDir)) {
|
302
|
+
this.copyDirectory(assetsDir, path.join(buildDir, ".learn", "assets"))
|
303
|
+
} else {
|
304
|
+
fs.mkdirSync(path.join(buildDir, ".learn", "assets"), { recursive: true })
|
305
|
+
}
|
306
|
+
|
307
|
+
// Copy .learn/_app directory files to the same level as config.json
|
308
|
+
const appDir = path.join(process.cwd(), ".learn", "_app")
|
309
|
+
if (fs.existsSync(appDir)) {
|
310
|
+
this.copyDirectory(appDir, buildDir)
|
311
|
+
} else {
|
312
|
+
this.error(".learn/_app directory not found")
|
313
|
+
}
|
314
|
+
|
315
|
+
// After copying the _app directory
|
316
|
+
const indexHtmlPath = path.join(appDir, "index.html")
|
317
|
+
const buildIndexHtmlPath = path.join(buildDir, "index.html")
|
318
|
+
const manifestPWA = path.join(appDir, "manifest.webmanifest")
|
319
|
+
const buildManifestPWA = path.join(buildDir, "manifest.webmanifest")
|
320
|
+
|
321
|
+
if (fs.existsSync(indexHtmlPath)) {
|
322
|
+
let indexHtmlContent = fs.readFileSync(indexHtmlPath, "utf-8")
|
323
|
+
|
324
|
+
const description = learnJson.description.us || "LearnPack is awesome!"
|
325
|
+
const title =
|
326
|
+
learnJson.title.us || "LearnPack: Interactive Learning as a Service"
|
327
|
+
|
328
|
+
const previewUrl =
|
329
|
+
learnJson.preview ||
|
330
|
+
"https://raw.githubusercontent.com/learnpack/ide/refs/heads/master/public/learnpack.svg"
|
331
|
+
// Replace placeholders and the <title>Old title </title> tag for a new tag with the title
|
332
|
+
indexHtmlContent = indexHtmlContent
|
333
|
+
.replace(/{{description}}/g, description)
|
334
|
+
.replace(/<title>.*<\/title>/, `<title>${title}</title>`)
|
335
|
+
.replace(/{{title}}/g, title)
|
336
|
+
.replace(/{{preview}}/g, previewUrl)
|
337
|
+
.replace(/{{slug}}/g, learnJson.slug)
|
338
|
+
.replace(/{{duration}}/g, minutesToISO8601Duration(learnJson.duration))
|
339
|
+
|
340
|
+
// Write the modified content to the build directory
|
341
|
+
fs.writeFileSync(buildIndexHtmlPath, indexHtmlContent)
|
342
|
+
} else {
|
343
|
+
this.error("index.html not found in _app directory")
|
344
|
+
}
|
345
|
+
|
346
|
+
if (fs.existsSync(manifestPWA)) {
|
347
|
+
let manifestPWAContent = fs.readFileSync(manifestPWA, "utf-8")
|
348
|
+
manifestPWAContent = manifestPWAContent.replace(
|
349
|
+
"{{course_title}}",
|
350
|
+
learnJson.title.us
|
351
|
+
)
|
352
|
+
|
353
|
+
const courseShortName = { answer: "testing-tutorial" }
|
354
|
+
// const courseShortName = await generateCourseShortName(rigoToken, {
|
355
|
+
// learnJSON: JSON.stringify(learnJson),
|
356
|
+
// })
|
357
|
+
|
358
|
+
manifestPWAContent = manifestPWAContent.replace(
|
359
|
+
"{{course_app_name}}",
|
360
|
+
courseShortName.answer
|
361
|
+
)
|
362
|
+
fs.writeFileSync(buildManifestPWA, manifestPWAContent)
|
363
|
+
} else {
|
364
|
+
this.error("manifest.webmanifest not found in _app directory")
|
365
|
+
}
|
366
|
+
|
367
|
+
// Copy exercises directory
|
368
|
+
const exercisesDir = path.join(process.cwd(), "exercises")
|
369
|
+
const learnExercisesDir = path.join(process.cwd(), ".learn", "exercises")
|
370
|
+
|
371
|
+
if (fs.existsSync(exercisesDir)) {
|
372
|
+
this.copyDirectory(exercisesDir, path.join(buildDir, "exercises"))
|
373
|
+
} else if (fs.existsSync(learnExercisesDir)) {
|
374
|
+
this.copyDirectory(learnExercisesDir, path.join(buildDir, "exercises"))
|
375
|
+
} else {
|
376
|
+
this.error("exercises directory not found in either location")
|
377
|
+
}
|
378
|
+
|
379
|
+
fs.copyFileSync(learnJsonPath, path.join(buildDir, "learn.json"))
|
380
|
+
const sidebarPath = path.join(process.cwd(), ".learn", "sidebar.json")
|
381
|
+
if (fs.existsSync(sidebarPath)) {
|
382
|
+
fs.copyFileSync(sidebarPath, path.join(buildDir, "sidebar.json"))
|
383
|
+
} else {
|
384
|
+
this.error("sidebar.json not found in .learn directory")
|
385
|
+
}
|
386
|
+
|
387
|
+
const output = fs.createWriteStream(zipFilePath)
|
388
|
+
const archive = archiver("zip", {
|
389
|
+
zlib: { level: 9 },
|
390
|
+
})
|
391
|
+
|
392
|
+
output.on("close", async () => {
|
393
|
+
this.log(
|
394
|
+
`Build completed: ${zipFilePath} (${archive.pointer()} total bytes)`
|
395
|
+
)
|
396
|
+
// Remove build directory after zip is created
|
397
|
+
|
398
|
+
Console.debug("Zip file saved in project root")
|
399
|
+
|
400
|
+
const formData = new FormData()
|
401
|
+
formData.append("file", fs.createReadStream(zipFilePath))
|
402
|
+
formData.append("config", JSON.stringify(learnJson))
|
403
|
+
|
404
|
+
try {
|
405
|
+
const res = await axios.post(uploadZipEndpont, formData, {
|
406
|
+
headers: {
|
407
|
+
...formData.getHeaders(),
|
408
|
+
Authorization: `Token ${rigoToken}`,
|
409
|
+
},
|
410
|
+
})
|
411
|
+
console.log(res.data)
|
412
|
+
|
413
|
+
fs.unlinkSync(zipFilePath)
|
414
|
+
this.removeDirectory(buildDir)
|
415
|
+
|
416
|
+
if (academy) {
|
417
|
+
await handleAssetCreation(
|
418
|
+
sessionPayload,
|
419
|
+
academy,
|
420
|
+
learnJson,
|
421
|
+
res.data.url,
|
422
|
+
category
|
423
|
+
)
|
424
|
+
}
|
425
|
+
} catch (error) {
|
426
|
+
if (axios.isAxiosError(error)) {
|
427
|
+
if (error.response && error.response.status === 403) {
|
428
|
+
console.error("Error 403:", error.response.data.error)
|
429
|
+
} else if (error.response && error.response.status === 400) {
|
430
|
+
console.error(error.response.data.error)
|
431
|
+
} else {
|
432
|
+
console.error("Error uploading file:", error)
|
433
|
+
}
|
434
|
+
} else {
|
435
|
+
console.error("Error uploading file:", error)
|
436
|
+
}
|
437
|
+
|
438
|
+
// fs.unlinkSync(zipFilePath)
|
439
|
+
// this.removeDirectory(buildDir)
|
440
|
+
}
|
441
|
+
})
|
442
|
+
|
443
|
+
archive.on("error", (err: any) => {
|
444
|
+
throw err
|
445
|
+
})
|
446
|
+
|
447
|
+
archive.pipe(output)
|
448
|
+
archive.directory(buildDir, false)
|
449
|
+
await archive.finalize()
|
450
|
+
}
|
451
|
+
|
452
|
+
copyDirectory(src: string, dest: string) {
|
453
|
+
if (!fs.existsSync(dest)) {
|
454
|
+
fs.mkdirSync(dest, { recursive: true })
|
455
|
+
}
|
456
|
+
|
457
|
+
const entries = fs.readdirSync(src, { withFileTypes: true })
|
458
|
+
|
459
|
+
for (const entry of entries) {
|
460
|
+
const srcPath = path.join(src, entry.name)
|
461
|
+
const destPath = path.join(dest, entry.name)
|
462
|
+
|
463
|
+
if (entry.isDirectory()) {
|
464
|
+
this.copyDirectory(srcPath, destPath)
|
465
|
+
} else {
|
466
|
+
fs.copyFileSync(srcPath, destPath)
|
467
|
+
}
|
468
|
+
}
|
469
|
+
}
|
470
|
+
|
471
|
+
removeDirectory(dir: string) {
|
472
|
+
if (fs.existsSync(dir)) {
|
473
|
+
fs.readdirSync(dir).forEach((file) => {
|
474
|
+
const currentPath = path.join(dir, file)
|
475
|
+
if (fs.lstatSync(currentPath).isDirectory()) {
|
476
|
+
this.removeDirectory(currentPath)
|
477
|
+
} else {
|
478
|
+
fs.unlinkSync(currentPath)
|
479
|
+
}
|
480
|
+
})
|
481
|
+
fs.rmdirSync(dir)
|
482
|
+
}
|
483
|
+
}
|
484
|
+
}
|
485
|
+
|
486
|
+
BuildCommand.flags = {
|
487
|
+
strict: flags.boolean({
|
488
|
+
char: "s",
|
489
|
+
description: "strict mode",
|
490
|
+
default: false,
|
491
|
+
}),
|
492
|
+
help: flags.help({ char: "h" }),
|
493
|
+
}
|
494
|
+
|
495
|
+
export default BuildCommand
|