77 lignes
2.4 KiB
JavaScript
77 lignes
2.4 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* tools/apply-selection.js
|
|
*
|
|
* Synchronise la galerie de index.html avec les photos retenues dans
|
|
* C:\Users\theog\Desktop\Selection_Photos_Portfolio
|
|
*
|
|
* Usage :
|
|
* node tools/apply-selection.js
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const ROOT_DIR = path.resolve(__dirname, '..');
|
|
const INDEX_HTML = path.join(ROOT_DIR, 'index.html');
|
|
const DESKTOP_DIR = 'C:\\Users\\theog\\Desktop\\Selection_Photos_Portfolio';
|
|
|
|
if (!fs.existsSync(DESKTOP_DIR)) {
|
|
console.error(`[ERREUR] Le dossier de sélection n'existe pas : ${DESKTOP_DIR}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const keptFiles = new Set(
|
|
fs.readdirSync(DESKTOP_DIR)
|
|
.filter(f => !f.startsWith('.') && fs.statSync(path.join(DESKTOP_DIR, f)).isFile())
|
|
);
|
|
|
|
console.log(`[SELECTION] ${keptFiles.size} fichier(s) détecté(s) dans le dossier de sélection.`);
|
|
|
|
if (keptFiles.size === 0) {
|
|
console.error('[ERREUR] Aucun fichier trouvé dans le dossier de sélection.');
|
|
process.exit(1);
|
|
}
|
|
|
|
let html = fs.readFileSync(INDEX_HTML, 'utf8');
|
|
|
|
// Extraction des éléments .gallery-item
|
|
const itemRegex = /<div class="gallery-item[^"]*">[\s\S]*?<\/div>/g;
|
|
const rootMatch = html.match(/(<div class="grid-layout" id="gallery-root">)([\s\S]*?)(<\/div>\s*<div class="cloud-wrapper")/);
|
|
|
|
if (!rootMatch) {
|
|
console.error('[ERREUR] Impossible de cibler #gallery-root dans index.html');
|
|
process.exit(1);
|
|
}
|
|
|
|
const prefix = rootMatch[1];
|
|
const galleryBody = rootMatch[2];
|
|
const suffix = rootMatch[3];
|
|
|
|
const items = [];
|
|
let itemMatch;
|
|
while ((itemMatch = itemRegex.exec(galleryBody)) !== null) {
|
|
const itemHtml = itemMatch[0];
|
|
const srcM = itemHtml.match(/src="assets\/img\/([^"]+)"/);
|
|
if (srcM) {
|
|
const filename = srcM[1];
|
|
items.push({ filename, html: itemHtml });
|
|
}
|
|
}
|
|
|
|
console.log(`[ANALYSE] ${items.length} éléments galerie trouvés dans le code.`);
|
|
|
|
const retained = items.filter(it => keptFiles.has(it.filename));
|
|
console.log(`[FILTRE] ${retained.length} éléments correspondent aux fichiers conservés.`);
|
|
|
|
if (retained.length === 0) {
|
|
console.error('[ERREUR] Aucune correspondance trouvée entre le dossier et index.html.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const newGalleryBody = '\n ' + retained.map(it => it.html).join('\n ') + '\n ';
|
|
const updatedHtml = html.replace(rootMatch[0], prefix + newGalleryBody + suffix);
|
|
|
|
fs.writeFileSync(INDEX_HTML, updatedHtml, 'utf8');
|
|
console.log(`[SUCCÈS] index.html mis à jour avec la sélection resserrée (${retained.length} clichés).`);
|