@learnpack/learnpack 2.1.25 → 2.1.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. package/README.md +16 -16
  2. package/bin/run +17 -17
  3. package/bin/run.cmd +3 -3
  4. package/lib/commands/audit.d.ts +6 -6
  5. package/lib/commands/audit.js +327 -302
  6. package/lib/commands/clean.d.ts +8 -8
  7. package/lib/commands/clean.js +25 -25
  8. package/lib/commands/download.d.ts +13 -13
  9. package/lib/commands/download.js +55 -55
  10. package/lib/commands/init.d.ts +9 -9
  11. package/lib/commands/init.js +123 -123
  12. package/lib/commands/login.d.ts +14 -14
  13. package/lib/commands/login.js +37 -37
  14. package/lib/commands/logout.d.ts +14 -14
  15. package/lib/commands/logout.js +37 -37
  16. package/lib/commands/publish.d.ts +14 -14
  17. package/lib/commands/publish.js +82 -82
  18. package/lib/commands/start.d.ts +7 -7
  19. package/lib/commands/start.js +165 -165
  20. package/lib/commands/test.d.ts +6 -6
  21. package/lib/commands/test.js +62 -62
  22. package/lib/index.d.ts +1 -1
  23. package/lib/index.js +4 -4
  24. package/lib/managers/config/allowed_files.d.ts +5 -5
  25. package/lib/managers/config/allowed_files.js +30 -30
  26. package/lib/managers/config/defaults.d.ts +39 -37
  27. package/lib/managers/config/defaults.js +40 -38
  28. package/lib/managers/config/exercise.d.ts +36 -36
  29. package/lib/managers/config/exercise.js +233 -230
  30. package/lib/managers/config/index.d.ts +3 -3
  31. package/lib/managers/config/index.js +320 -302
  32. package/lib/managers/file.d.ts +13 -13
  33. package/lib/managers/file.js +134 -134
  34. package/lib/managers/gitpod.d.ts +3 -3
  35. package/lib/managers/gitpod.js +67 -67
  36. package/lib/managers/server/index.d.ts +6 -6
  37. package/lib/managers/server/index.js +58 -51
  38. package/lib/managers/server/routes.d.ts +4 -4
  39. package/lib/managers/server/routes.js +167 -167
  40. package/lib/managers/session.d.ts +3 -3
  41. package/lib/managers/session.js +104 -104
  42. package/lib/managers/socket.d.ts +3 -3
  43. package/lib/managers/socket.js +164 -164
  44. package/lib/managers/test.js +84 -84
  45. package/lib/models/action.d.ts +2 -2
  46. package/lib/models/action.js +2 -2
  47. package/lib/models/audit.d.ts +15 -15
  48. package/lib/models/audit.js +2 -2
  49. package/lib/models/config-manager.d.ts +21 -21
  50. package/lib/models/config-manager.js +2 -2
  51. package/lib/models/config.d.ts +62 -60
  52. package/lib/models/config.js +2 -2
  53. package/lib/models/counter.d.ts +11 -11
  54. package/lib/models/counter.js +2 -2
  55. package/lib/models/errors.d.ts +15 -15
  56. package/lib/models/errors.js +2 -2
  57. package/lib/models/exercise-obj.d.ts +27 -27
  58. package/lib/models/exercise-obj.js +2 -2
  59. package/lib/models/file.d.ts +5 -5
  60. package/lib/models/file.js +2 -2
  61. package/lib/models/findings.d.ts +17 -17
  62. package/lib/models/findings.js +2 -2
  63. package/lib/models/flags.d.ts +10 -10
  64. package/lib/models/flags.js +2 -2
  65. package/lib/models/front-matter.d.ts +11 -11
  66. package/lib/models/front-matter.js +2 -2
  67. package/lib/models/gitpod-data.d.ts +16 -16
  68. package/lib/models/gitpod-data.js +2 -2
  69. package/lib/models/language.d.ts +4 -4
  70. package/lib/models/language.js +2 -2
  71. package/lib/models/package.d.ts +7 -7
  72. package/lib/models/package.js +2 -2
  73. package/lib/models/plugin-config.d.ts +16 -16
  74. package/lib/models/plugin-config.js +2 -2
  75. package/lib/models/session.d.ts +23 -23
  76. package/lib/models/session.js +2 -2
  77. package/lib/models/socket.d.ts +31 -31
  78. package/lib/models/socket.js +2 -2
  79. package/lib/models/status.d.ts +1 -1
  80. package/lib/models/status.js +2 -2
  81. package/lib/models/success-types.d.ts +1 -1
  82. package/lib/models/success-types.js +2 -2
  83. package/lib/plugin/command/compile.d.ts +6 -6
  84. package/lib/plugin/command/compile.js +18 -18
  85. package/lib/plugin/command/test.d.ts +6 -6
  86. package/lib/plugin/command/test.js +25 -25
  87. package/lib/plugin/index.d.ts +27 -27
  88. package/lib/plugin/index.js +7 -7
  89. package/lib/plugin/plugin.d.ts +8 -8
  90. package/lib/plugin/plugin.js +68 -68
  91. package/lib/plugin/utils.d.ts +16 -16
  92. package/lib/plugin/utils.js +58 -58
  93. package/lib/ui/download.d.ts +5 -5
  94. package/lib/ui/download.js +61 -61
  95. package/lib/utils/BaseCommand.d.ts +8 -8
  96. package/lib/utils/BaseCommand.js +41 -41
  97. package/lib/utils/SessionCommand.d.ts +10 -10
  98. package/lib/utils/SessionCommand.js +47 -47
  99. package/lib/utils/api.d.ts +12 -12
  100. package/lib/utils/api.js +173 -173
  101. package/lib/utils/audit.d.ts +16 -16
  102. package/lib/utils/audit.js +302 -302
  103. package/lib/utils/console.d.ts +12 -12
  104. package/lib/utils/console.js +19 -19
  105. package/lib/utils/errors.d.ts +17 -17
  106. package/lib/utils/errors.js +100 -100
  107. package/lib/utils/exercisesQueue.d.ts +9 -9
  108. package/lib/utils/exercisesQueue.js +38 -38
  109. package/lib/utils/fileQueue.d.ts +40 -40
  110. package/lib/utils/fileQueue.js +168 -168
  111. package/lib/utils/misc.d.ts +1 -1
  112. package/lib/utils/misc.js +23 -23
  113. package/lib/utils/validators.d.ts +5 -5
  114. package/lib/utils/validators.js +17 -17
  115. package/lib/utils/watcher.d.ts +2 -2
  116. package/lib/utils/watcher.js +23 -23
  117. package/oclif.manifest.json +1 -1
  118. package/package.json +138 -138
  119. package/src/commands/audit.ts +25 -0
  120. package/src/commands/clean.ts +29 -29
  121. package/src/commands/download.ts +62 -62
  122. package/src/commands/login.ts +42 -42
  123. package/src/commands/logout.ts +43 -43
  124. package/src/commands/publish.ts +107 -107
  125. package/src/commands/start.ts +238 -234
  126. package/src/commands/test.ts +85 -85
  127. package/src/index.ts +1 -1
  128. package/src/managers/config/allowed_files.ts +29 -29
  129. package/src/managers/config/defaults.ts +2 -0
  130. package/src/managers/config/exercise.ts +309 -302
  131. package/src/managers/config/index.ts +22 -1
  132. package/src/managers/file.ts +169 -169
  133. package/src/managers/gitpod.ts +84 -84
  134. package/src/managers/server/index.ts +77 -69
  135. package/src/managers/session.ts +118 -118
  136. package/src/managers/socket.ts +239 -239
  137. package/src/managers/test.ts +83 -83
  138. package/src/models/action.ts +3 -3
  139. package/src/models/config-manager.ts +23 -23
  140. package/src/models/config.ts +2 -0
  141. package/src/models/counter.ts +11 -11
  142. package/src/models/errors.ts +22 -22
  143. package/src/models/file.ts +5 -5
  144. package/src/models/findings.ts +18 -18
  145. package/src/models/flags.ts +10 -10
  146. package/src/models/front-matter.ts +11 -11
  147. package/src/models/gitpod-data.ts +19 -19
  148. package/src/models/language.ts +4 -4
  149. package/src/models/package.ts +7 -7
  150. package/src/models/plugin-config.ts +17 -17
  151. package/src/models/session.ts +26 -26
  152. package/src/models/socket.ts +48 -48
  153. package/src/models/status.ts +15 -15
  154. package/src/models/success-types.ts +1 -1
  155. package/src/plugin/command/compile.ts +17 -17
  156. package/src/plugin/command/test.ts +30 -30
  157. package/src/plugin/index.ts +6 -6
  158. package/src/plugin/plugin.ts +94 -94
  159. package/src/plugin/utils.ts +87 -87
  160. package/src/types/node-fetch.d.ts +1 -1
  161. package/src/ui/download.ts +71 -71
  162. package/src/utils/BaseCommand.ts +48 -48
  163. package/src/utils/SessionCommand.ts +48 -48
  164. package/src/utils/api.ts +194 -194
  165. package/src/utils/audit.ts +395 -395
  166. package/src/utils/console.ts +24 -24
  167. package/src/utils/errors.ts +117 -117
  168. package/src/utils/exercisesQueue.ts +51 -51
  169. package/src/utils/fileQueue.ts +198 -198
  170. package/src/utils/misc.ts +23 -23
  171. package/src/utils/templates/gitignore.txt +19 -19
  172. package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.es.md +24 -24
  173. package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.md +24 -24
  174. package/src/utils/templates/incremental/README.ejs +4 -4
  175. package/src/utils/templates/incremental/README.es.ejs +4 -4
  176. package/src/utils/templates/isolated/01-hello-world/README.es.md +26 -26
  177. package/src/utils/templates/isolated/01-hello-world/README.md +26 -26
  178. package/src/utils/templates/isolated/README.ejs +4 -4
  179. package/src/utils/templates/isolated/README.es.ejs +4 -4
  180. package/src/utils/templates/no-grading/README.ejs +4 -4
  181. package/src/utils/templates/no-grading/README.es.ejs +4 -4
  182. package/src/utils/validators.ts +18 -18
