UNPKG

@typespec/compiler

Version:

TypeSpec compiler and standard library

83 lines 3.7 kB
import { loadTypeSpecConfigForPath } from "../config/config-loader.js"; import { formatDiagnostic } from "../core/logger/console-sink.js"; import { getDirectoryPath, joinPaths } from "../core/path-utils.js"; import { doIO, loadFile } from "../utils/io.js"; import { resolveTspMain } from "../utils/misc.js"; import { debugLoggers } from "./debug.js"; export async function resolveEntrypointFile(host, entrypoints, path, fileSystemCache, log) { const options = { allowFileNotFound: true }; const debug = debugLoggers.compileConfig; const logDebug = debug.enabled ? log : () => { }; const pathStat = await doIO(() => host.stat(path), path, logMainFileSearchDiagnostic, options); const isFilePath = pathStat?.isFile() ?? false; let dir = isFilePath ? getDirectoryPath(path) : path; if (!entrypoints) { entrypoints = ["main.tsp"]; } while (true) { // Check for project tspconfig first (highest priority) const config = await loadTypeSpecConfigForPath(host, dir, false, false); if (config.kind === "project") { const entrypoint = config.entrypoint ?? "main.tsp"; const candidate = await existingFile(dir, entrypoint); logDebug({ level: "debug", message: `project tspconfig found in ${dir}, entrypoint: ${entrypoint}`, }); // Project boundary found — stop walking regardless of whether entrypoint exists return candidate ?? (isFilePath ? path : undefined); } let pkg; const pkgPath = joinPaths(dir, "package.json"); const cached = await fileSystemCache?.get(pkgPath); if (cached?.data) { pkg = cached.data; } else { [pkg] = await loadFile(host, pkgPath, JSON.parse, logMainFileSearchDiagnostic, options); await fileSystemCache?.setData(pkgPath, pkg ?? {}); } const tspMain = resolveTspMain(pkg); if (typeof tspMain === "string") { logDebug({ level: "debug", message: `tspMain resolved from package.json (${pkgPath}) as ${tspMain}`, }); const packageJsonEntrypoint = await existingFile(dir, tspMain); if (packageJsonEntrypoint) { logDebug({ level: "debug", message: `entrypoint file found as ${packageJsonEntrypoint}` }); return packageJsonEntrypoint; } } for (const entrypoint of entrypoints) { const candidate = await existingFile(dir, entrypoint); if (candidate) { logDebug({ level: "debug", message: `main file found using client provided entrypoint: ${candidate}`, }); return candidate; } } const parentDir = getDirectoryPath(dir); if (parentDir === dir) { break; } dir = parentDir; } logDebug({ level: "debug", message: `reached directory root, using '${path}' as main file` }); return isFilePath ? path : undefined; function logMainFileSearchDiagnostic(diagnostic) { log({ level: `error`, message: `Unexpected diagnostic while looking for main file of ${path}`, detail: formatDiagnostic(diagnostic), }); } async function existingFile(dir, file) { const candidate = joinPaths(dir, file); const stat = await doIO(() => host.stat(candidate), candidate, logMainFileSearchDiagnostic, options); return stat?.isFile() ? candidate : undefined; } } //# sourceMappingURL=entrypoint-resolver.js.map