Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
One-time setup that gathers your project's design context and saves it to CLAUDE.md for future sessions.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/is-generated.mjs
1/**2* Decide whether a given file is "generated" (regenerated by a build step,3* unsafe to write variants into) or "source" (safe to edit, changes persist).4*5* Why this matters: when the user picks an element on a page whose underlying6* file is regenerated by a build step (e.g. `scripts/build-sub-pages.js`7* rewriting `public/docs/*.html`), writing variants or accepted changes into8* that file is silent data loss — the next build wipes them.9*10* Signals, in order of reliability:11* 1. Git check-ignore: gitignored files are assumed generated.12* 2. File-header markers ("GENERATED", "DO NOT EDIT", "AUTO-GENERATED")13* within the first ~300 characters — catches non-git projects.14*/1516import { execSync } from 'node:child_process';17import fs from 'node:fs';18import path from 'node:path';1920const HEADER_SCAN_BYTES = 300;21const HEADER_MARKERS = [22/@generated\b/i,23/\bGENERATED\s+FILE\b/,24/\bAUTO-?GENERATED\b/i,25/\bDO\s+NOT\s+EDIT\b/i,26];2728/**29* @param {string} filePath - absolute or cwd-relative path30* @param {object} [options]31* @param {string} [options.cwd] - project root (defaults to process.cwd())32*/33export function isGeneratedFile(filePath, options = {}) {34const cwd = options.cwd || process.cwd();35const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);3637if (isGitIgnored(absPath, cwd)) return true;38if (hasGeneratedHeader(absPath)) return true;39return false;40}4142function isGitIgnored(absPath, cwd) {43try {44execSync(`git check-ignore --quiet ${JSON.stringify(absPath)}`, {45cwd,46stdio: 'ignore',47});48return true; // exit 0 = ignored49} catch (err) {50// Exit code 1 = not ignored. Exit code 128 = not a git repo or other error.51// In both cases, treat as "not known to be ignored."52return false;53}54}5556function hasGeneratedHeader(absPath) {57let fd;58try {59fd = fs.openSync(absPath, 'r');60const buf = Buffer.alloc(HEADER_SCAN_BYTES);61const bytesRead = fs.readSync(fd, buf, 0, HEADER_SCAN_BYTES, 0);62const head = buf.slice(0, bytesRead).toString('utf-8');63return HEADER_MARKERS.some((re) => re.test(head));64} catch {65return false;66} finally {67if (fd !== undefined) { try { fs.closeSync(fd); } catch {} }68}69}70