@@ -1,300 +1,325 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const fs = require("fs");
4
- const exercise_1 = require("../managers/config/exercise");
5
- const console_1 = require("../utils/console");
6
- const audit_1 = require("../utils/audit");
7
- const SessionCommand_1 = require("../utils/SessionCommand");
8
- const path = require("path");
9
- // eslint-disable-next-line
10
- const fetch = require("node-fetch");
11
- class AuditCommand extends SessionCommand_1.default {
12
- async init() {
13
- const { flags } = this.parse(AuditCommand);
14
- await this.initSession(flags);
15
- }
16
- async run() {
17
- var _a, _b, _c, _d, _e, _f, _g, _h;
18
- console_1.default.log("Running command audit...");
19
- // Get configuration object.
20
- let config = (_a = this.configManager) === null || _a === void 0 ? void 0 : _a.get();
21
- if (config) {
22
- const errors = [];
23
- const warnings = [];
24
- if (((_b = config === null || config === void 0 ? void 0 : config.config) === null || _b === void 0 ? void 0 : _b.projectType) === "tutorial") {
25
- const counter = {
26
- images: {
27
- error: 0,
28
- total: 0,
29
- },
30
- links: {
31
- error: 0,
32
- total: 0,
33
- },
34
- exercises: 0,
35
- readmeFiles: 0,
36
- };
37
- // Checks if learnpack clean has been run
38
- audit_1.default.checkLearnpackClean(config, errors);
39
- // Build exercises if they are not built yet.
40
- (_c = this.configManager) === null || _c === void 0 ? void 0 : _c.buildIndex();
41
- config = (_d = this.configManager) === null || _d === void 0 ? void 0 : _d.get();
42
- // Check if the exercises folder has some files within any ./exercise
43
- const exercisesPath = config.config.exercisesPath;
44
- fs.readdir(exercisesPath, (err, files) => {
45
- if (err) {
46
- return console.log("Unable to scan directory: " + err);
47
- }
48
- // listing all files using forEach
49
- for (const file of files) {
50
- // Do whatever you want to do with the file
51
- const filePath = path.join(exercisesPath, file);
52
- if (fs.statSync(filePath).isFile())
53
- warnings.push({
54
- exercise: file,
55
- msg: "This file is not inside any exercise folder.",
56
- });
57
- }
58
- });
59
- // This function is being created because the find method doesn't work with promises.
60
- const find = async (file, lang, exercise) => {
61
- if (file.name === lang) {
62
- await audit_1.default.checkUrl(config, file.path, file.name, exercise, errors, warnings, counter);
63
- return true;
64
- }
65
- return false;
66
- };
67
- console_1.default.debug("config", config);
68
- console_1.default.info(" Checking if the config file is fine...");
69
- // These two lines check if the 'slug' property is inside the configuration object.
70
- console_1.default.debug("Checking if the slug property is inside the configuration object...");
71
- if (!((_e = config.config) === null || _e === void 0 ? void 0 : _e.slug))
72
- errors.push({
73
- exercise: undefined,
74
- msg: "The slug property is not in the configuration object",
75
- });
76
- // These two lines check if the 'repository' property is inside the configuration object.
77
- console_1.default.debug("Checking if the repository property is inside the configuration object...");
78
- if (!((_f = config.config) === null || _f === void 0 ? void 0 : _f.repository))
79
- errors.push({
80
- exercise: undefined,
81
- msg: "The repository property is not in the configuration object",
82
- });
83
- else
84
- audit_1.default.isUrl((_g = config.config) === null || _g === void 0 ? void 0 : _g.repository, errors, counter);
85
- // These two lines check if the 'description' property is inside the configuration object.
86
- console_1.default.debug("Checking if the description property is inside the configuration object...");
87
- if (!((_h = config.config) === null || _h === void 0 ? void 0 : _h.description))
88
- errors.push({
89
- exercise: undefined,
90
- msg: "The description property is not in the configuration object",
91
- });
92
- if (errors.length === 0)
93
- console_1.default.log("The config file is ok");
94
- // Validates if images and links are working at every README file.
95
- const exercises = config.exercises;
96
- const readmeFiles = [];
97
- if (exercises && exercises.length > 0) {
98
- console_1.default.info(" Checking if the images are working...");
99
- for (const index in exercises) {
100
- if (Object.prototype.hasOwnProperty.call(exercises, index)) {
101
- const exercise = exercises[index];
102
- if (!exercise_1.validateExerciseDirectoryName(exercise.title))
103
- errors.push({
104
- exercise: exercise.title,
105
- msg: `The exercise ${exercise.title} has an invalid name.`,
106
- });
107
- let readmeFilesCount = { exercise: exercise.title, count: 0 };
108
- if (Object.keys(exercise.translations).length === 0)
109
- errors.push({
110
- exercise: exercise.title,
111
- msg: `The exercise ${exercise.title} doesn't have a README.md file.`,
112
- });
113
- if (exercise.language === "python3" ||
114
- exercise.language === "python") {
115
- for (const f of exercise.files.map(f => f)) {
116
- if (f.path.includes("test.py") ||
117
- f.path.includes("tests.py")) {
118
- const content = fs.readFileSync(f.path).toString();
119
- const isEmpty = audit_1.default.checkForEmptySpaces(content);
120
- if (isEmpty || !content)
121
- errors.push({
122
- exercise: exercise.title,
123
- msg: `This file (${f.name}) doesn't have any content inside.`,
124
- });
125
- }
126
- }
127
- }
128
- else {
129
- for (const f of exercise.files.map(f => f)) {
130
- if (f.path.includes("test.js") ||
131
- f.path.includes("tests.js")) {
132
- const content = fs.readFileSync(f.path).toString();
133
- const isEmpty = audit_1.default.checkForEmptySpaces(content);
134
- if (isEmpty || !content)
135
- errors.push({
136
- exercise: exercise.title,
137
- msg: `This file (${f.name}) doesn't have any content inside.`,
138
- });
139
- }
140
- }
141
- }
142
- for (const lang in exercise.translations) {
143
- if (Object.prototype.hasOwnProperty.call(exercise.translations, lang)) {
144
- const files = [];
145
- const findResultPromises = [];
146
- for (const file of exercise.files) {
147
- const found = find(file, exercise.translations[lang], exercise);
148
- findResultPromises.push(found);
149
- }
150
- // eslint-disable-next-line
151
- let findResults = await Promise.all(findResultPromises);
152
- for (const found of findResults) {
153
- if (found) {
154
- readmeFilesCount = Object.assign(Object.assign({}, readmeFilesCount), { count: readmeFilesCount.count + 1 });
155
- files.push(found);
156
- }
157
- }
158
- if (!files.includes(true))
159
- errors.push({
160
- exercise: exercise.title,
161
- msg: "This exercise doesn't have a README.md file.",
162
- });
163
- }
164
- }
165
- readmeFiles.push(readmeFilesCount);
166
- }
167
- }
168
- }
169
- else
170
- errors.push({
171
- exercise: undefined,
172
- msg: "The exercises array is empty.",
173
- });
174
- console_1.default.log(`${counter.images.total - counter.images.error} images ok from ${counter.images.total}`);
175
- console_1.default.info(" Checking if important files are missing... (README's, translations, gitignore...)");
176
- // 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.
177
- const files = [];
178
- let count = 0;
179
- for (const item of readmeFiles) {
180
- if (count < item.count)
181
- count = item.count;
182
- }
183
- for (const item of readmeFiles) {
184
- if (item.count !== count)
185
- files.push(` ${item.exercise}`);
186
- }
187
- if (files.length > 0) {
188
- const filesString = files.join(",");
189
- warnings.push({
190
- exercise: undefined,
191
- msg: files.length === 1 ?
192
- `This exercise is missing translations:${filesString}` :
193
- `These exercises are missing translations:${filesString}`,
194
- });
195
- }
196
- // Checks if the .gitignore file exists.
197
- if (!fs.existsSync(".gitignore"))
198
- warnings.push({
199
- exercise: undefined,
200
- msg: ".gitignore file doesn't exist",
201
- });
202
- counter.exercises = exercises.length;
203
- for (const readme of readmeFiles) {
204
- counter.readmeFiles += readme.count;
205
- }
206
- }
207
- else {
208
- // This is the audit code for Projects
209
- // Getting the learn.json schema
210
- const schemaResponse = await fetch("https://raw.githubusercontent.com/tommygonzaleza/project-template/main/.github/learn-schema.json");
211
- const schema = await schemaResponse.json();
212
- // Checking the "learn.json" file:
213
- const learnjson = JSON.parse(fs.readFileSync("./learn.json").toString());
214
- if (!learnjson) {
215
- console_1.default.error("There is no learn.json file located in the root of the project.");
216
- process.exit(1);
217
- }
218
- // Checking the README.md files and possible translations.
219
- let readmeFiles = [];
220
- const translations = [];
221
- const translationRegex = /README\.([a-z]{2,3})\.md/;
222
- try {
223
- const data = await fs.promises.readdir("./");
224
- readmeFiles = data.filter(file => file.includes("README"));
225
- if (readmeFiles.length === 0)
226
- errors.push({
227
- exercise: undefined,
228
- msg: `There is no README file in the repository.`,
229
- });
230
- }
231
- catch (error) {
232
- if (error)
233
- console_1.default.error("There was an error getting the directory files", error);
234
- }
235
- for (const readmeFile of readmeFiles) {
236
- // Checking the language of each README file.
237
- if (readmeFile === "README.md")
238
- translations.push("us");
239
- else {
240
- const regexGroups = translationRegex.exec(readmeFile);
241
- if (regexGroups)
242
- translations.push(regexGroups[1]);
243
- }
244
- const readme = fs.readFileSync(path.resolve(readmeFile)).toString();
245
- const isEmpty = audit_1.default.checkForEmptySpaces(readme);
246
- if (isEmpty || !readme) {
247
- errors.push({
248
- exercise: undefined,
249
- msg: `This file "${readmeFile}" doesn't have any content inside.`,
250
- });
251
- continue;
252
- }
253
- if (readme.length < 800)
254
- errors.push({
255
- exercise: undefined,
256
- msg: `The "${readmeFile}" file should have at least 800 characters (It currently have: ${readme.length}).`,
257
- });
258
- // eslint-disable-next-line
259
- await audit_1.default.checkUrl(config, path.resolve(readmeFile), readmeFile, undefined, errors, warnings,
260
- // eslint-disable-next-line
261
- undefined);
262
- }
263
- // Adding the translations to the learn.json
264
- learnjson.translations = translations;
265
- // Checking if the preview image (from the learn.json) is OK.
266
- try {
267
- const res = await fetch(learnjson.preview, { method: "HEAD" });
268
- if (res.status > 399 && res.status < 500) {
269
- errors.push({
270
- exercise: undefined,
271
- msg: `The link of the "preview" is broken: ${learnjson.preview}`,
272
- });
273
- }
274
- }
275
- catch (_j) {
276
- errors.push({
277
- exercise: undefined,
278
- msg: `The link of the "preview" is broken: ${learnjson.preview}`,
279
- });
280
- }
281
- const date = new Date();
282
- learnjson.validationAt = date.getTime();
283
- if (errors.length > 0)
284
- learnjson.validationStatus = "error";
285
- else if (warnings.length > 0)
286
- learnjson.validationStatus = "warning";
287
- else
288
- learnjson.validationStatus = "success";
289
- // Writes the "learn.json" file with all the new properties
290
- await fs.promises.writeFile("./learn.json", JSON.stringify(learnjson));
291
- }
292
- await audit_1.default.showWarnings(warnings);
293
- // eslint-disable-next-line
294
- await audit_1.default.showErrors(errors, undefined);
295
- }
296
- }
297
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fs = require("fs");
4
+ const exercise_1 = require("../managers/config/exercise");
5
+ const console_1 = require("../utils/console");
6
+ const audit_1 = require("../utils/audit");
7
+ const SessionCommand_1 = require("../utils/SessionCommand");
8
+ const path = require("path");
9
+ // eslint-disable-next-line
10
+ const fetch = require("node-fetch");
11
+ class AuditCommand extends SessionCommand_1.default {
12
+ async init() {
13
+ const { flags } = this.parse(AuditCommand);
14
+ await this.initSession(flags);
15
+ }
16
+ async run() {
17
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
18
+ console_1.default.log("Running command audit...");
19
+ // Get configuration object.
20
+ let config = (_a = this.configManager) === null || _a === void 0 ? void 0 : _a.get();
21
+ if (config) {
22
+ const errors = [];
23
+ const warnings = [];
24
+ if (((_b = config === null || config === void 0 ? void 0 : config.config) === null || _b === void 0 ? void 0 : _b.projectType) === "tutorial") {
25
+ const counter = {
26
+ images: {
27
+ error: 0,
28
+ total: 0,
29
+ },
30
+ links: {
31
+ error: 0,
32
+ total: 0,
33
+ },
34
+ exercises: 0,
35
+ readmeFiles: 0,
36
+ };
37
+ // Checks if learnpack clean has been run
38
+ audit_1.default.checkLearnpackClean(config, errors);
39
+ // Build exercises if they are not built yet.
40
+ (_c = this.configManager) === null || _c === void 0 ? void 0 : _c.buildIndex();
41
+ config = (_d = this.configManager) === null || _d === void 0 ? void 0 : _d.get();
42
+ // Check if the exercises folder has some files within any ./exercise
43
+ const exercisesPath = config.config.exercisesPath;
44
+ fs.readdir(exercisesPath, (err, files) => {
45
+ if (err) {
46
+ return console.log("Unable to scan directory: " + err);
47
+ }
48
+ // listing all files using forEach
49
+ for (const file of files) {
50
+ // Do whatever you want to do with the file
51
+ const filePath = path.join(exercisesPath, file);
52
+ if (fs.statSync(filePath).isFile())
53
+ warnings.push({
54
+ exercise: file,
55
+ msg: "This file is not inside any exercise folder.",
56
+ });
57
+ }
58
+ });
59
+ // This function is being created because the find method doesn't work with promises.
60
+ const find = async (file, lang, exercise) => {
61
+ if (file.name === lang) {
62
+ await audit_1.default.checkUrl(config, file.path, file.name, exercise, errors, warnings, counter);
63
+ return true;
64
+ }
65
+ return false;
66
+ };
67
+ console_1.default.debug("config", config);
68
+ console_1.default.info(" Checking if the config file is fine...");
69
+ // These two lines check if the 'slug' property is inside the configuration object.
70
+ console_1.default.debug("Checking if the slug property is inside the configuration object...");
71
+ // check if the slug property is in the configuration object
72
+ if (!((_e = config.config) === null || _e === void 0 ? void 0 : _e.slug))
73
+ errors.push({
74
+ exercise: undefined,
75
+ msg: "The slug property is not in the configuration object",
76
+ });
77
+ // check if the duration property is in the configuration object
78
+ if (!((_f = config.config) === null || _f === void 0 ? void 0 : _f.duration))
79
+ warnings.push({
80
+ exercise: undefined,
81
+ msg: "The duration property is not in the configuration object",
82
+ });
83
+ // check if the difficulty property is in the configuration object
84
+ if (!((_g = config.config) === null || _g === void 0 ? void 0 : _g.difficulty))
85
+ warnings.push({
86
+ exercise: undefined,
87
+ msg: "The difficulty property is not in the configuration object",
88
+ });
89
+ // check if the bugs_link property is in the configuration object
90
+ if (!((_h = config.config) === null || _h === void 0 ? void 0 : _h.bugsLink))
91
+ errors.push({
92
+ exercise: undefined,
93
+ msg: "The bugsLink property is not in the configuration object",
94
+ });
95
+ // check if the video_solutions property is in the configuration object
96
+ if (((_j = config.config) === null || _j === void 0 ? void 0 : _j.videoSolutions) === undefined)
97
+ warnings.push({
98
+ exercise: undefined,
99
+ msg: "The videoSolutions property is not in the configuration object",
100
+ });
101
+ // These two lines check if the 'repository' property is inside the configuration object.
102
+ console_1.default.debug("Checking if the repository property is inside the configuration object...");
103
+ if (!((_k = config.config) === null || _k === void 0 ? void 0 : _k.repository))
104
+ errors.push({
105
+ exercise: undefined,
106
+ msg: "The repository property is not in the configuration object",
107
+ });
108
+ else
109
+ audit_1.default.isUrl((_l = config.config) === null || _l === void 0 ? void 0 : _l.repository, errors, counter);
110
+ // These two lines check if the 'description' property is inside the configuration object.
111
+ console_1.default.debug("Checking if the description property is inside the configuration object...");
112
+ if (!((_m = config.config) === null || _m === void 0 ? void 0 : _m.description))
113
+ errors.push({
114
+ exercise: undefined,
115
+ msg: "The description property is not in the configuration object",
116
+ });
117
+ if (errors.length === 0)
118
+ console_1.default.log("The config file is ok");
119
+ // Validates if images and links are working at every README file.
120
+ const exercises = config.exercises;
121
+ const readmeFiles = [];
122
+ if (exercises && exercises.length > 0) {
123
+ console_1.default.info(" Checking if the images are working...");
124
+ for (const index in exercises) {
125
+ if (Object.prototype.hasOwnProperty.call(exercises, index)) {
126
+ const exercise = exercises[index];
127
+ if (!exercise_1.validateExerciseDirectoryName(exercise.title))
128
+ errors.push({
129
+ exercise: exercise.title,
130
+ msg: `The exercise ${exercise.title} has an invalid name.`,
131
+ });
132
+ let readmeFilesCount = { exercise: exercise.title, count: 0 };
133
+ if (Object.keys(exercise.translations).length === 0)
134
+ errors.push({
135
+ exercise: exercise.title,
136
+ msg: `The exercise ${exercise.title} doesn't have a README.md file.`,
137
+ });
138
+ if (exercise.language === "python3" ||
139
+ exercise.language === "python") {
140
+ for (const f of exercise.files.map(f => f)) {
141
+ if (f.path.includes("test.py") ||
142
+ f.path.includes("tests.py")) {
143
+ const content = fs.readFileSync(f.path).toString();
144
+ const isEmpty = audit_1.default.checkForEmptySpaces(content);
145
+ if (isEmpty || !content)
146
+ errors.push({
147
+ exercise: exercise.title,
148
+ msg: `This file (${f.name}) doesn't have any content inside.`,
149
+ });
150
+ }
151
+ }
152
+ }
153
+ else {
154
+ for (const f of exercise.files.map(f => f)) {
155
+ if (f.path.includes("test.js") ||
156
+ f.path.includes("tests.js")) {
157
+ const content = fs.readFileSync(f.path).toString();
158
+ const isEmpty = audit_1.default.checkForEmptySpaces(content);
159
+ if (isEmpty || !content)
160
+ errors.push({
161
+ exercise: exercise.title,
162
+ msg: `This file (${f.name}) doesn't have any content inside.`,
163
+ });
164
+ }
165
+ }
166
+ }
167
+ for (const lang in exercise.translations) {
168
+ if (Object.prototype.hasOwnProperty.call(exercise.translations, lang)) {
169
+ const files = [];
170
+ const findResultPromises = [];
171
+ for (const file of exercise.files) {
172
+ const found = find(file, exercise.translations[lang], exercise);
173
+ findResultPromises.push(found);
174
+ }
175
+ // eslint-disable-next-line
176
+ let findResults = await Promise.all(findResultPromises);
177
+ for (const found of findResults) {
178
+ if (found) {
179
+ readmeFilesCount = Object.assign(Object.assign({}, readmeFilesCount), { count: readmeFilesCount.count + 1 });
180
+ files.push(found);
181
+ }
182
+ }
183
+ if (!files.includes(true))
184
+ errors.push({
185
+ exercise: exercise.title,
186
+ msg: "This exercise doesn't have a README.md file.",
187
+ });
188
+ }
189
+ }
190
+ readmeFiles.push(readmeFilesCount);
191
+ }
192
+ }
193
+ }
194
+ else
195
+ errors.push({
196
+ exercise: undefined,
197
+ msg: "The exercises array is empty.",
198
+ });
199
+ console_1.default.log(`${counter.images.total - counter.images.error} images ok from ${counter.images.total}`);
200
+ console_1.default.info(" Checking if important files are missing... (README's, translations, gitignore...)");
201
+ // 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.
202
+ const files = [];
203
+ let count = 0;
204
+ for (const item of readmeFiles) {
205
+ if (count < item.count)
206
+ count = item.count;
207
+ }
208
+ for (const item of readmeFiles) {
209
+ if (item.count !== count)
210
+ files.push(` ${item.exercise}`);
211
+ }
212
+ if (files.length > 0) {
213
+ const filesString = files.join(",");
214
+ warnings.push({
215
+ exercise: undefined,
216
+ msg: files.length === 1 ?
217
+ `This exercise is missing translations:${filesString}` :
218
+ `These exercises are missing translations:${filesString}`,
219
+ });
220
+ }
221
+ // Checks if the .gitignore file exists.
222
+ if (!fs.existsSync(".gitignore"))
223
+ warnings.push({
224
+ exercise: undefined,
225
+ msg: ".gitignore file doesn't exist",
226
+ });
227
+ counter.exercises = exercises.length;
228
+ for (const readme of readmeFiles) {
229
+ counter.readmeFiles += readme.count;
230
+ }
231
+ }
232
+ else {
233
+ // This is the audit code for Projects
234
+ // Getting the learn.json schema
235
+ const schemaResponse = await fetch("https://raw.githubusercontent.com/tommygonzaleza/project-template/main/.github/learn-schema.json");
236
+ const schema = await schemaResponse.json();
237
+ // Checking the "learn.json" file:
238
+ const learnjson = JSON.parse(fs.readFileSync("./learn.json").toString());
239
+ if (!learnjson) {
240
+ console_1.default.error("There is no learn.json file located in the root of the project.");
241
+ process.exit(1);
242
+ }
243
+ // Checking the README.md files and possible translations.
244
+ let readmeFiles = [];
245
+ const translations = [];
246
+ const translationRegex = /README\.([a-z]{2,3})\.md/;
247
+ try {
248
+ const data = await fs.promises.readdir("./");
249
+ readmeFiles = data.filter(file => file.includes("README"));
250
+ if (readmeFiles.length === 0)
251
+ errors.push({
252
+ exercise: undefined,
253
+ msg: `There is no README file in the repository.`,
254
+ });
255
+ }
256
+ catch (error) {
257
+ if (error)
258
+ console_1.default.error("There was an error getting the directory files", error);
259
+ }
260
+ for (const readmeFile of readmeFiles) {
261
+ // Checking the language of each README file.
262
+ if (readmeFile === "README.md")
263
+ translations.push("us");
264
+ else {
265
+ const regexGroups = translationRegex.exec(readmeFile);
266
+ if (regexGroups)
267
+ translations.push(regexGroups[1]);
268
+ }
269
+ const readme = fs.readFileSync(path.resolve(readmeFile)).toString();
270
+ const isEmpty = audit_1.default.checkForEmptySpaces(readme);
271
+ if (isEmpty || !readme) {
272
+ errors.push({
273
+ exercise: undefined,
274
+ msg: `This file "${readmeFile}" doesn't have any content inside.`,
275
+ });
276
+ continue;
277
+ }
278
+ if (readme.length < 800)
279
+ errors.push({
280
+ exercise: undefined,
281
+ msg: `The "${readmeFile}" file should have at least 800 characters (It currently have: ${readme.length}).`,
282
+ });
283
+ // eslint-disable-next-line
284
+ await audit_1.default.checkUrl(config, path.resolve(readmeFile), readmeFile, undefined, errors, warnings,
285
+ // eslint-disable-next-line
286
+ undefined);
287
+ }
288
+ // Adding the translations to the learn.json
289
+ learnjson.translations = translations;
290
+ // Checking if the preview image (from the learn.json) is OK.
291
+ try {
292
+ const res = await fetch(learnjson.preview, { method: "HEAD" });
293
+ if (res.status > 399 && res.status < 500) {
294
+ errors.push({
295
+ exercise: undefined,
296
+ msg: `The link of the "preview" is broken: ${learnjson.preview}`,
297
+ });
298
+ }
299
+ }
300
+ catch (_o) {
301
+ errors.push({
302
+ exercise: undefined,
303
+ msg: `The link of the "preview" is broken: ${learnjson.preview}`,
304
+ });
305
+ }
306
+ const date = new Date();
307
+ learnjson.validationAt = date.getTime();
308
+ if (errors.length > 0)
309
+ learnjson.validationStatus = "error";
310
+ else if (warnings.length > 0)
311
+ learnjson.validationStatus = "warning";
312
+ else
313
+ learnjson.validationStatus = "success";
314
+ // Writes the "learn.json" file with all the new properties
315
+ await fs.promises.writeFile("./learn.json", JSON.stringify(learnjson));
316
+ }
317
+ await audit_1.default.showWarnings(warnings);
318
+ // eslint-disable-next-line
319
+ await audit_1.default.showErrors(errors, undefined);
320
+ }
321
+ }
322
+ }
298
323
  AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
299
324
  ...
300
325
  learnpack audit checks for the following information in a repository:
@@ -310,8 +335,8 @@ learnpack audit checks for the following information in a repository:
310
335
  10. The exercses have the same translations. (Warning)
311
336
  11. The .gitignore file exists. (Warning)
312
337
  12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
313
- `;
314
- AuditCommand.flags = {
315
- // name: flags.string({char: 'n', description: 'name to print'}),
316
- };
317
- exports.default = AuditCommand;
338
+ `;
339
+ AuditCommand.flags = {
340
+ // name: flags.string({char: 'n', description: 'name to print'}),
341
+ };
342
+ exports.default = AuditCommand;