import { dirname, join, normalize } from 'node:path' import type { DoctorContext } from '../project.js' import type { DoctorRule, RuleFinding } from '../types.js' import { stripComments } from '../util.js' /** The seed entry file and the project files it imports (relative imports, two levels deep). */ function seedFiles(ctx: DoctorContext, entry: string): string[] { const seen = new Set() const visit = (file: string, depth: number) => { if (seen.has(file) || !ctx.exists(file)) {return} seen.add(file) if (depth >= 2) {return} for (const m of ctx.read(file).matchAll(/from\s+['"](\.{1,2}\/[^'"]+)['"]/g)) { const base = normalize(join(dirname(file), m[1])).replace(/\.js$/, '') const hit = ['.ts', '.tsx', '.mjs', '.js', '/index.ts'].map((ext) => base + ext).find((f) => ctx.exists(f)) if (hit) {visit(hit, depth + 1)} } } visit(entry, 0) return [...seen] } const DELETES = /payload\s*\.\s*delete\s*\(|\.deleteMany\s*\(|dropDatabase|SEED_FRESH|--fresh\b/ export const seedRules: DoctorRule[] = [ { id: 'seed-named', check: (ctx) => { const out: RuleFinding[] = [] for (const [name, command] of Object.entries(ctx.pkg.scripts ?? {})) { if (!/seed/i.test(name)) {continue} if (/fresh|reset/i.test(name)) {out.push({ file: 'package.json', fix: 'remove it — seeds never delete (seeding.md)', message: `script "${name}" re-creates content from scratch`, severity: 'error' })} if (/IAPL_SEED_ALLOW_FULL=1/.test(command)) {out.push({ file: 'package.json', fix: 'set IAPL_SEED_ALLOW_FULL=1 by hand for one run, only with the owner\'s OK', message: `script "${name}" switches the full-seed guard off`, severity: 'error' })} const entry = command.match(/(?:^|\s)([\w./@-]+\.[cm]?[jt]s)(?:\s|$)/)?.[1]?.replace(/^\.\//, '') if (!entry) {continue} if (!ctx.exists(entry)) {out.push({ file: 'package.json', message: `script "${name}" runs ${entry}, which does not exist`, severity: 'warning' }); continue} const files = seedFiles(ctx, entry) const code = files.map((f) => stripComments(ctx.read(f))).join('\n') if (!/createSeeder\s*\(/.test(code)) {out.push({ file: entry, fix: "createSeeder(payload) from '@intecion/ipal-kit/seed' (only / SEED_ONLY, full-seed guard)", message: `script "${name}" does not use the IAPL seed helper`, severity: 'warning' })} const deleting = files.filter((f) => DELETES.test(stripComments(ctx.read(f)))) if (deleting.length) {out.push({ file: deleting[0], fix: 'seeds never delete — an editor removes records in the panel (seeding.md)', message: `script "${name}" deletes records (${deleting.join(', ')})`, severity: 'error' })} if (/guard\s*:\s*false|allowFull\s*:\s*true/.test(code)) {out.push({ file: entry, fix: 'keep the guard; IAPL_SEED_ALLOW_FULL=1 for one run with the owner\'s OK', message: `script "${name}" turns the full-seed guard off in code`, severity: 'error' })} if (ctx.launched && /createSeeder\s*\(/.test(code) && !/\bonly\s*:/.test(code)) {out.push({ file: entry, fix: 'run it with SEED_ONLY="pages:,global:" or pass only: [...] (seeding.md → after launch)', message: `script "${name}" has no record list — after launch every run needs SEED_ONLY`, severity: 'warning' })} } for (const file of ['.env.example', '.env.production']) { if (/^\s*IAPL_SEED_ALLOW_FULL\s*=\s*1/m.test(ctx.read(file))) {out.push({ file, message: `${file} sets IAPL_SEED_ALLOW_FULL=1`, severity: 'error' })} } return out }, docs: 'docs/seeding.md', group: 'seeds', source: 'static', }, ]