/** * ============================================================ * SVGarden — https://www.svgarden.dev * Author : Simon-Pierre Boucher * Contact: contact@spboucher.ai * File : scripts/new-snippet.mjs * Desc : Interactive scaffolder — creates a compliant snippet file skeleton * ============================================================ */ import { writeFile, mkdir, access } from 'node:fs/promises'; import path from 'node:path'; import readline from 'node:readline/promises'; import { SNIPPETS_DIR, CATEGORIES, DIFFICULTIES, attributionHeader } from '../src/lib/snippets.mjs'; const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const ask = async (q, { def, valid } = {}) => { for (;;) { const raw = (await rl.question(def ? `${q} [${def}]: ` : `${q}: `)).trim(); const answer = raw || def || ''; if (!answer) { console.log(' → required.'); continue; } if (valid && !valid.includes(answer)) { console.log(` → one of: ${valid.join(', ')}`); continue; } return answer; } }; console.log('SVGarden — new snippet scaffolder\n'); const title = await ask('Title (e.g. "Dash spinner")'); const slug = await ask('Slug (kebab-case)', { def: title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') }); const category = await ask('Category', { valid: CATEGORIES, def: 'loaders' }); const difficulty = await ask('Difficulty', { valid: DIFFICULTIES, def: 'beginner' }); const desc = await ask('One-line description'); const tags = (await ask('Tags (comma-separated)', { def: category.replace(/s$/, '') })).split(',').map((t) => t.trim()).filter(Boolean); rl.close(); const relPath = `snippets/${category}/${slug}.html`; const file = path.join(SNIPPETS_DIR, category, `${slug}.html`); try { await access(file); console.error(`✖ ${relPath} already exists.`); process.exit(1); } catch { /* good — does not exist */ } const today = new Date().toISOString().slice(0, 10); const cls = `sg-${slug}`; const content = `${attributionHeader(relPath, desc)}
`; await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, content, 'utf8'); console.log(`✔ created ${relPath} — fill in the TODOs, then run: npm run validate`);