@learnpack/learnpack 4.0.10 → 4.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. package/README.md +25 -10
  2. package/lib/commands/audit.d.ts +6 -6
  3. package/lib/commands/audit.js +327 -327
  4. package/lib/commands/build.d.ts +11 -0
  5. package/lib/commands/build.js +160 -0
  6. package/lib/commands/clean.d.ts +8 -8
  7. package/lib/commands/clean.js +22 -22
  8. package/lib/commands/download.d.ts +13 -13
  9. package/lib/commands/download.js +52 -52
  10. package/lib/commands/init.d.ts +9 -9
  11. package/lib/commands/init.js +127 -127
  12. package/lib/commands/login.d.ts +14 -14
  13. package/lib/commands/login.js +34 -34
  14. package/lib/commands/logout.d.ts +14 -14
  15. package/lib/commands/logout.js +34 -34
  16. package/lib/commands/publish.d.ts +14 -14
  17. package/lib/commands/publish.js +79 -79
  18. package/lib/commands/start.d.ts +7 -7
  19. package/lib/commands/start.js +252 -250
  20. package/lib/commands/test.d.ts +6 -6
  21. package/lib/commands/test.js +62 -62
  22. package/lib/index.d.ts +1 -1
  23. package/lib/index.js +4 -4
  24. package/lib/managers/config/allowed_files.d.ts +5 -5
  25. package/lib/managers/config/allowed_files.js +30 -30
  26. package/lib/managers/config/defaults.d.ts +47 -48
  27. package/lib/managers/config/defaults.js +51 -51
  28. package/lib/managers/config/exercise.d.ts +36 -36
  29. package/lib/managers/config/exercise.js +243 -236
  30. package/lib/managers/config/index.d.ts +3 -3
  31. package/lib/managers/config/index.js +464 -459
  32. package/lib/managers/file.d.ts +14 -14
  33. package/lib/managers/file.js +190 -184
  34. package/lib/managers/gitpod.d.ts +3 -3
  35. package/lib/managers/gitpod.js +67 -67
  36. package/lib/managers/server/index.d.ts +5 -6
  37. package/lib/managers/server/index.js +58 -58
  38. package/lib/managers/server/routes.d.ts +4 -4
  39. package/lib/managers/server/routes.js +228 -220
  40. package/lib/managers/session.d.ts +3 -3
  41. package/lib/managers/session.js +125 -125
  42. package/lib/managers/socket.d.ts +3 -3
  43. package/lib/managers/socket.js +188 -186
  44. package/lib/managers/telemetry.d.ts +74 -74
  45. package/lib/managers/telemetry.js +215 -214
  46. package/lib/managers/test.js +84 -84
  47. package/lib/models/action.d.ts +2 -2
  48. package/lib/models/action.js +2 -2
  49. package/lib/models/audit.d.ts +15 -15
  50. package/lib/models/audit.js +2 -2
  51. package/lib/models/config-manager.d.ts +21 -21
  52. package/lib/models/config-manager.js +2 -2
  53. package/lib/models/config.d.ts +86 -86
  54. package/lib/models/config.js +2 -2
  55. package/lib/models/counter.d.ts +11 -11
  56. package/lib/models/counter.js +2 -2
  57. package/lib/models/errors.d.ts +15 -15
  58. package/lib/models/errors.js +2 -2
  59. package/lib/models/exercise-obj.d.ts +29 -30
  60. package/lib/models/exercise-obj.js +2 -2
  61. package/lib/models/file.d.ts +5 -5
  62. package/lib/models/file.js +2 -2
  63. package/lib/models/findings.d.ts +17 -17
  64. package/lib/models/findings.js +2 -2
  65. package/lib/models/flags.d.ts +10 -10
  66. package/lib/models/flags.js +2 -2
  67. package/lib/models/front-matter.d.ts +11 -11
  68. package/lib/models/front-matter.js +2 -2
  69. package/lib/models/gitpod-data.d.ts +16 -16
  70. package/lib/models/gitpod-data.js +2 -2
  71. package/lib/models/language.d.ts +4 -4
  72. package/lib/models/language.js +2 -2
  73. package/lib/models/package.d.ts +7 -7
  74. package/lib/models/package.js +2 -2
  75. package/lib/models/plugin-config.d.ts +16 -16
  76. package/lib/models/plugin-config.js +2 -2
  77. package/lib/models/session.d.ts +31 -31
  78. package/lib/models/session.js +2 -2
  79. package/lib/models/socket.d.ts +37 -37
  80. package/lib/models/socket.js +2 -2
  81. package/lib/models/status.d.ts +1 -1
  82. package/lib/models/status.js +2 -2
  83. package/lib/models/success-types.d.ts +1 -1
  84. package/lib/models/success-types.js +2 -2
  85. package/lib/plugin/command/compile.d.ts +6 -6
  86. package/lib/plugin/command/compile.js +18 -18
  87. package/lib/plugin/command/test.d.ts +6 -6
  88. package/lib/plugin/command/test.js +25 -25
  89. package/lib/plugin/index.d.ts +27 -27
  90. package/lib/plugin/index.js +7 -7
  91. package/lib/plugin/plugin.d.ts +8 -8
  92. package/lib/plugin/plugin.js +68 -68
  93. package/lib/plugin/utils.d.ts +16 -16
  94. package/lib/plugin/utils.js +58 -58
  95. package/lib/ui/download.d.ts +5 -5
  96. package/lib/ui/download.js +62 -61
  97. package/lib/utils/BaseCommand.d.ts +8 -8
  98. package/lib/utils/BaseCommand.js +41 -41
  99. package/lib/utils/SessionCommand.d.ts +10 -10
  100. package/lib/utils/SessionCommand.js +43 -43
  101. package/lib/utils/api.d.ts +14 -14
  102. package/lib/utils/api.js +255 -255
  103. package/lib/utils/audit.d.ts +16 -16
  104. package/lib/utils/audit.js +303 -303
  105. package/lib/utils/checkNotInstalled.d.ts +8 -8
  106. package/lib/utils/checkNotInstalled.js +185 -181
  107. package/lib/utils/console.d.ts +12 -12
  108. package/lib/utils/console.js +19 -19
  109. package/lib/utils/errors.d.ts +17 -17
  110. package/lib/utils/errors.js +107 -100
  111. package/lib/utils/exercisesQueue.d.ts +9 -9
  112. package/lib/utils/exercisesQueue.js +38 -38
  113. package/lib/utils/fileQueue.d.ts +43 -43
  114. package/lib/utils/fileQueue.js +169 -169
  115. package/lib/utils/misc.d.ts +1 -1
  116. package/lib/utils/misc.js +24 -23
  117. package/lib/utils/osOperations.d.ts +5 -5
  118. package/lib/utils/osOperations.js +72 -72
  119. package/lib/utils/validators.d.ts +5 -5
  120. package/lib/utils/validators.js +16 -17
  121. package/lib/utils/watcher.d.ts +2 -2
  122. package/lib/utils/watcher.js +25 -25
  123. package/oclif.manifest.json +1 -1
  124. package/package.json +6 -4
  125. package/src/commands/build.ts +181 -0
  126. package/src/managers/config/index.ts +5 -0
  127. package/src/managers/server/routes.ts +10 -0
  128. package/src/managers/session.ts +1 -1
