@learnpack/learnpack 4.0.12 → 4.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,160 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /* eslint-disable arrow-parens */
4
- /* eslint-disable unicorn/no-array-for-each */
5
- const command_1 = require("@oclif/command");
6
- const SessionCommand_1 = require("../utils/SessionCommand");
7
- const session_1 = require("../managers/session");
8
- const fs = require("fs");
9
- const path = require("path");
10
- const archiver = require("archiver");
11
- const axios_1 = require("axios");
12
- const FormData = require("form-data");
13
- const console_1 = require("../utils/console");
14
- // const RIGOBOT_HOST = "https://rigobot-test-cca7d841c9d8.herokuapp.com"
15
- const RIGOBOT_HOST =
16
- // "https://8000-charlytoc-rigobot-bmwdeam7cev.ws-us116.gitpod.io"
17
- "https://rigobot.herokuapp.com";
18
- const uploadZipEndpont = RIGOBOT_HOST + "/v1/learnpack/upload";
19
- class BuildCommand extends SessionCommand_1.default {
20
- async init() {
21
- const { flags } = this.parse(BuildCommand);
22
- await this.initSession(flags);
23
- }
24
- async run() {
25
- const buildDir = path.join(process.cwd(), "build");
26
- const sessionPayload = await session_1.default.getPayload();
27
- if (!sessionPayload || !sessionPayload.rigobot) {
28
- console_1.default.error("You must be logged in to upload a LearnPack packge, please run: \n$ learnpack login");
29
- return;
30
- }
31
- const rigoToken = sessionPayload.rigobot.key;
32
- // const rigoToken = "417d612d226a1606ad3a4e94b1881a9f0124b667"
33
- // Read learn.json to get the slug
34
- const learnJsonPath = path.join(process.cwd(), "learn.json");
35
- if (!fs.existsSync(learnJsonPath)) {
36
- this.error("learn.json not found");
37
- }
38
- const learnJson = JSON.parse(fs.readFileSync(learnJsonPath, "utf-8"));
39
- const zipFilePath = path.join(process.cwd(), `${learnJson.slug}.zip`);
40
- // Ensure build directory exists
41
- if (!fs.existsSync(buildDir)) {
42
- fs.mkdirSync(buildDir);
43
- }
44
- // Copy config.json
45
- const configPath = path.join(process.cwd(), ".learn", "config.json");
46
- if (fs.existsSync(configPath)) {
47
- fs.copyFileSync(configPath, path.join(buildDir, "config.json"));
48
- }
49
- else {
50
- this.error("config.json not found");
51
- }
52
- // Copy .learn/assets directory
53
- const assetsDir = path.join(process.cwd(), ".learn", "assets");
54
- if (fs.existsSync(assetsDir)) {
55
- this.copyDirectory(assetsDir, path.join(buildDir, ".learn", "assets"));
56
- }
57
- else {
58
- this.error(".learn/assets directory not found");
59
- }
60
- // Copy .learn/_app directory files to the same level as config.json
61
- const appDir = path.join(process.cwd(), ".learn", "_app");
62
- if (fs.existsSync(appDir)) {
63
- this.copyDirectory(appDir, buildDir);
64
- }
65
- else {
66
- this.error(".learn/_app directory not found");
67
- }
68
- // Copy exercises directory
69
- const exercisesDir = path.join(process.cwd(), "exercises");
70
- const learnExercisesDir = path.join(process.cwd(), ".learn", "exercises");
71
- if (fs.existsSync(exercisesDir)) {
72
- this.copyDirectory(exercisesDir, path.join(buildDir, "exercises"));
73
- }
74
- else if (fs.existsSync(learnExercisesDir)) {
75
- this.copyDirectory(learnExercisesDir, path.join(buildDir, "exercises"));
76
- }
77
- else {
78
- this.error("exercises directory not found in either location");
79
- }
80
- // Copy learn.json
81
- fs.copyFileSync(learnJsonPath, path.join(buildDir, "learn.json"));
82
- // Create zip file
83
- const output = fs.createWriteStream(zipFilePath);
84
- const archive = archiver("zip", {
85
- zlib: { level: 9 },
86
- });
87
- output.on("close", async () => {
88
- this.log(`Build completed: ${zipFilePath} (${archive.pointer()} total bytes)`);
89
- // Remove build directory after zip is created
90
- this.removeDirectory(buildDir);
91
- console.log("Zip file saved in project root");
92
- const formData = new FormData();
93
- formData.append("file", fs.createReadStream(zipFilePath));
94
- formData.append("config", JSON.stringify(learnJson));
95
- try {
96
- const res = await axios_1.default.post(uploadZipEndpont, formData, {
97
- headers: Object.assign(Object.assign({}, formData.getHeaders()), { Authorization: `Token ${rigoToken}` }),
98
- });
99
- console.log(res.data);
100
- }
101
- catch (error) {
102
- if (axios_1.default.isAxiosError(error)) {
103
- if (error.response && error.response.status === 403) {
104
- console.error("Error 403:", error.response.data.error);
105
- }
106
- else if (error.response && error.response.status === 400) {
107
- console.error(error.response.data.error);
108
- }
109
- else {
110
- console.error("Error uploading file:", error.message);
111
- }
112
- }
113
- else {
114
- console.error("Error uploading file:", error);
115
- }
116
- }
117
- });
118
- archive.on("error", (err) => {
119
- throw err;
120
- });
121
- archive.pipe(output);
122
- archive.directory(buildDir, false);
123
- await archive.finalize();
124
- }
125
- copyDirectory(src, dest) {
126
- if (!fs.existsSync(dest)) {
127
- fs.mkdirSync(dest, { recursive: true });
128
- }
129
- const entries = fs.readdirSync(src, { withFileTypes: true });
130
- for (const entry of entries) {
131
- const srcPath = path.join(src, entry.name);
132
- const destPath = path.join(dest, entry.name);
133
- if (entry.isDirectory()) {
134
- this.copyDirectory(srcPath, destPath);
135
- }
136
- else {
137
- fs.copyFileSync(srcPath, destPath);
138
- }
139
- }
140
- }
141
- removeDirectory(dir) {
142
- if (fs.existsSync(dir)) {
143
- fs.readdirSync(dir).forEach((file) => {
144
- const currentPath = path.join(dir, file);
145
- if (fs.lstatSync(currentPath).isDirectory()) {
146
- this.removeDirectory(currentPath);
147
- }
148
- else {
149
- fs.unlinkSync(currentPath);
150
- }
151
- });
152
- fs.rmdirSync(dir);
153
- }
154
- }
155
- }
156
- BuildCommand.description = "Builds the project by copying necessary files and directories into a zip file";
157
- BuildCommand.flags = {
158
- help: command_1.flags.help({ char: "h" }),
159
- };
160
- exports.default = BuildCommand;
@@ -1,181 +0,0 @@
1
- /* eslint-disable arrow-parens */
2
- /* eslint-disable unicorn/no-array-for-each */
3
- import { flags } from "@oclif/command"
4
- import SessionCommand from "../utils/SessionCommand"
5
- import SessionManager from "../managers/session"
6
- import * as fs from "fs"
7
- import * as path from "path"
8
- import * as archiver from "archiver"
9
- import axios from "axios"
10
- import FormData = require("form-data")
11
- import Console from "../utils/console"
12
-
13
- // const RIGOBOT_HOST = "https://rigobot-test-cca7d841c9d8.herokuapp.com"
14
- const RIGOBOT_HOST =
15
- // "https://8000-charlytoc-rigobot-bmwdeam7cev.ws-us116.gitpod.io"
16
- "https://rigobot.herokuapp.com"
17
- const uploadZipEndpont = RIGOBOT_HOST + "/v1/learnpack/upload"
18
-
19
- export default class BuildCommand extends SessionCommand {
20
- static description =
21
- "Builds the project by copying necessary files and directories into a zip file"
22
-
23
- static flags = {
24
- help: flags.help({ char: "h" }),
25
- }
26
-
27
- async init() {
28
- const { flags } = this.parse(BuildCommand)
29
- await this.initSession(flags)
30
- }
31
-
32
- async run() {
33
- const buildDir = path.join(process.cwd(), "build")
34
- const sessionPayload = await SessionManager.getPayload()
35
- if (!sessionPayload || !sessionPayload.rigobot) {
36
- Console.error(
37
- "You must be logged in to upload a LearnPack packge, please run: \n$ learnpack login"
38
- )
39
- return
40
- }
41
-
42
- const rigoToken = sessionPayload.rigobot.key
43
- // const rigoToken = "417d612d226a1606ad3a4e94b1881a9f0124b667"
44
-
45
- // Read learn.json to get the slug
46
- const learnJsonPath = path.join(process.cwd(), "learn.json")
47
- if (!fs.existsSync(learnJsonPath)) {
48
- this.error("learn.json not found")
49
- }
50
-
51
- const learnJson = JSON.parse(fs.readFileSync(learnJsonPath, "utf-8"))
52
-
53
- const zipFilePath = path.join(process.cwd(), `${learnJson.slug}.zip`)
54
-
55
- // Ensure build directory exists
56
- if (!fs.existsSync(buildDir)) {
57
- fs.mkdirSync(buildDir)
58
- }
59
-
60
- // Copy config.json
61
- const configPath = path.join(process.cwd(), ".learn", "config.json")
62
- if (fs.existsSync(configPath)) {
63
- fs.copyFileSync(configPath, path.join(buildDir, "config.json"))
64
- } else {
65
- this.error("config.json not found")
66
- }
67
-
68
- // Copy .learn/assets directory
69
- const assetsDir = path.join(process.cwd(), ".learn", "assets")
70
- if (fs.existsSync(assetsDir)) {
71
- this.copyDirectory(assetsDir, path.join(buildDir, ".learn", "assets"))
72
- } else {
73
- this.error(".learn/assets directory not found")
74
- }
75
-
76
- // Copy .learn/_app directory files to the same level as config.json
77
- const appDir = path.join(process.cwd(), ".learn", "_app")
78
- if (fs.existsSync(appDir)) {
79
- this.copyDirectory(appDir, buildDir)
80
- } else {
81
- this.error(".learn/_app directory not found")
82
- }
83
-
84
- // Copy exercises directory
85
- const exercisesDir = path.join(process.cwd(), "exercises")
86
- const learnExercisesDir = path.join(process.cwd(), ".learn", "exercises")
87
-
88
- if (fs.existsSync(exercisesDir)) {
89
- this.copyDirectory(exercisesDir, path.join(buildDir, "exercises"))
90
- } else if (fs.existsSync(learnExercisesDir)) {
91
- this.copyDirectory(learnExercisesDir, path.join(buildDir, "exercises"))
92
- } else {
93
- this.error("exercises directory not found in either location")
94
- }
95
-
96
- // Copy learn.json
97
- fs.copyFileSync(learnJsonPath, path.join(buildDir, "learn.json"))
98
-
99
- // Create zip file
100
- const output = fs.createWriteStream(zipFilePath)
101
- const archive = archiver("zip", {
102
- zlib: { level: 9 },
103
- })
104
-
105
- output.on("close", async () => {
106
- this.log(
107
- `Build completed: ${zipFilePath} (${archive.pointer()} total bytes)`
108
- )
109
- // Remove build directory after zip is created
110
- this.removeDirectory(buildDir)
111
- console.log("Zip file saved in project root")
112
-
113
- const formData = new FormData()
114
- formData.append("file", fs.createReadStream(zipFilePath))
115
- formData.append("config", JSON.stringify(learnJson))
116
-
117
- try {
118
- const res = await axios.post(uploadZipEndpont, formData, {
119
- headers: {
120
- ...formData.getHeaders(),
121
- Authorization: `Token ${rigoToken}`,
122
- },
123
- })
124
- console.log(res.data)
125
- } catch (error) {
126
- if (axios.isAxiosError(error)) {
127
- if (error.response && error.response.status === 403) {
128
- console.error("Error 403:", error.response.data.error)
129
- } else if (error.response && error.response.status === 400) {
130
- console.error(error.response.data.error)
131
- } else {
132
- console.error("Error uploading file:", error.message)
133
- }
134
- } else {
135
- console.error("Error uploading file:", error)
136
- }
137
- }
138
- })
139
-
140
- archive.on("error", (err: any) => {
141
- throw err
142
- })
143
-
144
- archive.pipe(output)
145
- archive.directory(buildDir, false)
146
- await archive.finalize()
147
- }
148
-
149
- copyDirectory(src: string, dest: string) {
150
- if (!fs.existsSync(dest)) {
151
- fs.mkdirSync(dest, { recursive: true })
152
- }
153
-
154
- const entries = fs.readdirSync(src, { withFileTypes: true })
155
-
156
- for (const entry of entries) {
157
- const srcPath = path.join(src, entry.name)
158
- const destPath = path.join(dest, entry.name)
159
-
160
- if (entry.isDirectory()) {
161
- this.copyDirectory(srcPath, destPath)
162
- } else {
163
- fs.copyFileSync(srcPath, destPath)
164
- }
165
- }
166
- }
167
-
168
- removeDirectory(dir: string) {
169
- if (fs.existsSync(dir)) {
170
- fs.readdirSync(dir).forEach((file) => {
171
- const currentPath = path.join(dir, file)
172
- if (fs.lstatSync(currentPath).isDirectory()) {
173
- this.removeDirectory(currentPath)
174
- } else {
175
- fs.unlinkSync(currentPath)
176
- }
177
- })
178
- fs.rmdirSync(dir)
179
- }
180
- }
181
- }