UNPKG

@tricoteuses/senat

Version:

Handle French Sénat's open data

36 lines (35 loc) 1.16 kB
import fs from "fs-extra"; import path from "path"; export function isOptionEmptyOrHasValue(option, value) { return !option || option.length === 0 || option.includes(value); } export function ensureAndClearDirSync(dir) { fs.ensureDirSync(dir); for (const name of fs.readdirSync(dir)) { fs.rmSync(path.join(dir, name), { recursive: true, force: true }); } } export function ensureAndClearDir(path) { if (!fs.existsSync(path)) { fs.mkdirSync(path, { recursive: true }); } else { fs.emptyDirSync(path); } } export async function fetchWithRetry(url, retries = 3, backoff = 300) { for (let attempt = 0; attempt < retries; attempt++) { try { return await fetch(url); } catch (error) { if (attempt === retries - 1) { throw error; } console.warn(`Fetch attempt ${attempt + 1} for ${url} failed. Retrying in ${backoff}ms…`); await new Promise((resolve) => setTimeout(resolve, backoff)); backoff *= 2; } } throw new Error(`Failed to fetch ${url} after ${retries} attempts`); }