@learnpack/learnpack 4.0.6 → 4.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,8 +4,6 @@ import * as shell from "shelljs"
4
4
  import { exec } from "child_process"
5
5
  import { promisify } from "util"
6
6
 
7
- const execAsync = promisify(exec)
8
-
9
7
  import Console from "../../utils/console"
10
8
  import watch from "../../utils/watcher"
11
9
  import {
@@ -18,7 +16,12 @@ import defaults from "./defaults"
18
16
  import { exercise } from "./exercise"
19
17
 
20
18
  import { rmSync } from "../file"
21
- import { IConfigObj, TConfigObjAttributes, TMode } from "../../models/config"
19
+ import {
20
+ IConfigObj,
21
+ TConfigObjAttributes,
22
+ TMode,
23
+ TAgent,
24
+ } from "../../models/config"
22
25
  import {
23
26
  IConfigManagerAttributes,
24
27
  IConfigManager,
@@ -26,6 +29,7 @@ import {
26
29
  import { IFile } from "../../models/file"
27
30
 
28
31
  /* exercise folder name standard */
32
+ const execAsync = promisify(exec)
29
33
 
30
34
  // eslint-disable-next-line
31
35
  const fetch = require("node-fetch")
@@ -158,11 +162,23 @@ throw InternalError("Config object not found")
158
162
  const codespaces_workspace = getCodespacesNamespace()
159
163
  Console.debug("This is the codespace namespace: ", codespaces_workspace)
160
164
 
165
+ if (!configObj.config.editor) {
166
+ configObj.config.editor = {}
167
+ }
168
+
161
169
  Console.debug(
162
170
  "This is the agent, and this should be null an the beginning: ",
163
171
  configObj.config?.editor?.agent
164
172
  )
165
173
 
174
+ if (configObj.config?.editor?.agent) {
175
+ if (configObj.config.suggestions) {
176
+ configObj.config.suggestions.agent = configObj.config.editor.agent
177
+ } else {
178
+ configObj.config.suggestions = { agent: configObj.config.editor.agent }
179
+ }
180
+ }
181
+
166
182
  if (shell.which("gp") && configObj && configObj.config) {
167
183
  Console.debug("Gitpod detected")
168
184
  configObj.address = getGitpodAddress()
@@ -179,13 +195,52 @@ throw InternalError("Config object not found")
179
195
  Console.debug("Neither Gitpod nor Codespaces detected, using localhost.")
180
196
  configObj.address = `http://localhost:${configObj.config.port}`
181
197
  configObj.config.publicUrl = `http://localhost:${configObj.config.port}`
182
- }
183
-
184
- if (!configObj.config.editor.agent) {
185
198
  configObj.config.editor.agent =
186
199
  process.env.TERM_PROGRAM === "vscode" ? "vscode" : "os"
187
200
  }
188
201
 
202
+ if (configObj.config.editor.agent === "vscode" && isCodeCommandAvailable()) {
203
+ const extensionName = "learn-pack.learnpack-vscode"
204
+
205
+ if (!isExtensionInstalled(extensionName)) {
206
+ Console.info(
207
+ "The LearnPack extension is not installed, trying to install it automatically."
208
+ )
209
+
210
+ if (!installExtension(extensionName)) {
211
+ configObj.config.warnings = {
212
+ extension: EXTENSION_INSTALLATION_STEPS,
213
+ }
214
+ Console.warning(
215
+ "The LearnPack extension is not installed and this tutorial is meant to be run inside VSCode. Please install the LearnPack extension."
216
+ )
217
+ }
218
+ }
219
+ }
220
+
221
+ if (
222
+ configObj.config.suggestions &&
223
+ configObj.config.editor &&
224
+ configObj.config.suggestions.agent !== null &&
225
+ configObj.config.suggestions.agent !== configObj.config.editor.agent
226
+ ) {
227
+ Console.warning(
228
+ `The suggested agent "${configObj.config.suggestions.agent}" is different from the detected agent "${configObj.config.editor.agent}"`
229
+ )
230
+
231
+ try {
232
+ configObj.config.warnings = {
233
+ ...configObj.config.warnings, // Ensure we don't overwrite other warnings
234
+ agent: buildAgentWarning(
235
+ configObj.config.editor.agent,
236
+ configObj.config.suggestions.agent
237
+ ),
238
+ }
239
+ } catch {
240
+ Console.error("IMPOSSIBLE TO SET WARNING")
241
+ }
242
+ }
243
+
189
244
  if (configObj.config && !configObj.config.publicUrl)
190
245
  configObj.config.publicUrl = `${configObj.address}:${configObj.config.port}`
191
246
 
@@ -195,7 +250,7 @@ throw InternalError("Config object not found")
195
250
 
196
251
  if (!configObj.config.editor.mode) {
197
252
  configObj.config.editor.mode =
198
- configObj.config.editor.agent === "vscode" ? "extension" : "preview"
253
+ process.env.TERM_PROGRAM === "vscode" ? "extension" : "preview"
199
254
  }
200
255
 
201
256
  if (version)
@@ -206,7 +261,7 @@ configObj.config.editor.version = version
206
261
  "https://raw.githubusercontent.com/learnpack/ide/master/package.json"
207
262
  )
208
263
  const packageJSON = await resp.json()
209
- configObj.config.editor.version = packageJSON.version || "4.0.0"
264
+ configObj.config.editor.version = packageJSON.version || "4.0.2"
210
265
  }
211
266
 
212
267
  configObj.config.dirPath = "./" + confPath.base
@@ -258,7 +313,9 @@ return
258
313
 
259
314
  return {
260
315
  validLanguages: {},
261
- get: () => configObj,
316
+ get: () => {
317
+ return configObj
318
+ },
262
319
  validateEngine: function (language: string, server: any, socket: any) {
263
320
  // eslint-disable-next-line
264
321
  const alias = (_l: string) => {
@@ -281,7 +338,7 @@ return true
281
338
 
282
339
  if (
283
340
  result.code === 0 &&
284
- result.stdout.includes(`learnpack-${language}`)
341
+ result.stdout.includes(`@learnpack/${language}`)
285
342
  ) {
286
343
  this.validLanguages[language] = true
287
344
  return true
@@ -289,7 +346,7 @@ return true
289
346
 
290
347
  Console.info(`Language engine for ${language} not found, installing...`)
291
348
  // Install the compiler in their new versions
292
- result = shell.exec(`learnpack plugins:install learnpack-${language}`, {
349
+ result = shell.exec(`learnpack plugins:install @learnpack/${language}`, {
293
350
  silent: true,
294
351
  })
295
352
  if (result.code === 0) {
@@ -459,8 +516,7 @@ fs.mkdirSync(confPath.base)
459
516
  },
460
517
 
461
518
  save: () => {
462
- // Console.debug("Saving configuration with: ", configObj)
463
-
519
+ Console.debug("Saving configuration with: ", configObj)
464
520
  // remove the duplicates form the actions array
465
521
  // configObj.config.actions = [...new Set(configObj.config.actions)];
466
522
  if (configObj.config) {
@@ -500,3 +556,145 @@ acc = { ...acc, [key]: value }
500
556
 
501
557
  return acc
502
558
  }
559
+
560
+ const buildAgentWarning = (current: TAgent, suggested: TAgent): string => {
561
+ const message = `# Agent mismatch!\n
562
+
563
+ In LearnPack, the agent is in charge of running the LearnPack interface.
564
+
565
+ You're currently using LearnPack through \`${current}\` but the suggested agent is \`${suggested}\`.
566
+
567
+ We recommend strongly recommend changing your agent.
568
+
569
+ ${stepsToChangeAgent(suggested)}
570
+ `
571
+
572
+ return message
573
+ }
574
+
575
+ const stepsToChangeAgent = (agent: TAgent): string => {
576
+ if (agent === "vscode") {
577
+ return `
578
+ # Steps to Change Agent to VSCode
579
+
580
+ 1. **Install VSCode**:
581
+ - Visit the [VSCode website](https://code.visualstudio.com/Download) and download the latest version.
582
+ - Follow the installation instructions for your operating system.
583
+
584
+ 2. **Install LearnPack VSCode Extension**:
585
+ - Open VSCode.
586
+ - Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing \`Ctrl+Shift+X\`.
587
+ - Search for "LearnPack" and click "Install".
588
+ - If the extension is already installed but disabled, enable it.
589
+
590
+ 3. **Run LearnPack in VSCode**:
591
+ - Open your terminal in VSCode.
592
+ - Navigate to your LearnPack project directory.
593
+ - Run the following command:
594
+ \`\`\`sh
595
+ learnpack start
596
+ \`\`\`
597
+
598
+ We strongly recommend using VSCode for a better learning experience.
599
+ `
600
+ }
601
+
602
+ if (agent === "os") {
603
+ return `
604
+ # Steps to Change Agent to OS
605
+
606
+ This learning package was designed to run outside of VSCode. We strongly recommend closing VSCode and running the package independently.
607
+
608
+ 1. **Close VSCode**:
609
+ - Save your work and close the VSCode application.
610
+
611
+ 2. **Open a New Terminal**:
612
+ - Open a terminal or command prompt on your operating system.
613
+
614
+ 3. **Navigate to Your LearnPack Project Directory**:
615
+ - Use the \`cd\` command to navigate to the directory where your LearnPack project is located.
616
+
617
+ 4. **Run LearnPack**:
618
+ - Run the following command to start LearnPack:
619
+ \`\`\`sh
620
+ learnpack start
621
+ \`\`\`
622
+
623
+ We strongly recommend running the package independently for a better learning experience.
624
+ `
625
+ }
626
+
627
+ return ""
628
+ }
629
+
630
+ /**
631
+ * Checks if the 'code' command is available in the terminal.
632
+ *
633
+ * @returns {boolean} True if the 'code' command is available, false otherwise.
634
+ */
635
+ const isCodeCommandAvailable = (): boolean => {
636
+ return shell.which("code") !== null
637
+ }
638
+
639
+ /**
640
+ * Checks if a specific VSCode extension is installed.
641
+ *
642
+ * @param {string} extensionName - The name of the extension to check.
643
+ * @returns {boolean} True if the extension is installed, false otherwise.
644
+ */
645
+ const isExtensionInstalled = (extensionName: string): boolean => {
646
+ if (!isCodeCommandAvailable()) {
647
+ throw new Error(
648
+ "The 'code' command is not available in the terminal. Please ensure that VSCode is installed and the 'code' command is added to your PATH."
649
+ )
650
+ }
651
+
652
+ const result = shell.exec("code --list-extensions", { silent: true })
653
+ return result.stdout.split("\n").includes(extensionName)
654
+ }
655
+
656
+ const EXTENSION_INSTALLATION_STEPS = `
657
+ # Steps to Install LearnPack VSCode Extension
658
+
659
+ 1. **Open VSCode**:
660
+ - Launch the Visual Studio Code application on your computer.
661
+
662
+ 2. **Go to Extensions View**:
663
+ - Click on the Extensions icon in the Activity Bar on the side of the window.
664
+ - Alternatively, you can open the Extensions view by pressing \`Ctrl+Shift+X\`.
665
+
666
+ 3. **Search for LearnPack**:
667
+ - In the Extensions view, type "LearnPack" into the search bar.
668
+
669
+ 4. **Install the Extension**:
670
+ - Find the "LearnPack" extension in the search results and click the "Install" button.
671
+ - If the extension is already installed but disabled, click the "Enable" button.
672
+
673
+ 5. **Verify Installation**:
674
+ - Once installed, you can verify the installation by running the following command in the terminal:
675
+ \`\`\`sh
676
+ code --list-extensions | grep learn-pack.learnpack-vscode
677
+ \`\`\`
678
+ - If the extension is listed, it means the installation was successful.
679
+
680
+ We strongly recommend using the LearnPack extension in VSCode for a better learning experience.
681
+ `
682
+
683
+ /**
684
+ * Installs the LearnPack VSCode extension if the 'code' command is available.
685
+ *
686
+ * @param {string} extensionName - The name of the extension to install.
687
+ * @returns {boolean} True if the installation was successful, false otherwise.
688
+ */
689
+ const installExtension = (extensionName: string): boolean => {
690
+ if (!isCodeCommandAvailable()) {
691
+ throw new Error(
692
+ "The 'code' command is not available in the terminal. Please ensure that VSCode is installed and the 'code' command is added to your PATH."
693
+ )
694
+ }
695
+
696
+ const result = shell.exec(`code --install-extension ${extensionName}`, {
697
+ silent: true,
698
+ })
699
+ return result.code === 0
700
+ }