@learnpack/learnpack 2.1.39 → 2.1.40

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 (196) hide show
  1. package/README.md +370 -35
  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 +342 -342
  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 +239 -208
  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 +41 -41
  27. package/lib/managers/config/defaults.js +44 -44
  28. package/lib/managers/config/exercise.d.ts +36 -36
  29. package/lib/managers/config/exercise.js +236 -236
  30. package/lib/managers/config/index.d.ts +3 -3
  31. package/lib/managers/config/index.js +337 -337
  32. package/lib/managers/file.d.ts +14 -14
  33. package/lib/managers/file.js +153 -153
  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 -58
  38. package/lib/managers/server/routes.d.ts +4 -4
  39. package/lib/managers/server/routes.js +219 -219
  40. package/lib/managers/session.d.ts +3 -3
  41. package/lib/managers/session.js +125 -125
  42. package/lib/managers/socket.d.ts +3 -3
  43. package/lib/managers/socket.js +176 -176
  44. package/lib/managers/telemetry.d.ts +74 -74
  45. package/lib/managers/telemetry.js +206 -206
  46. package/lib/managers/test.js +84 -84
  47. package/lib/models/action.d.ts +2 -2
  48. package/lib/models/action.js +2 -2
  49. package/lib/models/audit.d.ts +15 -15
  50. package/lib/models/audit.js +2 -2
  51. package/lib/models/config-manager.d.ts +21 -21
  52. package/lib/models/config-manager.js +2 -2
  53. package/lib/models/config.d.ts +68 -67
  54. package/lib/models/config.js +2 -2
  55. package/lib/models/counter.d.ts +11 -11
  56. package/lib/models/counter.js +2 -2
  57. package/lib/models/errors.d.ts +15 -15
  58. package/lib/models/errors.js +2 -2
  59. package/lib/models/exercise-obj.d.ts +30 -30
  60. package/lib/models/exercise-obj.js +2 -2
  61. package/lib/models/file.d.ts +5 -5
  62. package/lib/models/file.js +2 -2
  63. package/lib/models/findings.d.ts +17 -17
  64. package/lib/models/findings.js +2 -2
  65. package/lib/models/flags.d.ts +10 -10
  66. package/lib/models/flags.js +2 -2
  67. package/lib/models/front-matter.d.ts +11 -11
  68. package/lib/models/front-matter.js +2 -2
  69. package/lib/models/gitpod-data.d.ts +16 -16
  70. package/lib/models/gitpod-data.js +2 -2
  71. package/lib/models/language.d.ts +4 -4
  72. package/lib/models/language.js +2 -2
  73. package/lib/models/package.d.ts +7 -7
  74. package/lib/models/package.js +2 -2
  75. package/lib/models/plugin-config.d.ts +16 -16
  76. package/lib/models/plugin-config.js +2 -2
  77. package/lib/models/session.d.ts +31 -31
  78. package/lib/models/session.js +2 -2
  79. package/lib/models/socket.d.ts +36 -32
  80. package/lib/models/socket.js +2 -2
  81. package/lib/models/status.d.ts +1 -1
  82. package/lib/models/status.js +2 -2
  83. package/lib/models/success-types.d.ts +1 -1
  84. package/lib/models/success-types.js +2 -2
  85. package/lib/plugin/command/compile.d.ts +6 -6
  86. package/lib/plugin/command/compile.js +18 -18
  87. package/lib/plugin/command/test.d.ts +6 -6
  88. package/lib/plugin/command/test.js +25 -25
  89. package/lib/plugin/index.d.ts +27 -27
  90. package/lib/plugin/index.js +7 -7
  91. package/lib/plugin/plugin.d.ts +8 -8
  92. package/lib/plugin/plugin.js +68 -68
  93. package/lib/plugin/utils.d.ts +16 -16
  94. package/lib/plugin/utils.js +58 -58
  95. package/lib/ui/download.d.ts +5 -5
  96. package/lib/ui/download.js +61 -61
  97. package/lib/utils/BaseCommand.d.ts +8 -8
  98. package/lib/utils/BaseCommand.js +41 -41
  99. package/lib/utils/SessionCommand.d.ts +10 -10
  100. package/lib/utils/SessionCommand.js +43 -43
  101. package/lib/utils/api.d.ts +14 -14
  102. package/lib/utils/api.js +255 -255
  103. package/lib/utils/audit.d.ts +16 -16
  104. package/lib/utils/audit.js +303 -303
  105. package/lib/utils/checkNotInstalled.d.ts +2 -0
  106. package/lib/utils/checkNotInstalled.js +36 -0
  107. package/lib/utils/console.d.ts +12 -12
  108. package/lib/utils/console.js +19 -19
  109. package/lib/utils/errors.d.ts +17 -17
  110. package/lib/utils/errors.js +100 -100
  111. package/lib/utils/exercisesQueue.d.ts +9 -9
  112. package/lib/utils/exercisesQueue.js +38 -38
  113. package/lib/utils/fileQueue.d.ts +40 -40
  114. package/lib/utils/fileQueue.js +168 -168
  115. package/lib/utils/misc.d.ts +1 -1
  116. package/lib/utils/misc.js +23 -23
  117. package/lib/utils/osOperations.d.ts +5 -5
  118. package/lib/utils/osOperations.js +72 -72
  119. package/lib/utils/validators.d.ts +5 -5
  120. package/lib/utils/validators.js +17 -17
  121. package/lib/utils/watcher.d.ts +2 -2
  122. package/lib/utils/watcher.js +25 -25
  123. package/oclif.manifest.json +1 -1
  124. package/package.json +139 -139
  125. package/src/commands/audit.ts +443 -443
  126. package/src/commands/clean.ts +29 -29
  127. package/src/commands/download.ts +61 -61
  128. package/src/commands/init.ts +170 -170
  129. package/src/commands/login.ts +42 -42
  130. package/src/commands/logout.ts +43 -43
  131. package/src/commands/publish.ts +107 -107
  132. package/src/commands/start.ts +53 -23
  133. package/src/commands/test.ts +85 -85
  134. package/src/index.ts +1 -1
  135. package/src/managers/config/allowed_files.ts +29 -29
  136. package/src/managers/config/defaults.ts +42 -42
  137. package/src/managers/config/exercise.ts +311 -311
  138. package/src/managers/config/index.ts +455 -455
  139. package/src/managers/file.ts +196 -196
  140. package/src/managers/gitpod.ts +84 -84
  141. package/src/managers/server/index.ts +78 -78
  142. package/src/managers/server/routes.ts +330 -330
  143. package/src/managers/session.ts +145 -145
  144. package/src/managers/socket.ts +250 -250
  145. package/src/managers/telemetry.ts +346 -346
  146. package/src/managers/test.ts +83 -83
  147. package/src/models/action.ts +10 -10
  148. package/src/models/audit.ts +16 -16
  149. package/src/models/config-manager.ts +23 -23
  150. package/src/models/config.ts +5 -3
  151. package/src/models/counter.ts +11 -11
  152. package/src/models/errors.ts +22 -22
  153. package/src/models/exercise-obj.ts +29 -29
  154. package/src/models/file.ts +5 -5
  155. package/src/models/findings.ts +18 -18
  156. package/src/models/flags.ts +10 -10
  157. package/src/models/front-matter.ts +11 -11
  158. package/src/models/gitpod-data.ts +19 -19
  159. package/src/models/language.ts +4 -4
  160. package/src/models/package.ts +7 -7
  161. package/src/models/plugin-config.ts +17 -17
  162. package/src/models/session.ts +34 -34
  163. package/src/models/socket.ts +5 -0
  164. package/src/models/status.ts +16 -16
  165. package/src/models/success-types.ts +1 -1
  166. package/src/plugin/command/compile.ts +17 -17
  167. package/src/plugin/command/test.ts +30 -30
  168. package/src/plugin/index.ts +6 -6
  169. package/src/plugin/plugin.ts +94 -94
  170. package/src/plugin/utils.ts +87 -87
  171. package/src/types/node-fetch.d.ts +1 -1
  172. package/src/ui/download.ts +71 -71
  173. package/src/utils/BaseCommand.ts +48 -48
  174. package/src/utils/SessionCommand.ts +43 -43
  175. package/src/utils/api.ts +303 -303
  176. package/src/utils/audit.ts +393 -393
  177. package/src/utils/checkNotInstalled.ts +46 -0
  178. package/src/utils/console.ts +24 -24
  179. package/src/utils/errors.ts +117 -117
  180. package/src/utils/exercisesQueue.ts +51 -51
  181. package/src/utils/fileQueue.ts +198 -198
  182. package/src/utils/misc.ts +23 -23
  183. package/src/utils/osOperations.ts +79 -79
  184. package/src/utils/templates/gitignore.txt +19 -19
  185. package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.es.md +24 -24
  186. package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.md +24 -24
  187. package/src/utils/templates/incremental/README.ejs +4 -4
  188. package/src/utils/templates/incremental/README.es.ejs +4 -4
  189. package/src/utils/templates/isolated/01-hello-world/README.es.md +26 -26
  190. package/src/utils/templates/isolated/01-hello-world/README.md +26 -26
  191. package/src/utils/templates/isolated/README.ejs +4 -4
  192. package/src/utils/templates/isolated/README.es.ejs +4 -4
  193. package/src/utils/templates/no-grading/README.ejs +4 -4
  194. package/src/utils/templates/no-grading/README.es.ejs +4 -4
  195. package/src/utils/validators.ts +18 -18
  196. package/src/utils/watcher.ts +27 -27
