@learnpack/learnpack 5.0.288 → 5.0.291

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/src/ui/app.tar.gz CHANGED
Binary file
@@ -1,82 +1,100 @@
1
- import { Bucket, File } from "@google-cloud/storage"
2
-
3
- type TFile = {
4
- name: string
5
- slug: string
6
- hidden: boolean
7
- }
8
-
9
- export type Exercise = {
10
- title: string
11
- slug: string
12
- graded: boolean
13
- files: TFile[]
14
- translations: Record<string, string>
15
- position: number
16
- }
17
-
18
- export type ConfigResponse = {
19
- config: any
20
- exercises: Exercise[]
21
- }
22
-
23
- /**
24
- * Crea la configuración y lista de ejercicios para un curso.
25
- *
26
- * @param bucket - Instancia de GCS Bucket donde está el curso.
27
- * @param courseSlug - Slug del curso a procesar.
28
- * @returns Promise con objeto { config, exercises } listo para usar.
29
- */
30
- export async function buildConfig(
31
- bucket: Bucket,
32
- courseSlug: string
33
- ): Promise<ConfigResponse> {
34
- const prefix = `courses/${courseSlug}/`
35
- const [files] = await bucket.getFiles({ prefix })
36
-
37
- // 1) Leer learn.json
38
- const learnFile = files.find(f => f.name.endsWith("learn.json"))!
39
- const [learnBuf] = await learnFile.download()
40
- const learnJson = JSON.parse(learnBuf.toString())
41
-
42
- // 2) Agrupar ejercicios
43
- const map: Record<string, Omit<Exercise, "position">> = {}
44
- for (const file of files) {
45
- const parts = file.name.split("/")
46
- if (!parts.includes("exercises")) continue
47
-
48
- const slug = parts[parts.indexOf("exercises") + 1]
49
- if (!map[slug]) {
50
- map[slug] = {
51
- title: slug,
52
- slug,
53
- graded: false,
54
- files: [],
55
- translations: {},
56
- }
57
- }
58
-
59
- const fname = parts.pop()!
60
- const m = fname.match(/^readme(?:\.([a-z]{2}))?\.md$/i)
61
- if (m) {
62
- const lang = m[1] || "en"
63
- map[slug].translations[lang] = fname
64
- } else {
65
- map[slug].files.push({
66
- name: fname,
67
- slug: fname,
68
- hidden: false,
69
- })
70
- }
71
- }
72
-
73
- const exercises = Object.values(map).map((ex, i) => ({
74
- ...ex,
75
- position: i,
76
- }))
77
-
78
- return {
79
- config: { ...learnJson },
80
- exercises,
81
- }
82
- }
1
+ import { Bucket, File } from "@google-cloud/storage"
2
+
3
+ type TFile = {
4
+ name: string;
5
+ slug: string;
6
+ hidden: boolean;
7
+ };
8
+
9
+ export type Exercise = {
10
+ title: string;
11
+ slug: string;
12
+ graded: boolean;
13
+ files: TFile[];
14
+ translations: Record<string, string>;
15
+ position: number;
16
+ };
17
+
18
+ export type ConfigResponse = {
19
+ config: any;
20
+ exercises: Exercise[];
21
+ };
22
+
23
+ function naturalCompare(a: string, b: string): number {
24
+ // Split by dots and hyphens, compare numbers as numbers
25
+ const regex = /(\d+|\D+)/g
26
+ const ax = a.match(regex)!
27
+ const bx = b.match(regex)!
28
+ for (let i = 0; i < Math.max(ax.length, bx.length); i++) {
29
+ const an = parseInt(ax[i], 10)
30
+ const bn = parseInt(bx[i], 10)
31
+ if (!isNaN(an) && !isNaN(bn)) {
32
+ if (an !== bn) return an - bn
33
+ } else if (ax[i] !== bx[i]) return (ax[i] || "").localeCompare(bx[i] || "")
34
+ }
35
+
36
+ return 0
37
+ }
38
+
39
+ /**
40
+ * Crea la configuración y lista de ejercicios para un curso.
41
+ *
42
+ * @param bucket - Instancia de GCS Bucket donde está el curso.
43
+ * @param courseSlug - Slug del curso a procesar.
44
+ * @returns Promise con objeto { config, exercises } listo para usar.
45
+ */
46
+ export async function buildConfig(
47
+ bucket: Bucket,
48
+ courseSlug: string
49
+ ): Promise<ConfigResponse> {
50
+ const prefix = `courses/${courseSlug}/`
51
+ const [files] = await bucket.getFiles({ prefix })
52
+
53
+ // 1) Leer learn.json
54
+ const learnFile = files.find(f => f.name.endsWith("learn.json"))!
55
+ const [learnBuf] = await learnFile.download()
56
+ const learnJson = JSON.parse(learnBuf.toString())
57
+
58
+ // 2) Agrupar ejercicios
59
+ const map: Record<string, Omit<Exercise, "position">> = {}
60
+ for (const file of files) {
61
+ const parts = file.name.split("/")
62
+ if (!parts.includes("exercises")) continue
63
+
64
+ const slug = parts[parts.indexOf("exercises") + 1]
65
+ if (!map[slug]) {
66
+ map[slug] = {
67
+ title: slug,
68
+ slug,
69
+ graded: false,
70
+ files: [],
71
+ translations: {},
72
+ }
73
+ }
74
+
75
+ const fname = parts.pop()!
76
+ const m = fname.match(/^readme(?:\.([a-z]{2}))?\.md$/i)
77
+ if (m) {
78
+ const lang = m[1] || "en"
79
+ map[slug].translations[lang] = fname
80
+ } else {
81
+ map[slug].files.push({
82
+ name: fname,
83
+ slug: fname,
84
+ hidden: false,
85
+ })
86
+ }
87
+ }
88
+
89
+ const exercises = Object.values(map)
90
+ .sort((a, b) => naturalCompare(a.slug, b.slug))
91
+ .map((ex, i) => ({
92
+ ...ex,
93
+ position: i,
94
+ }))
95
+
96
+ return {
97
+ config: { ...learnJson },
98
+ exercises,
99
+ }
100
+ }
@@ -1,3 +1,4 @@
1
1
  export { exportToScorm } from "./scorm";
