diff --git a/.gitignore b/.gitignore index 2309cc8..81a8e8a 100644 --- a/.gitignore +++ b/.gitignore @@ -136,3 +136,6 @@ dist .yarn/install-state.gz .pnp.* +# Zip output +.zip/ + diff --git a/zip-esercizi.js b/zip-esercizi.js new file mode 100644 index 0000000..bd16249 --- /dev/null +++ b/zip-esercizi.js @@ -0,0 +1,87 @@ +const fs = require("fs"); +const path = require("path"); +const { execFileSync } = require("child_process"); + +const baseDir = process.argv[2] + ? path.resolve(process.argv[2]) + : path.join(__dirname, "javascript"); +const outputDir = path.join(__dirname, ".zip"); + +function ensureDir(dirPath) { + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath, { recursive: true }); + } +} + +function psQuote(value) { + return `'${String(value).replace(/'/g, "''")}'`; +} + +function sanitizeFilename(name) { + return name.replace(/[<>:"/\\|?*]/g, "-").trim(); +} + +function splitName(dirName) { + let number = ""; + let desc = dirName; + + const underscoreIndex = dirName.indexOf("_"); + if (underscoreIndex !== -1) { + number = dirName.slice(0, underscoreIndex).trim(); + desc = dirName.slice(underscoreIndex + 1); + } else { + const match = dirName.match(/^(\d+)\s+(.+)$/); + if (match) { + number = match[1]; + desc = match[2]; + } + } + + desc = desc.replace(/_/g, " ").replace(/\s+/g, " ").trim(); + if (!number) { + number = "00"; + } + + return { number, desc }; +} + +function createZipForDir(dirPath) { + const dirName = path.basename(dirPath); + const { number, desc } = splitName(dirName); + const zipName = sanitizeFilename(`JS_Esercizi ${number} - ${desc}.zip`); + const zipPath = path.join(outputDir, zipName); + const sourceGlob = path.join(dirPath, "*"); + + const psCommand = [ + "$ErrorActionPreference = 'Stop'", + `if (Test-Path -LiteralPath ${psQuote(zipPath)}) { Remove-Item -LiteralPath ${psQuote(zipPath)} -Force }`, + `Compress-Archive -Path ${psQuote(sourceGlob)} -DestinationPath ${psQuote(zipPath)} -Force` + ].join("; "); + + execFileSync("powershell", ["-NoProfile", "-Command", psCommand], { + stdio: "inherit" + }); + + console.log(`Creato: ${zipName}`); +} + +function main() { + if (!fs.existsSync(baseDir)) { + throw new Error(`Cartella non trovata: ${baseDir}`); + } + + ensureDir(outputDir); + + const entries = fs.readdirSync(baseDir, { withFileTypes: true }) + .filter((entry) => entry.isDirectory()) + .map((entry) => path.join(baseDir, entry.name)); + + for (const dirPath of entries) { + if (path.basename(dirPath).toLowerCase() === ".zip") { + continue; + } + createZipForDir(dirPath); + } +} + +main();