@learnpack/learnpack 2.1.57 → 4.0.0
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/lib/managers/config/defaults.d.ts +1 -0
- package/lib/managers/config/defaults.js +1 -0
- package/lib/managers/config/index.js +2 -2
- package/lib/managers/file.js +17 -7
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
- package/src/managers/config/defaults.ts +1 -0
- package/src/managers/config/index.ts +6 -8
- package/src/managers/file.ts +212 -196
    
        package/README.md
    CHANGED
    
    | @@ -21,7 +21,7 @@ $ npm install -g @learnpack/learnpack | |
| 21 21 | 
             
            $ learnpack COMMAND
         | 
| 22 22 | 
             
            running command...
         | 
| 23 23 | 
             
            $ learnpack (-v|--version|version)
         | 
| 24 | 
            -
            @learnpack/learnpack/ | 
| 24 | 
            +
            @learnpack/learnpack/4.0.0 win32-x64 node-v20.10.0
         | 
| 25 25 | 
             
            $ learnpack --help [COMMAND]
         | 
| 26 26 | 
             
            USAGE
         | 
| 27 27 | 
             
              $ learnpack COMMAND
         | 
| @@ -74,7 +74,7 @@ DESCRIPTION | |
| 74 74 | 
             
                   12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
         | 
| 75 75 | 
             
            ```
         | 
| 76 76 |  | 
| 77 | 
            -
            _See code: [src\commands\audit.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 77 | 
            +
            _See code: [src\commands\audit.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\audit.ts)_
         | 
| 78 78 |  | 
| 79 79 | 
             
            ## `learnpack clean`
         | 
| 80 80 |  | 
| @@ -89,7 +89,7 @@ DESCRIPTION | |
| 89 89 | 
             
                 Extra documentation goes here
         | 
| 90 90 | 
             
            ```
         | 
