@learnpack/learnpack 5.0.272 → 5.0.274

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