@learnpack/learnpack 2.1.49 → 2.1.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,250 +1,250 @@
1
- import { Socket, Server } from "socket.io"
2
- import Console from "../utils/console"
3
- import queue from "../utils/fileQueue"
4
-
5
- import { ISocket, TPossibleActions } from "../models/socket"
6
- import { IConfig } from "../models/config"
7
- import { ICallback, TAction } from "../models/action"
8
- import { IExercise, IExerciseData } from "../models/exercise-obj"
9
- import { TStatus } from "../models/status"
10
- import { TSuccessType } from "../models/success-types"
11
- import * as http from "http"
12
-
13
- const SocketManager: ISocket = {
14
- socket: null,
15
- config: null,
16
- allowedActions: [],
17
- possibleActions: ["build", "reset", "test", "tutorial"],
18
- isTestingEnvironment: false,
19
- actionCallBacks: {
20
- clean: (_, s: { logs: Array<string> }) => {
21
- s.logs = []
22
- },
23
- },
24
- addAllowed: function (actions: Array<TPossibleActions> | TPossibleActions) {
25
- if (!Array.isArray(actions))
26
- actions = [actions]
27
-
28
- // avoid adding the "test" action if grading is disabled
29
- if (
30
- actions.includes("test") &&
31
- this.config?.disabledActions?.includes("test")
32
- ) {
33
- actions = actions.filter((a: TPossibleActions) => a !== "test")
34
- }
35
-
36
- this.allowedActions = [
37
- ...(this.allowedActions || []).filter(
38
- (a: TPossibleActions) => !actions.includes(a)
39
- ),
40
- ...actions,
41
- ]
42
- },
43
- removeAllowed: function (
44
- actions: Array<TPossibleActions> | TPossibleActions
45
- ) {
46
- if (!Array.isArray(actions)) {
47
- actions = [actions]
48
- }
49
-
50
- this.allowedActions = (this.allowedActions || []).filter(
51
- (a: TPossibleActions) => !actions.includes(a)
52
- )
53
- },
54
- start: function (
55
- config: IConfig,
56
- server: http.Server,
57
- isTestingEnvironment = false
58
- ) {
59
- this.config = config
60
- this.isTestingEnvironment = isTestingEnvironment
61
- this.socket = new Server(server, {
62
- allowEIO3: true,
63
- cors: {
64
- origin: "http://localhost:5173",
65
- methods: ["GET", "POST"],
66
- },
67
- })
68
-
69
- this.allowedActions =
70
- this.config?.disabledActions?.includes("test") ||
71
- this.config?.disableGrading ?
72
- this.possibleActions.filter(
73
- a => !this.config?.disabledActions?.includes(a) && a !== "test"
74
- ) :
75
- this.possibleActions.filter(a => !this.allowedActions?.includes(a))
76
-
77
- if (this.config?.grading === "incremental") {
78
- this.removeAllowed("reset")
79
- }
80
-
81
- if (this.socket) {
82
- this.socket.on("connection", (socket: Socket) => {
83
- Console.debug(
84
- "Connection with client successfully established",
85
- this.allowedActions
86
- )
87
- if (!this.isTestingEnvironment) {
88
- this.log("ready", ["Ready to compile or test..."])
89
- }
90
-
91
- socket.on(
92
- "compiler",
93
- ({ action, data }: { action: string; data: IExerciseData }) => {
94
- this.emit("clean", "pending", ["Working..."])
95
- if (typeof data.exerciseSlug === "undefined") {
96
- this.log("internal-error", ["No exercise slug specified"])
97
- Console.error("No exercise slug especified")
98
- return
99
- }
100
-
101
- if (
102
- this.actionCallBacks &&
103
- typeof this.actionCallBacks[action] === "function"
104
- ) {
105
- this.actionCallBacks[action](data)
106
- } else {
107
- this.log("internal-error", ["Uknown action " + action])
108
- }
109
- }
110
- )
111
- })
112
- }
113
- },
114
- on: function (action: TAction, callBack: ICallback) {
115
- if (this.actionCallBacks) {
116
- this.actionCallBacks[action] = callBack
117
- }
118
- },
119
- clean: function (_ = "pending", logs = []) {
120
- this.emit("clean", "pending", logs)
121
- },
122
- ask: function (questions = []) {
123
- return new Promise((resolve, _) => {
124
- this.emit("ask", "pending", ["Waiting for input..."], questions)
125
-
126
- this.on("input", ({ inputs }: any) => {
127
- // Workaround to fix issue because null inputs
128
-
129
- let isNull = false
130
- // eslint-disable-next-line
131
- inputs.forEach((input: any) => {
132
- if (input === null) {
133
- isNull = true
134
- }
135
- })
136
-
137
- if (!isNull) {
138
- resolve(inputs)
139
- }
140
- })
141
- })
142
- },
143
- reload: function (
144
- files: Array<string> | null = null,
145
- exercises: Array<string> | null = null
146
- ) {
147
- this.emit(
148
- "reload",
149
- files?.join("") || "" /* TODO: Check it out this */,
150
- exercises!
151
- )
152
- },
153
- openWindow: function (url = "") {
154
- queue.dispatcher().enqueue(queue.events.OPEN_WINDOW, url)
155
- this.emit(
156
- queue.events.OPEN_WINDOW as TAction,
157
- "ready",
158
- [`Opening ${url}`],
159
- [],
160
- [],
161
- url
162
- )
163
- },
164
- log: function (
165
- status: TStatus,
166
- messages: string | Array<string> = [],
167
- report: Array<string> = [],
168
- data: any = null
169
- ) {
170
- this.emit("log", status, messages, [], report, data)
171
- Console.log(messages)
172
- },
173
- emit: function (
174
- action: TAction,
175
- status: TStatus | string = "ready",
176
- logs: string | Array<string> = [],
177
- inputs: Array<string> = [],
178
- report: Array<string> = [],
179
- data: any = null
180
- ) {
181
- if (
182
- this.config?.compiler &&
183
- ["webpack", "vanillajs", "vue", "react", "css", "html"].includes(
184
- this.config?.compiler
185
- )
186
- ) {
187
- if (["compiler-success", "compiler-warning"].includes(status))
188
- this.addAllowed("preview")
189
- if (["compiler-error"].includes(status) || action === "ready")
190
- this.removeAllowed("preview")
191
- }
192
-
193
- if (this.config?.grading === "incremental") {
194
- this.removeAllowed("reset")
195
- }
196
-
197
- // eslint-disable-next-line
198
- this.config?.disabledActions?.forEach((a) => this.removeAllowed(a));
199
-
200
- this.socket?.emit("compiler", {
201
- action,
202
- status,
203
- logs,
204
- allowed: this.allowedActions,
205
- inputs,
206
- report,
207
- data,
208
- })
209
- },
210
-
211
- ready: function (message: string) {
212
- this.log("ready", [message])
213
- },
214
- success: function (type: TSuccessType, stdout: string) {
215
- const types = ["compiler", "testing"]
216
- if (!types.includes(type))
217
- this.fatal(`Invalid socket success type "${type}" on socket`)
218
- else if (stdout === "")
219
- this.log((type + "-success") as TSuccessType, [
220
- "No stdout to display on the console",
221
- ])
222
- else
223
- this.log((type + "-success") as TSuccessType, [stdout])
224
- },
225
- error: function (type: TStatus, stdout: string) {
226
- console.error("Socket error: " + type, stdout)
227
- this.log(type, [stdout])
228
-
229
- if (this.isTestingEnvironment) {
230
- this.onTestingFinished({
231
- result: "failed",
232
- })
233
- }
234
- },
235
- complete: function () {
236
- console.log("complete")
237
- },
238
-
239
- fatal: function (msg: string) {
240
- this.log("internal-error", [msg])
241
- throw msg
242
- },
243
- onTestingFinished: function (result: any) {
244
- if (this.config?.testingFinishedCallback) {
245
- this.config.testingFinishedCallback(result)
246
- }
247
- },
248
- }
249
-
250
- export default SocketManager
1
+ import { Socket, Server } from "socket.io"
2
+ import Console from "../utils/console"
3
+ import queue from "../utils/fileQueue"
4
+
5
+ import { ISocket, TPossibleActions } from "../models/socket"
6
+ import { IConfig } from "../models/config"
7
+ import { ICallback, TAction } from "../models/action"
8
+ import { IExercise, IExerciseData } from "../models/exercise-obj"
9
+ import { TStatus } from "../models/status"
10
+ import { TSuccessType } from "../models/success-types"
11
+ import * as http from "http"
12
+
13
+ const SocketManager: ISocket = {
14
+ socket: null,
15
+ config: null,
16
+ allowedActions: [],
17
+ possibleActions: ["build", "reset", "test", "tutorial"],
18
+ isTestingEnvironment: false,
19
+ actionCallBacks: {
20
+ clean: (_, s: { logs: Array<string> }) => {
21
+ s.logs = []
22
+ },
23
+ },
24
+ addAllowed: function (actions: Array<TPossibleActions> | TPossibleActions) {
25
+ if (!Array.isArray(actions))
26
+ actions = [actions]
27
+
28
+ // avoid adding the "test" action if grading is disabled
29
+ if (
30
+ actions.includes("test") &&
31
+ this.config?.disabledActions?.includes("test")
32
+ ) {
33
+ actions = actions.filter((a: TPossibleActions) => a !== "test")
34
+ }
35
+
36
+ this.allowedActions = [
37
+ ...(this.allowedActions || []).filter(
38
+ (a: TPossibleActions) => !actions.includes(a)
39
+ ),
40
+ ...actions,
41
+ ]
42
+ },
43
+ removeAllowed: function (
44
+ actions: Array<TPossibleActions> | TPossibleActions
45
+ ) {
46
+ if (!Array.isArray(actions)) {
47
+ actions = [actions]
48
+ }
49
+
50
+ this.allowedActions = (this.allowedActions || []).filter(
51
+ (a: TPossibleActions) => !actions.includes(a)
52
+ )
53
+ },
54
+ start: function (
55
+ config: IConfig,
56
+ server: http.Server,
57
+ isTestingEnvironment = false
58
+ ) {
59
+ this.config = config
60
+ this.isTestingEnvironment = isTestingEnvironment
61
+ this.socket = new Server(server, {
62
+ allowEIO3: true,
63
+ cors: {
64
+ origin: "http://localhost:5173",
65
+ methods: ["GET", "POST"],
66
+ },
67
+ })
68
+
69
+ this.allowedActions =
70
+ this.config?.disabledActions?.includes("test") ||
71
+ this.config?.disableGrading ?
72
+ this.possibleActions.filter(
73
+ a => !this.config?.disabledActions?.includes(a) && a !== "test"
74
+ ) :
75
+ this.possibleActions.filter(a => !this.allowedActions?.includes(a))
76
+
77
+ if (this.config?.grading === "incremental") {
78
+ this.removeAllowed("reset")
79
+ }
80
+
81
+ if (this.socket) {
82
+ this.socket.on("connection", (socket: Socket) => {
83
+ Console.debug(
84
+ "Connection with client successfully established",
85
+ this.allowedActions
86
+ )
87
+ if (!this.isTestingEnvironment) {
88
+ this.log("ready", ["Ready to compile or test..."])
89
+ }
90
+
91
+ socket.on(
92
+ "compiler",
93
+ ({ action, data }: { action: string; data: IExerciseData }) => {
94
+ this.emit("clean", "pending", ["Working..."])
95
+ if (typeof data.exerciseSlug === "undefined") {
96
+ this.log("internal-error", ["No exercise slug specified"])
97
+ Console.error("No exercise slug especified")
98
+ return
99
+ }
100
+
101
+ if (
102
+ this.actionCallBacks &&
103
+ typeof this.actionCallBacks[action] === "function"
104
+ ) {
105
+ this.actionCallBacks[action](data)
106
+ } else {
107
+ this.log("internal-error", ["Uknown action " + action])
108
+ }
109
+ }
110
+ )
111
+ })
112
+ }
113
+ },
114
+ on: function (action: TAction, callBack: ICallback) {
115
+ if (this.actionCallBacks) {
116
+ this.actionCallBacks[action] = callBack
117
+ }
118
+ },
119
+ clean: function (_ = "pending", logs = []) {
120
+ this.emit("clean", "pending", logs)
121
+ },
122
+ ask: function (questions = []) {
123
+ return new Promise((resolve, _) => {
124
+ this.emit("ask", "pending", ["Waiting for input..."], questions)
125
+
126
+ this.on("input", ({ inputs }: any) => {
127
+ // Workaround to fix issue because null inputs
128
+
129
+ let isNull = false
130
+ // eslint-disable-next-line
131
+ inputs.forEach((input: any) => {
132
+ if (input === null) {
133
+ isNull = true
134
+ }
135
+ })
136
+
137
+ if (!isNull) {
138
+ resolve(inputs)
139
+ }
140
+ })
141
+ })
142
+ },
143
+ reload: function (
144
+ files: Array<string> | null = null,
145
+ exercises: Array<string> | null = null
146
+ ) {
147
+ this.emit(
148
+ "reload",
149
+ files?.join("") || "" /* TODO: Check it out this */,
150
+ exercises!
151
+ )
152
+ },
153
+ openWindow: function (url = "") {
154
+ queue.dispatcher().enqueue(queue.events.OPEN_WINDOW, url)
155
+ this.emit(
156
+ queue.events.OPEN_WINDOW as TAction,
157
+ "ready",
158
+ [`Opening ${url}`],
159
+ [],
160
+ [],
161
+ url
162
+ )
163
+ },
164
+ log: function (
165
+ status: TStatus,
166
+ messages: string | Array<string> = [],
167
+ report: Array<string> = [],
168
+ data: any = null
169
+ ) {
170
+ this.emit("log", status, messages, [], report, data)
171
+ Console.log(messages)
172
+ },
173
+ emit: function (
174
+ action: TAction,
175
+ status: TStatus | string = "ready",
176
+ logs: string | Array<string> = [],
177
+ inputs: Array<string> = [],
178
+ report: Array<string> = [],
179
+ data: any = null
180
+ ) {
181
+ if (
182
+ this.config?.compiler &&
183
+ ["webpack", "vanillajs", "vue", "react", "css", "html"].includes(
184
+ this.config?.compiler
185
+ )
186
+ ) {
187
+ if (["compiler-success", "compiler-warning"].includes(status))
188
+ this.addAllowed("preview")
189
+ if (["compiler-error"].includes(status) || action === "ready")
190
+ this.removeAllowed("preview")
191
+ }
192
+
193
+ if (this.config?.grading === "incremental") {
194
+ this.removeAllowed("reset")
195
+ }
196
+
197
+ // eslint-disable-next-line
198
+ this.config?.disabledActions?.forEach((a) => this.removeAllowed(a));
199
+
200
+ this.socket?.emit("compiler", {
201
+ action,
202
+ status,
203
+ logs,
204
+ allowed: this.allowedActions,
205
+ inputs,
206
+ report,
207
+ data,
208
+ })
209
+ },
210
+
211
+ ready: function (message: string) {
212
+ this.log("ready", [message])
213
+ },
214
+ success: function (type: TSuccessType, stdout: string) {
215
+ const types = ["compiler", "testing"]
216
+ if (!types.includes(type))
217
+ this.fatal(`Invalid socket success type "${type}" on socket`)
218
+ else if (stdout === "")
219
+ this.log((type + "-success") as TSuccessType, [
220
+ "No stdout to display on the console",
221
+ ])
222
+ else
223
+ this.log((type + "-success") as TSuccessType, [stdout])
224
+ },
225
+ error: function (type: TStatus, stdout: string) {
226
+ queue.dispatcher().enqueue(queue.events.OPEN_TERMINAL, "")
227
+ this.log(type, [stdout])
228
+
229
+ if (this.isTestingEnvironment) {
230
+ this.onTestingFinished({
231
+ result: "failed",
232
+ })
233
+ }
234
+ },
235
+ complete: function () {
236
+ console.log("complete")
237
+ },
238
+
239
+ fatal: function (msg: string) {
240
+ this.log("internal-error", [msg])
241
+ throw msg
242
+ },
243
+ onTestingFinished: function (result: any) {
244
+ if (this.config?.testingFinishedCallback) {
245
+ this.config.testingFinishedCallback(result)
246
+ }
247
+ },
248
+ }
249
+
250
+ export default SocketManager