update admin script

This commit is contained in:
ozakione 2024-04-20 18:27:04 +02:00
parent 77aaf4a103
commit 34a0e3fc0e

View file

@ -7,31 +7,37 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import yaml from 'js-yaml'; import yaml from 'js-yaml';
import axios from 'axios';
import {load} from 'cheerio'; import {load} from 'cheerio';
// TODO remove axois
// TODO recursive yml search in showcase folder
// TODO filter out tags.yml
async function processYamlFiles(directoryPath) { async function processYamlFiles(directoryPath) {
try { try {
const files = await fs.readdir(directoryPath); const files = await fs.promises.readdir(directoryPath);
for (const file of files) { for (const file of files) {
const filePath = path.join(directoryPath, file); const filePath = path.join(directoryPath, file);
const data = await fs.readFile(filePath, 'utf8'); const stat = await fs.promises.stat(filePath);
if (stat.isDirectory()) {
await processYamlFiles(filePath); // Recursive call for subdirectory
} else {
const data = await fs.promises.readFile(filePath, 'utf8');
const yamlData = yaml.load(data); const yamlData = yaml.load(data);
const websiteUrl = yamlData.website; const websiteUrl = yamlData.website;
try { try {
const response = await axios.get(websiteUrl, { const response = await fetch(websiteUrl, {
headers: { headers: {
'Accept-Encoding': 'gzip, deflate', 'Accept-Encoding': 'gzip, deflate',
}, },
}); });
const html = response.data; if (!response.ok) {
throw new Error(
`Failed to fetch ${websiteUrl}: ${response.statusText}`,
);
}
const html = await response.text();
const $ = load(html); const $ = load(html);
const generatorMeta = $('meta[name="generator"]'); const generatorMeta = $('meta[name="generator"]');
@ -42,9 +48,11 @@ async function processYamlFiles(directoryPath) {
console.error(`Error fetching website ${websiteUrl}:`, error.message); console.error(`Error fetching website ${websiteUrl}:`, error.message);
} }
} }
}
} catch (err) { } catch (err) {
console.error('Error reading directory:', err); console.error('Error reading directory:', err);
} }
} }
// processYamlFiles('./admin/scripts/showcase');
processYamlFiles('../../website/showcase'); processYamlFiles('../../website/showcase');