diff --git a/src/tools/gitignore-generator/gitignore-generator.vue b/src/tools/gitignore-generator/gitignore-generator.vue index 32faa40f..9c56e11d 100644 --- a/src/tools/gitignore-generator/gitignore-generator.vue +++ b/src/tools/gitignore-generator/gitignore-generator.vue @@ -3,47 +3,75 @@ import { useI18n } from 'vue-i18n'; const { t } = useI18n(); -const availableTemplates = ref<{ label: string; value: string }[]>([]); -const selectedTemplates = ref([]); -const gitignoreContent = ref(''); +const options = useLocalStorage<{ label: string; value: string }[]>('gitignore-gen:opts', []); +const selected = ref([]); +const output = ref(''); +const error = ref(''); const loading = ref(false); -const error = ref(''); -// Fetch available templates from gitignore.io -async function fetchTemplates() { - try { - const response = await fetch('https://cors-anywhere.com/https://www.toptal.com/developers/gitignore/api/list'); - if (!response.ok) { - throw new Error('Failed to fetch template list'); - } - const text = await response.text(); - availableTemplates.value = text - .split(/[,\n]/) - .map(tpl => ({ label: tpl, value: tpl })) - .sort((a, b) => a.label.localeCompare(b.label)); +// Timestamp cache to allow refresh after X hours +const lastFetched = useLocalStorage('gitignore-gen:ts', 0); +const CACHE_TTL = 1000 * 60 * 60 * 24; // 24h + +async function loadOptions() { + const now = Date.now(); + const isStale = !options.value.length || (now - lastFetched.value > CACHE_TTL); + + if (!isStale) { + // Use cached options + return; } - catch (err: any) { - error.value = err.toString(); + + try { + const res = await fetch('https://api.github.com/repos/github/gitignore/git/trees/main?recursive=true'); + const files = await res.json(); + options.value = files.tree + .filter((f: any) => f.path.endsWith('.gitignore')) + .map((f: any) => f.path.replace('.gitignore', '')) + .filter(Boolean) + .map((name: string) => ({ + label: name, + value: name, + })); + lastFetched.value = now; + } + catch { + if (!options.value?.length) { + options.value = [ + 'C++', + 'CMake', + 'Dotnet', + 'Node', + 'Java', + 'Swift', + 'Symfony', + 'Python', + 'VisualStudio', + 'WordPress', + ].map(name => ({ + label: name, + value: name, + })); + } } } async function generateGitignore() { - if (!selectedTemplates.value.length) { + if (!selected.value.length) { return; } error.value = ''; loading.value = true; try { - const response = await fetch( - `https://cors-anywhere.com/https://www.toptal.com/developers/gitignore/api/${encodeURIComponent( - selectedTemplates.value.join(','), - )}`, - ); - if (!response.ok) { - throw new Error('Failed to fetch gitignore'); + let gitignores = ''; + for (const lang of selected.value) { + const url = `https://raw.githubusercontent.com/github/gitignore/main/${lang}.gitignore`; + const res = await fetch(url); + const text = await res.text(); + gitignores += `${gitignores ? '\n\n' : ''}# === .gitignore for ${lang} (${url}) ===\n\n${text}`; } - gitignoreContent.value = await response.text(); + output.value = gitignores; } catch (err: any) { error.value = err.toString(); @@ -53,25 +81,19 @@ async function generateGitignore() { } } -onMounted(fetchTemplates); +onMounted(loadOptions);