@learnpack/learnpack 2.0.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,229 +1,234 @@
1
- // import path from "path";
2
- import { flags } from "@oclif/command";
3
- import SessionCommand from "../utils/SessionCommand";
4
- import Console from "../utils/console";
5
- import socket from "../managers/socket";
6
- import queue from "../utils/fileQueue";
7
- import { decompress, downloadEditor } from "../managers/file";
8
- import { prioritizeHTMLFile } from "../utils/misc";
9
-
10
- import createServer from "../managers/server";
11
-
12
- import { IGitpodData } from "../models/gitpod-data";
13
- import { IExercise, IExerciseData } from "../models/exercise-obj";
14
-
15
- export default class StartCommand extends SessionCommand {
16
- static description = "Runs a small server with all the exercise instructions";
17
-
18
- static flags = {
19
- ...SessionCommand.flags,
20
- port: flags.string({ char: "p", description: "server port" }),
21
- host: flags.string({ char: "h", description: "server host" }),
22
- disableGrading: flags.boolean({
23
- char: "D",
24
- description: "disble grading functionality",
25
- default: false,
26
- }),
27
- // disableGrading: flags.boolean({char: 'dg', description: 'disble grading functionality', default: false }),
28
- watch: flags.boolean({
29
- char: "w",
30
- description: "Watch for file changes",
31
- default: false,
32
- }),
33
- editor: flags.string({
34
- char: "e",
35
- description: "[standalone, gitpod]",
36
- options: ["standalone", "gitpod"],
37
- }),
38
- version: flags.string({
39
- char: "v",
40
- description: "E.g: 1.0.1",
41
- default: undefined,
42
- }),
43
- grading: flags.string({
44
- char: "g",
45
- description: "[isolated, incremental]",
46
- options: ["isolated", "incremental"],
47
- }),
48
- debug: flags.boolean({
49
- char: "d",
50
- description: "debugger mode for more verbage",
51
- default: false,
52
- }),
53
- };
54
-
55
- // 🛑 IMPORTANT
56
- // Every command that will use the configManager needs this init method
57
- async init() {
58
- const { flags } = this.parse(StartCommand);
59
- await this.initSession(flags);
60
- }
61
-
62
- async run() {
63
- // get configuration object
64
- const configObject = this.configManager?.get();
65
- const config = configObject?.config;
66
-
67
- if (configObject) {
68
- const { config } = configObject;
69
-
70
- // build exerises
71
- this.configManager?.buildIndex();
72
-
73
- Console.debug(
74
- `Grading: ${config?.grading} ${
75
- config?.disabledActions?.includes("test") ? "(disabled)" : ""
76
- }, editor: ${config?.editor.mode} ${config?.editor.version}, for ${
77
- Array.isArray(configObject?.exercises) ? configObject?.exercises.length : 0
78
- } exercises found`
79
- );
80
-
81
- // download app and decompress
82
- await downloadEditor(
83
- config?.editor.version,
84
- `${config?.dirPath}/app.tar.gz`
85
- );
86
-
87
- Console.info("Decompressing LearnPack UI, this may take a minute...");
88
- await decompress(
89
- `${config?.dirPath}/app.tar.gz`,
90
- `${config?.dirPath}/_app/`
91
- );
92
-
93
- // listen to socket commands
94
- if (config && this.configManager) {
95
- const server = await createServer(configObject, this.configManager);
96
-
97
- const dispatcher = queue.dispatcher({
98
- create: true,
99
- path: `${config.dirPath}/vscode_queue.json`,
100
- });
101
-
102
- socket.start(config, server, false);
103
-
104
- socket.on("open", (data: IGitpodData) => {
105
- Console.debug("Opening these files: ", data);
106
- const files = prioritizeHTMLFile(data.files);
107
- dispatcher.enqueue(dispatcher.events.OPEN_FILES, files);
108
- socket.ready("Ready to compile...");
109
- });
110
-
111
- socket.on("open_window", (data: IGitpodData) => {
112
- Console.debug("Opening window: ", data);
113
- dispatcher.enqueue(dispatcher.events.OPEN_WINDOW, data);
114
- socket.ready("Ready to compile...");
115
- });
116
-
117
- socket.on("reset", (exercise: IExerciseData) => {
118
- try {
119
- this.configManager?.reset(exercise.exerciseSlug);
120
- dispatcher.enqueue(
121
- dispatcher.events.RESET_EXERCISE,
122
- exercise.exerciseSlug
123
- );
124
- socket.ready("Ready to compile...");
125
- } catch (error) {
126
- socket.error(
127
- "compiler-error",
128
- (error as TypeError).message ||
129
- "There was an error reseting the exercise"
130
- );
131
- setTimeout(() => socket.ready("Ready to compile..."), 2000);
132
- }
133
- });
134
- // socket.on("preview", (data) => {
135
- // Console.debug("Preview triggered, removing the 'preview' action ")
136
- // socket.removeAllowed("preview")
137
- // socket.log('ready',['Ready to compile...'])
138
- // })
139
-
140
- socket.on("build", async (data: IExerciseData) => {
141
- const exercise = this.configManager?.getExercise(data.exerciseSlug);
142
-
143
- if (!exercise?.language) {
144
- socket.error(
145
- "compiler-error",
146
- "Impossible to detect language to build for " +
147
- data.exerciseSlug +
148
- "..."
149
- );
150
- return;
151
- }
152
-
153
- socket.log(
154
- "compiling",
155
- "Building exercise " +
156
- data.exerciseSlug +
157
- " with " +
158
- exercise.language +
159
- "..."
160
- );
161
- await this.config.runHook("action", {
162
- action: "compile",
163
- socket,
164
- configuration: config,
165
- exercise,
166
- });
167
- });
168
-
169
- socket.on("test", async (data: IExerciseData) => {
170
- const exercise = this.configManager?.getExercise(data.exerciseSlug);
171
-
172
- if (!exercise?.language) {
173
- socket.error(
174
- "compiler-error",
175
- "Impossible to detect engine language for testing for " +
176
- data.exerciseSlug +
177
- "..."
178
- );
179
- return;
180
- }
181
-
182
- if (
183
- config?.disabledActions!.includes("test") ||
184
- config?.disableGrading
185
- ) {
186
- socket.ready("Grading is disabled on configuration");
187
- return true;
188
- }
189
-
190
- socket.log(
191
- "testing",
192
- "Testing your exercise using the " + exercise.language + " engine."
193
- );
194
-
195
- await this.config.runHook("action", {
196
- action: "test",
197
- socket,
198
- configuration: config,
199
- exercise,
200
- });
201
- this.configManager?.save();
202
-
203
- return true;
204
- });
205
-
206
- const terminate = () => {
207
- Console.debug("Terminating Learnpack...");
208
- server.terminate(() => {
209
- this.configManager?.noCurrentExercise();
210
- dispatcher.enqueue(dispatcher.events.END);
211
- process.exit();
212
- });
213
- };
214
-
215
- server.on("close", terminate);
216
- process.on("SIGINT", terminate);
217
- process.on("SIGTERM", terminate);
218
- process.on("SIGHUP", terminate);
219
-
220
- // finish the server startup
221
- setTimeout(() => dispatcher.enqueue(dispatcher.events.RUNNING), 1000);
222
-
223
- // start watching for file changes
224
- // eslint-disable-next-line
225
- if (StartCommand.flags.watch) this.configManager.watchIndex(_exercises => socket.reload(null, _exercises));
226
- }
227
- }
228
- }
229
- }
1
+ // import path from "path";
2
+ import { flags } from "@oclif/command";
3
+ import SessionCommand from "../utils/SessionCommand";
4
+ import Console from "../utils/console";
5
+ import socket from "../managers/socket";
6
+ import queue from "../utils/fileQueue";
7
+ import { decompress, downloadEditor } from "../managers/file";
8
+ import { prioritizeHTMLFile } from "../utils/misc";
9
+
10
+ import createServer from "../managers/server";
11
+
12
+ import { IGitpodData } from "../models/gitpod-data";
13
+ import { IExercise, IExerciseData } from "../models/exercise-obj";
14
+
15
+ export default class StartCommand extends SessionCommand {
16
+ static description = "Runs a small server with all the exercise instructions";
17
+
18
+ static flags = {
19
+ ...SessionCommand.flags,
20
+ port: flags.string({ char: "p", description: "server port" }),
21
+ host: flags.string({ char: "h", description: "server host" }),
22
+ disableGrading: flags.boolean({
23
+ char: "D",
24
+ description: "disble grading functionality",
25
+ default: false,
26
+ }),
27
+ // disableGrading: flags.boolean({char: 'dg', description: 'disble grading functionality', default: false }),
28
+ watch: flags.boolean({
29
+ char: "w",
30
+ description: "Watch for file changes",
31
+ default: false,
32
+ }),
33
+ editor: flags.string({
34
+ char: "e",
35
+ description: "[standalone, gitpod]",
36
+ options: ["standalone", "gitpod"],
37
+ }),
38
+ version: flags.string({
39
+ char: "v",
40
+ description: "E.g: 1.0.1",
41
+ default: undefined,
42
+ }),
43
+ grading: flags.string({
44
+ char: "g",
45
+ description: "[isolated, incremental]",
46
+ options: ["isolated", "incremental"],
47
+ }),
48
+ debug: flags.boolean({
49
+ char: "d",
50
+ description: "debugger mode for more verbage",
51
+ default: false,
52
+ }),
53
+ };
54
+
55
+ // 🛑 IMPORTANT
56
+ // Every command that will use the configManager needs this init method
57
+ async init() {
58
+ const { flags } = this.parse(StartCommand);
59
+ await this.initSession(flags);
60
+ }
61
+
62
+ async run() {
63
+ // get configuration object
64
+ const configObject = this.configManager?.get();
65
+ const config = configObject?.config;
66
+
67
+ if (configObject) {
68
+ const { config } = configObject;
69
+
70
+ // build exerises
71
+ this.configManager?.buildIndex();
72
+
73
+ Console.debug(
74
+ `Grading: ${config?.grading} ${
75
+ config?.disabledActions?.includes("test") ? "(disabled)" : ""
76
+ }, editor: ${config?.editor.mode} ${config?.editor.version}, for ${
77
+ Array.isArray(configObject?.exercises) ?
78
+ configObject?.exercises.length :
79
+ 0
80
+ } exercises found`
81
+ );
82
+
83
+ // download app and decompress
84
+ await downloadEditor(
85
+ config?.editor.version,
86
+ `${config?.dirPath}/app.tar.gz`
87
+ );
88
+
89
+ Console.info("Decompressing LearnPack UI, this may take a minute...");
90
+ await decompress(
91
+ `${config?.dirPath}/app.tar.gz`,
92
+ `${config?.dirPath}/_app/`
93
+ );
94
+
95
+ // listen to socket commands
96
+ if (config && this.configManager) {
97
+ const server = await createServer(configObject, this.configManager);
98
+
99
+ const dispatcher = queue.dispatcher({
100
+ create: true,
101
+ path: `${config.dirPath}/vscode_queue.json`,
102
+ });
103
+
104
+ socket.start(config, server, false);
105
+
106
+ socket.on("open", (data: IGitpodData) => {
107
+ Console.debug("Opening these files: ", data);
108
+ const files = prioritizeHTMLFile(data.files);
109
+ dispatcher.enqueue(dispatcher.events.OPEN_FILES, files);
110
+ socket.ready("Ready to compile...");
111
+ });
112
+
113
+ socket.on("open_window", (data: IGitpodData) => {
114
+ Console.debug("Opening window: ", data);
115
+ dispatcher.enqueue(dispatcher.events.OPEN_WINDOW, data);
116
+ socket.ready("Ready to compile...");
117
+ });
118
+
119
+ socket.on("reset", (exercise: IExerciseData) => {
120
+ try {
121
+ this.configManager?.reset(exercise.exerciseSlug);
122
+ dispatcher.enqueue(
123
+ dispatcher.events.RESET_EXERCISE,
124
+ exercise.exerciseSlug
125
+ );
126
+ socket.ready("Ready to compile...");
127
+ } catch (error) {
128
+ socket.error(
129
+ "compiler-error",
130
+ (error as TypeError).message ||
131
+ "There was an error reseting the exercise"
132
+ );
133
+ setTimeout(() => socket.ready("Ready to compile..."), 2000);
134
+ }
135
+ });
136
+ // socket.on("preview", (data) => {
137
+ // Console.debug("Preview triggered, removing the 'preview' action ")
138
+ // socket.removeAllowed("preview")
139
+ // socket.log('ready',['Ready to compile...'])
140
+ // })
141
+
142
+ socket.on("build", async (data: IExerciseData) => {
143
+ const exercise = this.configManager?.getExercise(data.exerciseSlug);
144
+
145
+ if (!exercise?.language) {
146
+ socket.error(
147
+ "compiler-error",
148
+ "Impossible to detect language to build for " +
149
+ data.exerciseSlug +
150
+ "..."
151
+ );
152
+ return;
153
+ }
154
+
155
+ socket.log(
156
+ "compiling",
157
+ "Building exercise " +
158
+ data.exerciseSlug +
159
+ " with " +
160
+ exercise.language +
161
+ "..."
162
+ );
163
+ await this.config.runHook("action", {
164
+ action: "compile",
165
+ socket,
166
+ configuration: config,
167
+ exercise,
168
+ });
169
+ });
170
+
171
+ socket.on("test", async (data: IExerciseData) => {
172
+ const exercise = this.configManager?.getExercise(data.exerciseSlug);
173
+
174
+ if (!exercise?.language) {
175
+ socket.error(
176
+ "compiler-error",
177
+ "Impossible to detect engine language for testing for " +
178
+ data.exerciseSlug +
179
+ "..."
180
+ );
181
+ return;
182
+ }
183
+
184
+ if (
185
+ config?.disabledActions!.includes("test") ||
186
+ config?.disableGrading
187
+ ) {
188
+ socket.ready("Grading is disabled on configuration");
189
+ return true;
190
+ }
191
+
192
+ socket.log(
193
+ "testing",
194
+ "Testing your exercise using the " + exercise.language + " engine."
195
+ );
196
+
197
+ await this.config.runHook("action", {
198
+ action: "test",
199
+ socket,
200
+ configuration: config,
201
+ exercise,
202
+ });
203
+ this.configManager?.save();
204
+
205
+ return true;
206
+ });
207
+
208
+ const terminate = () => {
209
+ Console.debug("Terminating Learnpack...");
210
+ server.terminate(() => {
211
+ this.configManager?.noCurrentExercise();
212
+ dispatcher.enqueue(dispatcher.events.END);
213
+ process.exit();
214
+ });
215
+ };
216
+
217
+ server.on("close", terminate);
218
+ process.on("SIGINT", terminate);
219
+ process.on("SIGTERM", terminate);
220
+ process.on("SIGHUP", terminate);
221
+
222
+ // finish the server startup
223
+ setTimeout(() => dispatcher.enqueue(dispatcher.events.RUNNING), 1000);
224
+
225
+ // start watching for file changes
226
+
227
+ if (StartCommand.flags.watch)
228
+ this.configManager.watchIndex(_exercises =>
229
+ socket.reload(null, _exercises)
230
+ );
231
+ }
232
+ }
233
+ }
234
+ }
@@ -1,83 +1,85 @@
1
- import Console from "../utils/console";
2
- import SessionCommand from "../utils/SessionCommand";
3
- import socket from "../managers/socket";
4
-
5
- import createServer from "../managers/server";
6
- import ExercisesQueue from "../utils/exercisesQueue";
7
- import { IExercise } from "../models/exercise-obj";
8
-
9
- class TestCommand extends SessionCommand {
10
- async init() {
11
- const { flags } = this.parse(TestCommand);
12
- await this.initSession(flags);
13
- }
14
-
15
- async run() {
16
- const {
17
- args: { exerciseSlug },
18
- } = this.parse(TestCommand);
19
-
20
- // Build exercises index
21
- this.configManager?.buildIndex();
22
-
23
- let exercises: IExercise[] | undefined = [];
24
-
25
- // test all exercises
26
- !exerciseSlug ? exercises = this.configManager?.getAllExercises() : exercises = [this.configManager!.getExercise(exerciseSlug)];
27
-
28
- const exercisesQueue = new ExercisesQueue(exercises);
29
-
30
- const configObject = this.configManager?.get();
31
-
32
- let hasFailed = false;
33
- let failedTestsCount = 0;
34
- let successTestsCount = 0;
35
- const testsToRunCount = exercisesQueue.size();
36
-
37
- configObject!.config!.testingFinishedCallback = ({ result }) => {
38
- if (result === "failed") {
39
- hasFailed = true;
40
- failedTestsCount++;
41
- } else {
42
- successTestsCount++;
43
- }
44
-
45
- if (exercisesQueue.isEmpty()) {
46
- Console.info(
47
- `${testsToRunCount} test${testsToRunCount > 1 ? "s" : ""} runned`
48
- );
49
- Console.success(
50
- `${successTestsCount} test${successTestsCount > 1 ? "s" : ""} passed`
51
- );
52
- Console.error(
53
- `${failedTestsCount} test${failedTestsCount > 1 ? "s" : ""} failed`
54
- );
55
-
56
- process.exit(hasFailed ? 1 : 0);
57
- } else {
58
- exercisesQueue.pop()!.test!(this.config, config!, socket);
59
- }
60
- };
61
-
62
- const config = configObject?.config;
63
-
64
- const server = await createServer(configObject!, this.configManager!, true);
65
-
66
- socket.start(config!, server, true);
67
-
68
- exercisesQueue.pop()!.test!(this.config, config!, socket);
69
- }
70
- }
71
-
72
- TestCommand.description = `Test exercises`;
73
-
74
- TestCommand.args = [
75
- {
76
- name: "exerciseSlug",
77
- required: false,
78
- description: "The name of the exercise to test",
79
- hidden: false,
80
- },
81
- ];
82
-
83
- export default TestCommand;
1
+ import Console from "../utils/console";
2
+ import SessionCommand from "../utils/SessionCommand";
3
+ import socket from "../managers/socket";
4
+
5
+ import createServer from "../managers/server";
6
+ import ExercisesQueue from "../utils/exercisesQueue";
7
+ import { IExercise } from "../models/exercise-obj";
8
+
9
+ class TestCommand extends SessionCommand {
10
+ async init() {
11
+ const { flags } = this.parse(TestCommand);
12
+ await this.initSession(flags);
13
+ }
14
+
15
+ async run() {
16
+ const {
17
+ args: { exerciseSlug },
18
+ } = this.parse(TestCommand);
19
+
20
+ // Build exercises index
21
+ this.configManager?.buildIndex();
22
+
23
+ let exercises: IExercise[] | undefined = [];
24
+
25
+ // test all exercises
26
+ !exerciseSlug ?
27
+ (exercises = this.configManager?.getAllExercises()) :
28
+ (exercises = [this.configManager!.getExercise(exerciseSlug)]);
29
+
30
+ const exercisesQueue = new ExercisesQueue(exercises);
31
+
32
+ const configObject = this.configManager?.get();
33
+
34
+ let hasFailed = false;
35
+ let failedTestsCount = 0;
36
+ let successTestsCount = 0;
37
+ const testsToRunCount = exercisesQueue.size();
38
+
39
+ configObject!.config!.testingFinishedCallback = ({ result }) => {
40
+ if (result === "failed") {
41
+ hasFailed = true;
42
+ failedTestsCount++;
43
+ } else {
44
+ successTestsCount++;
45
+ }
46
+
47
+ if (exercisesQueue.isEmpty()) {
48
+ Console.info(
49
+ `${testsToRunCount} test${testsToRunCount > 1 ? "s" : ""} runned`
50
+ );
51
+ Console.success(
52
+ `${successTestsCount} test${successTestsCount > 1 ? "s" : ""} passed`
53
+ );
54
+ Console.error(
55
+ `${failedTestsCount} test${failedTestsCount > 1 ? "s" : ""} failed`
56
+ );
57
+
58
+ process.exit(hasFailed ? 1 : 0);
59
+ } else {
60
+ exercisesQueue.pop()!.test!(this.config, config!, socket);
61
+ }
62
+ };
63
+
64
+ const config = configObject?.config;
65
+
66
+ const server = await createServer(configObject!, this.configManager!, true);
67
+
68
+ socket.start(config!, server, true);
69
+
70
+ exercisesQueue.pop()!.test!(this.config, config!, socket);
71
+ }
72
+ }
73
+
74
+ TestCommand.description = `Test exercises`;
75
+
76
+ TestCommand.args = [
77
+ {
78
+ name: "exerciseSlug",
79
+ required: false,
80
+ description: "The name of the exercise to test",
81
+ hidden: false,
82
+ },
83
+ ];
84
+
85
+ export default TestCommand;