| 91 91 |  | 
| 92 | 
            -
            _See code: [src\commands\clean.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 92 | 
            +
            _See code: [src\commands\clean.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\clean.ts)_
         | 
| 93 93 |  | 
| 94 94 | 
             
            ## `learnpack download [PACKAGE]`
         | 
| 95 95 |  | 
| @@ -107,7 +107,7 @@ DESCRIPTION | |
| 107 107 | 
             
              Extra documentation goes here
         | 
| 108 108 | 
             
            ```
         | 
| 109 109 |  | 
| 110 | 
            -
            _See code: [src\commands\download.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 110 | 
            +
            _See code: [src\commands\download.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\download.ts)_
         | 
| 111 111 |  | 
| 112 112 | 
             
            ## `learnpack help [COMMAND]`
         | 
| 113 113 |  | 
| @@ -138,7 +138,7 @@ OPTIONS | |
| 138 138 | 
             
              -h, --grading  show CLI help
         | 
| 139 139 | 
             
            ```
         | 
| 140 140 |  | 
| 141 | 
            -
            _See code: [src\commands\init.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 141 | 
            +
            _See code: [src\commands\init.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\init.ts)_
         | 
| 142 142 |  | 
| 143 143 | 
             
            ## `learnpack login [PACKAGE]`
         | 
| 144 144 |  | 
| @@ -156,7 +156,7 @@ DESCRIPTION | |
| 156 156 | 
             
                 Extra documentation goes here
         | 
| 157 157 | 
             
            ```
         | 
| 158 158 |  | 
| 159 | 
            -
            _See code: [src\commands\login.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 159 | 
            +
            _See code: [src\commands\login.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\login.ts)_
         | 
| 160 160 |  | 
| 161 161 | 
             
            ## `learnpack logout [PACKAGE]`
         | 
| 162 162 |  | 
| @@ -174,7 +174,7 @@ DESCRIPTION | |
| 174 174 | 
             
                 Extra documentation goes here
         | 
| 175 175 | 
             
            ```
         | 
| 176 176 |  | 
| 177 | 
            -
            _See code: [src\commands\logout.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 177 | 
            +
            _See code: [src\commands\logout.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\logout.ts)_
         | 
| 178 178 |  | 
| 179 179 | 
             
            ## `learnpack plugins`
         | 
| 180 180 |  | 
| @@ -309,7 +309,7 @@ DESCRIPTION | |
| 309 309 | 
             
                 Extra documentation goes here
         | 
| 310 310 | 
             
            ```
         | 
| 311 311 |  | 
| 312 | 
            -
            _See code: [src\commands\publish.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 312 | 
            +
            _See code: [src\commands\publish.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\publish.ts)_
         | 
| 313 313 |  | 
| 314 314 | 
             
            ## `learnpack start`
         | 
| 315 315 |  | 
| @@ -330,7 +330,7 @@ OPTIONS | |
| 330 330 | 
             
              -w, --watch                         Watch for file changes
         | 
| 331 331 | 
             
            ```
         | 
| 332 332 |  | 
| 333 | 
            -
            _See code: [src\commands\start.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 333 | 
            +
            _See code: [src\commands\start.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\start.ts)_
         | 
| 334 334 |  | 
| 335 335 | 
             
            ## `learnpack test [EXERCISESLUG]`
         | 
| 336 336 |  | 
| @@ -344,7 +344,7 @@ ARGUMENTS | |
| 344 344 | 
             
              EXERCISESLUG  The name of the exercise to test
         | 
| 345 345 | 
             
            ```
         | 
| 346 346 |  | 
| 347 | 
            -
            _See code: [src\commands\test.ts](https://github.com/learnpack/learnpack-cli/blob/ | 
| 347 | 
            +
            _See code: [src\commands\test.ts](https://github.com/learnpack/learnpack-cli/blob/v4.0.0/src\commands\test.ts)_
         | 
| 348 348 | 
             
            <!-- commandsstop -->
         | 
| 349 349 |  | 
| 350 350 | 
             
            > > > > > > > 0cb3e56d84c197f9d008836bb573eade212b7e57
         | 
| @@ -145,12 +145,12 @@ exports.default = async ({ grading, mode, disableGrading, version, }) => { | |
| 145 145 | 
             
                    console_1.default.debug("Config version not found, downloading default.");
         | 
| 146 146 | 
             
                    const resp = await fetch("https://raw.githubusercontent.com/learnpack/ide/master/package.json");
         | 
| 147 147 | 
             
                    const packageJSON = await resp.json();
         | 
| 148 | 
            -
                    configObj.config.editor.version = packageJSON.version || " | 
| 148 | 
            +
                    configObj.config.editor.version = packageJSON.version || "4.0.0";
         | 
| 149 149 | 
             
                }
         | 
| 150 150 | 
             
                configObj.config.dirPath = "./" + confPath.base;
         | 
| 151 151 | 
             
                configObj.config.exercisesPath = getExercisesPath(confPath.base) || "./";
         | 
| 152 | 
            -
                const allowedCommands = new Set(["ls", "pwd", "echo", "node", "python"]);
         | 
| 153 152 | 
             
                if (configObj.config.variables) {
         | 
| 153 | 
            +
                    const allowedCommands = new Set(["ls", "pwd", "echo", "node", "python"]);
         | 
| 154 154 | 
             
                    const variableKeys = Object.keys(configObj.config.variables);
         | 
| 155 155 | 
             
                    const promises = [];
         | 
| 156 156 | 
             
                    for (const v of variableKeys) {
         | 
    
        package/lib/managers/file.js
    CHANGED
    
    | @@ -28,22 +28,32 @@ exports.decompress = (sourcePath, destinationPath) => new Promise((resolve, reje | |
| 28 28 | 
             
                });
         | 
| 29 29 | 
             
            });
         | 
| 30 30 | 
             
            exports.downloadEditor = async (version, destination) => {
         | 
| 31 | 
            -
                // https://raw.githubusercontent.com/learnpack/coding-ide/master/dist/app.tar.gz
         | 
| 32 | 
            -
                // if(versions[version] === undefined) throw new Error(`Invalid editor version ${version}`)
         | 
| 33 31 | 
             
                if (!version) {
         | 
| 34 32 | 
             
                    const res = await fetch("https://raw.githubusercontent.com/learnpack/ide/master/package.json");
         | 
| 35 33 | 
             
                    const json = await res.json();
         | 
| 36 | 
            -
                    version = json.version;
         | 
| 34 | 
            +
                    version = json.version || "4.0.0";
         | 
| 37 35 | 
             
                    if (!version)
         | 
| 38 36 | 
             
                        throw errors_1.InternalError(`Coding Editor version was not found on learnpack repository, check the config.editor.version property on learn.json`);
         | 
| 39 37 | 
             
                }
         | 
| 40 | 
            -
                const  | 
| 41 | 
            -
                 | 
| 38 | 
            +
                const [major, minor] = version.split(".");
         | 
| 39 | 
            +
                const versionPrefix = `${major}.${minor}`;
         | 
| 40 | 
            +
                const tagsRes = await fetch("https://api.github.com/repos/learnpack/ide/contents/bin");
         | 
| 41 | 
            +
                const tags = await tagsRes.json();
         | 
| 42 | 
            +
                const matchingTags = tags
         | 
| 43 | 
            +
                    .filter((tag) => tag.name.includes(versionPrefix))
         | 
| 44 | 
            +
                    .sort((a, b) => b.name.localeCompare(a.name, undefined, { numeric: true }));
         | 
| 45 | 
            +
                if (matchingTags.length === 0)
         | 
| 46 | 
            +
                    throw errors_1.InternalError(`No matching version found for prefix ${versionPrefix} in the learnpack repository`);
         | 
| 47 | 
            +
                const latestVersion = matchingTags[0].name
         | 
| 48 | 
            +
                    .replace("learnpack-", "")
         | 
| 49 | 
            +
                    .replace(".tar.gz", "");
         | 
| 50 | 
            +
                const versionNumber = parseInt(latestVersion.split(".")[0]);
         | 
| 51 | 
            +
                let url = `https://github.com/learnpack/coding-ide/blob/${latestVersion}/dist`;
         | 
| 42 52 | 
             
                if (versionNumber > 2)
         | 
| 43 | 
            -
                    url = `https://github.com/learnpack/ide/blob/master/bin/learnpack-${ | 
| 53 | 
            +
                    url = `https://github.com/learnpack/ide/blob/master/bin/learnpack-${latestVersion}.tar.gz`;
         | 
| 44 54 | 
             
                const resp2 = await fetch(url, { method: "HEAD" });
         | 
| 45 55 | 
             
                if (!resp2.ok)
         | 
| 46 | 
            -
                    throw errors_1.InternalError(`Coding Editor ${ | 
| 56 | 
            +
                    throw errors_1.InternalError(`Coding Editor ${latestVersion} was not found on learnpack repository, check the config.editor.version property on learn.json`);
         | 
| 47 57 | 
             
                console_1.default.info("Downloading the LearnPack coding UI, this may take a minute...");
         | 
| 48 58 | 
             
                url += versionNumber > 2 ? `?raw=true` : `/app.tar.gz?raw=true`;
         | 
| 49 59 | 
             
                return exports.download(url, destination);
         | 
    
        package/oclif.manifest.json
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            {"version":" | 
| 1 | 
            +
            {"version":"4.0.0","commands":{"audit":{"id":"audit","description":"learnpack audit is the command in charge of creating an auditory of the repository\n...\nlearnpack audit checks for the following information in a repository:\n    1. The configuration object has slug, repository and description. (Error)\n    2. The command learnpack clean has been run. (Error)\n    3. If a markdown or test file doesn't have any content. (Error)\n    4. The links are accessing to valid servers. (Error)\n    5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)\n    6. The external images are working (If they are pointing to a valid server). (Error)\n    7. The exercises directory names are valid. (Error)\n    8. If an exercise doesn't have a README file. (Error)\n    9. The exercises array (Of the config file) has content. (Error)\n    10. The exercses have the same translations. (Warning)\n    11. The .gitignore file exists. (Warning)\n    12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)\n","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[]},"clean":{"id":"clean","description":"Clean the configuration object\n  ...\n  Extra documentation goes here\n  ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[]},"download":{"id":"download","description":"Describe the command here\n...\nExtra documentation goes here\n","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"init":{"id":"init","description":"Create a new learning package: Book, Tutorial or Exercise","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{"grading":{"name":"grading","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"login":{"id":"login","description":"Describe the command here\n  ...\n  Extra documentation goes here\n  ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"logout":{"id":"logout","description":"Describe the command here\n  ...\n  Extra documentation goes here\n  ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"publish":{"id":"publish","description":"Describe the command here\n  ...\n  Extra documentation goes here\n  ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"start":{"id":"start","description":"Runs a small server with all the exercise instructions","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{"port":{"name":"port","type":"option","char":"p","description":"server port"},"host":{"name":"host","type":"option","char":"h","description":"server host"},"disableGrading":{"name":"disableGrading","type":"boolean","char":"D","description":"disble grading functionality","allowNo":false},"watch":{"name":"watch","type":"boolean","char":"w","description":"Watch for file changes","allowNo":false},"editor":{"name":"editor","type":"option","char":"e","description":"[preview, extension]","options":["extension","preview"]},"version":{"name":"version","type":"option","char":"v","description":"E.g: 1.0.1"},"grading":{"name":"grading","type":"option","char":"g","description":"[isolated, incremental]","options":["isolated","incremental"]},"debug":{"name":"debug","type":"boolean","char":"d","description":"debugger mode for more verbage","allowNo":false}},"args":[]},"test":{"id":"test","description":"Test exercises","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"exerciseSlug","description":"The name of the exercise to test","required":false,"hidden":false}]}}}
         | 
    
        package/package.json
    CHANGED
    
    
| @@ -28,9 +28,9 @@ import { IFile } from "../../models/file" | |
| 28 28 | 
             
            /* exercise folder name standard */
         | 
| 29 29 |  | 
| 30 30 | 
             
            // eslint-disable-next-line
         | 
| 31 | 
            -
            const fetch = require("node-fetch") | 
| 31 | 
            +
            const fetch = require("node-fetch")
         | 
| 32 32 | 
             
            // eslint-disable-next-line
         | 
| 33 | 
            -
            const chalk = require("chalk") | 
| 33 | 
            +
            const chalk = require("chalk")
         | 
| 34 34 |  | 
| 35 35 | 
             
            /* exercise folder name standard */
         | 
| 36 36 |  | 
| @@ -206,15 +206,15 @@ configObj.config.editor.version = version | |
| 206 206 | 
             
                  "https://raw.githubusercontent.com/learnpack/ide/master/package.json"
         | 
| 207 207 | 
             
                )
         | 
| 208 208 | 
             
                const packageJSON = await resp.json()
         | 
| 209 | 
            -
                configObj.config.editor.version = packageJSON.version || " | 
| 209 | 
            +
                configObj.config.editor.version = packageJSON.version || "4.0.0"
         | 
| 210 210 | 
             
              }
         | 
| 211 211 |  | 
| 212 212 | 
             
              configObj.config.dirPath = "./" + confPath.base
         | 
| 213 213 | 
             
              configObj.config.exercisesPath = getExercisesPath(confPath.base) || "./"
         | 
| 214 214 |  | 
| 215 | 
            -
              const allowedCommands = new Set(["ls", "pwd", "echo", "node", "python"])
         | 
| 216 | 
            -
             | 
| 217 215 | 
             
              if (configObj.config.variables) {
         | 
| 216 | 
            +
                const allowedCommands = new Set(["ls", "pwd", "echo", "node", "python"])
         | 
| 217 | 
            +
             | 
| 218 218 | 
             
                const variableKeys = Object.keys(configObj.config.variables)
         | 
| 219 219 | 
             
                const promises = []
         | 
| 220 220 |  | 
| @@ -384,9 +384,7 @@ fs.unlinkSync(_path) | |
| 384 384 | 
             
                    ex => ex.slug === slug
         | 
| 385 385 | 
             
                  )
         | 
| 386 386 | 
             
                  if (!exercise)
         | 
| 387 | 
            -
                    throw ValidationError(
         | 
| 388 | 
            -
                      `Exercise ${slug} not found on the configuration`
         | 
| 389 | 
            -
                    )
         | 
| 387 | 
            +
                    throw ValidationError(`Exercise ${slug} not found on the configuration`)
         | 
| 390 388 |  | 
| 391 389 | 
             
                  if (configObj.config) {
         | 
| 392 390 | 
             
                    for (const fileName of fs.readdirSync(
         | 
    
        package/src/managers/file.ts
    CHANGED
    
    | @@ -1,196 +1,212 @@ | |
| 1 | 
            -
            import * as fs from "fs"
         | 
| 2 | 
            -
            import * as p from "path"
         | 
| 3 | 
            -
            import * as shell from "shelljs"
         | 
| 4 | 
            -
            import { cli } from "cli-ux"
         | 
| 5 | 
            -
            import * as targz from "targz"
         | 
| 6 | 
            -
            import Console from "../utils/console"
         | 
| 7 | 
            -
            import * as https from "https"
         | 
| 8 | 
            -
            import { InternalError } from "../utils/errors"
         | 
| 9 | 
            -
             | 
| 10 | 
            -
            // eslint-disable-next-line
         | 
| 11 | 
            -
            const fetch = require("node-fetch") | 
| 12 | 
            -
             | 
| 13 | 
            -
            export const decompress = (sourcePath: string, destinationPath: string) =>
         | 
| 14 | 
            -
              new Promise((resolve, reject) => {
         | 
| 15 | 
            -
                Console.debug("Decompressing " + sourcePath)
         | 
| 16 | 
            -
                targz.decompress(
         | 
| 17 | 
            -
                  {
         | 
| 18 | 
            -
                    src: sourcePath,
         | 
| 19 | 
            -
                    dest: destinationPath,
         | 
| 20 | 
            -
                  },
         | 
| 21 | 
            -
                  function (err: string | Error | null) {
         | 
| 22 | 
            -
                    if (err) {
         | 
| 23 | 
            -
                      Console.error("Error when trying to decompress")
         | 
| 24 | 
            -
                      reject(err)
         | 
| 25 | 
            -
                    } else {
         | 
| 26 | 
            -
                      Console.info("Decompression finished successfully")
         | 
| 27 | 
            -
                      resolve(/* */ "")
         | 
| 28 | 
            -
                    }
         | 
| 29 | 
            -
                  }
         | 
| 30 | 
            -
                )
         | 
| 31 | 
            -
              })
         | 
| 32 | 
            -
             | 
| 33 | 
            -
            export const downloadEditor = async (
         | 
| 34 | 
            -
              version: string | undefined,
         | 
| 35 | 
            -
              destination: string
         | 
| 36 | 
            -
            ) => {
         | 
| 37 | 
            -
               | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
                const  | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
                 | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
              }
         | 
| 52 | 
            -
             | 
| 53 | 
            -
              const  | 
| 54 | 
            -
             | 
| 55 | 
            -
               | 
| 56 | 
            -
             | 
| 57 | 
            -
               | 
| 58 | 
            -
                 | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
                 | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
               | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
            }
         | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
                   | 
| 96 | 
            -
                     | 
| 97 | 
            -
                     | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 101 | 
            -
             | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
                         | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
                       | 
| 109 | 
            -
                    )
         | 
| 110 | 
            -
                     | 
| 111 | 
            -
             | 
| 112 | 
            -
                    )
         | 
| 113 | 
            -
             | 
| 114 | 
            -
             | 
| 115 | 
            -
             | 
| 116 | 
            -
             | 
| 117 | 
            -
             | 
| 118 | 
            -
             | 
| 119 | 
            -
             | 
| 120 | 
            -
            }
         | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
                 | 
| 131 | 
            -
             | 
| 132 | 
            -
             | 
| 133 | 
            -
             | 
| 134 | 
            -
             | 
| 135 | 
            -
             | 
| 136 | 
            -
             | 
| 137 | 
            -
             | 
| 138 | 
            -
             | 
| 139 | 
            -
             | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 142 | 
            -
             | 
| 143 | 
            -
                 | 
| 144 | 
            -
             | 
| 145 | 
            -
             | 
| 146 | 
            -
             | 
| 147 | 
            -
                  )
         | 
| 148 | 
            -
                  // return false
         | 
| 149 | 
            -
                }
         | 
| 150 | 
            -
             | 
| 151 | 
            -
                cli.action. | 
| 152 | 
            -
             | 
| 153 | 
            -
             | 
| 154 | 
            -
                 | 
| 155 | 
            -
             | 
| 156 | 
            -
             | 
| 157 | 
            -
             | 
| 158 | 
            -
             | 
| 159 | 
            -
                 | 
| 160 | 
            -
             | 
| 161 | 
            -
             | 
| 162 | 
            -
             | 
| 163 | 
            -
             | 
| 164 | 
            -
             | 
| 165 | 
            -
                 | 
| 166 | 
            -
             | 
| 167 | 
            -
             | 
| 168 | 
            -
             | 
| 169 | 
            -
             | 
| 170 | 
            -
             | 
| 171 | 
            -
             | 
| 172 | 
            -
                 | 
| 173 | 
            -
             | 
| 174 | 
            -
             | 
| 175 | 
            -
             | 
| 176 | 
            -
             | 
| 177 | 
            -
             | 
| 178 | 
            -
             | 
| 179 | 
            -
             | 
| 180 | 
            -
             | 
| 181 | 
            -
                 | 
| 182 | 
            -
             | 
| 183 | 
            -
             | 
| 184 | 
            -
             | 
| 185 | 
            -
             | 
| 186 | 
            -
             | 
| 187 | 
            -
             | 
| 188 | 
            -
             | 
| 189 | 
            -
             | 
| 190 | 
            -
             | 
| 191 | 
            -
             | 
| 192 | 
            -
             | 
| 193 | 
            -
             | 
| 194 | 
            -
             | 
| 195 | 
            -
             | 
| 196 | 
            -
             | 
| 1 | 
            +
            import * as fs from "fs"
         | 
| 2 | 
            +
            import * as p from "path"
         | 
| 3 | 
            +
            import * as shell from "shelljs"
         | 
| 4 | 
            +
            import { cli } from "cli-ux"
         | 
| 5 | 
            +
            import * as targz from "targz"
         | 
| 6 | 
            +
            import Console from "../utils/console"
         | 
| 7 | 
            +
            import * as https from "https"
         | 
| 8 | 
            +
            import { InternalError } from "../utils/errors"
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            // eslint-disable-next-line
         | 
| 11 | 
            +
            const fetch = require("node-fetch")
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            export const decompress = (sourcePath: string, destinationPath: string) =>
         | 
| 14 | 
            +
              new Promise((resolve, reject) => {
         | 
| 15 | 
            +
                Console.debug("Decompressing " + sourcePath)
         | 
| 16 | 
            +
                targz.decompress(
         | 
| 17 | 
            +
                  {
         | 
| 18 | 
            +
                    src: sourcePath,
         | 
| 19 | 
            +
                    dest: destinationPath,
         | 
| 20 | 
            +
                  },
         | 
| 21 | 
            +
                  function (err: string | Error | null) {
         | 
| 22 | 
            +
                    if (err) {
         | 
| 23 | 
            +
                      Console.error("Error when trying to decompress")
         | 
| 24 | 
            +
                      reject(err)
         | 
| 25 | 
            +
                    } else {
         | 
| 26 | 
            +
                      Console.info("Decompression finished successfully")
         | 
| 27 | 
            +
                      resolve(/* */ "")
         | 
| 28 | 
            +
                    }
         | 
| 29 | 
            +
                  }
         | 
| 30 | 
            +
                )
         | 
| 31 | 
            +
              })
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            export const downloadEditor = async (
         | 
| 34 | 
            +
              version: string | undefined,
         | 
| 35 | 
            +
              destination: string
         | 
| 36 | 
            +
            ) => {
         | 
| 37 | 
            +
              if (!version) {
         | 
| 38 | 
            +
                const res = await fetch(
         | 
| 39 | 
            +
                  "https://raw.githubusercontent.com/learnpack/ide/master/package.json"
         | 
| 40 | 
            +
                )
         | 
| 41 | 
            +
                const json = await res.json()
         | 
| 42 | 
            +
                version = json.version || "4.0.0"
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                if (!version)
         | 
| 45 | 
            +
                  throw InternalError(
         | 
| 46 | 
            +
                    `Coding Editor version was not found on learnpack repository, check the config.editor.version property on learn.json`
         | 
| 47 | 
            +
                  )
         | 
| 48 | 
            +
              }
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              const [major, minor] = version.split(".")
         | 
| 51 | 
            +
              const versionPrefix = `${major}.${minor}`
         | 
| 52 | 
            +
             | 
| 53 | 
            +
              const tagsRes = await fetch(
         | 
| 54 | 
            +
                "https://api.github.com/repos/learnpack/ide/contents/bin"
         | 
| 55 | 
            +
              )
         | 
| 56 | 
            +
              const tags = await tagsRes.json()
         | 
| 57 | 
            +
              const matchingTags = tags
         | 
| 58 | 
            +
                .filter((tag: any) => tag.name.includes(versionPrefix))
         | 
| 59 | 
            +
                .sort((a: any, b: any) =>
         | 
| 60 | 
            +
                  b.name.localeCompare(a.name, undefined, { numeric: true })
         | 
| 61 | 
            +
                )
         | 
| 62 | 
            +
             | 
| 63 | 
            +
              if (matchingTags.length === 0)
         | 
| 64 | 
            +
                throw InternalError(
         | 
| 65 | 
            +
                  `No matching version found for prefix ${versionPrefix} in the learnpack repository`
         | 
| 66 | 
            +
                )
         | 
| 67 | 
            +
             | 
| 68 | 
            +
              const latestVersion = matchingTags[0].name
         | 
| 69 | 
            +
                .replace("learnpack-", "")
         | 
| 70 | 
            +
                .replace(".tar.gz", "")
         | 
| 71 | 
            +
              const versionNumber = parseInt(latestVersion.split(".")[0])
         | 
| 72 | 
            +
             | 
| 73 | 
            +
              let url = `https://github.com/learnpack/coding-ide/blob/${latestVersion}/dist`
         | 
| 74 | 
            +
             | 
| 75 | 
            +
              if (versionNumber > 2)
         | 
| 76 | 
            +
                url = `https://github.com/learnpack/ide/blob/master/bin/learnpack-${latestVersion}.tar.gz`
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              const resp2 = await fetch(url, { method: "HEAD" })
         | 
| 79 | 
            +
              if (!resp2.ok)
         | 
| 80 | 
            +
                throw InternalError(
         | 
| 81 | 
            +
                  `Coding Editor ${latestVersion} was not found on learnpack repository, check the config.editor.version property on learn.json`
         | 
| 82 | 
            +
                )
         | 
| 83 | 
            +
             | 
| 84 | 
            +
              Console.info("Downloading the LearnPack coding UI, this may take a minute...")
         | 
| 85 | 
            +
             | 
| 86 | 
            +
              url += versionNumber > 2 ? `?raw=true` : `/app.tar.gz?raw=true`
         | 
| 87 | 
            +
             | 
| 88 | 
            +
              return download(url, destination)
         | 
| 89 | 
            +
            }
         | 
| 90 | 
            +
             | 
| 91 | 
            +
            export const download = (url: string, dest: string) => {
         | 
| 92 | 
            +
              Console.debug("Downloading " + url)
         | 
| 93 | 
            +
              return new Promise((resolve, reject) => {
         | 
| 94 | 
            +
                const request = https.get(url, response => {
         | 
| 95 | 
            +
                  if (response.statusCode === 200) {
         | 
| 96 | 
            +
                    const file = fs.createWriteStream(dest, { flags: "wx" })
         | 
| 97 | 
            +
                    file.on("finish", () => {
         | 
| 98 | 
            +
                      resolve(true)
         | 
| 99 | 
            +
                    })
         | 
| 100 | 
            +
                    file.on("error", err => {
         | 
| 101 | 
            +
                      file.close()
         | 
| 102 | 
            +
                      if (err.code === "EEXIST") {
         | 
| 103 | 
            +
                        Console.debug("File already exists")
         | 
| 104 | 
            +
                        resolve("File already exists")
         | 
| 105 | 
            +
                      } else {
         | 
| 106 | 
            +
                        Console.debug("Error ", err.message)
         | 
| 107 | 
            +
                        fs.unlink(dest, () => reject(err.message)) // Delete temp file
         | 
| 108 | 
            +
                      }
         | 
| 109 | 
            +
                    })
         | 
| 110 | 
            +
                    response.pipe(file)
         | 
| 111 | 
            +
                  } else if (response.statusCode === 302 || response.statusCode === 301) {
         | 
| 112 | 
            +
                    // Console.debug("Servers redirected to "+response.headers.location)
         | 
| 113 | 
            +
                    // Recursively follow redirects, only a 200 will resolve.
         | 
| 114 | 
            +
                    if (response.headers.location) {
         | 
| 115 | 
            +
                      download(response.headers.location, dest)
         | 
| 116 | 
            +
                        .then(() => resolve(/* */ ""))
         | 
| 117 | 
            +
                        .catch(error => {
         | 
| 118 | 
            +
                          Console.error(error)
         | 
| 119 | 
            +
                          reject(error)
         | 
| 120 | 
            +
                        })
         | 
| 121 | 
            +
                    }
         | 
| 122 | 
            +
                  } else {
         | 
| 123 | 
            +
                    Console.debug(
         | 
| 124 | 
            +
                      `Server responded with ${response.statusCode}: ${response.statusMessage}`
         | 
| 125 | 
            +
                    )
         | 
| 126 | 
            +
                    reject(
         | 
| 127 | 
            +
                      `Server responded with ${response.statusCode}: ${response.statusMessage}`
         | 
| 128 | 
            +
                    )
         | 
| 129 | 
            +
                  }
         | 
| 130 | 
            +
                })
         | 
| 131 | 
            +
             | 
| 132 | 
            +
                request.on("error", err => {
         | 
| 133 | 
            +
                  reject(err.message)
         | 
| 134 | 
            +
                })
         | 
| 135 | 
            +
              })
         | 
| 136 | 
            +
            }
         | 
| 137 | 
            +
             | 
| 138 | 
            +
            export const clone = (repository = "", folder = "./") =>
         | 
| 139 | 
            +
              new Promise((resolve, reject) => {
         | 
| 140 | 
            +
                if (!repository) {
         | 
| 141 | 
            +
                  reject("Missing repository url for this package")
         | 
| 142 | 
            +
                  // return false
         | 
| 143 | 
            +
                }
         | 
| 144 | 
            +
             | 
| 145 | 
            +
                cli.action.start("Verifying GIT...")
         | 
| 146 | 
            +
                if (!shell.which("git")) {
         | 
| 147 | 
            +
                  reject("Sorry, this script requires git")
         | 
| 148 | 
            +
                  // return false
         | 
| 149 | 
            +
                }
         | 
| 150 | 
            +
             | 
| 151 | 
            +
                cli.action.stop()
         | 
| 152 | 
            +
             | 
| 153 | 
            +
                let fileName = p.basename(repository)
         | 
| 154 | 
            +
                if (!fileName) {
         | 
| 155 | 
            +
                  reject("Invalid repository information on package: " + repository)
         | 
| 156 | 
            +
                  // return false
         | 
| 157 | 
            +
                }
         | 
| 158 | 
            +
             | 
| 159 | 
            +
                fileName = fileName.split(".")[0]
         | 
| 160 | 
            +
                if (fs.existsSync("./" + fileName)) {
         | 
| 161 | 
            +
                  reject(
         | 
| 162 | 
            +
                    `Directory ${fileName} already exists; Did you download this package already?`
         | 
| 163 | 
            +
                  )
         | 
| 164 | 
            +
                  // return false
         | 
| 165 | 
            +
                }
         | 
| 166 | 
            +
             | 
| 167 | 
            +
                cli.action.start(`Cloning repository ${repository}...`)
         | 
| 168 | 
            +
                if (shell.exec(`git clone ${repository}`).code !== 0) {
         | 
| 169 | 
            +
                  reject("Error: Installation failed")
         | 
| 170 | 
            +
                }
         | 
| 171 | 
            +
             | 
| 172 | 
            +
                cli.action.stop()
         | 
| 173 | 
            +
             | 
| 174 | 
            +
                cli.action.start("Cleaning installation...")
         | 
| 175 | 
            +
                if (shell.exec(`rm -R -f ${folder}${fileName}/.git`).code !== 0) {
         | 
| 176 | 
            +
                  reject("Error: removing .git directory")
         | 
| 177 | 
            +
                }
         | 
| 178 | 
            +
             | 
| 179 | 
            +
                cli.action.stop()
         | 
| 180 | 
            +
             | 
| 181 | 
            +
                resolve("Done")
         | 
| 182 | 
            +
              })
         | 
| 183 | 
            +
             | 
| 184 | 
            +
            export const rmSync = function (path: string) {
         | 
| 185 | 
            +
              let files = []
         | 
| 186 | 
            +
              if (fs.existsSync(path)) {
         | 
| 187 | 
            +
                files = fs.readdirSync(path)
         | 
| 188 | 
            +
                for (const [, file] of files.entries()) {
         | 
| 189 | 
            +
                  const curPath = path + "/" + file
         | 
| 190 | 
            +
                  if (fs.lstatSync(curPath).isDirectory()) {
         | 
| 191 | 
            +
                    // recurse
         | 
| 192 | 
            +
                    rmSync(curPath)
         | 
| 193 | 
            +
                  } else {
         | 
| 194 | 
            +
                    // delete file
         | 
| 195 | 
            +
                    fs.unlinkSync(curPath)
         | 
| 196 | 
            +
                  }
         | 
| 197 | 
            +
                }
         | 
| 198 | 
            +
             | 
| 199 | 
            +
                fs.rmdirSync(path)
         | 
| 200 | 
            +
              }
         | 
| 201 | 
            +
            }
         | 
| 202 | 
            +
             | 
| 203 | 
            +
            export const checkIfDirectoryExists = (path: string) => {
         | 
| 204 | 
            +
              const fs = require("fs")
         | 
| 205 | 
            +
              if (fs.existsSync(path)) {
         | 
| 206 | 
            +
                return true
         | 
| 207 | 
            +
              }
         | 
| 208 | 
            +
             | 
| 209 | 
            +
              return false
         | 
| 210 | 
            +
            }
         | 
| 211 | 
            +
             | 
| 212 | 
            +
            export default { download, decompress, downloadEditor, clone, rmSync }
         |