Aggiunge il controllo dei tipi e copre con un test lo script di backup

- npm run typecheck (tsc --noEmit) entra nella checklist di fine lavoro:
  bun esegue i test senza controllare i tipi, ed è così che si erano
  accumulati errori di tipo con la suite verde.
- La logica pura del download dello storage passa in storage-core.ts,
  testata con dati finti (nessuna lettura del .env reale).
- Il download dello storage esce con errore se anche un solo file non
  si scarica, così backup.sh non dichiara completo un backup parziale.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
davideandClaude Opus 5.5 committed 2026-09-24 12:25:35 +02:00
1 parent 431530af2a
commit c6437a605c
10 files changed
+140 -26

No files matched your search

+15 -19
View File
@@ -3,41 +3,33 @@
import { createClient } from "@supabase/supabase-js";
import { mkdir, writeFile, readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { elencaFile, leggiEnv } from "./storage-core.ts";
for (const line of (await readFile(new URL("../.env", import.meta.url), "utf8")).split("\n")) {
const match = /^([\w.]+)=(.*)$/.exec(line.trim());
if (match) process.env[match[1]] ??= match[2].replace(/^["']|["']$/g, "");
}
const env = leggiEnv(await readFile(new URL("../.env", import.meta.url), "utf8"));
for (const [chiave, valore] of Object.entries(env)) process.env[chiave] ??= valore;
const BUCKETS = ["avatar-giocatori", "profili-giocatore"];
const OUT_DIR = process.argv[2] ?? new URL("./storage/", import.meta.url).pathname;
const supabase = createClient(process.env.VITE_SUPABASE_URL, process.env.SUPABASE_SERVICE_ROLE_KEY);
async function listAll(bucket, prefix = "") {
const { data, error } = await supabase.storage.from(bucket).list(prefix, { limit: 1000 });
const elencaCartella = (bucket) => async (prefisso) => {
const { data, error } = await supabase.storage.from(bucket).list(prefisso, { limit: 1000 });
if (error) throw error;
let files = [];
for (const entry of data) {
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
if (entry.id === null) {
// è una "cartella" (nessun id): ricorri
files = files.concat(await listAll(bucket, path));
} else {
files.push(path);
}
}
return files;
}
return data;
};
const falliti = [];
for (const bucket of BUCKETS) {
console.log(`Bucket: ${bucket}`);
const files = await listAll(bucket);
const files = await elencaFile(elencaCartella(bucket));
console.log(` ${files.length} file trovati`);
for (const path of files) {
const { data, error } = await supabase.storage.from(bucket).download(path);
if (error) {
console.error(` ERRORE ${path}:`, error.message);
falliti.push(`${bucket}/${path}`);
continue;
}
const destPath = join(OUT_DIR, bucket, path);
@@ -46,4 +38,8 @@ for (const bucket of BUCKETS) {
console.log(` scaricato: ${bucket}/${path}`);
}
}
if (falliti.length) {
console.error(`${falliti.length} file non scaricati: il backup storage è incompleto.`);
process.exit(1);
}
console.log("Fatto.");