@learnpack/learnpack 2.1.24 → 2.1.25
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +10 -10
- package/lib/commands/audit.js +16 -16
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
- package/src/commands/audit.ts +418 -418
package/README.md
CHANGED
@@ -21,7 +21,7 @@ $ npm install -g @learnpack/learnpack
|
|
21
21
|
$ learnpack COMMAND
|
22
22
|
running command...
|
23
23
|
$ learnpack (-v|--version|version)
|
24
|
-
@learnpack/learnpack/2.1.
|
24
|
+
@learnpack/learnpack/2.1.25 win32-x64 node-v16.14.0
|
25
25
|
$ learnpack --help [COMMAND]
|
26
26
|
USAGE
|
27
27
|
$ learnpack COMMAND
|
@@ -74,7 +74,7 @@ DESCRIPTION
|
|
74
74
|
12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
|
75
75
|
```
|
76
76
|
|
77
|
-
_See code: [src\commands\audit.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
77
|
+
_See code: [src\commands\audit.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\audit.ts)_
|
78
78
|
|
79
79
|
## `learnpack clean`
|
80
80
|
|
@@ -89,7 +89,7 @@ DESCRIPTION
|
|
89
89
|
Extra documentation goes here
|
90
90
|
```
|
91
91
|
|
92
|
-
_See code: [src\commands\clean.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
92
|
+
_See code: [src\commands\clean.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\clean.ts)_
|
93
93
|
|
94
94
|
## `learnpack download [PACKAGE]`
|
95
95
|
|
@@ -107,7 +107,7 @@ DESCRIPTION
|
|
107
107
|
Extra documentation goes here
|
108
108
|
```
|
109
109
|
|
110
|
-
_See code: [src\commands\download.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
110
|
+
_See code: [src\commands\download.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\download.ts)_
|
111
111
|
|
112
112
|
## `learnpack help [COMMAND]`
|
113
113
|
|
@@ -138,7 +138,7 @@ OPTIONS
|
|
138
138
|
-h, --grading show CLI help
|
139
139
|
```
|
140
140
|
|
141
|
-
_See code: [src\commands\init.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
141
|
+
_See code: [src\commands\init.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\init.ts)_
|
142
142
|
|
143
143
|
## `learnpack login [PACKAGE]`
|
144
144
|
|
@@ -156,7 +156,7 @@ DESCRIPTION
|
|
156
156
|
Extra documentation goes here
|
157
157
|
```
|
158
158
|
|
159
|
-
_See code: [src\commands\login.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
159
|
+
_See code: [src\commands\login.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\login.ts)_
|
160
160
|
|
161
161
|
## `learnpack logout [PACKAGE]`
|
162
162
|
|
@@ -174,7 +174,7 @@ DESCRIPTION
|
|
174
174
|
Extra documentation goes here
|
175
175
|
```
|
176
176
|
|
177
|
-
_See code: [src\commands\logout.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
177
|
+
_See code: [src\commands\logout.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\logout.ts)_
|
178
178
|
|
179
179
|
## `learnpack plugins`
|
180
180
|
|
@@ -309,7 +309,7 @@ DESCRIPTION
|
|
309
309
|
Extra documentation goes here
|
310
310
|
```
|
311
311
|
|
312
|
-
_See code: [src\commands\publish.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
312
|
+
_See code: [src\commands\publish.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\publish.ts)_
|
313
313
|
|
314
314
|
## `learnpack start`
|
315
315
|
|
@@ -330,7 +330,7 @@ OPTIONS
|
|
330
330
|
-w, --watch Watch for file changes
|
331
331
|
```
|
332
332
|
|
333
|
-
_See code: [src\commands\start.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
333
|
+
_See code: [src\commands\start.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\start.ts)_
|
334
334
|
|
335
335
|
## `learnpack test [EXERCISESLUG]`
|
336
336
|
|
@@ -344,5 +344,5 @@ ARGUMENTS
|
|
344
344
|
EXERCISESLUG The name of the exercise to test
|
345
345
|
```
|
346
346
|
|
347
|
-
_See code: [src\commands\test.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.
|
347
|
+
_See code: [src\commands\test.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.25/src\commands\test.ts)_
|
348
348
|
<!-- commandsstop -->
|
package/lib/commands/audit.js
CHANGED
@@ -265,7 +265,7 @@ class AuditCommand extends SessionCommand_1.default {
|
|
265
265
|
// Checking if the preview image (from the learn.json) is OK.
|
266
266
|
try {
|
267
267
|
const res = await fetch(learnjson.preview, { method: "HEAD" });
|
268
|
-
if (
|
268
|
+
if (res.status > 399 && res.status < 500) {
|
269
269
|
errors.push({
|
270
270
|
exercise: undefined,
|
271
271
|
msg: `The link of the "preview" is broken: ${learnjson.preview}`,
|
@@ -295,21 +295,21 @@ class AuditCommand extends SessionCommand_1.default {
|
|
295
295
|
}
|
296
296
|
}
|
297
297
|
}
|
298
|
-
AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
|
299
|
-
...
|
300
|
-
learnpack audit checks for the following information in a repository:
|
301
|
-
1. The configuration object has slug, repository and description. (Error)
|
302
|
-
2. The command learnpack clean has been run. (Error)
|
303
|
-
3. If a markdown or test file doesn't have any content. (Error)
|
304
|
-
4. The links are accessing to valid servers. (Error)
|
305
|
-
5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
|
306
|
-
6. The external images are working (If they are pointing to a valid server). (Error)
|
307
|
-
7. The exercises directory names are valid. (Error)
|
308
|
-
8. If an exercise doesn't have a README file. (Error)
|
309
|
-
9. The exercises array (Of the config file) has content. (Error)
|
310
|
-
10. The exercses have the same translations. (Warning)
|
311
|
-
11. The .gitignore file exists. (Warning)
|
312
|
-
12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
|
298
|
+
AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
|
299
|
+
...
|
300
|
+
learnpack audit checks for the following information in a repository:
|
301
|
+
1. The configuration object has slug, repository and description. (Error)
|
302
|
+
2. The command learnpack clean has been run. (Error)
|
303
|
+
3. If a markdown or test file doesn't have any content. (Error)
|
304
|
+
4. The links are accessing to valid servers. (Error)
|
305
|
+
5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
|
306
|
+
6. The external images are working (If they are pointing to a valid server). (Error)
|
307
|
+
7. The exercises directory names are valid. (Error)
|
308
|
+
8. If an exercise doesn't have a README file. (Error)
|
309
|
+
9. The exercises array (Of the config file) has content. (Error)
|
310
|
+
10. The exercses have the same translations. (Warning)
|
311
|
+
11. The .gitignore file exists. (Warning)
|
312
|
+
12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
|
313
313
|
`;
|
314
314
|
AuditCommand.flags = {
|
315
315
|
// name: flags.string({char: 'n', description: 'name to print'}),
|
package/oclif.manifest.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":"2.1.
|
1
|
+
{"version":"2.1.25","commands":{"audit":{"id":"audit","description":"learnpack audit is the command in charge of creating an auditory of the repository\n...\nlearnpack audit checks for the following information in a repository:\n 1. The configuration object has slug, repository and description. (Error)\n 2. The command learnpack clean has been run. (Error)\n 3. If a markdown or test file doesn't have any content. (Error)\n 4. The links are accessing to valid servers. (Error)\n 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)\n 6. The external images are working (If they are pointing to a valid server). (Error)\n 7. The exercises directory names are valid. (Error)\n 8. If an exercise doesn't have a README file. (Error)\n 9. The exercises array (Of the config file) has content. (Error)\n 10. The exercses have the same translations. (Warning)\n 11. The .gitignore file exists. (Warning)\n 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)\n","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[]},"clean":{"id":"clean","description":"Clean the configuration object\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[]},"download":{"id":"download","description":"Describe the command here\n...\nExtra documentation goes here\n","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"init":{"id":"init","description":"Create a new learning package: Book, Tutorial or Exercise","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{"grading":{"name":"grading","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"login":{"id":"login","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"logout":{"id":"logout","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"publish":{"id":"publish","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"start":{"id":"start","description":"Runs a small server with all the exercise instructions","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{"port":{"name":"port","type":"option","char":"p","description":"server port"},"host":{"name":"host","type":"option","char":"h","description":"server host"},"disableGrading":{"name":"disableGrading","type":"boolean","char":"D","description":"disble grading functionality","allowNo":false},"watch":{"name":"watch","type":"boolean","char":"w","description":"Watch for file changes","allowNo":false},"editor":{"name":"editor","type":"option","char":"e","description":"[standalone, gitpod]","options":["standalone","gitpod"]},"version":{"name":"version","type":"option","char":"v","description":"E.g: 1.0.1"},"grading":{"name":"grading","type":"option","char":"g","description":"[isolated, incremental]","options":["isolated","incremental"]},"debug":{"name":"debug","type":"boolean","char":"d","description":"debugger mode for more verbage","allowNo":false}},"args":[]},"test":{"id":"test","description":"Test exercises","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"exerciseSlug","description":"The name of the exercise to test","required":false,"hidden":false}]}}}
|
package/package.json
CHANGED
package/src/commands/audit.ts
CHANGED
@@ -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 (
|
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.status > 399 && res.status < 500) {
|
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;
|