import { access, readdir, readFile } from "node:fs/promises"; import { join, relative } from "node:path"; import { detectFramework } from "./framework.js"; import { detectIntegrations } from "./integrations.js"; import { detectPackageManager } from "./package-manager.js"; import { detectProvider } from "./provider.js"; import { collectEnvNames } from "../collectors/env-names.js"; import { collectSourceEnvRefs } from "../collectors/source-env-refs.js"; import type { Observation, ProjectSnapshot } from "../model/observation.js"; const IGNORED = new Set([".git", "node_modules", "dist", "build", ".next", ".turbo", "coverage"]); async function walk(root: string, current = root, output: string[] = []): Promise { const entries = await readdir(current, { withFileTypes: true }); for (const entry of entries) { if (entry.name.startsWith(".") && !/^\.env(?:\..+)?(?:\.example)?$/.test(entry.name) && ![".nvmrc", ".node-version", ".npmrc"].includes(entry.name)) continue; if (entry.isDirectory() && !IGNORED.has(entry.name)) { await walk(root, join(current, entry.name), output); } else if (entry.isFile()) { output.push(relative(root, join(current, entry.name)).replaceAll("\\", "/")); } } return output; } export async function detectProject(root: string): Promise { const files = await walk(root); const fileSet = new Set(files); let packageJson: { dependencies?: Record; devDependencies?: Record } | undefined; try { packageJson = JSON.parse(await readFile(join(root, "package.json"), "utf8")) as typeof packageJson; } catch { packageJson = undefined; } const framework = detectFramework(packageJson, fileSet); const packageManager = detectPackageManager(fileSet); const provider = detectProvider(fileSet, packageJson); const integrations = detectIntegrations(packageJson); const envNames = await collectEnvNames(root, files); const sourceEnvRefs = await collectSourceEnvRefs(root, files); const observations: Observation[] = [framework, packageManager, provider, ...integrations, ...envNames, ...sourceEnvRefs]; // `access` makes the root validation explicit without exposing any content. await access(root); return { root, files, packageJson, framework: framework.value, packageManager: packageManager.value as ProjectSnapshot["packageManager"], provider: provider.value, integrations: integrations.map((item) => item.value), observations, }; }