@learnpack/learnpack 5.0.270 → 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 (77) 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 +48 -20
  8. package/lib/creatorDist/assets/{index-CQXTTbaZ.js → index-BfLyIQVh.js} +11535 -11409
  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/models/creator.d.ts +1 -0
  13. package/lib/utils/api.js +1 -0
  14. package/lib/utils/creatorUtilities.js +14 -14
  15. package/package.json +1 -1
  16. package/src/commands/audit.ts +487 -487
  17. package/src/commands/breakToken.ts +67 -67
  18. package/src/commands/clean.ts +30 -30
  19. package/src/commands/init.ts +650 -650
  20. package/src/commands/logout.ts +38 -38
  21. package/src/commands/publish.ts +522 -522
  22. package/src/commands/serve.ts +64 -33
  23. package/src/commands/start.ts +333 -333
  24. package/src/commands/translate.ts +123 -123
  25. package/src/creator/README.md +54 -54
  26. package/src/creator/eslint.config.js +28 -28
  27. package/src/creator/src/components/syllabus/ContentIndex.tsx +1 -1
  28. package/src/creator/src/components/syllabus/SyllabusEditor.tsx +3 -1
  29. package/src/creator/src/i18n.ts +28 -28
  30. package/src/creator/src/index.css +217 -217
  31. package/src/creator/src/locales/en.json +1 -0
  32. package/src/creator/src/locales/es.json +1 -0
  33. package/src/creator/src/utils/configTypes.ts +122 -122
  34. package/src/creator/src/utils/constants.ts +13 -13
  35. package/src/creator/src/utils/creatorUtils.ts +46 -46
  36. package/src/creator/src/utils/eventBus.ts +2 -2
  37. package/src/creator/src/utils/lib.ts +468 -468
  38. package/src/creator/src/utils/rigo.ts +26 -26
  39. package/src/creator/src/utils/socket.ts +61 -61
  40. package/src/creator/src/utils/store.ts +222 -222
  41. package/src/creator/src/vite-env.d.ts +1 -1
  42. package/src/creator/vite.config.ts +13 -13
  43. package/src/creatorDist/assets/{index-CQXTTbaZ.js → index-BfLyIQVh.js} +11535 -11409
  44. package/src/creatorDist/assets/{index-B4khtb0r.css → index-C39zeF3W.css} +3 -3
  45. package/src/creatorDist/index.html +2 -2
  46. package/src/managers/config/defaults.ts +49 -49
  47. package/src/managers/config/exercise.ts +364 -364
  48. package/src/managers/config/index.ts +775 -775
  49. package/src/managers/file.ts +236 -236
  50. package/src/managers/server/routes.ts +554 -554
  51. package/src/managers/session.ts +182 -182
  52. package/src/managers/telemetry.ts +188 -188
  53. package/src/models/action.ts +13 -13
  54. package/src/models/config-manager.ts +28 -28
  55. package/src/models/config.ts +106 -106
  56. package/src/models/creator.ts +40 -39
  57. package/src/models/exercise-obj.ts +30 -30
  58. package/src/models/session.ts +39 -39
  59. package/src/models/socket.ts +61 -61
  60. package/src/models/status.ts +16 -16
  61. package/src/ui/_app/app.css +1 -1
  62. package/src/ui/_app/app.js +435 -414
  63. package/src/ui/_app/learnpack.svg +7 -7
  64. package/src/ui/app.tar.gz +0 -0
  65. package/src/utils/BaseCommand.ts +56 -56
  66. package/src/utils/api.ts +31 -30
  67. package/src/utils/audit.ts +392 -392
  68. package/src/utils/checkNotInstalled.ts +267 -267
  69. package/src/utils/configBuilder.ts +82 -82
  70. package/src/utils/convertCreds.js +34 -34
  71. package/src/utils/creatorUtilities.ts +504 -504
  72. package/src/utils/incrementVersion.js +74 -74
  73. package/src/utils/misc.ts +58 -58
  74. package/src/utils/rigoActions.ts +500 -500
  75. package/src/utils/sidebarGenerator.ts +195 -195
  76. package/src/utils/templates/isolated/exercises/01-hello-world/README.es.md +26 -26
  77. package/src/utils/templates/isolated/exercises/01-hello-world/README.md +26 -26
