Replaced by an explicit list of forms only (ten exported names, runtime env names, project file names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are not touched. package.json keeps the five old bin names beside the five new ones. The old names stop working in this commit and come back as aliases in the next; docs follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
57 lines
3.6 KiB
TypeScript
57 lines
3.6 KiB
TypeScript
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<string>()
|
|
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:<slug>,global:<slug>" 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',
|
|
},
|
|
]
|