93 lines
2.7 KiB
JavaScript
93 lines
2.7 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import zlib from 'zlib';
|
|
import { fileURLToPath } from 'url';
|
|
import { pipeline } from 'stream/promises';
|
|
|
|
// --- Konfiguration ---
|
|
const GZIP_OPTIONS = { level: zlib.constants.Z_BEST_COMPRESSION };
|
|
// --------------------
|
|
|
|
/**
|
|
* Kopiert und komprimiert nur die index.html Datei von sourceDir nach destDir.
|
|
* Die Datei wird nur als .gz gespeichert.
|
|
*
|
|
* @param {string} sourceDir - Quellordner
|
|
* @param {string} destDir - Zielordner
|
|
*/
|
|
async function processIndexHtml(sourceDir, destDir) {
|
|
console.log(`Processing index.html from '${sourceDir}' to '${destDir}'`);
|
|
|
|
// Erstelle Zielverzeichnis, wenn es nicht existiert
|
|
if (!fs.existsSync(destDir)) {
|
|
fs.mkdirSync(destDir, { recursive: true });
|
|
}
|
|
|
|
const sourcePath = path.join(sourceDir, 'index.html');
|
|
const destPathGz = path.join(destDir, 'index.html.gz');
|
|
|
|
// Prüfe ob index.html existiert
|
|
if (!fs.existsSync(sourcePath)) {
|
|
throw new Error(`index.html not found in '${sourceDir}'`);
|
|
}
|
|
|
|
// Komprimiere index.html
|
|
console.log(`Compressing ${sourcePath} -> ${destPathGz}`);
|
|
try {
|
|
const readStream = fs.createReadStream(sourcePath);
|
|
const gzipStream = zlib.createGzip(GZIP_OPTIONS);
|
|
const writeStream = fs.createWriteStream(destPathGz);
|
|
await pipeline(readStream, gzipStream, writeStream);
|
|
} catch (err) {
|
|
console.error(`Error compressing ${sourcePath}:`, err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Lösche den Ordner und seinen gesamten Inhalt
|
|
*
|
|
* @param {string} dir - Der zu löschende Ordner
|
|
*/
|
|
function removeDir(dir) {
|
|
if (fs.existsSync(dir)) {
|
|
console.log(`Removing existing directory '${dir}'`);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
// Hauptfunktion
|
|
async function main() {
|
|
// Projekt-Root-Verzeichnis ermitteln
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const scriptDir = __dirname;
|
|
const projectRoot = path.resolve(scriptDir, '..');
|
|
const rootDir = path.resolve(projectRoot, '..');
|
|
|
|
// Quell- und Zielverzeichnisse
|
|
const sourceDir = path.join(projectRoot, 'build');
|
|
const destDir = path.join(rootDir, 'data', 'web');
|
|
|
|
if (!fs.existsSync(sourceDir) || !fs.statSync(sourceDir).isDirectory()) {
|
|
console.error(`Error: Source directory '${sourceDir}' doesn't exist or is not a directory!`);
|
|
console.error("Make sure to build the Svelte app first with 'npm run build'");
|
|
process.exit(1);
|
|
}
|
|
|
|
removeDir(destDir);
|
|
|
|
try {
|
|
await processIndexHtml(sourceDir, destDir);
|
|
console.log("index.html processed successfully!");
|
|
} catch(err) {
|
|
console.error("Error during file processing:", err);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Ausführen des Skripts
|
|
main(); |