@@ -1,487 +1,487 @@
1
- import * as fs from "fs"
2
- import { validateExerciseDirectoryName } from "../managers/config/exercise"
3
- import Console from "../utils/console"
4
- import Audit from "../utils/audit"
5
- import SessionCommand from "../utils/SessionCommand"
6
- import * as path from "path"
7
- import { IFile } from "../models/file"
8
- import { IExercise } from "../models/exercise-obj"
9
- import { IFrontmatter } from "../models/front-matter"
10
- import { IAuditErrors } from "../models/audit"
11
- import { ICounter } from "../models/counter"
12
- import { IFindings } from "../models/findings"
13
- import { flags } from "@oclif/command"
14
- import { checkAndFixSidebar } from "../utils/sidebarGenerator"
15
-
16
- // eslint-disable-next-line
17
- const fetch = require("node-fetch")
18
-
19
- class AuditCommand extends SessionCommand {
20
- async init() {
21
- const { flags } = this.parse(AuditCommand)
22
- await this.initSession(flags)
23
- }
24
-
25
- async run() {
26
- const { flags }: { flags: { strict: boolean } } = this.parse(AuditCommand)
27
-
28
- const strict = flags.strict
29
- Console.log("Running command audit...")
30
-
31
- // Get configuration object.
32
- let config = this.configManager?.get()
33
-
34
- if (config) {
35
- const errors: IAuditErrors[] = []
36
- const warnings: IAuditErrors[] = []
37
- if (config?.config?.projectType === "tutorial") {
38
- const counter: ICounter = {
39
- images: {
40
- error: 0,
41
- total: 0,
42
- },
43
- links: {
44
- error: 0,
45
- total: 0,
46
- },
47
- exercises: 0,
48
- readmeFiles: 0,
49
- }
50
-
51
- // Checks if learnpack clean has been run
52
- Audit.checkLearnpackClean(config, errors)
53
-
54
- // Build exercises if they are not built yet.
55
- this.configManager?.buildIndex()
56
- config = this.configManager?.get()
57
-
58
- // Check if the exercises folder has some files within any ./exercise
59
- const exercisesPath: string = config!.config!.exercisesPath
60
-
61
- fs.readdir(exercisesPath, (err, files) => {
62
- if (err) {
63
- return console.log("Unable to scan directory: " + err)
64
- }
65
-
66
- // listing all files using forEach
67
- for (const file of files) {
68
- // Do whatever you want to do with the file
69
- const filePath: string = path.join(exercisesPath, file)
70
- if (fs.statSync(filePath).isFile())
71
- warnings.push({
72
- exercise: file!,
73
- msg: "This file is not inside any exercise folder.",
74
- })
75
- }
76
- })
77
-
78
- // This function is being created because the find method doesn't work with promises.
79
- const find = async (file: IFile, lang: string, exercise: IExercise) => {
80
- if (file.name === lang) {
81
- await Audit.checkUrl(
82
- config!,
83
- file.path,
84
- file.name,
85
- exercise,
86
- errors,
87
- warnings,
88
- counter
89
- )
90
- return true
91
- }
92
-
93
- return false
94
- }
95
-
96
- Console.debug("config", config)
97
-
98
- Console.info(" Checking if the config file is fine...")
99
- // These two lines check if the 'slug' property is inside the configuration object.
100
- Console.debug(
101
- "Checking if the slug property is inside the configuration object..."
102
- )
103
- // check if the slug property is in the configuration object
104
- if (!config!.config?.slug)
105
- errors.push({
106
- exercise: undefined,
107
- msg: "The slug property is not in the configuration object",
108
- })
109
-
110
- if (config!.config?.slug && !Audit.checkSlug(config!.config?.slug)) {
111
- errors.push({
112
- exercise: undefined,
113
- msg: "The slug property is not a valid slug. It must start and end with a letter and be less than 50 characters.",
114
- })
115
- }
116
-
117
- Console.info("Checking if the sidebar.json file is fine...")
118
- const isSidebarFine = await checkAndFixSidebar(config!, false)
119
- if (!isSidebarFine) {
120
- if (strict) {
121
- errors.push({
122
- exercise: undefined,
123
- msg: "The sidebar.json file has errors, please fill all missing translations",
124
- })
125
- } else {
126
- warnings.push({
127
- exercise: undefined,
128
- msg: "The sidebar.json file has errors, please fill all missing translations",
129
- })
130
- }
131
- }
132
-
133
- // check if the duration property is in the configuration object
134
- if (!config!.config?.duration)
135
- warnings.push({
136
- exercise: undefined,
137
- msg: "The duration property is not in the configuration object",
138
- })
139
- // check if the difficulty property is in the configuration object
140
- if (!config!.config?.difficulty)
141
- warnings.push({
142
- exercise: undefined,
143
- msg: "The difficulty property is not in the configuration object",
144
- })
145
- // check if the bugs_link property is in the configuration object
146
- if (!config!.config?.bugsLink) {
147
- if (strict)
148
- errors.push({
149
- exercise: undefined,
150
- msg: "The bugsLink property is not in the configuration object",
151
- })
152
- else
153
- warnings.push({
154
- exercise: undefined,
155
- msg: "The bugsLink property is not in the configuration object",
156
- })
157
- }
158
-
159
- // check if the video_solutions property is in the configuration object
160
- if (config!.config?.videoSolutions === undefined)
161
- warnings.push({
162
- exercise: undefined,
163
- msg: "The videoSolutions property is not in the configuration object",
164
- })
165
-
166
- // These two lines check if the 'repository' property is inside the configuration object.
167
- Console.debug(
168
- "Checking if the repository property is inside the configuration object..."
169
- )
170
- if (!config!.config?.repository) {
171
- if (strict)
172
- errors.push({
173
- exercise: undefined,
174
- msg: "The repository property is not in the configuration object",
175
- })
176
- else
177
- warnings.push({
178
- exercise: undefined,
179
- msg: "The repository property is not in the configuration object",
180
- })
181
- } else Audit.isUrl(config!.config?.repository, errors, counter)
182
-
183
- // These two lines check if the 'description' property is inside the configuration object.
184
- Console.debug(
185
- "Checking if the description property is inside the configuration object..."
186
- )
187
- if (!config!.config?.description)
188
- errors.push({
189
- exercise: undefined,
190
- msg: "The description property is not in the configuration object",
191
- })
192
- if (!config!.config?.projectType)
193
- errors.push({
194
- exercise: undefined,
195
- msg: "The projectType property is mandatory in the configuration object",
196
- })
197
-
198
- if (errors.length === 0) Console.log("The config file is ok")
199
-
200
- // Validates if images and links are working at every README file.
201
- const exercises = config!.exercises
202
- const readmeFiles = []
203
-
204
- if (exercises && exercises.length > 0) {
205
- Console.info(" Checking if the images are working...")
206
- for (const index in exercises) {
207
- if (Object.prototype.hasOwnProperty.call(exercises, index)) {
208
- const exercise = exercises[index]
209
- if (!validateExerciseDirectoryName(exercise.title))
210
- errors.push({
211
- exercise: exercise.title,
212
- msg: `The exercise ${exercise.title} has an invalid name.`,
213
- })
214
- let readmeFilesCount = { exercise: exercise.title, count: 0 }
215
- if (Object.keys(exercise.translations!).length === 0)
216
- errors.push({
217
- exercise: exercise.title,
218
- msg: `The exercise ${exercise.title} doesn't have a README.md file.`,
219
- })
220
-
221
- if (
222
- exercise.language === "python3" ||
223
- exercise.language === "python"
224
- ) {
225
- for (const f of exercise.files.map(f => f)) {
226
- if (
227
- f.path.includes("test.py") ||
228
- f.path.includes("tests.py")
229
- ) {
230
- const content = fs.readFileSync(f.path).toString()
231
- const isEmpty = Audit.checkForEmptySpaces(content)
232
- if (isEmpty || !content)
233
- errors.push({
234
- exercise: exercise.title,
235
- msg: `This file (${f.name}) doesn't have any content inside.`,
236
- })
237
- }
238
- }
239
- } else {
240
- for (const f of exercise.files.map(f => f)) {
241
- if (
242
- f.path.includes("test.js") ||
243
- f.path.includes("tests.js")
244
- ) {
245
- const content = fs.readFileSync(f.path).toString()
246
- const isEmpty: boolean = Audit.checkForEmptySpaces(content)
247
- if (isEmpty || !content)
248
- errors.push({
249
- exercise: exercise.title,
250
- msg: `This file (${f.name}) doesn't have any content inside.`,
251
- })
252
- }
253
- }
254
- }
255
-
256
- for (const lang in exercise.translations) {
257
- if (
258
- Object.prototype.hasOwnProperty.call(
259
- exercise.translations,
260
- lang
261
- )
262
- ) {
263
- const files: any[] = []
264
- const findResultPromises = []
265
- for (const file of exercise.files) {
266
- const found = find(
267
- file,
268
- exercise.translations[lang],
269
- exercise
270
- )
271
- findResultPromises.push(found)
272
- }
273
- // eslint-disable-next-line
274
- let findResults = await Promise.all(findResultPromises)
275
- for (const found of findResults) {
276
- if (found) {
277
- readmeFilesCount = {
278
- ...readmeFilesCount,
279
- count: readmeFilesCount.count + 1,
280
- }
281
- files.push(found)
282
- }
283
- }
284
-
285
- if (!files.includes(true))
286
- errors.push({
287
- exercise: exercise.title,
288
- msg: "This exercise doesn't have a README.md file.",
289
- })
290
- }
291
- }
292
-
293
- readmeFiles.push(readmeFilesCount)
294
- }
295
- }
296
- } else
297
- errors.push({
298
- exercise: undefined,
299
- msg: "The exercises array is empty.",
300
- })
301
-
302
- Console.log(
303
- `${counter.images.total - counter.images.error} images ok from ${
304
- counter.images.total
305
- }`
306
- )
307
-
308
- Console.info(
309
- " Checking if important files are missing... (README's, translations, gitignore...)"
310
- )
311
- // Check if all the exercises has the same ammount of README's, this way we can check if they have the same ammount of translations.
312
- const files: string[] = []
313
- let count = 0
314
- for (const item of readmeFiles) {
315
- if (count < item.count) count = item.count
316
- }
317
-
318
- for (const item of readmeFiles) {
319
- if (item.count !== count) files.push(` ${item.exercise}`)
320
- }
321
-
322
- if (files.length > 0) {
323
- const filesString: string = files.join(",")
324
- warnings.push({
325
- exercise: undefined,
326
- msg:
327
- files.length === 1 ?
328
- `This exercise is missing translations:${filesString}` :
329
- `These exercises are missing translations:${filesString}`,
330
- })
331
- }
332
-
333
- // Checks if the .gitignore file exists.
334
- if (!fs.existsSync(".gitignore"))
335
- warnings.push({
336
- exercise: undefined,
337
- msg: ".gitignore file doesn't exist",
338
- })
339
-
340
- counter.exercises = exercises!.length
341
- for (const readme of readmeFiles) {
342
- counter.readmeFiles += readme.count
343
- }
344
- } else {
345
- // This is the audit code for Projects
346
-
347
- // Checking the "learn.json" file:
348
- let learnjson
349
- try {
350
- learnjson = JSON.parse(fs.readFileSync("./learn.json").toString())
351
- } catch (_error: any) {
352
- Console.error(
353
- "Invalid JSON syntax in learn.json file:",
354
- _error.message
355
- )
356
- process.exit(1)
357
- }
358
-
359
- if (!learnjson) {
360
- Console.error(
361
- "There is no learn.json file located in the root of the project."
362
- )
363
- process.exit(1)
364
- }
365
-
366
- // Checking the README.md files and possible translations.
367
- let readmeFiles: any[] = []
368
- const translations: string[] = []
369
- const translationRegex = /README\.([a-z]{2,3})\.md/
370
-
371
- try {
372
- const data = await fs.promises.readdir("./")
373
- readmeFiles = data.filter(file => file.includes("README"))
374
- if (readmeFiles.length === 0)
375
- errors.push({
376
- exercise: undefined!,
377
- msg: `There is no README file in the repository.`,
378
- })
379
- } catch (error) {
380
- if (error)
381
- Console.error(
382
- "There was an error getting the directory files",
383
- error
384
- )
385
- }
386
-
387
- for (const readmeFile of readmeFiles) {
388
- // Checking the language of each README file.
389
- if (readmeFile === "README.md") translations.push("en")
390
- else {
391
- const regexGroups = translationRegex.exec(readmeFile)
392
- if (regexGroups) translations.push(regexGroups[1])
393
- }
394
-
395
- const readme = fs.readFileSync(path.resolve(readmeFile)).toString()
396
-
397
- const isEmpty = Audit.checkForEmptySpaces(readme)
398
- if (isEmpty || !readme) {
399
- errors.push({
400
- exercise: undefined!,
401
- msg: `This file "${readmeFile}" doesn't have any content inside.`,
402
- })
403
- continue
404
- }
405
-
406
- if (readme.length < 800)
407
- errors.push({
408
- exercise: undefined,
409
- msg: `The "${readmeFile}" file should have at least 800 characters (It currently have: ${readme.length}).`,
410
- })
411
-
412
- // eslint-disable-next-line
413
- await Audit.checkUrl(
414
- config!,
415
- path.resolve(readmeFile),
416
- readmeFile,
417
- undefined,
418
- errors,
419
- warnings,
420
- // eslint-disable-next-line
421
- undefined
422
- )
423
- }
424
-
425
- // Adding the translations to the learn.json
426
- learnjson.translations = translations
427
-
428
- // Checking if the preview image (from the learn.json) is OK.
429
- try {
430
- const res = await fetch(learnjson.preview, { method: "HEAD" })
431
- if (res.status > 399 && res.status < 500) {
432
- errors.push({
433
- exercise: undefined,
434
- msg: `The link of the "preview" is broken: ${learnjson.preview}`,
435
- })
436
- }
437
- } catch {
438
- errors.push({
439
- exercise: undefined,
440
- msg: `The link of the "preview" is broken: ${learnjson.preview}`,
441
- })
442
- }
443
-
444
- const date = new Date()
445
- learnjson.validationAt = date.getTime()
446
-
447
- if (errors.length > 0) learnjson.validationStatus = "error"
448
- else if (warnings.length > 0) learnjson.validationStatus = "warning"
449
- else learnjson.validationStatus = "success"
450
-
451
- // Writes the "learn.json" file with all the new properties
452
- await fs.promises.writeFile("./learn.json", JSON.stringify(learnjson))
453
- }
454
-
455
- await Audit.showWarnings(warnings)
456
- // eslint-disable-next-line
457
- await Audit.showErrors(errors, undefined)
458
- }
459
- }
460
- }
461
-
462
- AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
463
- ...
464
- learnpack audit checks for the following information in a repository:
465
- 1. The configuration object has slug, repository and description. (Error)
466
- 2. The command learnpack clean has been run. (Error)
467
- 3. If a markdown or test file doesn't have any content. (Error)
468
- 4. The links are accessing to valid servers. (Error)
469
- 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
470
- 6. The external images are working (If they are pointing to a valid server). (Error)
471
- 7. The exercises directory names are valid. (Error)
472
- 8. If an exercise doesn't have a README file. (Error)
473
- 9. The exercises array (Of the config file) has content. (Error)
474
- 10. The exercses have the same translations. (Warning)
475
- 11. The .gitignore file exists. (Warning)
476
- 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
477
- `
478
-
479
- AuditCommand.flags = {
480
- strict: flags.boolean({
481
- char: "s",
482
- description: "strict mode",
483
- default: false,
484
- }),
485
- }
486
-
487
- export default AuditCommand
1
+ import * as fs from "fs"
2
+ import { validateExerciseDirectoryName } from "../managers/config/exercise"
3
+ import Console from "../utils/console"
4
+ import Audit from "../utils/audit"
5
+ import SessionCommand from "../utils/SessionCommand"
6
+ import * as path from "path"
7
+ import { IFile } from "../models/file"
8
+ import { IExercise } from "../models/exercise-obj"
9
+ import { IFrontmatter } from "../models/front-matter"
10
+ import { IAuditErrors } from "../models/audit"
11
+ import { ICounter } from "../models/counter"
12
+ import { IFindings } from "../models/findings"
13
+ import { flags } from "@oclif/command"
14
+ import { checkAndFixSidebar } from "../utils/sidebarGenerator"
15
+
16
+ // eslint-disable-next-line
17
+ const fetch = require("node-fetch")
18
+
19
+ class AuditCommand extends SessionCommand {
20
+ async init() {
21
+ const { flags } = this.parse(AuditCommand)
22
+ await this.initSession(flags)
23
+ }
24
+
25
+ async run() {
26
+ const { flags }: { flags: { strict: boolean } } = this.parse(AuditCommand)
27
+
28
+ const strict = flags.strict
29
+ Console.log("Running command audit...")
30
+
31
+ // Get configuration object.
32
+ let config = this.configManager?.get()
33
+
34
+ if (config) {
35
+ const errors: IAuditErrors[] = []
36
+ const warnings: IAuditErrors[] = []
37
+ if (config?.config?.projectType === "tutorial") {
38
+ const counter: ICounter = {
39
+ images: {
40
+ error: 0,
41
+ total: 0,
42
+ },
43
+ links: {
44
+ error: 0,
45
+ total: 0,
46
+ },
47
+ exercises: 0,
48
+ readmeFiles: 0,
49
+ }
50
+
51
+ // Checks if learnpack clean has been run
52
+ Audit.checkLearnpackClean(config, errors)
53
+
54
+ // Build exercises if they are not built yet.
55
+ this.configManager?.buildIndex()
56
+ config = this.configManager?.get()
57
+
58
+ // Check if the exercises folder has some files within any ./exercise
59
+ const exercisesPath: string = config!.config!.exercisesPath
60
+
61
+ fs.readdir(exercisesPath, (err, files) => {
62
+ if (err) {
63
+ return console.log("Unable to scan directory: " + err)
64
+ }
65
+
66
+ // listing all files using forEach
67
+ for (const file of files) {
68
+ // Do whatever you want to do with the file
69
+ const filePath: string = path.join(exercisesPath, file)
70
+ if (fs.statSync(filePath).isFile())
71
+ warnings.push({
72
+ exercise: file!,
73
+ msg: "This file is not inside any exercise folder.",
74
+ })
75
+ }
76
+ })
77
+
78
+ // This function is being created because the find method doesn't work with promises.
79
+ const find = async (file: IFile, lang: string, exercise: IExercise) => {
80
+ if (file.name === lang) {
81
+ await Audit.checkUrl(
82
+ config!,
83
+ file.path,
84
+ file.name,
85
+ exercise,
86
+ errors,
87
+ warnings,
88
+ counter
89
+ )
90
+ return true
91
+ }
92
+
93
+ return false
94
+ }
95
+
96
+ Console.debug("config", config)
97
+
98
+ Console.info(" Checking if the config file is fine...")
99
+ // These two lines check if the 'slug' property is inside the configuration object.
100
+ Console.debug(
101
+ "Checking if the slug property is inside the configuration object..."
102
+ )
103
+ // check if the slug property is in the configuration object
104
+ if (!config!.config?.slug)
105
+ errors.push({
106
+ exercise: undefined,
107
+ msg: "The slug property is not in the configuration object",
108
+ })
109
+
110
+ if (config!.config?.slug && !Audit.checkSlug(config!.config?.slug)) {
111
+ errors.push({
112
+ exercise: undefined,
113
+ msg: "The slug property is not a valid slug. It must start and end with a letter and be less than 50 characters.",
114
+ })
115
+ }
116
+
117
+ Console.info("Checking if the sidebar.json file is fine...")
118
+ const isSidebarFine = await checkAndFixSidebar(config!, false)
119
+ if (!isSidebarFine) {
120
+ if (strict) {
121
+ errors.push({
122
+ exercise: undefined,
123
+ msg: "The sidebar.json file has errors, please fill all missing translations",
124
+ })
125
+ } else {
126
+ warnings.push({
127
+ exercise: undefined,
128
+ msg: "The sidebar.json file has errors, please fill all missing translations",
129
+ })
130
+ }
131
+ }
132
+
133
+ // check if the duration property is in the configuration object
134
+ if (!config!.config?.duration)
135
+ warnings.push({
136
+ exercise: undefined,
137
+ msg: "The duration property is not in the configuration object",
138
+ })
139
+ // check if the difficulty property is in the configuration object
140
+ if (!config!.config?.difficulty)
141
+ warnings.push({
142
+ exercise: undefined,
143
+ msg: "The difficulty property is not in the configuration object",
144
+ })
145
+ // check if the bugs_link property is in the configuration object
146
+ if (!config!.config?.bugsLink) {
147
+ if (strict)
148
+ errors.push({
149
+ exercise: undefined,
150
+ msg: "The bugsLink property is not in the configuration object",
151
+ })
152
+ else
153
+ warnings.push({
154
+ exercise: undefined,
155
+ msg: "The bugsLink property is not in the configuration object",
156
+ })
157
+ }
158
+
159
+ // check if the video_solutions property is in the configuration object
160
+ if (config!.config?.videoSolutions === undefined)
161
+ warnings.push({
162
+ exercise: undefined,
163
+ msg: "The videoSolutions property is not in the configuration object",
164
+ })
165
+
166
+ // These two lines check if the 'repository' property is inside the configuration object.
167
+ Console.debug(
168
+ "Checking if the repository property is inside the configuration object..."
169
+ )
170
+ if (!config!.config?.repository) {
171
+ if (strict)
172
+ errors.push({
173
+ exercise: undefined,
174
+ msg: "The repository property is not in the configuration object",
175
+ })
176
+ else
177
+ warnings.push({
178
+ exercise: undefined,
179
+ msg: "The repository property is not in the configuration object",
180
+ })
181
+ } else Audit.isUrl(config!.config?.repository, errors, counter)
182
+
183
+ // These two lines check if the 'description' property is inside the configuration object.
184
+ Console.debug(
185
+ "Checking if the description property is inside the configuration object..."
186
+ )
187
+ if (!config!.config?.description)
188
+ errors.push({
189
+ exercise: undefined,
190
+ msg: "The description property is not in the configuration object",
191
+ })
192
+ if (!config!.config?.projectType)
193
+ errors.push({
194
+ exercise: undefined,
195
+ msg: "The projectType property is mandatory in the configuration object",
196
+ })
197
+
198
+ if (errors.length === 0) Console.log("The config file is ok")
199
+
200
+ // Validates if images and links are working at every README file.
201
+ const exercises = config!.exercises
202
+ const readmeFiles = []
203
+
204
+ if (exercises && exercises.length > 0) {
205
+ Console.info(" Checking if the images are working...")
206
+ for (const index in exercises) {
207
+ if (Object.prototype.hasOwnProperty.call(exercises, index)) {
208
+ const exercise = exercises[index]
209
+ if (!validateExerciseDirectoryName(exercise.title))
210
+ errors.push({
211
+ exercise: exercise.title,
212
+ msg: `The exercise ${exercise.title} has an invalid name.`,
213
+ })
214
+ let readmeFilesCount = { exercise: exercise.title, count: 0 }
215
+ if (Object.keys(exercise.translations!).length === 0)
216
+ errors.push({
217
+ exercise: exercise.title,
218
+ msg: `The exercise ${exercise.title} doesn't have a README.md file.`,
219
+ })
220
+
221
+ if (
222
+ exercise.language === "python3" ||
223
+ exercise.language === "python"
224
+ ) {
225
+ for (const f of exercise.files.map(f => f)) {
226
+ if (
227
+ f.path.includes("test.py") ||
228
+ f.path.includes("tests.py")
229
+ ) {
230
+ const content = fs.readFileSync(f.path).toString()
231
+ const isEmpty = Audit.checkForEmptySpaces(content)
232
+ if (isEmpty || !content)
233
+ errors.push({
234
+ exercise: exercise.title,
235
+ msg: `This file (${f.name}) doesn't have any content inside.`,
236
+ })
237
+ }
238
+ }
239
+ } else {
240
+ for (const f of exercise.files.map(f => f)) {
241
+ if (
242
+ f.path.includes("test.js") ||
243
+ f.path.includes("tests.js")
244
+ ) {
245
+ const content = fs.readFileSync(f.path).toString()
246
+ const isEmpty: boolean = Audit.checkForEmptySpaces(content)
247
+ if (isEmpty || !content)
248
+ errors.push({
249
+ exercise: exercise.title,
250
+ msg: `This file (${f.name}) doesn't have any content inside.`,
251
+ })
252
+ }
253
+ }
254
+ }
255
+
256
+ for (const lang in exercise.translations) {
257
+ if (
258
+ Object.prototype.hasOwnProperty.call(
259
+ exercise.translations,
260
+ lang
261
+ )
262
+ ) {
263
+ const files: any[] = []
264
+ const findResultPromises = []
265
+ for (const file of exercise.files) {
266
+ const found = find(
267
+ file,
268
+ exercise.translations[lang],
269
+ exercise
270
+ )
271
+ findResultPromises.push(found)
272
+ }
273
+ // eslint-disable-next-line
274
+ let findResults = await Promise.all(findResultPromises)
275
+ for (const found of findResults) {
276
+ if (found) {
277
+ readmeFilesCount = {
278
+ ...readmeFilesCount,
279
+ count: readmeFilesCount.count + 1,
280
+ }
281
+ files.push(found)
282
+ }
283
+ }
284
+
285
+ if (!files.includes(true))
286
+ errors.push({
287
+ exercise: exercise.title,
288
+ msg: "This exercise doesn't have a README.md file.",
289
+ })
290
+ }
291
+ }
292
+
293
+ readmeFiles.push(readmeFilesCount)
294
+ }
295
+ }
296
+ } else
297
+ errors.push({
298
+ exercise: undefined,
299
+ msg: "The exercises array is empty.",
300
+ })
301
+
302
+ Console.log(
303
+ `${counter.images.total - counter.images.error} images ok from ${
304
+ counter.images.total
305
+ }`
306
+ )
307
+
308
+ Console.info(
309
+ " Checking if important files are missing... (README's, translations, gitignore...)"
310
+ )
311
+ // Check if all the exercises has the same ammount of README's, this way we can check if they have the same ammount of translations.
312
+ const files: string[] = []
313
+ let count = 0
314
+ for (const item of readmeFiles) {
315
+ if (count < item.count) count = item.count
316
+ }
317
+
318
+ for (const item of readmeFiles) {
319
+ if (item.count !== count) files.push(` ${item.exercise}`)
320
+ }
321
+
322
+ if (files.length > 0) {
323
+ const filesString: string = files.join(",")
324
+ warnings.push({
325
+ exercise: undefined,
326
+ msg:
327
+ files.length === 1 ?
328
+ `This exercise is missing translations:${filesString}` :
329
+ `These exercises are missing translations:${filesString}`,
330
+ })
331
+ }
332
+
333
+ // Checks if the .gitignore file exists.
334
+ if (!fs.existsSync(".gitignore"))
335
+ warnings.push({
336
+ exercise: undefined,
337
+ msg: ".gitignore file doesn't exist",
338
+ })
339
+
340
+ counter.exercises = exercises!.length
341
+ for (const readme of readmeFiles) {
342
+ counter.readmeFiles += readme.count
343
+ }
344
+ } else {
345
+ // This is the audit code for Projects
346
+
347
+ // Checking the "learn.json" file:
348
+ let learnjson
349
+ try {
350
+ learnjson = JSON.parse(fs.readFileSync("./learn.json").toString())
351
+ } catch (_error: any) {
352
+ Console.error(
353
+ "Invalid JSON syntax in learn.json file:",
354
+ _error.message
355
+ )
356
+ process.exit(1)
357
+ }
358
+
359
+ if (!learnjson) {
360
+ Console.error(
361
+ "There is no learn.json file located in the root of the project."
362
+ )
363
+ process.exit(1)
364
+ }
365
+
366
+ // Checking the README.md files and possible translations.
367
+ let readmeFiles: any[] = []
368
+ const translations: string[] = []
369
+ const translationRegex = /README\.([a-z]{2,3})\.md/
370
+
371
+ try {
372
+ const data = await fs.promises.readdir("./")
373
+ readmeFiles = data.filter(file => file.includes("README"))
374
+ if (readmeFiles.length === 0)
375
+ errors.push({
376
+ exercise: undefined!,
377
+ msg: `There is no README file in the repository.`,
378
+ })
379
+ } catch (error) {
380
+ if (error)
381
+ Console.error(
382
+ "There was an error getting the directory files",
383
+ error
384
+ )
385
+ }
386
+
387
+ for (const readmeFile of readmeFiles) {
388
+ // Checking the language of each README file.
389
+ if (readmeFile === "README.md") translations.push("en")
390
+ else {
391
+ const regexGroups = translationRegex.exec(readmeFile)
392
+ if (regexGroups) translations.push(regexGroups[1])
393
+ }
394
+
395
+ const readme = fs.readFileSync(path.resolve(readmeFile)).toString()
396
+
397
+ const isEmpty = Audit.checkForEmptySpaces(readme)
398
+ if (isEmpty || !readme) {
399
+ errors.push({
400
+ exercise: undefined!,
401
+ msg: `This file "${readmeFile}" doesn't have any content inside.`,
402
+ })
403
+ continue
404
+ }
405
+
406
+ if (readme.length < 800)
407
+ errors.push({
408
+ exercise: undefined,
409
+ msg: `The "${readmeFile}" file should have at least 800 characters (It currently have: ${readme.length}).`,
410
+ })
411
+
412
+ // eslint-disable-next-line
413
+ await Audit.checkUrl(
414
+ config!,
415
+ path.resolve(readmeFile),
416
+ readmeFile,
417
+ undefined,
418
+ errors,
419
+ warnings,
420
+ // eslint-disable-next-line
421
+ undefined
422
+ )
423
+ }
424
+
425
+ // Adding the translations to the learn.json
426
+ learnjson.translations = translations
427
+
428
+ // Checking if the preview image (from the learn.json) is OK.
429
+ try {
430
+ const res = await fetch(learnjson.preview, { method: "HEAD" })
431
+ if (res.status > 399 && res.status < 500) {
432
+ errors.push({
433
+ exercise: undefined,
434
+ msg: `The link of the "preview" is broken: ${learnjson.preview}`,
435
+ })
436
+ }
437
+ } catch {
438
+ errors.push({
439
+ exercise: undefined,
440
+ msg: `The link of the "preview" is broken: ${learnjson.preview}`,
441
+ })
442
+ }
443
+
444
+ const date = new Date()
445
+ learnjson.validationAt = date.getTime()
446
+
447
+ if (errors.length > 0) learnjson.validationStatus = "error"
448
+ else if (warnings.length > 0) learnjson.validationStatus = "warning"
449
+ else learnjson.validationStatus = "success"
450
+
451
+ // Writes the "learn.json" file with all the new properties
452
+ await fs.promises.writeFile("./learn.json", JSON.stringify(learnjson))
453
+ }
454
+
455
+ await Audit.showWarnings(warnings)
456
+ // eslint-disable-next-line
457
+ await Audit.showErrors(errors, undefined)
458
+ }
459
+ }
460
+ }
461
+
462
+ AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
463
+ ...
464
+ learnpack audit checks for the following information in a repository:
465
+ 1. The configuration object has slug, repository and description. (Error)
466
+ 2. The command learnpack clean has been run. (Error)
467
+ 3. If a markdown or test file doesn't have any content. (Error)
468
+ 4. The links are accessing to valid servers. (Error)
469
+ 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
470
+ 6. The external images are working (If they are pointing to a valid server). (Error)
471
+ 7. The exercises directory names are valid. (Error)
472
+ 8. If an exercise doesn't have a README file. (Error)
473
+ 9. The exercises array (Of the config file) has content. (Error)
474
+ 10. The exercses have the same translations. (Warning)
475
+ 11. The .gitignore file exists. (Warning)
476
+ 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
477
+ `
478
+
479
+ AuditCommand.flags = {
480
+ strict: flags.boolean({
481
+ char: "s",
482
+ description: "strict mode",
483
+ default: false,
484
+ }),
485
+ }
486
+
487
+ export default AuditCommand