@learnpack/learnpack 2.1.20 → 2.1.23
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 +62 -220
- package/lib/utils/audit.d.ts +3 -0
- package/lib/utils/audit.js +279 -116
- package/oclif.manifest.json +1 -1
- package/package.json +2 -2
- package/src/commands/audit.ts +87 -281
- package/src/utils/audit.ts +395 -181
package/lib/utils/audit.js
CHANGED
@@ -1,139 +1,302 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
const console_1 = require("./console");
|
4
|
+
const fs = require("fs");
|
5
|
+
const path = require("path");
|
4
6
|
// eslint-disable-next-line
|
5
7
|
const fetch = require("node-fetch");
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
8
|
+
// eslint-disable-next-line
|
9
|
+
const fm = require("front-matter");
|
10
|
+
// This function checks if a url is valid.
|
11
|
+
const isUrl = async (url, errors, counter) => {
|
12
|
+
const regexUrl = /(https?:\/\/[\w./-]+)/gm;
|
13
|
+
counter.links.total++;
|
14
|
+
if (!regexUrl.test(url)) {
|
15
|
+
counter.links.error++;
|
16
|
+
errors.push({
|
17
|
+
exercise: undefined,
|
18
|
+
msg: `The repository value of the configuration file is not a link: ${url}`,
|
19
|
+
});
|
20
|
+
return false;
|
21
|
+
}
|
22
|
+
const res = await fetch(url, { method: "HEAD" });
|
23
|
+
if (!res.ok) {
|
24
|
+
counter.links.error++;
|
25
|
+
errors.push({
|
26
|
+
exercise: undefined,
|
27
|
+
msg: `The link of the repository is broken: ${url}`,
|
28
|
+
});
|
29
|
+
}
|
30
|
+
return true;
|
31
|
+
};
|
32
|
+
const checkForEmptySpaces = (str) => {
|
33
|
+
const isEmpty = true;
|
34
|
+
for (const letter of str) {
|
35
|
+
if (letter !== " ") {
|
18
36
|
return false;
|
19
37
|
}
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
}
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
38
|
+
}
|
39
|
+
return isEmpty;
|
40
|
+
};
|
41
|
+
const checkLearnpackClean = (configObj, errors) => {
|
42
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
43
|
+
if ((((_a = configObj.config) === null || _a === void 0 ? void 0 : _a.outputPath) &&
|
44
|
+
fs.existsSync((_b = configObj.config) === null || _b === void 0 ? void 0 : _b.outputPath)) ||
|
45
|
+
fs.existsSync(`${(_c = configObj.config) === null || _c === void 0 ? void 0 : _c.dirPath}/_app`) ||
|
46
|
+
fs.existsSync(`${(_d = configObj.config) === null || _d === void 0 ? void 0 : _d.dirPath}/reports`) ||
|
47
|
+
fs.existsSync(`${(_e = configObj.config) === null || _e === void 0 ? void 0 : _e.dirPath}/resets`) ||
|
48
|
+
fs.existsSync(`${(_f = configObj.config) === null || _f === void 0 ? void 0 : _f.dirPath}/app.tar.gz`) ||
|
49
|
+
fs.existsSync(`${(_g = configObj.config) === null || _g === void 0 ? void 0 : _g.dirPath}/config.json`) ||
|
50
|
+
fs.existsSync(`${(_h = configObj.config) === null || _h === void 0 ? void 0 : _h.dirPath}/vscode_queue.json`)) {
|
51
|
+
errors.push({
|
52
|
+
exercise: undefined,
|
53
|
+
msg: "You have to run learnpack clean command",
|
54
|
+
});
|
55
|
+
}
|
56
|
+
};
|
57
|
+
const findInFile = (types, content) => {
|
58
|
+
const regex = {
|
59
|
+
relativeImages: /!\[.*]\s*\((((\.\/)?(\.{2}\/){1,5})(.*\/)*(.[^\s/]*\.[A-Za-z]{2,4})\S*)\)/gm,
|
60
|
+
externalImages: /!\[.*]\((https?:\/(\/[^)/]+)+\/?)\)/gm,
|
61
|
+
markdownLinks: /(\s)+\[.*]\((https?:\/(\/[^)/]+)+\/?)\)/gm,
|
62
|
+
url: /(https?:\/\/[\w./-]+)/gm,
|
63
|
+
uploadcare: /https:\/\/ucarecdn.com\/(?:.*\/)*([\w./-]+)/gm,
|
64
|
+
};
|
65
|
+
const validTypes = Object.keys(regex);
|
66
|
+
if (!Array.isArray(types))
|
67
|
+
types = [types];
|
68
|
+
const findings = {};
|
69
|
+
for (const type of types) {
|
70
|
+
if (!validTypes.includes(type))
|
71
|
+
throw new Error("Invalid type: " + type);
|
72
|
+
else
|
73
|
+
findings[type] = {};
|
74
|
+
}
|
75
|
+
for (const type of types) {
|
76
|
+
let m;
|
77
|
+
while ((m = regex[type].exec(content)) !== null) {
|
78
|
+
// This is necessary to avoid infinite loops with zero-width matches
|
79
|
+
if (m.index === regex.lastIndex) {
|
80
|
+
regex.lastIndex++;
|
35
81
|
}
|
82
|
+
// The result can be accessed through the `m`-variable.
|
83
|
+
// m.forEach((match, groupIndex) => values.push(match));
|
84
|
+
findings[type][m[0]] = {
|
85
|
+
content: m[0],
|
86
|
+
absUrl: m[1],
|
87
|
+
mdUrl: m[2],
|
88
|
+
relUrl: m[6],
|
89
|
+
};
|
36
90
|
}
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
if (!validTypes.includes(type))
|
69
|
-
throw new Error("Invalid type: " + type);
|
70
|
-
else
|
71
|
-
findings[type] = {};
|
72
|
-
}
|
73
|
-
for (const type of types) {
|
74
|
-
let m;
|
75
|
-
while ((m = regex[type].exec(content)) !== null) {
|
76
|
-
// This is necessary to avoid infinite loops with zero-width matches
|
77
|
-
if (m.index === regex.lastIndex) {
|
78
|
-
regex.lastIndex++;
|
91
|
+
}
|
92
|
+
return findings;
|
93
|
+
};
|
94
|
+
// This function checks that each of the url's are working.
|
95
|
+
const checkUrl = async (config, filePath, fileName, exercise, errors, warnings, counter) => {
|
96
|
+
var _a, _b, _c, _d;
|
97
|
+
if (!fs.existsSync(filePath))
|
98
|
+
return false;
|
99
|
+
const content = fs.readFileSync(filePath).toString();
|
100
|
+
const isEmpty = checkForEmptySpaces(content);
|
101
|
+
if (isEmpty || !content)
|
102
|
+
errors.push({
|
103
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
104
|
+
msg: `This file (${fileName}) doesn't have any content inside.`,
|
105
|
+
});
|
106
|
+
const frontmatter = fm(content);
|
107
|
+
for (const attribute in frontmatter.attributes) {
|
108
|
+
if (Object.prototype.hasOwnProperty.call(frontmatter.attributes, attribute) &&
|
109
|
+
(attribute === "intro" || attribute === "tutorial")) {
|
110
|
+
counter && counter.links.total++;
|
111
|
+
try {
|
112
|
+
// eslint-disable-next-line
|
113
|
+
let res = await fetch(frontmatter.attributes[attribute], {
|
114
|
+
method: "HEAD",
|
115
|
+
});
|
116
|
+
if (!res.ok) {
|
117
|
+
counter && counter.links.error++;
|
118
|
+
errors.push({
|
119
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
120
|
+
msg: `This link is broken (${res.ok}): ${frontmatter.attributes[attribute]}`,
|
121
|
+
});
|
79
122
|
}
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
};
|
123
|
+
}
|
124
|
+
catch (_e) {
|
125
|
+
counter && counter.links.error++;
|
126
|
+
errors.push({
|
127
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
128
|
+
msg: `This link is broken: ${frontmatter.attributes[attribute]}`,
|
129
|
+
});
|
88
130
|
}
|
89
131
|
}
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
132
|
+
}
|
133
|
+
// Check url's of each README file.
|
134
|
+
const findings = findInFile(["relativeImages", "externalImages", "markdownLinks"], content);
|
135
|
+
for (const finding in findings) {
|
136
|
+
if (Object.prototype.hasOwnProperty.call(findings, finding)) {
|
137
|
+
const obj = findings[finding];
|
138
|
+
// Valdites all the relative path images.
|
139
|
+
if (finding === "relativeImages" && Object.keys(obj).length > 0) {
|
140
|
+
for (const img in obj) {
|
141
|
+
if (Object.prototype.hasOwnProperty.call(obj, img)) {
|
142
|
+
// Validates if the image is in the assets folder.
|
143
|
+
counter && counter.images.total++;
|
144
|
+
const relativePath = path
|
145
|
+
.relative(exercise ? exercise.path.replace(/\\/gm, "/") : "./", `${(_a = config.config) === null || _a === void 0 ? void 0 : _a.dirPath}/assets/${obj[img].relUrl}`)
|
146
|
+
.replace(/\\/gm, "/");
|
147
|
+
if (relativePath !== obj[img].absUrl.split("?").shift()) {
|
148
|
+
counter && counter.images.error++;
|
149
|
+
errors.push({
|
150
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
151
|
+
msg: `This relative path (${obj[img].relUrl}) is not pointing to the assets folder.`,
|
152
|
+
});
|
153
|
+
}
|
154
|
+
if (!fs.existsSync(`${(_b = config.config) === null || _b === void 0 ? void 0 : _b.dirPath}/assets/${obj[img].relUrl}`)) {
|
155
|
+
counter && counter.images.error++;
|
156
|
+
errors.push({
|
157
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
158
|
+
msg: `The file ${obj[img].relUrl} doesn't exist in the assets folder.`,
|
159
|
+
});
|
160
|
+
}
|
102
161
|
}
|
103
|
-
else {
|
104
|
-
console_1.default.error(` We found ${errors.length} error${errors.length > 1 ? "s" : ""} related with the project integrity.`);
|
105
|
-
}
|
106
|
-
process.exit(1);
|
107
162
|
}
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
163
|
+
}
|
164
|
+
else if (finding === "externalImages" && Object.keys(obj).length > 0) {
|
165
|
+
// Valdites all the aboslute path images.
|
166
|
+
for (const img in obj) {
|
167
|
+
if (Object.prototype.hasOwnProperty.call(obj, img)) {
|
168
|
+
counter && counter.images.total++;
|
169
|
+
if (fs.existsSync(`${(_c = config.config) === null || _c === void 0 ? void 0 : _c.dirPath}/assets${obj[img].mdUrl
|
170
|
+
.split("?")
|
171
|
+
.shift()}`)) {
|
172
|
+
const relativePath = path
|
173
|
+
.relative(exercise ? exercise.path.replace(/\\/gm, "/") : "./", `${(_d = config.config) === null || _d === void 0 ? void 0 : _d.dirPath}/assets/${obj[img].mdUrl}`)
|
174
|
+
.replace(/\\/gm, "/");
|
175
|
+
warnings.push({
|
176
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
177
|
+
msg: `On this exercise you have an image with an absolute path "${obj[img].absUrl}". We recommend you to replace it by the relative path: "${relativePath}".`,
|
178
|
+
});
|
179
|
+
}
|
180
|
+
try {
|
181
|
+
// eslint-disable-next-line
|
182
|
+
let res = await fetch(obj[img].absUrl, {
|
183
|
+
method: "HEAD",
|
184
|
+
});
|
185
|
+
if (!res.ok) {
|
186
|
+
counter && counter.images.error++;
|
187
|
+
errors.push({
|
188
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
189
|
+
msg: `This link is broken: ${obj[img].absUrl}`,
|
190
|
+
});
|
191
|
+
}
|
192
|
+
}
|
193
|
+
catch (_f) {
|
194
|
+
counter && counter.images.error++;
|
195
|
+
errors.push({
|
196
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
197
|
+
msg: `This link is broken: ${obj[img].absUrl}`,
|
198
|
+
});
|
199
|
+
}
|
114
200
|
}
|
115
|
-
process.exit(0);
|
116
201
|
}
|
117
202
|
}
|
118
|
-
else {
|
119
|
-
|
203
|
+
else if (finding === "markdownLinks" && Object.keys(obj).length > 0) {
|
204
|
+
for (const link in obj) {
|
205
|
+
if (Object.prototype.hasOwnProperty.call(obj, link)) {
|
206
|
+
counter && counter.links.total++;
|
207
|
+
if (!obj[link].mdUrl.includes("twitter")) {
|
208
|
+
try {
|
209
|
+
// eslint-disable-next-line
|
210
|
+
let res = await fetch(obj[link].mdUrl, {
|
211
|
+
method: "HEAD",
|
212
|
+
});
|
213
|
+
if (res.status > 399 && res.status < 500) {
|
214
|
+
counter && counter.links.error++;
|
215
|
+
errors.push({
|
216
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
217
|
+
msg: `This link is broken: ${obj[link].mdUrl}`,
|
218
|
+
});
|
219
|
+
}
|
220
|
+
}
|
221
|
+
catch (_g) {
|
222
|
+
counter && counter.links.error++;
|
223
|
+
errors.push({
|
224
|
+
exercise: exercise === null || exercise === void 0 ? void 0 : exercise.title,
|
225
|
+
msg: `This link is broken: ${obj[link].mdUrl}`,
|
226
|
+
});
|
227
|
+
}
|
228
|
+
}
|
229
|
+
}
|
230
|
+
}
|
120
231
|
}
|
121
|
-
}
|
122
|
-
}
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
232
|
+
}
|
233
|
+
}
|
234
|
+
return true;
|
235
|
+
};
|
236
|
+
// This function writes a given file with the given content.
|
237
|
+
const writeFile = async (content, filePath) => {
|
238
|
+
try {
|
239
|
+
await fs.promises.writeFile(filePath, content);
|
240
|
+
}
|
241
|
+
catch (error) {
|
242
|
+
if (error)
|
243
|
+
console_1.default.error(`We weren't able to write the file in this path "${filePath}".`, error);
|
244
|
+
}
|
245
|
+
};
|
246
|
+
// This function checks if there are errors, and show them in the console at the end.
|
247
|
+
const showErrors = (errors, counter) => {
|
248
|
+
return new Promise((resolve, reject) => {
|
249
|
+
if (errors) {
|
250
|
+
if (errors.length > 0) {
|
251
|
+
console_1.default.log("Checking for errors...");
|
252
|
+
for (const [i, error] of errors.entries())
|
253
|
+
console_1.default.error(`${i + 1}) ${error.msg} ${error.exercise ? `(Exercise: ${error.exercise})` : ""}`);
|
254
|
+
if (counter) {
|
255
|
+
console_1.default.error(` We found ${errors.length} error${errors.length > 1 ? "s" : ""} among ${counter.images.total} images, ${counter.links.total} link, ${counter.readmeFiles} README files and ${counter.exercises} exercises.`);
|
256
|
+
}
|
257
|
+
else {
|
258
|
+
console_1.default.error(` We found ${errors.length} error${errors.length > 1 ? "s" : ""} related with the project integrity.`);
|
131
259
|
}
|
132
|
-
|
260
|
+
process.exit(1);
|
133
261
|
}
|
134
262
|
else {
|
135
|
-
|
263
|
+
if (counter) {
|
264
|
+
console_1.default.success(`We didn't find any errors in this repository among ${counter.images.total} images, ${counter.links.total} link, ${counter.readmeFiles} README files and ${counter.exercises} exercises.`);
|
265
|
+
}
|
266
|
+
else {
|
267
|
+
console_1.default.success(`We didn't find any errors in this repository.`);
|
268
|
+
}
|
269
|
+
process.exit(0);
|
136
270
|
}
|
137
|
-
}
|
138
|
-
|
271
|
+
}
|
272
|
+
else {
|
273
|
+
reject("Failed");
|
274
|
+
}
|
275
|
+
});
|
276
|
+
};
|
277
|
+
// This function checks if there are warnings, and show them in the console at the end.
|
278
|
+
const showWarnings = (warnings) => {
|
279
|
+
return new Promise((resolve, reject) => {
|
280
|
+
if (warnings) {
|
281
|
+
if (warnings.length > 0) {
|
282
|
+
console_1.default.log("Checking for warnings...");
|
283
|
+
for (const [i, warning] of warnings.entries())
|
284
|
+
console_1.default.warning(`${i + 1}) ${warning.msg} ${warning.exercise ? `File: ${warning.exercise}` : ""}`);
|
285
|
+
}
|
286
|
+
resolve("SUCCESS");
|
287
|
+
}
|
288
|
+
else {
|
289
|
+
reject("Failed");
|
290
|
+
}
|
291
|
+
});
|
292
|
+
};
|
293
|
+
exports.default = {
|
294
|
+
isUrl,
|
295
|
+
checkForEmptySpaces,
|
296
|
+
checkLearnpackClean,
|
297
|
+
findInFile,
|
298
|
+
checkUrl,
|
299
|
+
writeFile,
|
300
|
+
showErrors,
|
301
|
+
showWarnings,
|
139
302
|
};
|
package/oclif.manifest.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":"2.1.
|
1
|
+
{"version":"2.1.23","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
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@learnpack/learnpack",
|
3
3
|
"description": "Create, sell or download and take learning amazing learning packages",
|
4
|
-
"version": "2.1.
|
4
|
+
"version": "2.1.23",
|
5
5
|
"author": "Alejandro Sanchez @alesanchezr",
|
6
6
|
"bin": {
|
7
7
|
"learnpack": "bin/run"
|
@@ -78,7 +78,7 @@
|
|
78
78
|
"typescript": "4.0"
|
79
79
|
},
|
80
80
|
"engines": {
|
81
|
-
"node": ">=
|
81
|
+
"node": ">=14.0.0"
|
82
82
|
},
|
83
83
|
"files": [
|
84
84
|
"/bin",
|