import { existsSync, readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname, join } from 'node:path'; import { z } from 'zod'; import type { CheckCategory } from '../shared/types.js'; const categorySchema = z.array(z.string()); const repoSchema = z.object({ name: z.string().regex(/^[\w.-]+\/[\w.-]+$/), checkCategories: z .object({ lint: categorySchema.optional(), specs: categorySchema.optional(), e2e: categorySchema.optional(), }) .default({}), }); const configSchema = z.object({ user: z.string(), onlyInvolved: z.boolean().default(true), pollIntervalSeconds: z.number().int().min(15).default(60), port: z.number().int().default(4000), repos: z.array(repoSchema).min(1), }); export type RawConfig = z.infer; interface GlobMatcher { isMatch: (name: string) => boolean; category: CheckCategory; } export interface RepoRules { exact: Map; globs: GlobMatcher[]; } export interface AppConfig extends RawConfig { rulesByRepo: Map; searchQuery: string; } const GLOB_CHARS = /[*?]/; /** Check names are plain strings, not paths — `*` must match anything including `/` * (e.g. "Preview E2E *" → "Preview E2E e2e/regression/srm"), and `${{ }}` in names * must stay literal. So we compile a minimal glob ourselves instead of picomatch. */ function compileNameGlob(pattern: string): (name: string) => boolean { const regex = new RegExp( `^${pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.')}$`, ); return (name) => regex.test(name); } /** Project root: walk up from this module (which may live in server/ or * dist-server/server/) until config.json appears. */ function findRoot(): string { let dir = dirname(fileURLToPath(import.meta.url)); for (let i = 0; i < 4; i++) { dir = dirname(dir); if (existsSync(join(dir, 'config.json'))) return dir; } return process.cwd(); } export const rootDir = findRoot(); export function loadConfig(path = join(rootDir, 'config.json')): AppConfig { const raw = configSchema.parse(JSON.parse(readFileSync(path, 'utf8'))); const rulesByRepo = new Map(); for (const repo of raw.repos) { const exact = new Map(); const globs: GlobMatcher[] = []; for (const [category, patterns] of Object.entries(repo.checkCategories) as [ CheckCategory, string[], ][]) { for (const pattern of patterns ?? []) { if (GLOB_CHARS.test(pattern)) { globs.push({ isMatch: compileNameGlob(pattern), category }); } else { exact.set(pattern, category); } } } rulesByRepo.set(repo.name, { exact, globs }); } const parts = ['is:pr', 'is:open', ...raw.repos.map((r) => `repo:${r.name}`)]; if (raw.onlyInvolved) parts.push(`involves:${raw.user}`); return { ...raw, rulesByRepo, searchQuery: parts.join(' ') }; }