@learnpack/learnpack 5.0.30 → 5.0.31
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 +12 -12
- package/lib/commands/audit.js +15 -15
- package/lib/commands/init.js +39 -15
- package/lib/commands/translate.js +2 -10
- package/lib/managers/server/routes.js +38 -0
- package/lib/utils/creatorUtilities.d.ts +2 -0
- package/lib/utils/creatorUtilities.js +48 -7
- package/lib/utils/rigoActions.d.ts +1 -0
- package/lib/utils/rigoActions.js +13 -2
- package/oclif.manifest.json +1 -1
- package/package.json +2 -1
- package/src/commands/audit.ts +449 -449
- package/src/commands/breakToken.ts +36 -36
- package/src/commands/init.ts +48 -17
- package/src/commands/publish.ts +312 -312
- package/src/commands/translate.ts +1 -20
- package/src/managers/config/index.ts +1 -0
- package/src/managers/server/routes.ts +61 -1
- package/src/utils/console.ts +24 -24
- package/src/utils/creatorUtilities.ts +66 -6
- package/src/utils/rigoActions.ts +13 -1
package/src/commands/publish.ts
CHANGED
@@ -1,312 +1,312 @@
|
|
1
|
-
/* eslint-disable arrow-parens */
|
2
|
-
/* eslint-disable unicorn/no-array-for-each */
|
3
|
-
import { execSync } from "child_process"
|
4
|
-
import { flags } from "@oclif/command"
|
5
|
-
import SessionCommand from "../utils/SessionCommand"
|
6
|
-
import SessionManager from "../managers/session"
|
7
|
-
import * as fs from "fs"
|
8
|
-
import * as path from "path"
|
9
|
-
import * as archiver from "archiver"
|
10
|
-
import axios from "axios"
|
11
|
-
import FormData = require("form-data")
|
12
|
-
import Console from "../utils/console"
|
13
|
-
import {
|
14
|
-
decompress,
|
15
|
-
downloadEditor,
|
16
|
-
checkIfDirectoryExists,
|
17
|
-
} from "../managers/file"
|
18
|
-
import api, { TAcademy } from "../utils/api"
|
19
|
-
import * as prompts from "prompts"
|
20
|
-
|
21
|
-
const RIGOBOT_HOST = "https://rigobot.herokuapp.com"
|
22
|
-
// const RIGOBOT_HOST =
|
23
|
-
// "https://8000-charlytoc-rigobot-bmwdeam7cev.ws-us116.gitpod.io"
|
24
|
-
const uploadZipEndpont = RIGOBOT_HOST + "/v1/learnpack/upload"
|
25
|
-
|
26
|
-
const runAudit = () => {
|
27
|
-
try {
|
28
|
-
Console.info("Running learnpack audit before publishing...")
|
29
|
-
execSync("learnpack audit", { stdio: "inherit" })
|
30
|
-
} catch (error) {
|
31
|
-
Console.error("Failed to audit with learnpack")
|
32
|
-
|
33
|
-
// Si `execSync` lanza un error, capturamos el mensaje de error
|
34
|
-
if (error instanceof Error) {
|
35
|
-
Console.error(error.message)
|
36
|
-
} else {
|
37
|
-
Console.error("Unknown error occurred")
|
38
|
-
}
|
39
|
-
|
40
|
-
// Detener la ejecución del comando si `learnpack publish` falla
|
41
|
-
process.exit(1)
|
42
|
-
}
|
43
|
-
|
44
|
-
// Continuar con el proceso de build solo si `learnpack publish` fue exitoso
|
45
|
-
Console.info(
|
46
|
-
"Learnpack publish completed successfully. Proceeding with build..."
|
47
|
-
)
|
48
|
-
}
|
49
|
-
|
50
|
-
const selectAcademy = async (academies: TAcademy[]) => {
|
51
|
-
if (academies.length === 0) {
|
52
|
-
return null
|
53
|
-
}
|
54
|
-
|
55
|
-
if (academies.length === 1) {
|
56
|
-
return academies[0]
|
57
|
-
}
|
58
|
-
|
59
|
-
// prompts the user to select an academy to upload the assets
|
60
|
-
Console.info("In which academy do you want to publish the asset?")
|
61
|
-
const response = await prompts({
|
62
|
-
type: "select",
|
63
|
-
name: "academy",
|
64
|
-
message: "Select an academy",
|
65
|
-
choices: academies.map((academy) => ({
|
66
|
-
title: academy.name,
|
67
|
-
value: academy,
|
68
|
-
})),
|
69
|
-
})
|
70
|
-
return response.academy
|
71
|
-
}
|
72
|
-
|
73
|
-
export default class BuildCommand extends SessionCommand {
|
74
|
-
static description =
|
75
|
-
"Builds the project by copying necessary files and directories into a zip file"
|
76
|
-
|
77
|
-
static flags = {
|
78
|
-
help: flags.help({ char: "h" }),
|
79
|
-
}
|
80
|
-
|
81
|
-
async init() {
|
82
|
-
const { flags } = this.parse(BuildCommand)
|
83
|
-
await this.initSession(flags)
|
84
|
-
}
|
85
|
-
|
86
|
-
async run() {
|
87
|
-
const buildDir = path.join(process.cwd(), "build")
|
88
|
-
// this.configManager?.clean()
|
89
|
-
|
90
|
-
const configObject = this.configManager?.get()
|
91
|
-
|
92
|
-
let sessionPayload = await SessionManager.getPayload()
|
93
|
-
if (
|
94
|
-
!sessionPayload ||
|
95
|
-
!sessionPayload.rigobot ||
|
96
|
-
(sessionPayload.token && !(await api.validateToken(sessionPayload.token)))
|
97
|
-
) {
|
98
|
-
Console.error("You must be logged in to upload a LearnPack package")
|
99
|
-
try {
|
100
|
-
sessionPayload = await SessionManager.login()
|
101
|
-
} catch (error) {
|
102
|
-
Console.error("Error trying to authenticate")
|
103
|
-
Console.error((error as TypeError).message || (error as string))
|
104
|
-
}
|
105
|
-
}
|
106
|
-
|
107
|
-
const rigoToken = sessionPayload.rigobot.key
|
108
|
-
|
109
|
-
if (configObject) {
|
110
|
-
Console.info("Cleaning configuration files")
|
111
|
-
this.configManager?.clean()
|
112
|
-
// build exerises
|
113
|
-
runAudit()
|
114
|
-
|
115
|
-
Console.debug("Building exercises")
|
116
|
-
this.configManager?.buildIndex()
|
117
|
-
}
|
118
|
-
|
119
|
-
// const academies = await api.listUserAcademies(sessionPayload.token)
|
120
|
-
// // console.log(academies, "academies")
|
121
|
-
// const academy = await selectAcademy(academies)
|
122
|
-
// console.log(academy, "academy")
|
123
|
-
|
124
|
-
// Read learn.json to get the slug
|
125
|
-
const learnJsonPath = path.join(process.cwd(), "learn.json")
|
126
|
-
if (!fs.existsSync(learnJsonPath)) {
|
127
|
-
this.error("learn.json not found")
|
128
|
-
}
|
129
|
-
|
130
|
-
const learnJson = JSON.parse(fs.readFileSync(learnJsonPath, "utf-8"))
|
131
|
-
|
132
|
-
const zipFilePath = path.join(process.cwd(), `${learnJson.slug}.zip`)
|
133
|
-
|
134
|
-
// Ensure build directory exists
|
135
|
-
if (!fs.existsSync(buildDir)) {
|
136
|
-
fs.mkdirSync(buildDir)
|
137
|
-
}
|
138
|
-
|
139
|
-
if (configObject) {
|
140
|
-
const { config } = configObject
|
141
|
-
const appAlreadyExists = checkIfDirectoryExists(`${config?.dirPath}/_app`)
|
142
|
-
|
143
|
-
if (!appAlreadyExists) {
|
144
|
-
// download app and decompress
|
145
|
-
await downloadEditor(
|
146
|
-
config?.editor.version,
|
147
|
-
`${config?.dirPath}/app.tar.gz`
|
148
|
-
)
|
149
|
-
|
150
|
-
Console.info("Decompressing LearnPack UI, this may take a minute...")
|
151
|
-
await decompress(
|
152
|
-
`${config?.dirPath}/app.tar.gz`,
|
153
|
-
`${config?.dirPath}/_app/`
|
154
|
-
)
|
155
|
-
}
|
156
|
-
}
|
157
|
-
|
158
|
-
// Copy config.json
|
159
|
-
const configPath = path.join(process.cwd(), ".learn", "config.json")
|
160
|
-
if (fs.existsSync(configPath)) {
|
161
|
-
fs.copyFileSync(configPath, path.join(buildDir, "config.json"))
|
162
|
-
} else {
|
163
|
-
this.error("config.json not found")
|
164
|
-
}
|
165
|
-
|
166
|
-
// Copy .learn/assets directory, if it exists else create it
|
167
|
-
const assetsDir = path.join(process.cwd(), ".learn", "assets")
|
168
|
-
if (fs.existsSync(assetsDir)) {
|
169
|
-
this.copyDirectory(assetsDir, path.join(buildDir, ".learn", "assets"))
|
170
|
-
} else {
|
171
|
-
fs.mkdirSync(path.join(buildDir, ".learn", "assets"), { recursive: true })
|
172
|
-
}
|
173
|
-
|
174
|
-
// Copy .learn/_app directory files to the same level as config.json
|
175
|
-
const appDir = path.join(process.cwd(), ".learn", "_app")
|
176
|
-
if (fs.existsSync(appDir)) {
|
177
|
-
this.copyDirectory(appDir, buildDir)
|
178
|
-
} else {
|
179
|
-
this.error(".learn/_app directory not found")
|
180
|
-
}
|
181
|
-
|
182
|
-
// After copying the _app directory
|
183
|
-
const indexHtmlPath = path.join(appDir, "index.html")
|
184
|
-
const buildIndexHtmlPath = path.join(buildDir, "index.html")
|
185
|
-
|
186
|
-
if (fs.existsSync(indexHtmlPath)) {
|
187
|
-
let indexHtmlContent = fs.readFileSync(indexHtmlPath, "utf-8")
|
188
|
-
|
189
|
-
const description = learnJson.description.us || "LearnPack is awesome!"
|
190
|
-
const title =
|
191
|
-
learnJson.title.us || "LearnPack: Interactive Learning as a Service"
|
192
|
-
|
193
|
-
const previewUrl =
|
194
|
-
learnJson.preview ||
|
195
|
-
"https://raw.githubusercontent.com/learnpack/ide/refs/heads/master/public/learnpack.svg"
|
196
|
-
// Replace placeholders and the <title>Old title </title> tag for a new tag with the title
|
197
|
-
indexHtmlContent = indexHtmlContent
|
198
|
-
.replace(/{{description}}/g, description)
|
199
|
-
.replace(/<title>.*<\/title>/, `<title>${title}</title>`)
|
200
|
-
.replace(/{{title}}/g, title)
|
201
|
-
.replace(/{{preview}}/g, previewUrl)
|
202
|
-
|
203
|
-
// Write the modified content to the build directory
|
204
|
-
fs.writeFileSync(buildIndexHtmlPath, indexHtmlContent)
|
205
|
-
} else {
|
206
|
-
this.error("index.html not found in _app directory")
|
207
|
-
}
|
208
|
-
|
209
|
-
// Copy exercises directory
|
210
|
-
const exercisesDir = path.join(process.cwd(), "exercises")
|
211
|
-
const learnExercisesDir = path.join(process.cwd(), ".learn", "exercises")
|
212
|
-
|
213
|
-
if (fs.existsSync(exercisesDir)) {
|
214
|
-
this.copyDirectory(exercisesDir, path.join(buildDir, "exercises"))
|
215
|
-
} else if (fs.existsSync(learnExercisesDir)) {
|
216
|
-
this.copyDirectory(learnExercisesDir, path.join(buildDir, "exercises"))
|
217
|
-
} else {
|
218
|
-
this.error("exercises directory not found in either location")
|
219
|
-
}
|
220
|
-
|
221
|
-
// Copy learn.json
|
222
|
-
fs.copyFileSync(learnJsonPath, path.join(buildDir, "learn.json"))
|
223
|
-
|
224
|
-
// Create zip file
|
225
|
-
const output = fs.createWriteStream(zipFilePath)
|
226
|
-
const archive = archiver("zip", {
|
227
|
-
zlib: { level: 9 },
|
228
|
-
})
|
229
|
-
|
230
|
-
output.on("close", async () => {
|
231
|
-
this.log(
|
232
|
-
`Build completed: ${zipFilePath} (${archive.pointer()} total bytes)`
|
233
|
-
)
|
234
|
-
// Remove build directory after zip is created
|
235
|
-
|
236
|
-
Console.debug("Zip file saved in project root")
|
237
|
-
|
238
|
-
const formData = new FormData()
|
239
|
-
formData.append("file", fs.createReadStream(zipFilePath))
|
240
|
-
formData.append("config", JSON.stringify(learnJson))
|
241
|
-
|
242
|
-
try {
|
243
|
-
const res = await axios.post(uploadZipEndpont, formData, {
|
244
|
-
headers: {
|
245
|
-
...formData.getHeaders(),
|
246
|
-
Authorization: `Token ${rigoToken}`,
|
247
|
-
},
|
248
|
-
})
|
249
|
-
console.log(res.data)
|
250
|
-
// Remove the zip file after uploading
|
251
|
-
fs.unlinkSync(zipFilePath)
|
252
|
-
this.removeDirectory(buildDir)
|
253
|
-
} catch (error) {
|
254
|
-
if (axios.isAxiosError(error)) {
|
255
|
-
if (error.response && error.response.status === 403) {
|
256
|
-
console.error("Error 403:", error.response.data.error)
|
257
|
-
} else if (error.response && error.response.status === 400) {
|
258
|
-
console.error(error.response.data.error)
|
259
|
-
} else {
|
260
|
-
console.error("Error uploading file:", error)
|
261
|
-
}
|
262
|
-
} else {
|
263
|
-
console.error("Error uploading file:", error)
|
264
|
-
}
|
265
|
-
|
266
|
-
fs.unlinkSync(zipFilePath)
|
267
|
-
this.removeDirectory(buildDir)
|
268
|
-
}
|
269
|
-
})
|
270
|
-
|
271
|
-
archive.on("error", (err: any) => {
|
272
|
-
throw err
|
273
|
-
})
|
274
|
-
|
275
|
-
archive.pipe(output)
|
276
|
-
archive.directory(buildDir, false)
|
277
|
-
await archive.finalize()
|
278
|
-
}
|
279
|
-
|
280
|
-
copyDirectory(src: string, dest: string) {
|
281
|
-
if (!fs.existsSync(dest)) {
|
282
|
-
fs.mkdirSync(dest, { recursive: true })
|
283
|
-
}
|
284
|
-
|
285
|
-
const entries = fs.readdirSync(src, { withFileTypes: true })
|
286
|
-
|
287
|
-
for (const entry of entries) {
|
288
|
-
const srcPath = path.join(src, entry.name)
|
289
|
-
const destPath = path.join(dest, entry.name)
|
290
|
-
|
291
|
-
if (entry.isDirectory()) {
|
292
|
-
this.copyDirectory(srcPath, destPath)
|
293
|
-
} else {
|
294
|
-
fs.copyFileSync(srcPath, destPath)
|
295
|
-
}
|
296
|
-
}
|
297
|
-
}
|
298
|
-
|
299
|
-
removeDirectory(dir: string) {
|
300
|
-
if (fs.existsSync(dir)) {
|
301
|
-
fs.readdirSync(dir).forEach((file) => {
|
302
|
-
const currentPath = path.join(dir, file)
|
303
|
-
if (fs.lstatSync(currentPath).isDirectory()) {
|
304
|
-
this.removeDirectory(currentPath)
|
305
|
-
} else {
|
306
|
-
fs.unlinkSync(currentPath)
|
307
|
-
}
|
308
|
-
})
|
309
|
-
fs.rmdirSync(dir)
|
310
|
-
}
|
311
|
-
}
|
312
|
-
}
|
1
|
+
/* eslint-disable arrow-parens */
|
2
|
+
/* eslint-disable unicorn/no-array-for-each */
|
3
|
+
import { execSync } from "child_process"
|
4
|
+
import { flags } from "@oclif/command"
|
5
|
+
import SessionCommand from "../utils/SessionCommand"
|
6
|
+
import SessionManager from "../managers/session"
|
7
|
+
import * as fs from "fs"
|
8
|
+
import * as path from "path"
|
9
|
+
import * as archiver from "archiver"
|
10
|
+
import axios from "axios"
|
11
|
+
import FormData = require("form-data")
|
12
|
+
import Console from "../utils/console"
|
13
|
+
import {
|
14
|
+
decompress,
|
15
|
+
downloadEditor,
|
16
|
+
checkIfDirectoryExists,
|
17
|
+
} from "../managers/file"
|
18
|
+
import api, { TAcademy } from "../utils/api"
|
19
|
+
import * as prompts from "prompts"
|
20
|
+
|
21
|
+
const RIGOBOT_HOST = "https://rigobot.herokuapp.com"
|
22
|
+
// const RIGOBOT_HOST =
|
23
|
+
// "https://8000-charlytoc-rigobot-bmwdeam7cev.ws-us116.gitpod.io"
|
24
|
+
const uploadZipEndpont = RIGOBOT_HOST + "/v1/learnpack/upload"
|
25
|
+
|
26
|
+
const runAudit = () => {
|
27
|
+
try {
|
28
|
+
Console.info("Running learnpack audit before publishing...")
|
29
|
+
execSync("learnpack audit", { stdio: "inherit" })
|
30
|
+
} catch (error) {
|
31
|
+
Console.error("Failed to audit with learnpack")
|
32
|
+
|
33
|
+
// Si `execSync` lanza un error, capturamos el mensaje de error
|
34
|
+
if (error instanceof Error) {
|
35
|
+
Console.error(error.message)
|
36
|
+
} else {
|
37
|
+
Console.error("Unknown error occurred")
|
38
|
+
}
|
39
|
+
|
40
|
+
// Detener la ejecución del comando si `learnpack publish` falla
|
41
|
+
process.exit(1)
|
42
|
+
}
|
43
|
+
|
44
|
+
// Continuar con el proceso de build solo si `learnpack publish` fue exitoso
|
45
|
+
Console.info(
|
46
|
+
"Learnpack publish completed successfully. Proceeding with build..."
|
47
|
+
)
|
48
|
+
}
|
49
|
+
|
50
|
+
const selectAcademy = async (academies: TAcademy[]) => {
|
51
|
+
if (academies.length === 0) {
|
52
|
+
return null
|
53
|
+
}
|
54
|
+
|
55
|
+
if (academies.length === 1) {
|
56
|
+
return academies[0]
|
57
|
+
}
|
58
|
+
|
59
|
+
// prompts the user to select an academy to upload the assets
|
60
|
+
Console.info("In which academy do you want to publish the asset?")
|
61
|
+
const response = await prompts({
|
62
|
+
type: "select",
|
63
|
+
name: "academy",
|
64
|
+
message: "Select an academy",
|
65
|
+
choices: academies.map((academy) => ({
|
66
|
+
title: academy.name,
|
67
|
+
value: academy,
|
68
|
+
})),
|
69
|
+
})
|
70
|
+
return response.academy
|
71
|
+
}
|
72
|
+
|
73
|
+
export default class BuildCommand extends SessionCommand {
|
74
|
+
static description =
|
75
|
+
"Builds the project by copying necessary files and directories into a zip file"
|
76
|
+
|
77
|
+
static flags = {
|
78
|
+
help: flags.help({ char: "h" }),
|
79
|
+
}
|
80
|
+
|
81
|
+
async init() {
|
82
|
+
const { flags } = this.parse(BuildCommand)
|
83
|
+
await this.initSession(flags)
|
84
|
+
}
|
85
|
+
|
86
|
+
async run() {
|
87
|
+
const buildDir = path.join(process.cwd(), "build")
|
88
|
+
// this.configManager?.clean()
|
89
|
+
|
90
|
+
const configObject = this.configManager?.get()
|
91
|
+
|
92
|
+
let sessionPayload = await SessionManager.getPayload()
|
93
|
+
if (
|
94
|
+
!sessionPayload ||
|
95
|
+
!sessionPayload.rigobot ||
|
96
|
+
(sessionPayload.token && !(await api.validateToken(sessionPayload.token)))
|
97
|
+
) {
|
98
|
+
Console.error("You must be logged in to upload a LearnPack package")
|
99
|
+
try {
|
100
|
+
sessionPayload = await SessionManager.login()
|
101
|
+
} catch (error) {
|
102
|
+
Console.error("Error trying to authenticate")
|
103
|
+
Console.error((error as TypeError).message || (error as string))
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
const rigoToken = sessionPayload.rigobot.key
|
108
|
+
|
109
|
+
if (configObject) {
|
110
|
+
Console.info("Cleaning configuration files")
|
111
|
+
this.configManager?.clean()
|
112
|
+
// build exerises
|
113
|
+
runAudit()
|
114
|
+
|
115
|
+
Console.debug("Building exercises")
|
116
|
+
this.configManager?.buildIndex()
|
117
|
+
}
|
118
|
+
|
119
|
+
// const academies = await api.listUserAcademies(sessionPayload.token)
|
120
|
+
// // console.log(academies, "academies")
|
121
|
+
// const academy = await selectAcademy(academies)
|
122
|
+
// console.log(academy, "academy")
|
123
|
+
|
124
|
+
// Read learn.json to get the slug
|
125
|
+
const learnJsonPath = path.join(process.cwd(), "learn.json")
|
126
|
+
if (!fs.existsSync(learnJsonPath)) {
|
127
|
+
this.error("learn.json not found")
|
128
|
+
}
|
129
|
+
|
130
|
+
const learnJson = JSON.parse(fs.readFileSync(learnJsonPath, "utf-8"))
|
131
|
+
|
132
|
+
const zipFilePath = path.join(process.cwd(), `${learnJson.slug}.zip`)
|
133
|
+
|
134
|
+
// Ensure build directory exists
|
135
|
+
if (!fs.existsSync(buildDir)) {
|
136
|
+
fs.mkdirSync(buildDir)
|
137
|
+
}
|
138
|
+
|
139
|
+
if (configObject) {
|
140
|
+
const { config } = configObject
|
141
|
+
const appAlreadyExists = checkIfDirectoryExists(`${config?.dirPath}/_app`)
|
142
|
+
|
143
|
+
if (!appAlreadyExists) {
|
144
|
+
// download app and decompress
|
145
|
+
await downloadEditor(
|
146
|
+
config?.editor.version,
|
147
|
+
`${config?.dirPath}/app.tar.gz`
|
148
|
+
)
|
149
|
+
|
150
|
+
Console.info("Decompressing LearnPack UI, this may take a minute...")
|
151
|
+
await decompress(
|
152
|
+
`${config?.dirPath}/app.tar.gz`,
|
153
|
+
`${config?.dirPath}/_app/`
|
154
|
+
)
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
// Copy config.json
|
159
|
+
const configPath = path.join(process.cwd(), ".learn", "config.json")
|
160
|
+
if (fs.existsSync(configPath)) {
|
161
|
+
fs.copyFileSync(configPath, path.join(buildDir, "config.json"))
|
162
|
+
} else {
|
163
|
+
this.error("config.json not found")
|
164
|
+
}
|
165
|
+
|
166
|
+
// Copy .learn/assets directory, if it exists else create it
|
167
|
+
const assetsDir = path.join(process.cwd(), ".learn", "assets")
|
168
|
+
if (fs.existsSync(assetsDir)) {
|
169
|
+
this.copyDirectory(assetsDir, path.join(buildDir, ".learn", "assets"))
|
170
|
+
} else {
|
171
|
+
fs.mkdirSync(path.join(buildDir, ".learn", "assets"), { recursive: true })
|
172
|
+
}
|
173
|
+
|
174
|
+
// Copy .learn/_app directory files to the same level as config.json
|
175
|
+
const appDir = path.join(process.cwd(), ".learn", "_app")
|
176
|
+
if (fs.existsSync(appDir)) {
|
177
|
+
this.copyDirectory(appDir, buildDir)
|
178
|
+
} else {
|
179
|
+
this.error(".learn/_app directory not found")
|
180
|
+
}
|
181
|
+
|
182
|
+
// After copying the _app directory
|
183
|
+
const indexHtmlPath = path.join(appDir, "index.html")
|
184
|
+
const buildIndexHtmlPath = path.join(buildDir, "index.html")
|
185
|
+
|
186
|
+
if (fs.existsSync(indexHtmlPath)) {
|
187
|
+
let indexHtmlContent = fs.readFileSync(indexHtmlPath, "utf-8")
|
188
|
+
|
189
|
+
const description = learnJson.description.us || "LearnPack is awesome!"
|
190
|
+
const title =
|
191
|
+
learnJson.title.us || "LearnPack: Interactive Learning as a Service"
|
192
|
+
|
193
|
+
const previewUrl =
|
194
|
+
learnJson.preview ||
|
195
|
+
"https://raw.githubusercontent.com/learnpack/ide/refs/heads/master/public/learnpack.svg"
|
196
|
+
// Replace placeholders and the <title>Old title </title> tag for a new tag with the title
|
197
|
+
indexHtmlContent = indexHtmlContent
|
198
|
+
.replace(/{{description}}/g, description)
|
199
|
+
.replace(/<title>.*<\/title>/, `<title>${title}</title>`)
|
200
|
+
.replace(/{{title}}/g, title)
|
201
|
+
.replace(/{{preview}}/g, previewUrl)
|
202
|
+
|
203
|
+
// Write the modified content to the build directory
|
204
|
+
fs.writeFileSync(buildIndexHtmlPath, indexHtmlContent)
|
205
|
+
} else {
|
206
|
+
this.error("index.html not found in _app directory")
|
207
|
+
}
|
208
|
+
|
209
|
+
// Copy exercises directory
|
210
|
+
const exercisesDir = path.join(process.cwd(), "exercises")
|
211
|
+
const learnExercisesDir = path.join(process.cwd(), ".learn", "exercises")
|
212
|
+
|
213
|
+
if (fs.existsSync(exercisesDir)) {
|
214
|
+
this.copyDirectory(exercisesDir, path.join(buildDir, "exercises"))
|
215
|
+
} else if (fs.existsSync(learnExercisesDir)) {
|
216
|
+
this.copyDirectory(learnExercisesDir, path.join(buildDir, "exercises"))
|
217
|
+
} else {
|
218
|
+
this.error("exercises directory not found in either location")
|
219
|
+
}
|
220
|
+
|
221
|
+
// Copy learn.json
|
222
|
+
fs.copyFileSync(learnJsonPath, path.join(buildDir, "learn.json"))
|
223
|
+
|
224
|
+
// Create zip file
|
225
|
+
const output = fs.createWriteStream(zipFilePath)
|
226
|
+
const archive = archiver("zip", {
|
227
|
+
zlib: { level: 9 },
|
228
|
+
})
|
229
|
+
|
230
|
+
output.on("close", async () => {
|
231
|
+
this.log(
|
232
|
+
`Build completed: ${zipFilePath} (${archive.pointer()} total bytes)`
|
233
|
+
)
|
234
|
+
// Remove build directory after zip is created
|
235
|
+
|
236
|
+
Console.debug("Zip file saved in project root")
|
237
|
+
|
238
|
+
const formData = new FormData()
|
239
|
+
formData.append("file", fs.createReadStream(zipFilePath))
|
240
|
+
formData.append("config", JSON.stringify(learnJson))
|
241
|
+
|
242
|
+
try {
|
243
|
+
const res = await axios.post(uploadZipEndpont, formData, {
|
244
|
+
headers: {
|
245
|
+
...formData.getHeaders(),
|
246
|
+
Authorization: `Token ${rigoToken}`,
|
247
|
+
},
|
248
|
+
})
|
249
|
+
console.log(res.data)
|
250
|
+
// Remove the zip file after uploading
|
251
|
+
fs.unlinkSync(zipFilePath)
|
252
|
+
this.removeDirectory(buildDir)
|
253
|
+
} catch (error) {
|
254
|
+
if (axios.isAxiosError(error)) {
|
255
|
+
if (error.response && error.response.status === 403) {
|
256
|
+
console.error("Error 403:", error.response.data.error)
|
257
|
+
} else if (error.response && error.response.status === 400) {
|
258
|
+
console.error(error.response.data.error)
|
259
|
+
} else {
|
260
|
+
console.error("Error uploading file:", error)
|
261
|
+
}
|
262
|
+
} else {
|
263
|
+
console.error("Error uploading file:", error)
|
264
|
+
}
|
265
|
+
|
266
|
+
fs.unlinkSync(zipFilePath)
|
267
|
+
this.removeDirectory(buildDir)
|
268
|
+
}
|
269
|
+
})
|
270
|
+
|
271
|
+
archive.on("error", (err: any) => {
|
272
|
+
throw err
|
273
|
+
})
|
274
|
+
|
275
|
+
archive.pipe(output)
|
276
|
+
archive.directory(buildDir, false)
|
277
|
+
await archive.finalize()
|
278
|
+
}
|
279
|
+
|
280
|
+
copyDirectory(src: string, dest: string) {
|
281
|
+
if (!fs.existsSync(dest)) {
|
282
|
+
fs.mkdirSync(dest, { recursive: true })
|
283
|
+
}
|
284
|
+
|
285
|
+
const entries = fs.readdirSync(src, { withFileTypes: true })
|
286
|
+
|
287
|
+
for (const entry of entries) {
|
288
|
+
const srcPath = path.join(src, entry.name)
|
289
|
+
const destPath = path.join(dest, entry.name)
|
290
|
+
|
291
|
+
if (entry.isDirectory()) {
|
292
|
+
this.copyDirectory(srcPath, destPath)
|
293
|
+
} else {
|
294
|
+
fs.copyFileSync(srcPath, destPath)
|
295
|
+
}
|
296
|
+
}
|
297
|
+
}
|
298
|
+
|
299
|
+
removeDirectory(dir: string) {
|
300
|
+
if (fs.existsSync(dir)) {
|
301
|
+
fs.readdirSync(dir).forEach((file) => {
|
302
|
+
const currentPath = path.join(dir, file)
|
303
|
+
if (fs.lstatSync(currentPath).isDirectory()) {
|
304
|
+
this.removeDirectory(currentPath)
|
305
|
+
} else {
|
306
|
+
fs.unlinkSync(currentPath)
|
307
|
+
}
|
308
|
+
})
|
309
|
+
fs.rmdirSync(dir)
|
310
|
+
}
|
311
|
+
}
|
312
|
+
}
|
@@ -5,6 +5,7 @@ import * as prompts from "prompts"
|
|
5
5
|
import { translateExercise } from "../utils/rigoActions"
|
6
6
|
import SessionManager from "../managers/session"
|
7
7
|
import Console from "../utils/console"
|
8
|
+
import { saveTranslatedReadme } from "../utils/creatorUtilities"
|
8
9
|
|
9
10
|
// This function list the names of the exercise directories inside the ./exercises folder, if the exercises folder doesn't exist, it will look for the ./.learn/exercises folder
|
10
11
|
const listExercises = async () => {
|
@@ -24,11 +25,6 @@ const listExercises = async () => {
|
|
24
25
|
})
|
25
26
|
}
|
26
27
|
|
27
|
-
const cleanReadme = (readme: string) => {
|
28
|
-
// Replace <text> and </text> with nothing
|
29
|
-
return readme.replace(/<text>/g, "").replace(/<\/text>/g, "")
|
30
|
-
}
|
31
|
-
|
32
28
|
const getReadmeForExercise = async (exercise: string) => {
|
33
29
|
const readmePath = path.join(
|
34
30
|
process.cwd(),
|
@@ -39,20 +35,6 @@ const getReadmeForExercise = async (exercise: string) => {
|
|
39
35
|
return fs.readFileSync(readmePath, "utf8")
|
40
36
|
}
|
41
37
|
|
42
|
-
const saveTranslatedReadme = async (
|
43
|
-
exercise: string,
|
44
|
-
languageCode: string,
|
45
|
-
readme: string
|
46
|
-
) => {
|
47
|
-
const readmePath = path.join(
|
48
|
-
process.cwd(),
|
49
|
-
"exercises",
|
50
|
-
exercise,
|
51
|
-
`README.${languageCode}.md`
|
52
|
-
)
|
53
|
-
fs.writeFileSync(readmePath, cleanReadme(readme))
|
54
|
-
}
|
55
|
-
|
56
38
|
export default class BuildCommand extends SessionCommand {
|
57
39
|
static description =
|
58
40
|
"List all the lessons, the user is able of select many of them to translate to the given languages"
|
@@ -118,7 +100,6 @@ export default class BuildCommand extends SessionCommand {
|
|
118
100
|
text_to_translate: readme,
|
119
101
|
output_language: language,
|
120
102
|
})
|
121
|
-
console.log(response, "RESPONSE")
|
122
103
|
|
123
104
|
await saveTranslatedReadme(
|
124
105
|
exercise,
|