@@ -1,125 +1,125 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const console_1 = require("../utils/console");
4
- const api_1 = require("../utils/api");
5
- const validator_1 = require("validator");
6
- const errors_1 = require("../utils/errors");
7
- const fs = require("fs");
8
- const cli_ux_1 = require("cli-ux");
9
- const storage = require("node-persist");
10
- const telemetry_1 = require("./telemetry");
11
- const Session = {
12
- sessionStarted: false,
13
- token: null,
14
- config: null,
15
- currentCohort: null,
16
- initialize: async function () {
17
- if (!this.sessionStarted) {
18
- if (!this.config) {
19
- throw errors_1.InternalError("Configuration not found");
20
- }
21
- if (!fs.existsSync(this.config.dirPath)) {
22
- fs.mkdirSync(this.config.dirPath);
23
- }
24
- await storage.init({ dir: `${this.config.dirPath}/.session` });
25
- this.sessionStarted = true;
26
- }
27
- return true;
28
- },
29
- setRigoToken: async function (token) {
30
- await this.initialize();
31
- const payload = await storage.getItem("bc-payload");
32
- await storage.setItem("bc-payload", Object.assign(Object.assign({}, payload), { rigobot: { key: token } }));
33
- console_1.default.debug("Rigobot token successfuly set");
34
- return true;
35
- },
36
- setPayload: async function (value) {
37
- await this.initialize();
38
- await storage.setItem("bc-payload", Object.assign({ token: this.token }, value));
39
- console_1.default.debug("Payload successfuly found and set for " + value.email);
40
- return true;
41
- },
42
- getPayload: async function () {
43
- await this.initialize();
44
- let payload = null;
45
- try {
46
- payload = await storage.getItem("bc-payload");
47
- }
48
- catch (_a) {
49
- console_1.default.debug("Error retriving session payload");
50
- }
51
- return payload;
52
- },
53
- isActive: function () {
54
- /* if (this.token) {
55
- return true
56
- } else {
57
- return false
58
- } */
59
- return !!this.token;
60
- },
61
- get: async function (configObj) {
62
- if (configObj && configObj.config) {
63
- this.config = configObj.config;
64
- }
65
- await this.sync();
66
- if (!this.isActive()) {
67
- return null;
68
- }
69
- const payload = await this.getPayload();
70
- return {
71
- payload,
72
- token: this.token,
73
- };
74
- },
75
- login: async function () {
76
- const email = await cli_ux_1.default.prompt("What is your email?");
77
- if (!validator_1.default.isEmail(email)) {
78
- throw errors_1.ValidationError("Invalid email");
79
- }
80
- const password = await cli_ux_1.default.prompt("What is your password?", {
81
- type: "hide",
82
- });
83
- const data = await api_1.default.login(email, password);
84
- if (data) {
85
- cli_ux_1.default.log(data);
86
- this.start({ token: data.token, payload: data });
87
- }
88
- },
89
- loginWeb: async function (email, password) {
90
- if (!validator_1.default.isEmail(email)) {
91
- throw errors_1.ValidationError("Invalid email");
92
- }
93
- const data = await api_1.default.login(email, password);
94
- if (data) {
95
- this.start({ token: data.token, payload: data });
96
- telemetry_1.default.setStudent({
97
- user_id: data.user_id,
98
- email: data.email,
99
- token: data.token,
100
- });
101
- return data;
102
- }
103
- },
104
- sync: async function () {
105
- const payload = await this.getPayload();
106
- if (payload) {
107
- this.token = payload.token;
108
- }
109
- },
110
- start: async function ({ token, payload = null }) {
111
- if (!token) {
112
- throw new Error("A token and email is needed to start a session");
113
- }
114
- this.token = token;
115
- if (payload && (await this.setPayload(payload))) {
116
- console_1.default.success(`Successfully logged in as ${payload.email}`);
117
- }
118
- },
119
- destroy: async function () {
120
- await storage.clear();
121
- this.token = null;
122
- console_1.default.success("You have logged out");
123
- },
124
- };
125
- exports.default = Session;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const console_1 = require("../utils/console");
4
+ const api_1 = require("../utils/api");
5
+ const validator_1 = require("validator");
6
+ const errors_1 = require("../utils/errors");
7
+ const fs = require("fs");
8
+ const cli_ux_1 = require("cli-ux");
9
+ const storage = require("node-persist");
10
+ const telemetry_1 = require("./telemetry");
11
+ const Session = {
12
+ sessionStarted: false,
13
+ token: null,
14
+ config: null,
15
+ currentCohort: null,
16
+ initialize: async function () {
17
+ if (!this.sessionStarted) {
18
+ if (!this.config) {
19
+ throw (0, errors_1.InternalError)("Configuration not found");
20
+ }
21
+ if (!fs.existsSync(this.config.dirPath)) {
22
+ fs.mkdirSync(this.config.dirPath);
23
+ }
24
+ await storage.init({ dir: `${this.config.dirPath}/.session` });
25
+ this.sessionStarted = true;
26
+ }
27
+ return true;
28
+ },
29
+ setRigoToken: async function (token) {
30
+ await this.initialize();
31
+ const payload = await storage.getItem("bc-payload");
32
+ await storage.setItem("bc-payload", Object.assign(Object.assign({}, payload), { rigobot: { key: token } }));
33
+ console_1.default.debug("Rigobot token successfuly set");
34
+ return true;
35
+ },
36
+ setPayload: async function (value) {
37
+ await this.initialize();
38
+ await storage.setItem("bc-payload", Object.assign({ token: this.token }, value));
39
+ console_1.default.debug("Payload successfuly found and set for " + value.email);
40
+ return true;
41
+ },
42
+ getPayload: async function () {
43
+ await this.initialize();
44
+ let payload = null;
45
+ try {
46
+ payload = await storage.getItem("bc-payload");
47
+ }
48
+ catch (_a) {
49
+ console_1.default.debug("Error retriving session payload");
50
+ }
51
+ return payload;
52
+ },
53
+ isActive: function () {
54
+ /* if (this.token) {
55
+ return true
56
+ } else {
57
+ return false
58
+ } */
59
+ return !!this.token;
60
+ },
61
+ get: async function (configObj) {
62
+ if (configObj && configObj.config) {
63
+ this.config = configObj.config;
64
+ }
65
+ await this.sync();
66
+ if (!this.isActive()) {
67
+ return null;
68
+ }
69
+ const payload = await this.getPayload();
70
+ return {
71
+ payload,
72
+ token: this.token,
73
+ };
74
+ },
75
+ login: async function () {
76
+ const email = await cli_ux_1.default.prompt("What is your email?");
77
+ if (!validator_1.default.isEmail(email)) {
78
+ throw (0, errors_1.ValidationError)("Invalid email");
79
+ }
80
+ const password = await cli_ux_1.default.prompt("What is your password?", {
81
+ type: "hide",
82
+ });
83
+ const data = await api_1.default.login(email, password);
84
+ if (data) {
85
+ // cli.log(data)
86
+ this.start({ token: data.token, payload: data });
87
+ }
88
+ },
89
+ loginWeb: async function (email, password) {
90
+ if (!validator_1.default.isEmail(email)) {
91
+ throw (0, errors_1.ValidationError)("Invalid email");
92
+ }
93
+ const data = await api_1.default.login(email, password);
94
+ if (data) {
95
+ this.start({ token: data.token, payload: data });
96
+ telemetry_1.default.setStudent({
97
+ user_id: data.user_id,
98
+ email: data.email,
99
+ token: data.token,
100
+ });
101
+ return data;
102
+ }
103
+ },
104
+ sync: async function () {
105
+ const payload = await this.getPayload();
106
+ if (payload) {
107
+ this.token = payload.token;
108
+ }
109
+ },
110
+ start: async function ({ token, payload = null }) {
111
+ if (!token) {
112
+ throw new Error("A token and email is needed to start a session");
113
+ }
114
+ this.token = token;
115
+ if (payload && (await this.setPayload(payload))) {
116
+ console_1.default.success(`Successfully logged in as ${payload.email}`);
117
+ }
118
+ },
119
+ destroy: async function () {
120
+ await storage.clear();
121
+ this.token = null;
122
+ console_1.default.success("You have logged out");
123
+ },
124
+ };
125
+ exports.default = Session;
@@ -1,3 +1,3 @@
1
- import { ISocket } from "../models/socket";
2
- declare const SocketManager: ISocket;
3
- export default SocketManager;
1
+ import { ISocket } from "../models/socket";
2
+ declare const SocketManager: ISocket;
3
+ export default SocketManager;
@@ -1,186 +1,188 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const socket_io_1 = require("socket.io");
4
- const console_1 = require("../utils/console");
5
- const fileQueue_1 = require("../utils/fileQueue");
6
- const SocketManager = {
7
- socket: null,
8
- config: null,
9
- allowedActions: [],
10
- possibleActions: ["build", "reset", "test", "tutorial"],
11
- isTestingEnvironment: false,
12
- actionCallBacks: {
13
- clean: (_, s) => {
14
- s.logs = [];
15
- },
16
- },
17
- addAllowed: function (actions) {
18
- var _a, _b;
19
- if (!Array.isArray(actions))
20
- actions = [actions];
21
- // avoid adding the "test" action if grading is disabled
22
- if (actions.includes("test") && ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.disabledActions) === null || _b === void 0 ? void 0 : _b.includes("test"))) {
23
- actions = actions.filter((a) => a !== "test");
24
- }
25
- this.allowedActions = [
26
- ...(this.allowedActions || []).filter((a) => !actions.includes(a)),
27
- ...actions,
28
- ];
29
- },
30
- removeAllowed: function (actions) {
31
- if (!Array.isArray(actions)) {
32
- actions = [actions];
33
- }
34
- this.allowedActions = (this.allowedActions || []).filter((a) => !actions.includes(a));
35
- },
36
- start: function (config, server, isTestingEnvironment = false) {
37
- var _a, _b, _c, _d;
38
- this.config = config;
39
- this.isTestingEnvironment = isTestingEnvironment;
40
- this.socket = new socket_io_1.Server(server, {
41
- allowEIO3: true,
42
- cors: {
43
- origin: "http://localhost:5173",
44
- methods: ["GET", "POST"],
45
- },
46
- });
47
- this.allowedActions =
48
- ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.disabledActions) === null || _b === void 0 ? void 0 : _b.includes("test")) || ((_c = this.config) === null || _c === void 0 ? void 0 : _c.disableGrading) ?
49
- this.possibleActions.filter(a => { var _a, _b; return !((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.disabledActions) === null || _b === void 0 ? void 0 : _b.includes(a)) && a !== "test"; }) :
50
- this.possibleActions.filter(a => { var _a; return !((_a = this.allowedActions) === null || _a === void 0 ? void 0 : _a.includes(a)); });
51
- if (((_d = this.config) === null || _d === void 0 ? void 0 : _d.grading) === "incremental") {
52
- this.removeAllowed("reset");
53
- }
54
- if (this.socket) {
55
- this.socket.on("connection", (socket) => {
56
- console_1.default.debug("Connection with client successfully established", this.allowedActions);
57
- if (!this.isTestingEnvironment) {
58
- this.log("ready", ["Ready to compile or test..."]);
59
- }
60
- socket.on("compiler", ({ action, data }) => {
61
- this.emit("clean", "pending", ["Working..."]);
62
- if (typeof data.exerciseSlug === "undefined") {
63
- this.log("internal-error", ["No exercise slug specified"]);
64
- console_1.default.error("No exercise slug especified");
65
- return;
66
- }
67
- if (this.actionCallBacks &&
68
- typeof this.actionCallBacks[action] === "function") {
69
- this.actionCallBacks[action](data);
70
- }
71
- else {
72
- this.log("internal-error", ["Uknown action " + action]);
73
- }
74
- });
75
- });
76
- }
77
- },
78
- on: function (action, callBack) {
79
- if (this.actionCallBacks) {
80
- this.actionCallBacks[action] = callBack;
81
- }
82
- },
83
- clean: function (_ = "pending", logs = []) {
84
- this.emit("clean", "pending", logs);
85
- },
86
- ask: function (questions = []) {
87
- return new Promise((resolve, _) => {
88
- this.emit("ask", "pending", ["Waiting for input..."], questions);
89
- this.on("input", ({ inputs }) => {
90
- // Workaround to fix issue because null inputs
91
- let isNull = false;
92
- // eslint-disable-next-line
93
- inputs.forEach((input) => {
94
- if (input === null) {
95
- isNull = true;
96
- }
97
- });
98
- if (!isNull) {
99
- resolve(inputs);
100
- }
101
- });
102
- });
103
- },
104
- reload: function (files = null, exercises = null) {
105
- this.emit("reload", (files === null || files === void 0 ? void 0 : files.join("")) || "", exercises);
106
- },
107
- openWindow: function (url = "") {
108
- fileQueue_1.default.dispatcher().enqueue(fileQueue_1.default.events.OPEN_WINDOW, url);
109
- this.emit(fileQueue_1.default.events.OPEN_WINDOW, "ready", [`Opening ${url}`], [], [], url);
110
- },
111
- log: function (status, messages = [], report = [], data = null) {
112
- this.emit("log", status, messages, [], report, data);
113
- console_1.default.log(messages);
114
- },
115
- emit: function (action, status = "ready", logs = [], inputs = [], report = [], data = null) {
116
- var _a, _b, _c, _d, _e, _f;
117
- if (((_a = this.config) === null || _a === void 0 ? void 0 : _a.compiler) &&
118
- ["webpack", "vanillajs", "vue", "react", "css", "html"].includes((_b = this.config) === null || _b === void 0 ? void 0 : _b.compiler)) {
119
- if (["compiler-success", "compiler-warning"].includes(status))
120
- this.addAllowed("preview");
121
- if (["compiler-error"].includes(status) || action === "ready")
122
- this.removeAllowed("preview");
123
- }
124
- if (((_c = this.config) === null || _c === void 0 ? void 0 : _c.grading) === "incremental") {
125
- this.removeAllowed("reset");
126
- }
127
- // eslint-disable-next-line
128
- (_e = (_d = this.config) === null || _d === void 0 ? void 0 : _d.disabledActions) === null || _e === void 0 ? void 0 : _e.forEach((a) => this.removeAllowed(a));
129
- (_f = this.socket) === null || _f === void 0 ? void 0 : _f.emit("compiler", {
130
- action,
131
- status,
132
- logs,
133
- allowed: this.allowedActions,
134
- inputs,
135
- report,
136
- data,
137
- });
138
- },
139
- ready: function (message) {
140
- this.log("ready", [message]);
141
- },
142
- success: function (type, stdout) {
143
- const types = ["compiler", "testing"];
144
- if (!types.includes(type))
145
- this.fatal(`Invalid socket success type "${type}" on socket`);
146
- else if (stdout === "")
147
- this.log((type + "-success"), [
148
- "No stdout to display on the console",
149
- ]);
150
- else
151
- this.log((type + "-success"), [stdout]);
152
- },
153
- error: function (type, stdout) {
154
- var _a;
155
- if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.editor.hideTerminal)) {
156
- fileQueue_1.default.dispatcher().enqueue(fileQueue_1.default.events.OPEN_TERMINAL, "");
157
- }
158
- this.log(type, [stdout]);
159
- if (this.isTestingEnvironment) {
160
- this.onTestingFinished({
161
- result: "failed",
162
- });
163
- }
164
- },
165
- complete: function () {
166
- console.log("complete");
167
- },
168
- dialog: function (message, format = "md") {
169
- if (!this.socket) {
170
- this.fatal("Socket is not initialized");
171
- return;
172
- }
173
- this.emit("dialog", "talk", [], undefined, undefined, { message, format });
174
- },
175
- fatal: function (msg) {
176
- this.log("internal-error", [msg]);
177
- throw msg;
178
- },
179
- onTestingFinished: function (result) {
180
- var _a;
181
- if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.testingFinishedCallback) {
182
- this.config.testingFinishedCallback(result);
183
- }
184
- },
185
- };
186
- exports.default = SocketManager;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const socket_io_1 = require("socket.io");
4
+ const console_1 = require("../utils/console");
5
+ const fileQueue_1 = require("../utils/fileQueue");
6
+ const SocketManager = {
7
+ socket: null,
8
+ config: null,
9
+ allowedActions: [],
10
+ possibleActions: ["build", "reset", "test", "tutorial"],
11
+ isTestingEnvironment: false,
12
+ actionCallBacks: {
13
+ clean: (_, s) => {
14
+ s.logs = [];
15
+ },
16
+ },
17
+ addAllowed: function (actions) {
18
+ var _a, _b;
19
+ if (!Array.isArray(actions))
20
+ actions = [actions];
21
+ // avoid adding the "test" action if grading is disabled
22
+ if (actions.includes("test") &&
23
+ ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.disabledActions) === null || _b === void 0 ? void 0 : _b.includes("test"))) {
24
+ actions = actions.filter((a) => a !== "test");
25
+ }
26
+ this.allowedActions = [
27
+ ...(this.allowedActions || []).filter((a) => !actions.includes(a)),
28
+ ...actions,
29
+ ];
30
+ },
31
+ removeAllowed: function (actions) {
32
+ if (!Array.isArray(actions)) {
33
+ actions = [actions];
34
+ }
35
+ this.allowedActions = (this.allowedActions || []).filter((a) => !actions.includes(a));
36
+ },
37
+ start: function (config, server, isTestingEnvironment = false) {
38
+ var _a, _b, _c, _d;
39
+ this.config = config;
40
+ this.isTestingEnvironment = isTestingEnvironment;
41
+ this.socket = new socket_io_1.Server(server, {
42
+ allowEIO3: true,
43
+ cors: {
44
+ origin: "http://localhost:5173",
45
+ methods: ["GET", "POST"],
46
+ },
47
+ });
48
+ this.allowedActions =
49
+ ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.disabledActions) === null || _b === void 0 ? void 0 : _b.includes("test")) ||
50
+ ((_c = this.config) === null || _c === void 0 ? void 0 : _c.disableGrading) ?
51
+ this.possibleActions.filter(a => { var _a, _b; return !((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.disabledActions) === null || _b === void 0 ? void 0 : _b.includes(a)) && a !== "test"; }) :
52
+ this.possibleActions.filter(a => { var _a; return !((_a = this.allowedActions) === null || _a === void 0 ? void 0 : _a.includes(a)); });
53
+ if (((_d = this.config) === null || _d === void 0 ? void 0 : _d.grading) === "incremental") {
54
+ this.removeAllowed("reset");
55
+ }
56
+ if (this.socket) {
57
+ this.socket.on("connection", (socket) => {
58
+ console_1.default.debug("Connection with client successfully established", this.allowedActions);
59
+ if (!this.isTestingEnvironment) {
60
+ this.log("ready", ["Ready to compile or test..."]);
61
+ }
62
+ socket.on("compiler", ({ action, data }) => {
63
+ this.emit("clean", "pending", ["Working..."]);
64
+ if (typeof data.exerciseSlug === "undefined") {
65
+ this.log("internal-error", ["No exercise slug specified"]);
66
+ console_1.default.error("No exercise slug especified");
67
+ return;
68
+ }
69
+ if (this.actionCallBacks &&
70
+ typeof this.actionCallBacks[action] === "function") {
71
+ this.actionCallBacks[action](data);
72
+ }
73
+ else {
74
+ this.log("internal-error", ["Uknown action " + action]);
75
+ }
76
+ });
77
+ });
78
+ }
79
+ },
80
+ on: function (action, callBack) {
81
+ if (this.actionCallBacks) {
82
+ this.actionCallBacks[action] = callBack;
83
+ }
84
+ },
85
+ clean: function (_ = "pending", logs = []) {
86
+ this.emit("clean", "pending", logs);
87
+ },
88
+ ask: function (questions = []) {
89
+ return new Promise((resolve, _) => {
90
+ this.emit("ask", "pending", ["Waiting for input..."], questions);
91
+ this.on("input", ({ inputs }) => {
92
+ // Workaround to fix issue because null inputs
93
+ let isNull = false;
94
+ // eslint-disable-next-line
95
+ inputs.forEach((input) => {
96
+ if (input === null) {
97
+ isNull = true;
98
+ }
99
+ });
100
+ if (!isNull) {
101
+ resolve(inputs);
102
+ }
103
+ });
104
+ });
105
+ },
106
+ reload: function (files = null, exercises = null) {
107
+ this.emit("reload", (files === null || files === void 0 ? void 0 : files.join("")) || "", exercises);
108
+ },
109
+ openWindow: function (url = "") {
110
+ fileQueue_1.default.dispatcher().enqueue(fileQueue_1.default.events.OPEN_WINDOW, url);
111
+ this.emit(fileQueue_1.default.events.OPEN_WINDOW, "ready", [`Opening ${url}`], [], [], url);
112
+ },
113
+ log: function (status, messages = [], report = [], data = null) {
114
+ this.emit("log", status, messages, [], report, data);
115
+ console_1.default.log(messages);
116
+ },
117
+ emit: function (action, status = "ready", logs = [], inputs = [], report = [], data = null) {
118
+ var _a, _b, _c, _d, _e, _f;
119
+ if (((_a = this.config) === null || _a === void 0 ? void 0 : _a.compiler) &&
120
+ ["webpack", "vanillajs", "vue", "react", "css", "html"].includes((_b = this.config) === null || _b === void 0 ? void 0 : _b.compiler)) {
121
+ if (["compiler-success", "compiler-warning"].includes(status))
122
+ this.addAllowed("preview");
123
+ if (["compiler-error"].includes(status) || action === "ready")
124
+ this.removeAllowed("preview");
125
+ }
126
+ if (((_c = this.config) === null || _c === void 0 ? void 0 : _c.grading) === "incremental") {
127
+ this.removeAllowed("reset");
128
+ }
129
+ // eslint-disable-next-line
130
+ (_e = (_d = this.config) === null || _d === void 0 ? void 0 : _d.disabledActions) === null || _e === void 0 ? void 0 : _e.forEach((a) => this.removeAllowed(a));
131
+ (_f = this.socket) === null || _f === void 0 ? void 0 : _f.emit("compiler", {
132
+ action,
133
+ status,
134
+ logs,
135
+ allowed: this.allowedActions,
136
+ inputs,
137
+ report,
138
+ data,
139
+ });
140
+ },
141
+ ready: function (message) {
142
+ this.log("ready", [message]);
143
+ },
144
+ success: function (type, stdout) {
145
+ const types = ["compiler", "testing"];
146
+ if (!types.includes(type))
147
+ this.fatal(`Invalid socket success type "${type}" on socket`);
148
+ else if (stdout === "")
149
+ this.log((type + "-success"), [
150
+ "No stdout to display on the console",
151
+ ]);
152
+ else
153
+ this.log((type + "-success"), [stdout]);
154
+ },
155
+ error: function (type, stdout) {
156
+ var _a;
157
+ if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.editor.hideTerminal)) {
158
+ fileQueue_1.default.dispatcher().enqueue(fileQueue_1.default.events.OPEN_TERMINAL, "");
159
+ }
160
+ this.log(type, [stdout]);
161
+ if (this.isTestingEnvironment) {
162
+ this.onTestingFinished({
163
+ result: "failed",
164
+ });
165
+ }
166
+ },
167
+ complete: function () {
168
+ console.log("complete");
169
+ },
170
+ dialog: function (message, format = "md") {
171
+ if (!this.socket) {
172
+ this.fatal("Socket is not initialized");
173
+ return;
174
+ }
175
+ this.emit("dialog", "talk", [], undefined, undefined, { message, format });
176
+ },
177
+ fatal: function (msg) {
178
+ this.log("internal-error", [msg]);
179
+ throw msg;
180
+ },
181
+ onTestingFinished: function (result) {
182
+ var _a;
183
+ if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.testingFinishedCallback) {
184
+ this.config.testingFinishedCallback(result);
185
+ }
186
+ },
187
+ };
188
+ exports.default = SocketManager;