58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
|
|
/**
|
||
|
|
* Construir paquete SCORM (ZIP)
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { readFile, readdir, stat } from 'fs/promises';
|
||
|
|
import { join, relative } from 'path';
|
||
|
|
import JSZip from 'jszip';
|
||
|
|
import { Guide } from '../../src/data/guides-index.js';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Construir paquete ZIP SCORM
|
||
|
|
*/
|
||
|
|
export async function buildScormPackage(
|
||
|
|
guide: Guide,
|
||
|
|
packageDir: string,
|
||
|
|
outputPath: string
|
||
|
|
): Promise<void> {
|
||
|
|
const zip = new JSZip();
|
||
|
|
|
||
|
|
// Leer todos los archivos del paquete
|
||
|
|
await addDirectoryToZip(zip, packageDir, '');
|
||
|
|
|
||
|
|
// Generar ZIP
|
||
|
|
const buffer = await zip.generateAsync({
|
||
|
|
type: 'nodebuffer',
|
||
|
|
compression: 'DEFLATE',
|
||
|
|
compressionOptions: { level: 9 }
|
||
|
|
});
|
||
|
|
|
||
|
|
// Guardar ZIP
|
||
|
|
const fs = await import('fs/promises');
|
||
|
|
await fs.writeFile(outputPath, buffer);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Añadir directorio recursivamente al ZIP
|
||
|
|
*/
|
||
|
|
async function addDirectoryToZip(
|
||
|
|
zip: JSZip,
|
||
|
|
dirPath: string,
|
||
|
|
zipPath: string
|
||
|
|
): Promise<void> {
|
||
|
|
const entries = await readdir(dirPath, { withFileTypes: true });
|
||
|
|
|
||
|
|
for (const entry of entries) {
|
||
|
|
const fullPath = join(dirPath, entry.name);
|
||
|
|
const relativePath = zipPath ? `${zipPath}/${entry.name}` : entry.name;
|
||
|
|
|
||
|
|
if (entry.isDirectory()) {
|
||
|
|
await addDirectoryToZip(zip, fullPath, relativePath);
|
||
|
|
} else {
|
||
|
|
const content = await readFile(fullPath);
|
||
|
|
zip.file(relativePath, content);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|