@@ -1,342 +1,342 @@
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
- }
323
- AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
324
- ...
325
- learnpack audit checks for the following information in a repository:
326
- 1. The configuration object has slug, repository and description. (Error)
327
- 2. The command learnpack clean has been run. (Error)
328
- 3. If a markdown or test file doesn't have any content. (Error)
329
- 4. The links are accessing to valid servers. (Error)
330
- 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
331
- 6. The external images are working (If they are pointing to a valid server). (Error)
332
- 7. The exercises directory names are valid. (Error)
333
- 8. If an exercise doesn't have a README file. (Error)
334
- 9. The exercises array (Of the config file) has content. (Error)
335
- 10. The exercses have the same translations. (Warning)
336
- 11. The .gitignore file exists. (Warning)
337
- 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
338
- `;
339
- AuditCommand.flags = {
340
- // name: flags.string({char: 'n', description: 'name to print'}),
341
- };
342
- exports.default = AuditCommand;
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
+ }
323
+ AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
324
+ ...
325
+ learnpack audit checks for the following information in a repository:
326
+ 1. The configuration object has slug, repository and description. (Error)
327
+ 2. The command learnpack clean has been run. (Error)
328
+ 3. If a markdown or test file doesn't have any content. (Error)
329
+ 4. The links are accessing to valid servers. (Error)
330
+ 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
331
+ 6. The external images are working (If they are pointing to a valid server). (Error)
332
+ 7. The exercises directory names are valid. (Error)
333
+ 8. If an exercise doesn't have a README file. (Error)
334
+ 9. The exercises array (Of the config file) has content. (Error)
335
+ 10. The exercses have the same translations. (Warning)
336
+ 11. The .gitignore file exists. (Warning)
337
+ 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
338
+ `;
339
+ AuditCommand.flags = {
340
+ // name: flags.string({char: 'n', description: 'name to print'}),
341
+ };
342
+ exports.default = AuditCommand;