@learnpack/learnpack 2.1.23 → 2.1.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,418 +1,418 @@
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
-
14
- // eslint-disable-next-line
15
- const fetch = require("node-fetch");
16
-
17
- class AuditCommand extends SessionCommand {
18
- async init() {
19
- const { flags } = this.parse(AuditCommand);
20
- await this.initSession(flags);
21
- }
22
-
23
- async run() {
24
- Console.log("Running command audit...");
25
-
26
- // Get configuration object.
27
- let config = this.configManager?.get();
28
-
29
- if (config) {
30
- const errors: IAuditErrors[] = [];
31
- const warnings: IAuditErrors[] = [];
32
- if (config?.config?.projectType === "tutorial") {
33
- const counter: ICounter = {
34
- images: {
35
- error: 0,
36
- total: 0,
37
- },
38
- links: {
39
- error: 0,
40
- total: 0,
41
- },
42
- exercises: 0,
43
- readmeFiles: 0,
44
- };
45
-
46
- // Checks if learnpack clean has been run
47
- Audit.checkLearnpackClean(config, errors);
48
-
49
- // Build exercises if they are not built yet.
50
- this.configManager?.buildIndex();
51
- config = this.configManager?.get();
52
-
53
- // Check if the exercises folder has some files within any ./exercise
54
- const exercisesPath: string = config!.config!.exercisesPath;
55
-
56
- fs.readdir(exercisesPath, (err, files) => {
57
- if (err) {
58
- return console.log("Unable to scan directory: " + err);
59
- }
60
-
61
- // listing all files using forEach
62
- for (const file of files) {
63
- // Do whatever you want to do with the file
64
- const filePath: string = path.join(exercisesPath, file);
65
- if (fs.statSync(filePath).isFile())
66
- warnings.push({
67
- exercise: file!,
68
- msg: "This file is not inside any exercise folder.",
69
- });
70
- }
71
- });
72
-
73
- // This function is being created because the find method doesn't work with promises.
74
- const find = async (file: IFile, lang: string, exercise: IExercise) => {
75
- if (file.name === lang) {
76
- await Audit.checkUrl(
77
- config!,
78
- file.path,
79
- file.name,
80
- exercise,
81
- errors,
82
- warnings,
83
- counter
84
- );
85
- return true;
86
- }
87
-
88
- return false;
89
- };
90
-
91
- Console.debug("config", config);
92
-
93
- Console.info(" Checking if the config file is fine...");
94
- // These two lines check if the 'slug' property is inside the configuration object.
95
- Console.debug(
96
- "Checking if the slug property is inside the configuration object..."
97
- );
98
- if (!config!.config?.slug)
99
- errors.push({
100
- exercise: undefined,
101
- msg: "The slug property is not in the configuration object",
102
- });
103
-
104
- // These two lines check if the 'repository' property is inside the configuration object.
105
- Console.debug(
106
- "Checking if the repository property is inside the configuration object..."
107
- );
108
- if (!config!.config?.repository)
109
- errors.push({
110
- exercise: undefined,
111
- msg: "The repository property is not in the configuration object",
112
- });
113
- else
114
- Audit.isUrl(config!.config?.repository, errors, counter);
115
-
116
- // These two lines check if the 'description' property is inside the configuration object.
117
- Console.debug(
118
- "Checking if the description property is inside the configuration object..."
119
- );
120
- if (!config!.config?.description)
121
- errors.push({
122
- exercise: undefined,
123
- msg: "The description property is not in the configuration object",
124
- });
125
-
126
- if (errors.length === 0)
127
- Console.log("The config file is ok");
128
-
129
- // Validates if images and links are working at every README file.
130
- const exercises = config!.exercises;
131
- const readmeFiles = [];
132
-
133
- if (exercises && exercises.length > 0) {
134
- Console.info(" Checking if the images are working...");
135
- for (const index in exercises) {
136
- if (Object.prototype.hasOwnProperty.call(exercises, index)) {
137
- const exercise = exercises[index];
138
- if (!validateExerciseDirectoryName(exercise.title))
139
- errors.push({
140
- exercise: exercise.title,
141
- msg: `The exercise ${exercise.title} has an invalid name.`,
142
- });
143
- let readmeFilesCount = { exercise: exercise.title, count: 0 };
144
- if (Object.keys(exercise.translations!).length === 0)
145
- errors.push({
146
- exercise: exercise.title,
147
- msg: `The exercise ${exercise.title} doesn't have a README.md file.`,
148
- });
149
-
150
- if (
151
- exercise.language === "python3" ||
152
- exercise.language === "python"
153
- ) {
154
- for (const f of exercise.files.map(f => f)) {
155
- if (
156
- f.path.includes("test.py") ||
157
- f.path.includes("tests.py")
158
- ) {
159
- const content = fs.readFileSync(f.path).toString();
160
- const isEmpty = Audit.checkForEmptySpaces(content);
161
- if (isEmpty || !content)
162
- errors.push({
163
- exercise: exercise.title,
164
- msg: `This file (${f.name}) doesn't have any content inside.`,
165
- });
166
- }
167
- }
168
- } else {
169
- for (const f of exercise.files.map(f => f)) {
170
- if (
171
- f.path.includes("test.js") ||
172
- f.path.includes("tests.js")
173
- ) {
174
- const content = fs.readFileSync(f.path).toString();
175
- const isEmpty: boolean = Audit.checkForEmptySpaces(content);
176
- if (isEmpty || !content)
177
- errors.push({
178
- exercise: exercise.title,
179
- msg: `This file (${f.name}) doesn't have any content inside.`,
180
- });
181
- }
182
- }
183
- }
184
-
185
- for (const lang in exercise.translations) {
186
- if (
187
- Object.prototype.hasOwnProperty.call(
188
- exercise.translations,
189
- lang
190
- )
191
- ) {
192
- const files: any[] = [];
193
- const findResultPromises = [];
194
- for (const file of exercise.files) {
195
- const found = find(
196
- file,
197
- exercise.translations[lang],
198
- exercise
199
- );
200
- findResultPromises.push(found);
201
- }
202
- // eslint-disable-next-line
203
- let findResults = await Promise.all(findResultPromises);
204
- for (const found of findResults) {
205
- if (found) {
206
- readmeFilesCount = {
207
- ...readmeFilesCount,
208
- count: readmeFilesCount.count + 1,
209
- };
210
- files.push(found);
211
- }
212
- }
213
-
214
- if (!files.includes(true))
215
- errors.push({
216
- exercise: exercise.title,
217
- msg: "This exercise doesn't have a README.md file.",
218
- });
219
- }
220
- }
221
-
222
- readmeFiles.push(readmeFilesCount);
223
- }
224
- }
225
- } else
226
- errors.push({
227
- exercise: undefined,
228
- msg: "The exercises array is empty.",
229
- });
230
-
231
- Console.log(
232
- `${counter.images.total - counter.images.error} images ok from ${
233
- counter.images.total
234
- }`
235
- );
236
-
237
- Console.info(
238
- " Checking if important files are missing... (README's, translations, gitignore...)"
239
- );
240
- // 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.
241
- const files: string[] = [];
242
- let count = 0;
243
- for (const item of readmeFiles) {
244
- if (count < item.count)
245
- count = item.count;
246
- }
247
-
248
- for (const item of readmeFiles) {
249
- if (item.count !== count)
250
- files.push(` ${item.exercise}`);
251
- }
252
-
253
- if (files.length > 0) {
254
- const filesString: string = files.join(",");
255
- warnings.push({
256
- exercise: undefined,
257
- msg:
258
- files.length === 1 ?
259
- `This exercise is missing translations:${filesString}` :
260
- `These exercises are missing translations:${filesString}`,
261
- });
262
- }
263
-
264
- // Checks if the .gitignore file exists.
265
- if (!fs.existsSync(".gitignore"))
266
- warnings.push({
267
- exercise: undefined,
268
- msg: ".gitignore file doesn't exist",
269
- });
270
-
271
- counter.exercises = exercises!.length;
272
- for (const readme of readmeFiles) {
273
- counter.readmeFiles += readme.count;
274
- }
275
- } else {
276
- // This is the audit code for Projects
277
-
278
- // Getting the learn.json schema
279
- const schemaResponse = await fetch(
280
- "https://raw.githubusercontent.com/tommygonzaleza/project-template/main/.github/learn-schema.json"
281
- );
282
- const schema = await schemaResponse.json();
283
-
284
- // Checking the "learn.json" file:
285
- const learnjson = JSON.parse(
286
- fs.readFileSync("./learn.json").toString()
287
- );
288
-
289
- if (!learnjson) {
290
- Console.error(
291
- "There is no learn.json file located in the root of the project."
292
- );
293
- process.exit(1);
294
- }
295
-
296
- // Checking the README.md files and possible translations.
297
- let readmeFiles: any[] = [];
298
- const translations: string[] = [];
299
- const translationRegex = /README\.([a-z]{2,3})\.md/;
300
-
301
- try {
302
- const data = await fs.promises.readdir("./");
303
- readmeFiles = data.filter(file => file.includes("README"));
304
- if (readmeFiles.length === 0)
305
- errors.push({
306
- exercise: undefined!,
307
- msg: `There is no README file in the repository.`,
308
- });
309
- } catch (error) {
310
- if (error)
311
- Console.error(
312
- "There was an error getting the directory files",
313
- error
314
- );
315
- }
316
-
317
- for (const readmeFile of readmeFiles) {
318
- // Checking the language of each README file.
319
- if (readmeFile === "README.md")
320
- translations.push("us");
321
- else {
322
- const regexGroups = translationRegex.exec(readmeFile);
323
- if (regexGroups)
324
- translations.push(regexGroups[1]);
325
- }
326
-
327
- const readme = fs.readFileSync(path.resolve(readmeFile)).toString();
328
-
329
- const isEmpty = Audit.checkForEmptySpaces(readme);
330
- if (isEmpty || !readme) {
331
- errors.push({
332
- exercise: undefined!,
333
- msg: `This file "${readmeFile}" doesn't have any content inside.`,
334
- });
335
- continue;
336
- }
337
-
338
- if (readme.length < 800)
339
- errors.push({
340
- exercise: undefined,
341
- msg: `The "${readmeFile}" file should have at least 800 characters (It currently have: ${readme.length}).`,
342
- });
343
-
344
- // eslint-disable-next-line
345
- await Audit.checkUrl(
346
- config!,
347
- path.resolve(readmeFile),
348
- readmeFile,
349
- undefined,
350
- errors,
351
- warnings,
352
- // eslint-disable-next-line
353
- undefined
354
- );
355
- }
356
-
357
- // Adding the translations to the learn.json
358
- learnjson.translations = translations;
359
-
360
- // Checking if the preview image (from the learn.json) is OK.
361
- try {
362
- const res = await fetch(learnjson.preview, { method: "HEAD" });
363
- if (!res.ok) {
364
- errors.push({
365
- exercise: undefined,
366
- msg: `The link of the "preview" is broken: ${learnjson.preview}`,
367
- });
368
- }
369
- } catch {
370
- errors.push({
371
- exercise: undefined,
372
- msg: `The link of the "preview" is broken: ${learnjson.preview}`,
373
- });
374
- }
375
-
376
- const date = new Date();
377
- learnjson.validationAt = date.getTime();
378
-
379
- if (errors.length > 0)
380
- learnjson.validationStatus = "error";
381
- else if (warnings.length > 0)
382
- learnjson.validationStatus = "warning";
383
- else
384
- learnjson.validationStatus = "success";
385
-
386
- // Writes the "learn.json" file with all the new properties
387
- await fs.promises.writeFile("./learn.json", JSON.stringify(learnjson));
388
- }
389
-
390
- await Audit.showWarnings(warnings);
391
- // eslint-disable-next-line
392
- await Audit.showErrors(errors, undefined);
393
- }
394
- }
395
- }
396
-
397
- AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
398
- ...
399
- learnpack audit checks for the following information in a repository:
400
- 1. The configuration object has slug, repository and description. (Error)
401
- 2. The command learnpack clean has been run. (Error)
402
- 3. If a markdown or test file doesn't have any content. (Error)
403
- 4. The links are accessing to valid servers. (Error)
404
- 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
405
- 6. The external images are working (If they are pointing to a valid server). (Error)
406
- 7. The exercises directory names are valid. (Error)
407
- 8. If an exercise doesn't have a README file. (Error)
408
- 9. The exercises array (Of the config file) has content. (Error)
409
- 10. The exercses have the same translations. (Warning)
410
- 11. The .gitignore file exists. (Warning)
411
- 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
412
- `;
413
-
414
- AuditCommand.flags = {
415
- // name: flags.string({char: 'n', description: 'name to print'}),
416
- };
417
-
418
- 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
+
14
+ // eslint-disable-next-line
15
+ const fetch = require("node-fetch");
16
+
17
+ class AuditCommand extends SessionCommand {
18
+ async init() {
19
+ const { flags } = this.parse(AuditCommand);
20
+ await this.initSession(flags);
21
+ }
22
+
23
+ async run() {
24
+ Console.log("Running command audit...");
25
+
26
+ // Get configuration object.
27
+ let config = this.configManager?.get();
28
+
29
+ if (config) {
30
+ const errors: IAuditErrors[] = [];
31
+ const warnings: IAuditErrors[] = [];
32
+ if (config?.config?.projectType === "tutorial") {
33
+ const counter: ICounter = {
34
+ images: {
35
+ error: 0,
36
+ total: 0,
37
+ },
38
+ links: {
39
+ error: 0,
40
+ total: 0,
41
+ },
42
+ exercises: 0,
43
+ readmeFiles: 0,
44
+ };
45
+
46
+ // Checks if learnpack clean has been run
47
+ Audit.checkLearnpackClean(config, errors);
48
+
49
+ // Build exercises if they are not built yet.
50
+ this.configManager?.buildIndex();
51
+ config = this.configManager?.get();
52
+
53
+ // Check if the exercises folder has some files within any ./exercise
54
+ const exercisesPath: string = config!.config!.exercisesPath;
55
+
56
+ fs.readdir(exercisesPath, (err, files) => {
57
+ if (err) {
58
+ return console.log("Unable to scan directory: " + err);
59
+ }
60
+
61
+ // listing all files using forEach
62
+ for (const file of files) {
63
+ // Do whatever you want to do with the file
64
+ const filePath: string = path.join(exercisesPath, file);
65
+ if (fs.statSync(filePath).isFile())
66
+ warnings.push({
67
+ exercise: file!,
68
+ msg: "This file is not inside any exercise folder.",
69
+ });
70
+ }
71
+ });
72
+
73
+ // This function is being created because the find method doesn't work with promises.
74
+ const find = async (file: IFile, lang: string, exercise: IExercise) => {
75
+ if (file.name === lang) {
76
+ await Audit.checkUrl(
77
+ config!,
78
+ file.path,
79
+ file.name,
80
+ exercise,
81
+ errors,
82
+ warnings,
83
+ counter
84
+ );
85
+ return true;
86
+ }
87
+
88
+ return false;
89
+ };
90
+
91
+ Console.debug("config", config);
92
+
93
+ Console.info(" Checking if the config file is fine...");
94
+ // These two lines check if the 'slug' property is inside the configuration object.
95
+ Console.debug(
96
+ "Checking if the slug property is inside the configuration object..."
97
+ );
98
+ if (!config!.config?.slug)
99
+ errors.push({
100
+ exercise: undefined,
101
+ msg: "The slug property is not in the configuration object",
102
+ });
103
+
104
+ // These two lines check if the 'repository' property is inside the configuration object.
105
+ Console.debug(
106
+ "Checking if the repository property is inside the configuration object..."
107
+ );
108
+ if (!config!.config?.repository)
109
+ errors.push({
110
+ exercise: undefined,
111
+ msg: "The repository property is not in the configuration object",
112
+ });
113
+ else
114
+ Audit.isUrl(config!.config?.repository, errors, counter);
115
+
116
+ // These two lines check if the 'description' property is inside the configuration object.
117
+ Console.debug(
118
+ "Checking if the description property is inside the configuration object..."
119
+ );
120
+ if (!config!.config?.description)
121
+ errors.push({
122
+ exercise: undefined,
123
+ msg: "The description property is not in the configuration object",
124
+ });
125
+
126
+ if (errors.length === 0)
127
+ Console.log("The config file is ok");
128
+
129
+ // Validates if images and links are working at every README file.
130
+ const exercises = config!.exercises;
131
+ const readmeFiles = [];
132
+
133
+ if (exercises && exercises.length > 0) {
134
+ Console.info(" Checking if the images are working...");
135
+ for (const index in exercises) {
136
+ if (Object.prototype.hasOwnProperty.call(exercises, index)) {
137
+ const exercise = exercises[index];
138
+ if (!validateExerciseDirectoryName(exercise.title))
139
+ errors.push({
140
+ exercise: exercise.title,
141
+ msg: `The exercise ${exercise.title} has an invalid name.`,
142
+ });
143
+ let readmeFilesCount = { exercise: exercise.title, count: 0 };
144
+ if (Object.keys(exercise.translations!).length === 0)
145
+ errors.push({
146
+ exercise: exercise.title,
147
+ msg: `The exercise ${exercise.title} doesn't have a README.md file.`,
148
+ });
149
+
150
+ if (
151
+ exercise.language === "python3" ||
152
+ exercise.language === "python"
153
+ ) {
154
+ for (const f of exercise.files.map(f => f)) {
155
+ if (
156
+ f.path.includes("test.py") ||
157
+ f.path.includes("tests.py")
158
+ ) {
159
+ const content = fs.readFileSync(f.path).toString();
160
+ const isEmpty = Audit.checkForEmptySpaces(content);
161
+ if (isEmpty || !content)
162
+ errors.push({
163
+ exercise: exercise.title,
164
+ msg: `This file (${f.name}) doesn't have any content inside.`,
165
+ });
166
+ }
167
+ }
168
+ } else {
169
+ for (const f of exercise.files.map(f => f)) {
170
+ if (
171
+ f.path.includes("test.js") ||
172
+ f.path.includes("tests.js")
173
+ ) {
174
+ const content = fs.readFileSync(f.path).toString();
175
+ const isEmpty: boolean = Audit.checkForEmptySpaces(content);
176
+ if (isEmpty || !content)
177
+ errors.push({
178
+ exercise: exercise.title,
179
+ msg: `This file (${f.name}) doesn't have any content inside.`,
180
+ });
181
+ }
182
+ }
183
+ }
184
+
185
+ for (const lang in exercise.translations) {
186
+ if (
187
+ Object.prototype.hasOwnProperty.call(
188
+ exercise.translations,
189
+ lang
190
+ )
191
+ ) {
192
+ const files: any[] = [];
193
+ const findResultPromises = [];
194
+ for (const file of exercise.files) {
195
+ const found = find(
196
+ file,
197
+ exercise.translations[lang],
198
+ exercise
199
+ );
200
+ findResultPromises.push(found);
201
+ }
202
+ // eslint-disable-next-line
203
+ let findResults = await Promise.all(findResultPromises);
204
+ for (const found of findResults) {
205
+ if (found) {
206
+ readmeFilesCount = {
207
+ ...readmeFilesCount,
208
+ count: readmeFilesCount.count + 1,
209
+ };
210
+ files.push(found);
211
+ }
212
+ }
213
+
214
+ if (!files.includes(true))
215
+ errors.push({
216
+ exercise: exercise.title,
217
+ msg: "This exercise doesn't have a README.md file.",
218
+ });
219
+ }
220
+ }
221
+
222
+ readmeFiles.push(readmeFilesCount);
223
+ }
224
+ }
225
+ } else
226
+ errors.push({
227
+ exercise: undefined,
228
+ msg: "The exercises array is empty.",
229
+ });
230
+
231
+ Console.log(
232
+ `${counter.images.total - counter.images.error} images ok from ${
233
+ counter.images.total
234
+ }`
235
+ );
236
+
237
+ Console.info(
238
+ " Checking if important files are missing... (README's, translations, gitignore...)"
239
+ );
240
+ // 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.
241
+ const files: string[] = [];
242
+ let count = 0;
243
+ for (const item of readmeFiles) {
244
+ if (count < item.count)
245
+ count = item.count;
246
+ }
247
+
248
+ for (const item of readmeFiles) {
249
+ if (item.count !== count)
250
+ files.push(` ${item.exercise}`);
251
+ }
252
+
253
+ if (files.length > 0) {
254
+ const filesString: string = files.join(",");
255
+ warnings.push({
256
+ exercise: undefined,
257
+ msg:
258
+ files.length === 1 ?
259
+ `This exercise is missing translations:${filesString}` :
260
+ `These exercises are missing translations:${filesString}`,
261
+ });
262
+ }
263
+
264
+ // Checks if the .gitignore file exists.
265
+ if (!fs.existsSync(".gitignore"))
266
+ warnings.push({
267
+ exercise: undefined,
268
+ msg: ".gitignore file doesn't exist",
269
+ });
270
+
271
+ counter.exercises = exercises!.length;
272
+ for (const readme of readmeFiles) {
273
+ counter.readmeFiles += readme.count;
274
+ }
275
+ } else {
276
+ // This is the audit code for Projects
277
+
278
+ // Getting the learn.json schema
279
+ const schemaResponse = await fetch(
280
+ "https://raw.githubusercontent.com/tommygonzaleza/project-template/main/.github/learn-schema.json"
281
+ );
282
+ const schema = await schemaResponse.json();
283
+
284
+ // Checking the "learn.json" file:
285
+ const learnjson = JSON.parse(
286
+ fs.readFileSync("./learn.json").toString()
287
+ );
288
+
289
+ if (!learnjson) {
290
+ Console.error(
291
+ "There is no learn.json file located in the root of the project."
292
+ );
293
+ process.exit(1);
294
+ }
295
+
296
+ // Checking the README.md files and possible translations.
297
+ let readmeFiles: any[] = [];
298
+ const translations: string[] = [];
299
+ const translationRegex = /README\.([a-z]{2,3})\.md/;
300
+
301
+ try {
302
+ const data = await fs.promises.readdir("./");
303
+ readmeFiles = data.filter(file => file.includes("README"));
304
+ if (readmeFiles.length === 0)
305
+ errors.push({
306
+ exercise: undefined!,
307
+ msg: `There is no README file in the repository.`,
308
+ });
309
+ } catch (error) {
310
+ if (error)
311
+ Console.error(
312
+ "There was an error getting the directory files",
313
+ error
314
+ );
315
+ }
316
+
317
+ for (const readmeFile of readmeFiles) {
318
+ // Checking the language of each README file.
319
+ if (readmeFile === "README.md")
320
+ translations.push("us");
321
+ else {
322
+ const regexGroups = translationRegex.exec(readmeFile);
323
+ if (regexGroups)
324
+ translations.push(regexGroups[1]);
325
+ }
326
+
327
+ const readme = fs.readFileSync(path.resolve(readmeFile)).toString();
328
+
329
+ const isEmpty = Audit.checkForEmptySpaces(readme);
330
+ if (isEmpty || !readme) {
331
+ errors.push({
332
+ exercise: undefined!,
333
+ msg: `This file "${readmeFile}" doesn't have any content inside.`,
334
+ });
335
+ continue;
336
+ }
337
+
338
+ if (readme.length < 800)
339
+ errors.push({
340
+ exercise: undefined,
341
+ msg: `The "${readmeFile}" file should have at least 800 characters (It currently have: ${readme.length}).`,
342
+ });
343
+
344
+ // eslint-disable-next-line
345
+ await Audit.checkUrl(
346
+ config!,
347
+ path.resolve(readmeFile),
348
+ readmeFile,
349
+ undefined,
350
+ errors,
351
+ warnings,
352
+ // eslint-disable-next-line
353
+ undefined
354
+ );
355
+ }
356
+
357
+ // Adding the translations to the learn.json
358
+ learnjson.translations = translations;
359
+
360
+ // Checking if the preview image (from the learn.json) is OK.
361
+ try {
362
+ const res = await fetch(learnjson.preview, { method: "HEAD" });
363
+ if (!res.ok) {
364
+ errors.push({
365
+ exercise: undefined,
366
+ msg: `The link of the "preview" is broken: ${learnjson.preview}`,
367
+ });
368
+ }
369
+ } catch {
370
+ errors.push({
371
+ exercise: undefined,
372
+ msg: `The link of the "preview" is broken: ${learnjson.preview}`,
373
+ });
374
+ }
375
+
376
+ const date = new Date();
377
+ learnjson.validationAt = date.getTime();
378
+
379
+ if (errors.length > 0)
380
+ learnjson.validationStatus = "error";
381
+ else if (warnings.length > 0)
382
+ learnjson.validationStatus = "warning";
383
+ else
384
+ learnjson.validationStatus = "success";
385
+
386
+ // Writes the "learn.json" file with all the new properties
387
+ await fs.promises.writeFile("./learn.json", JSON.stringify(learnjson));
388
+ }
389
+
390
+ await Audit.showWarnings(warnings);
391
+ // eslint-disable-next-line
392
+ await Audit.showErrors(errors, undefined);
393
+ }
394
+ }
395
+ }
396
+
397
+ AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
398
+ ...
399
+ learnpack audit checks for the following information in a repository:
400
+ 1. The configuration object has slug, repository and description. (Error)
401
+ 2. The command learnpack clean has been run. (Error)
402
+ 3. If a markdown or test file doesn't have any content. (Error)
403
+ 4. The links are accessing to valid servers. (Error)
404
+ 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
405
+ 6. The external images are working (If they are pointing to a valid server). (Error)
406
+ 7. The exercises directory names are valid. (Error)
407
+ 8. If an exercise doesn't have a README file. (Error)
408
+ 9. The exercises array (Of the config file) has content. (Error)
409
+ 10. The exercses have the same translations. (Warning)
410
+ 11. The .gitignore file exists. (Warning)
411
+ 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
412
+ `;
413
+
414
+ AuditCommand.flags = {
415
+ // name: flags.string({char: 'n', description: 'name to print'}),
416
+ };
417
+
418
+ export default AuditCommand;