@learnpack/learnpack 2.1.26 → 2.1.27
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.
- package/README.md +10 -10
- package/oclif.manifest.json +1 -1
- package/package.json +2 -1
- package/src/commands/audit.ts +113 -113
- package/src/commands/clean.ts +10 -10
- package/src/commands/download.ts +18 -18
- package/src/commands/init.ts +39 -39
- package/src/commands/login.ts +13 -13
- package/src/commands/logout.ts +9 -9
- package/src/commands/publish.ts +25 -25
- package/src/commands/start.ts +101 -75
- package/src/commands/test.ts +34 -34
- package/src/managers/config/allowed_files.ts +2 -2
- package/src/managers/config/defaults.ts +2 -2
- package/src/managers/config/exercise.ts +79 -79
- package/src/managers/config/index.ts +145 -145
- package/src/managers/file.ts +74 -65
- package/src/managers/server/index.ts +32 -31
- package/src/managers/server/routes.ts +139 -90
- package/src/managers/session.ts +53 -24
- package/src/managers/socket.ts +92 -79
- package/src/models/action.ts +8 -1
- package/src/models/config-manager.ts +2 -2
- package/src/models/config.ts +7 -2
- package/src/models/exercise-obj.ts +6 -3
- package/src/models/plugin-config.ts +2 -2
- package/src/models/session.ts +5 -2
- package/src/models/socket.ts +12 -6
- package/src/models/status.ts +15 -14
- package/src/plugin/command/compile.ts +10 -10
- package/src/plugin/command/test.ts +14 -14
- package/src/plugin/index.ts +5 -5
- package/src/plugin/plugin.ts +26 -26
- package/src/plugin/utils.ts +23 -23
- package/src/utils/BaseCommand.ts +16 -16
- package/src/utils/api.ts +143 -91
- package/src/utils/audit.ts +93 -96
- package/src/utils/exercisesQueue.ts +15 -15
- package/src/utils/fileQueue.ts +85 -85
- package/src/utils/watcher.ts +14 -14
- package/lib/commands/audit.d.ts +0 -6
- package/lib/commands/audit.js +0 -342
- package/lib/commands/clean.d.ts +0 -8
- package/lib/commands/clean.js +0 -25
- package/lib/commands/download.d.ts +0 -13
- package/lib/commands/download.js +0 -55
- package/lib/commands/init.d.ts +0 -9
- package/lib/commands/init.js +0 -123
- package/lib/commands/login.d.ts +0 -14
- package/lib/commands/login.js +0 -37
- package/lib/commands/logout.d.ts +0 -14
- package/lib/commands/logout.js +0 -37
- package/lib/commands/publish.d.ts +0 -14
- package/lib/commands/publish.js +0 -82
- package/lib/commands/start.d.ts +0 -7
- package/lib/commands/start.js +0 -165
- package/lib/commands/test.d.ts +0 -6
- package/lib/commands/test.js +0 -62
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -4
- package/lib/managers/config/allowed_files.d.ts +0 -5
- package/lib/managers/config/allowed_files.js +0 -30
- package/lib/managers/config/defaults.d.ts +0 -39
- package/lib/managers/config/defaults.js +0 -40
- package/lib/managers/config/exercise.d.ts +0 -36
- package/lib/managers/config/exercise.js +0 -233
- package/lib/managers/config/index.d.ts +0 -3
- package/lib/managers/config/index.js +0 -320
- package/lib/managers/file.d.ts +0 -13
- package/lib/managers/file.js +0 -134
- package/lib/managers/gitpod.d.ts +0 -3
- package/lib/managers/gitpod.js +0 -67
- package/lib/managers/server/index.d.ts +0 -6
- package/lib/managers/server/index.js +0 -58
- package/lib/managers/server/routes.d.ts +0 -4
- package/lib/managers/server/routes.js +0 -167
- package/lib/managers/session.d.ts +0 -3
- package/lib/managers/session.js +0 -104
- package/lib/managers/socket.d.ts +0 -3
- package/lib/managers/socket.js +0 -164
- package/lib/managers/test.d.ts +0 -0
- package/lib/managers/test.js +0 -84
- package/lib/models/action.d.ts +0 -2
- package/lib/models/action.js +0 -2
- package/lib/models/audit.d.ts +0 -15
- package/lib/models/audit.js +0 -2
- package/lib/models/config-manager.d.ts +0 -21
- package/lib/models/config-manager.js +0 -2
- package/lib/models/config.d.ts +0 -62
- package/lib/models/config.js +0 -2
- package/lib/models/counter.d.ts +0 -11
- package/lib/models/counter.js +0 -2
- package/lib/models/errors.d.ts +0 -15
- package/lib/models/errors.js +0 -2
- package/lib/models/exercise-obj.d.ts +0 -27
- package/lib/models/exercise-obj.js +0 -2
- package/lib/models/file.d.ts +0 -5
- package/lib/models/file.js +0 -2
- package/lib/models/findings.d.ts +0 -17
- package/lib/models/findings.js +0 -2
- package/lib/models/flags.d.ts +0 -10
- package/lib/models/flags.js +0 -2
- package/lib/models/front-matter.d.ts +0 -11
- package/lib/models/front-matter.js +0 -2
- package/lib/models/gitpod-data.d.ts +0 -16
- package/lib/models/gitpod-data.js +0 -2
- package/lib/models/language.d.ts +0 -4
- package/lib/models/language.js +0 -2
- package/lib/models/package.d.ts +0 -7
- package/lib/models/package.js +0 -2
- package/lib/models/plugin-config.d.ts +0 -16
- package/lib/models/plugin-config.js +0 -2
- package/lib/models/session.d.ts +0 -23
- package/lib/models/session.js +0 -2
- package/lib/models/socket.d.ts +0 -31
- package/lib/models/socket.js +0 -2
- package/lib/models/status.d.ts +0 -1
- package/lib/models/status.js +0 -2
- package/lib/models/success-types.d.ts +0 -1
- package/lib/models/success-types.js +0 -2
- package/lib/plugin/command/compile.d.ts +0 -6
- package/lib/plugin/command/compile.js +0 -18
- package/lib/plugin/command/test.d.ts +0 -6
- package/lib/plugin/command/test.js +0 -25
- package/lib/plugin/index.d.ts +0 -27
- package/lib/plugin/index.js +0 -7
- package/lib/plugin/plugin.d.ts +0 -8
- package/lib/plugin/plugin.js +0 -68
- package/lib/plugin/utils.d.ts +0 -16
- package/lib/plugin/utils.js +0 -58
- package/lib/ui/download.d.ts +0 -5
- package/lib/ui/download.js +0 -61
- package/lib/utils/BaseCommand.d.ts +0 -8
- package/lib/utils/BaseCommand.js +0 -41
- package/lib/utils/SessionCommand.d.ts +0 -10
- package/lib/utils/SessionCommand.js +0 -47
- package/lib/utils/api.d.ts +0 -12
- package/lib/utils/api.js +0 -173
- package/lib/utils/audit.d.ts +0 -16
- package/lib/utils/audit.js +0 -302
- package/lib/utils/console.d.ts +0 -12
- package/lib/utils/console.js +0 -19
- package/lib/utils/errors.d.ts +0 -17
- package/lib/utils/errors.js +0 -100
- package/lib/utils/exercisesQueue.d.ts +0 -9
- package/lib/utils/exercisesQueue.js +0 -38
- package/lib/utils/fileQueue.d.ts +0 -40
- package/lib/utils/fileQueue.js +0 -168
- package/lib/utils/misc.d.ts +0 -1
- package/lib/utils/misc.js +0 -23
- package/lib/utils/validators.d.ts +0 -5
- package/lib/utils/validators.js +0 -17
- package/lib/utils/watcher.d.ts +0 -2
- package/lib/utils/watcher.js +0 -23
package/src/utils/fileQueue.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import logger from "../utils/console"
|
|
2
|
-
import * as fs from "fs"
|
|
1
|
+
import logger from "../utils/console"
|
|
2
|
+
import * as fs from "fs"
|
|
3
3
|
// import em from "events"
|
|
4
|
-
import * as XXH from "xxhashjs"
|
|
4
|
+
import * as XXH from "xxhashjs"
|
|
5
5
|
|
|
6
6
|
// possible events to dispatch
|
|
7
7
|
const events = {
|
|
@@ -13,186 +13,186 @@ const events = {
|
|
|
13
13
|
OPEN_FILES: "open_files",
|
|
14
14
|
OPEN_WINDOW: "open_window",
|
|
15
15
|
INSTRUCTIONS_CLOSED: "instructions_closed",
|
|
16
|
-
}
|
|
16
|
+
}
|
|
17
17
|
|
|
18
18
|
let options = {
|
|
19
19
|
path: null,
|
|
20
20
|
create: false,
|
|
21
|
-
}
|
|
22
|
-
let lastHash: any = null
|
|
23
|
-
let watcher: any = null
|
|
24
|
-
let actions: any = null
|
|
21
|
+
}
|
|
22
|
+
let lastHash: any = null
|
|
23
|
+
let watcher: any = null // subscribe to file and listen to changes
|
|
24
|
+
let actions: any = null // action queue
|
|
25
25
|
|
|
26
26
|
const loadDispatcher = (opts: any) => {
|
|
27
|
-
actions = [{ name: "initializing", time: now() }]
|
|
28
|
-
logger.debug(`Loading from ${opts.path}`)
|
|
27
|
+
actions = [{ name: "initializing", time: now() }]
|
|
28
|
+
logger.debug(`Loading from ${opts.path}`)
|
|
29
29
|
|
|
30
|
-
let exists = fs.existsSync(opts.path)
|
|
30
|
+
let exists = fs.existsSync(opts.path)
|
|
31
31
|
if (opts.create) {
|
|
32
32
|
if (exists)
|
|
33
|
-
actions.push({ name: "reset", time: now() })
|
|
34
|
-
fs.writeFileSync(opts.path, JSON.stringify(actions), { flag: "w" })
|
|
35
|
-
exists = true
|
|
33
|
+
actions.push({ name: "reset", time: now() })
|
|
34
|
+
fs.writeFileSync(opts.path, JSON.stringify(actions), { flag: "w" })
|
|
35
|
+
exists = true
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
if (!exists)
|
|
39
|
-
throw new Error(`Invalid queue path, missing file at: ${opts.path}`)
|
|
39
|
+
throw new Error(`Invalid queue path, missing file at: ${opts.path}`)
|
|
40
40
|
|
|
41
|
-
let incomingActions = []
|
|
41
|
+
let incomingActions = []
|
|
42
42
|
try {
|
|
43
|
-
const content = fs.readFileSync(opts.path, "utf-8")
|
|
44
|
-
incomingActions = JSON.parse(content)
|
|
43
|
+
const content = fs.readFileSync(opts.path, "utf-8")
|
|
44
|
+
incomingActions = JSON.parse(content)
|
|
45
45
|
if (!Array.isArray(incomingActions))
|
|
46
|
-
incomingActions = []
|
|
46
|
+
incomingActions = []
|
|
47
47
|
} catch {
|
|
48
|
-
incomingActions = []
|
|
49
|
-
logger.debug("Error loading VSCode Actions file")
|
|
48
|
+
incomingActions = []
|
|
49
|
+
logger.debug("Error loading VSCode Actions file")
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
logger.debug("Actions load ", incomingActions)
|
|
53
|
-
return incomingActions
|
|
54
|
-
}
|
|
52
|
+
logger.debug("Actions load ", incomingActions)
|
|
53
|
+
return incomingActions
|
|
54
|
+
}
|
|
55
55
|
|
|
56
56
|
// eslint-disable-next-line
|
|
57
57
|
const enqueue = (name: string, data: any | undefined = undefined) => {
|
|
58
58
|
if (!Object.values(events).includes(name)) {
|
|
59
|
-
logger.debug(`Invalid event ${name}`)
|
|
60
|
-
throw new Error(`Invalid action ${name}`)
|
|
59
|
+
logger.debug(`Invalid event ${name}`)
|
|
60
|
+
throw new Error(`Invalid action ${name}`)
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
if (!actions)
|
|
64
|
-
actions = []
|
|
64
|
+
actions = []
|
|
65
65
|
|
|
66
|
-
actions.push({ name, time: now(), data: data })
|
|
67
|
-
logger.debug(`EMIT -> ${name}:Exporting changes to ${options.path}`)
|
|
66
|
+
actions.push({ name, time: now(), data: data })
|
|
67
|
+
logger.debug(`EMIT -> ${name}:Exporting changes to ${options.path}`)
|
|
68
68
|
|
|
69
|
-
return fs.writeFileSync(options.path || "", JSON.stringify(actions))
|
|
70
|
-
}
|
|
69
|
+
return fs.writeFileSync(options.path || "", JSON.stringify(actions))
|
|
70
|
+
}
|
|
71
71
|
|
|
72
72
|
const now = () => {
|
|
73
|
-
const hrTime = process.hrtime()
|
|
73
|
+
const hrTime = process.hrtime()
|
|
74
74
|
// eslint-disable-next-line
|
|
75
75
|
const htTime0 = hrTime[0] * 1000000;
|
|
76
|
-
return (htTime0 + hrTime[1]) / 1000
|
|
77
|
-
}
|
|
76
|
+
return (htTime0 + hrTime[1]) / 1000
|
|
77
|
+
}
|
|
78
78
|
|
|
79
79
|
const loadFile = (filePath: string) => {
|
|
80
80
|
if (!fs.existsSync(filePath))
|
|
81
|
-
throw new Error(`No queue.json file to load on ${filePath}`)
|
|
81
|
+
throw new Error(`No queue.json file to load on ${filePath}`)
|
|
82
82
|
|
|
83
|
-
const content = fs.readFileSync(filePath, "utf8")
|
|
84
|
-
const newHash = XXH.h32(content, 0xAB_CD).toString(16)
|
|
85
|
-
const isUpdated = lastHash !== newHash
|
|
86
|
-
lastHash = newHash
|
|
87
|
-
const incomingActions = JSON.parse(content)
|
|
88
|
-
return { isUpdated, incomingActions }
|
|
89
|
-
}
|
|
83
|
+
const content = fs.readFileSync(filePath, "utf8")
|
|
84
|
+
const newHash = XXH.h32(content, 0xAB_CD).toString(16)
|
|
85
|
+
const isUpdated = lastHash !== newHash
|
|
86
|
+
lastHash = newHash
|
|
87
|
+
const incomingActions = JSON.parse(content)
|
|
88
|
+
return { isUpdated, incomingActions }
|
|
89
|
+
}
|
|
90
90
|
|
|
91
91
|
const dequeue = () => {
|
|
92
92
|
// first time dequeue loads
|
|
93
93
|
if (!actions)
|
|
94
|
-
actions = []
|
|
94
|
+
actions = []
|
|
95
95
|
|
|
96
|
-
const { isUpdated, incomingActions } = loadFile(options.path || "")
|
|
96
|
+
const { isUpdated, incomingActions } = loadFile(options.path || "")
|
|
97
97
|
|
|
98
98
|
if (!isUpdated) {
|
|
99
99
|
/**
|
|
100
100
|
* make sure no tasks are executed from the queue by matching both
|
|
101
101
|
* queues (the incoming with current one)
|
|
102
102
|
*/
|
|
103
|
-
actions = incomingActions
|
|
103
|
+
actions = incomingActions
|
|
104
104
|
logger.debug(
|
|
105
105
|
`No new actions to process: ${actions.length}/${incomingActions.length}`
|
|
106
|
-
)
|
|
107
|
-
return null
|
|
106
|
+
)
|
|
107
|
+
return null
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
// do i need to reset actions to zero?
|
|
111
111
|
if (actions.length > 0 && actions[0].time !== incomingActions[0].time) {
|
|
112
|
-
actions = []
|
|
112
|
+
actions = []
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
const action = incomingActions[incomingActions.length - 1]
|
|
116
|
-
logger.debug("Dequeing action ", action)
|
|
117
|
-
actions.push(action)
|
|
118
|
-
return action
|
|
119
|
-
}
|
|
115
|
+
const action = incomingActions[incomingActions.length - 1]
|
|
116
|
+
logger.debug("Dequeing action ", action)
|
|
117
|
+
actions.push(action)
|
|
118
|
+
return action
|
|
119
|
+
}
|
|
120
120
|
|
|
121
121
|
const pull = (callback: (T: any) => any) => {
|
|
122
|
-
logger.debug("Pulling actions")
|
|
123
|
-
let incoming = dequeue()
|
|
122
|
+
logger.debug("Pulling actions")
|
|
123
|
+
let incoming = dequeue()
|
|
124
124
|
while (incoming) {
|
|
125
|
-
callback(incoming)
|
|
126
|
-
incoming = dequeue()
|
|
125
|
+
callback(incoming)
|
|
126
|
+
incoming = dequeue()
|
|
127
127
|
}
|
|
128
|
-
}
|
|
128
|
+
}
|
|
129
129
|
|
|
130
130
|
const reset = (callback: (T?: any) => any) => {
|
|
131
|
-
logger.debug("Queue reseted")
|
|
132
|
-
actions = []
|
|
131
|
+
logger.debug("Queue reseted")
|
|
132
|
+
actions = []
|
|
133
133
|
if (fs.existsSync(options.path || "")) {
|
|
134
|
-
fs.writeFileSync(options.path || "", "[]")
|
|
135
|
-
callback()
|
|
134
|
+
fs.writeFileSync(options.path || "", "[]")
|
|
135
|
+
callback()
|
|
136
136
|
}
|
|
137
|
-
}
|
|
137
|
+
}
|
|
138
138
|
|
|
139
139
|
const onPull = (callback: (T?: any) => any) => {
|
|
140
140
|
// eslint-disable-next-line
|
|
141
141
|
const chokidar = require("chokidar");
|
|
142
142
|
|
|
143
|
-
logger.debug("Starting to listen...")
|
|
143
|
+
logger.debug("Starting to listen...")
|
|
144
144
|
try {
|
|
145
|
-
loadFile(options.path || "")
|
|
145
|
+
loadFile(options.path || "")
|
|
146
146
|
} catch {
|
|
147
|
-
logger.debug("No previoues queue file, waiting for it to be created...")
|
|
147
|
+
logger.debug("No previoues queue file, waiting for it to be created...")
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
if (!watcher) {
|
|
151
|
-
logger.debug(`Watching ${options.path}`)
|
|
151
|
+
logger.debug(`Watching ${options.path}`)
|
|
152
152
|
watcher = chokidar.watch(`${options.path}`, {
|
|
153
153
|
persistent: true,
|
|
154
|
-
})
|
|
154
|
+
})
|
|
155
155
|
} else
|
|
156
|
-
logger.debug("Already watching queue path")
|
|
156
|
+
logger.debug("Already watching queue path")
|
|
157
157
|
|
|
158
|
-
watcher.on("add", () => pull(callback)).on("change", () => pull(callback))
|
|
158
|
+
watcher.on("add", () => pull(callback)).on("change", () => pull(callback))
|
|
159
159
|
|
|
160
|
-
return true
|
|
161
|
-
}
|
|
160
|
+
return true
|
|
161
|
+
}
|
|
162
162
|
|
|
163
163
|
const onReset = (callback: (T?: any) => any) => {
|
|
164
164
|
// eslint-disable-next-line
|
|
165
165
|
const chokidar = require("chokidar");
|
|
166
166
|
|
|
167
167
|
if (!watcher) {
|
|
168
|
-
logger.debug(`Watching ${options.path}`)
|
|
168
|
+
logger.debug(`Watching ${options.path}`)
|
|
169
169
|
watcher = chokidar.watch(`${options.path}`, {
|
|
170
170
|
persistent: true,
|
|
171
|
-
})
|
|
171
|
+
})
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
watcher.on("unlink", () => reset(callback))
|
|
174
|
+
watcher.on("unlink", () => reset(callback))
|
|
175
175
|
|
|
176
|
-
return true
|
|
177
|
-
}
|
|
176
|
+
return true
|
|
177
|
+
}
|
|
178
178
|
|
|
179
179
|
export default {
|
|
180
180
|
events,
|
|
181
181
|
dispatcher: (opts: any = {}) => {
|
|
182
182
|
if (!actions) {
|
|
183
|
-
options = { ...options, ...opts }
|
|
184
|
-
logger.debug("Initializing queue dispatcher", options)
|
|
185
|
-
actions = loadDispatcher(options)
|
|
183
|
+
options = { ...options, ...opts }
|
|
184
|
+
logger.debug("Initializing queue dispatcher", options)
|
|
185
|
+
actions = loadDispatcher(options)
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
return { enqueue, events }
|
|
188
|
+
return { enqueue, events }
|
|
189
189
|
},
|
|
190
190
|
listener: (opts: any = {}) => {
|
|
191
191
|
if (!actions) {
|
|
192
|
-
options = { ...options, ...opts }
|
|
193
|
-
logger.debug("Initializing queue listener", options)
|
|
192
|
+
options = { ...options, ...opts }
|
|
193
|
+
logger.debug("Initializing queue listener", options)
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
return { onPull, onReset, events }
|
|
196
|
+
return { onPull, onReset, events }
|
|
197
197
|
},
|
|
198
|
-
}
|
|
198
|
+
}
|
package/src/utils/watcher.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import * as chokidar from "chokidar"
|
|
2
|
-
import Console from "./console"
|
|
3
|
-
import * as debounce from "debounce"
|
|
4
|
-
import { IConfigManager } from "../models/config-manager"
|
|
1
|
+
import * as chokidar from "chokidar"
|
|
2
|
+
import Console from "./console"
|
|
3
|
+
import * as debounce from "debounce"
|
|
4
|
+
import { IConfigManager } from "../models/config-manager"
|
|
5
5
|
|
|
6
6
|
export default (path: string, reloadSocket: () => void) =>
|
|
7
7
|
new Promise((resolve /* , reject */) => {
|
|
8
|
-
Console.debug("PATH:", path)
|
|
8
|
+
Console.debug("PATH:", path)
|
|
9
9
|
const watcher = chokidar.watch(path, {
|
|
10
10
|
// TODO: This watcher is not ready yet
|
|
11
11
|
ignored: /^(?=.*(\.\w+)$)(?!.*\.md$).*$/gm,
|
|
12
12
|
ignoreInitial: true,
|
|
13
|
-
})
|
|
13
|
+
})
|
|
14
14
|
const onChange = (eventname: string, _filename: string) => {
|
|
15
|
-
resolve(eventname /* , filename */)
|
|
16
|
-
reloadSocket()
|
|
17
|
-
}
|
|
15
|
+
resolve(eventname /* , filename */)
|
|
16
|
+
reloadSocket()
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
watcher.on("all", debounce(onChange, 500, true))
|
|
19
|
+
watcher.on("all", debounce(onChange, 500, true))
|
|
20
20
|
// watcher.on('all', onChange)
|
|
21
21
|
process.on("SIGINT", function () {
|
|
22
|
-
watcher.close()
|
|
23
|
-
process.exit()
|
|
24
|
-
})
|
|
25
|
-
})
|
|
22
|
+
watcher.close()
|
|
23
|
+
process.exit()
|
|
24
|
+
})
|
|
25
|
+
})
|
package/lib/commands/audit.d.ts
DELETED
package/lib/commands/audit.js
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
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;
|