2
2
  export { exportToEpub } from "./epub";
3
+ export { exportToZip } from "./zip";
3
4
  export { ExportFormat, ExportOptions, EpubMetadata } from "./types";
@@ -1,4 +1,4 @@
1
- export type ExportFormat = "scorm" | "epub";
1
+ export type ExportFormat = "scorm" | "epub" | "zip";
2
2
 
3
3
  export interface ExportOptions {
4
4
  courseSlug: string;
@@ -0,0 +1,55 @@
1
+ import * as path from "path";
2
+ import * as fs from "fs";
3
+ import * as archiver from "archiver";
4
+ import * as mkdirp from "mkdirp";
5
+ import * as rimraf from "rimraf";
6
+ import { v4 as uuidv4 } from "uuid";
7
+ import { ExportOptions } from "./types";
8
+ import { downloadS3Folder, getCourseMetadata } from "./shared";
9
+
10
+ export async function exportToZip(options: ExportOptions): Promise<string> {
11
+ const { courseSlug, bucket, outDir } = options;
12
+
13
+ // 1. Create temporary folder
14
+ const tmpName = uuidv4();
15
+ const zipOutDir = path.join(outDir, tmpName);
16
+ rimraf.sync(zipOutDir);
17
+ mkdirp.sync(zipOutDir);
18
+
19
+ // 2. Download all course files from bucket to temporary directory
20
+ await downloadS3Folder(bucket, `courses/${courseSlug}/`, zipOutDir);
21
+
22
+ // 3. Create ZIP file
23
+ const zipName = `${courseSlug}.zip`;
24
+ const zipPath = path.join(outDir, zipName);
25
+
26
+ // Remove existing zip file if it exists
27
+ if (fs.existsSync(zipPath)) {
28
+ fs.unlinkSync(zipPath);
29
+ }
30
+
31
+ const output = fs.createWriteStream(zipPath);
32
+ const archive = archiver("zip", { zlib: { level: 9 } });
33
+
34
+ return new Promise((resolve, reject) => {
35
+ output.on("close", () => {
36
+ console.log(`✅ ZIP export completed: ${archive.pointer()} total bytes`);
37
+ // Clean up temporary directory
38
+ rimraf.sync(zipOutDir);
39
+ resolve(zipPath);
40
+ });
41
+
42
+ archive.on("error", (err) => {
43
+ console.error("❌ ZIP creation error:", err);
44
+ rimraf.sync(zipOutDir);
45
+ reject(err);
46
+ });
47
+
48
+ archive.pipe(output);
49
+
50
+ // Add all files from the temporary directory to the ZIP
51
+ archive.directory(zipOutDir, false);
52
+
53
+ archive.finalize();
54
+ });
55
+ }
@@ -1,47 +0,0 @@
1
- import React, { useEffect } from 'react';
2
- import { RIGOBOT_HOST } from '../utils/constants';
3
-
4
- interface PassphraseValidatorProps {
5
- onSuccess?: (response: any) => void;
6
- onError?: (error: any) => void;
7
- }
8
-
9
- const PassphraseValidator: React.FC<PassphraseValidatorProps> = ({
10
- onSuccess,
11
- onError
12
- }) => {
13
- useEffect(() => {
14
- const validatePassphrase = async () => {
15
- try {
16
- const response = await fetch(`${RIGOBOT_HOST}/v1/auth/public/token`, {
17
- method: 'POST',
18
- headers: {
19
- 'Content-Type': 'application/json',
20
- },
21
- body: JSON.stringify({
22
- passphrase: 'bm29vnv4h43n'
23
- })
24
- });
25
-
26
- const data = await response.json();
27
-
28
- if (response.ok) {
29
- onSuccess?.(data);
30
- } else {
31
- onError?.(data);
32
- }
33
- } catch (error) {
34
- onError?.(error);
35
- }
36
- };
37
-
38
- validatePassphrase();
39
- }, [onSuccess, onError]);
40
-
41
- return (
42
- <div>
43
- </div>
44
- );
45
- };
46
-
47
- export default PassphraseValidator;