| 1 | const fs = require('fs/promises'); |
| 2 | const path = require('path'); |
| 3 | |
| 4 | |
| 5 | const PUBLIC_DIR = 'public'; |
| 6 | const OUTPUT_FILE = 'lista_archivos_sw.txt'; |
| 7 | |
| 8 | |
| 9 | const EXCLUDED_DIRS = ['.vscode']; |
| 10 | const EXCLUDED_FILES = [ |
| 11 | '.firebaserc', |
| 12 | '.gitignore', |
| 13 | '.htaccess', |
| 14 | '404.html', |
| 15 | 'sw.js', |
| 16 | 'lista_archivos_sw.txt', |
| 17 | 'generar-listado-sw.js', |
| 18 | 'firebase.json', |
| 19 | 'jsconfig.json', |
| 20 | 'LICENSE', |
| 21 | 'README.md', |
| 22 | ]; |
| 23 | const EXCLUDED_EXTENSIONS = ['.php', '.db']; |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | async function getFiles(dir) { |
| 30 | |
| 31 | |
| 32 | |
| 33 | let results = []; |
| 34 | const list = await fs.readdir(dir, { withFileTypes: true }); |
| 35 | |
| 36 | for (const dirent of list) { |
| 37 | const fullPath = path.join(dir, dirent.name); |
| 38 | |
| 39 | if (dirent.isDirectory()) { |
| 40 | |
| 41 | if (!EXCLUDED_DIRS.includes(dirent.name)) { |
| 42 | results = results.concat(await getFiles(fullPath)); |
| 43 | } |
| 44 | } else { |
| 45 | const fileName = dirent.name; |
| 46 | const ext = path.extname(fileName).toLowerCase(); |
| 47 | |
| 48 | |
| 49 | if (!EXCLUDED_FILES.includes(fileName) && !EXCLUDED_EXTENSIONS.includes(ext)) { |
| 50 | |
| 51 | |
| 52 | let relativePath = path.relative(PUBLIC_DIR, fullPath); |
| 53 | |
| 54 | |
| 55 | relativePath = relativePath.split(path.sep).join('/'); |
| 56 | |
| 57 | |
| 58 | results.push(`"${ relativePath }"`); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return results; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | async function generateSWList() { |
| 69 | try { |
| 70 | console.log(`Explorando la carpeta "${ PUBLIC_DIR }"...`); |
| 71 | const files = await getFiles(PUBLIC_DIR); |
| 72 | |
| 73 | |
| 74 | files.push('"/"'); |
| 75 | |
| 76 | |
| 77 | const arrayContent = `const ARCHIVOS = [\n ${ files.join(',\n ') }\n]`; |
| 78 | |
| 79 | |
| 80 | await fs.writeFile(OUTPUT_FILE, arrayContent, 'utf-8'); |
| 81 | |
| 82 | console.log(`¡Éxito! El listado se ha generado correctamente.`); |
| 83 | console.log(`Revisa el archivo "${ OUTPUT_FILE }" y copia el contenido a tu public/sw.js.`); |
| 84 | |
| 85 | } catch (error) { |
| 86 | console.error('Error al generar el listado:', error.message); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | generateSWList(); |