1import { T3_PROJECT_FILE_NAME, type EnvironmentId, type T3ProjectFile } from "@t3tools/contracts";
2import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
3import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
4
5import {
6 getProjectFileQueryAtom,
7 resolveProjectFileQueryData,
8} from "~/components/files/projectFilesQueryState";
9import { appAtomRegistry } from "~/rpc/atomRegistry";
10
11export async function readT3ProjectFile(
12 environmentId: EnvironmentId,
13 workspaceRoot: string,
14): Promise<T3ProjectFile | null> {
15 const result = await executeAtomQuery(
16 appAtomRegistry,
17 getProjectFileQueryAtom(environmentId, workspaceRoot, T3_PROJECT_FILE_NAME),
18 { reportDefect: false, reportFailure: false },
19 );
20 const data = resolveProjectFileQueryData(
21 environmentId,
22 workspaceRoot,
23 T3_PROJECT_FILE_NAME,
24 result._tag === "Success" ? result.value : null,
25 );
26 if (data === null || data.exists === false || data.truncated) return null;
27 return parseT3ProjectFile(data.contents);
28}