@learnpack/learnpack 5.0.274 → 5.0.276

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