Open History to see the last commit
1import {
2 T3_PROJECT_FILE_NAME,
3 type EnvironmentId,
4 type ProjectReadFileResult,
5 type T3ProjectFile,
6} from "@t3tools/contracts";
7import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
8import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
9import { AsyncResult } from "effect/reactivity";
10
11import {
12 getProjectFileQueryAtom,
13 resolveProjectFileQueryData,
14} from "~/components/files/projectFilesQueryState";
15import { appAtomRegistry } from "~/rpc/atomRegistry";
16
17function toT3ProjectFile(
18 environmentId: EnvironmentId,
19 workspaceRoot: string,
20 result: ProjectReadFileResult | null,
21): T3ProjectFile | null {
22 const data = resolveProjectFileQueryData(
23 environmentId,
24 workspaceRoot,
25 T3_PROJECT_FILE_NAME,
26 result,
27 );
28 if (data === null || data.exists === false || data.truncated) return null;
29 return parseT3ProjectFile(data.contents);
30}
31
32export async function readT3ProjectFile(
33 environmentId: EnvironmentId,
34 workspaceRoot: string,
35): Promise<T3ProjectFile | null> {
36 const result = await executeAtomQuery(
37 appAtomRegistry,
38 getProjectFileQueryAtom(environmentId, workspaceRoot, T3_PROJECT_FILE_NAME),
39 { reportDefect: false, reportFailure: false },
40 );
41 return toT3ProjectFile(
42 environmentId,
43 workspaceRoot,
44 result._tag === "Success" ? result.value : null,
45 );
46}
47
48/** The project's t3.json from an earlier read, or `undefined` when none has loaded yet. */
49export function peekT3ProjectFile(
50 environmentId: EnvironmentId,
51 workspaceRoot: string,
52): T3ProjectFile | null | undefined {
53 const result = appAtomRegistry.get(
54 getProjectFileQueryAtom(environmentId, workspaceRoot, T3_PROJECT_FILE_NAME),
55 );
56 return AsyncResult.isSuccess(result)
57 ? toT3ProjectFile(environmentId, workspaceRoot, result.value)
58 : undefined;
59}