-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add shared t3.json project configuration support #4317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
2
commits into
main
Choose a base branch
from
t3code/add-t3-project-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { APIRoute } from "astro"; | ||
|
|
||
| import { buildT3ProjectFileJsonSchema } from "@t3tools/shared/t3ProjectFile"; | ||
|
|
||
| // Rendered at build time; published at https://t3.codes/schema/t3.json so | ||
| // t3.json files can reference it via "$schema" for editor/LSP support. | ||
| export const GET: APIRoute = () => | ||
| new Response(`${JSON.stringify(buildT3ProjectFileJsonSchema(), null, 2)}\n`, { | ||
| headers: { "Content-Type": "application/json" }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import type { VercelConfig } from "@vercel/config/v1"; | ||
|
|
||
| export const config: VercelConfig = { | ||
| installCommand: "npm install -g vite-plus && vp install --filter '@t3tools/marketing'", | ||
| installCommand: "npm install -g vite-plus && vp install --filter '@t3tools/marketing...'", | ||
| buildCommand: "vp run --filter @t3tools/marketing build", | ||
| outputDirectory: "dist", | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import * as NodeServices from "@effect/platform-node/NodeServices"; | ||
| import { it, describe, expect } from "@effect/vitest"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Path from "effect/Path"; | ||
|
|
||
| import * as T3ProjectFileLoader from "./T3ProjectFileLoader.ts"; | ||
|
|
||
| const TestLayer = Layer.empty.pipe( | ||
| Layer.provideMerge(T3ProjectFileLoader.layer), | ||
| Layer.provideMerge(NodeServices.layer), | ||
| ); | ||
|
|
||
| const makeTempDir = Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| return yield* fileSystem.makeTempDirectoryScoped({ | ||
| prefix: "t3code-project-file-", | ||
| }); | ||
| }); | ||
|
|
||
| const writeProjectFile = Effect.fn("writeProjectFile")(function* (cwd: string, contents: string) { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| yield* fileSystem.writeFileString(path.join(cwd, "t3.json"), contents).pipe(Effect.orDie); | ||
| }); | ||
|
|
||
| it.layer(TestLayer)("T3ProjectFileLoader", (it) => { | ||
| describe("load", () => { | ||
| it.effect("loads and decodes a valid t3.json", () => | ||
| Effect.gen(function* () { | ||
| const loader = yield* T3ProjectFileLoader.T3ProjectFileLoader; | ||
| const cwd = yield* makeTempDir; | ||
| yield* writeProjectFile( | ||
| cwd, | ||
| `{ | ||
| // JSONC is tolerated | ||
| "iconPath": "assets/logo.svg", | ||
| "scripts": [{ "name": "Dev", "command": "pnpm dev" }], | ||
| }`, | ||
| ); | ||
|
|
||
| const loaded = yield* loader.load(cwd); | ||
|
|
||
| expect(Option.isSome(loaded)).toBe(true); | ||
| if (Option.isSome(loaded)) { | ||
| expect(loaded.value.iconPath).toBe("assets/logo.svg"); | ||
| expect(loaded.value.scripts).toEqual([{ name: "Dev", command: "pnpm dev" }]); | ||
| } | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("returns none when t3.json is missing", () => | ||
| Effect.gen(function* () { | ||
| const loader = yield* T3ProjectFileLoader.T3ProjectFileLoader; | ||
| const cwd = yield* makeTempDir; | ||
|
|
||
| const loaded = yield* loader.load(cwd); | ||
|
|
||
| expect(Option.isNone(loaded)).toBe(true); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("returns none for malformed JSON without failing", () => | ||
| Effect.gen(function* () { | ||
| const loader = yield* T3ProjectFileLoader.T3ProjectFileLoader; | ||
| const cwd = yield* makeTempDir; | ||
| yield* writeProjectFile(cwd, "{ not json"); | ||
|
|
||
| const loaded = yield* loader.load(cwd); | ||
|
|
||
| expect(Option.isNone(loaded)).toBe(true); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("returns none for schema-invalid files without failing", () => | ||
| Effect.gen(function* () { | ||
| const loader = yield* T3ProjectFileLoader.T3ProjectFileLoader; | ||
| const cwd = yield* makeTempDir; | ||
| yield* writeProjectFile(cwd, '{ "scripts": [{ "name": "Dev" }] }'); | ||
|
|
||
| const loaded = yield* loader.load(cwd); | ||
|
|
||
| expect(Option.isNone(loaded)).toBe(true); | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * T3ProjectFileLoader - Effect service that loads the checked-in `t3.json` | ||
| * project file from a workspace root. | ||
| * | ||
| * Loading is best-effort: a missing file resolves to `Option.none`, and | ||
| * unreadable or invalid files are logged and treated as absent so callers | ||
| * can fall back to their defaults. | ||
| * | ||
| * @module T3ProjectFileLoader | ||
| */ | ||
| import * as Context from "effect/Context"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Path from "effect/Path"; | ||
| import * as Schema from "effect/Schema"; | ||
|
|
||
| import { T3_PROJECT_FILE_NAME, type T3ProjectFile } from "@t3tools/contracts"; | ||
| import { T3ProjectFileFromJson } from "@t3tools/shared/t3ProjectFile"; | ||
|
|
||
| const decodeT3ProjectFileJson = Schema.decodeEffect(T3ProjectFileFromJson); | ||
|
|
||
| export class T3ProjectFileLoadError extends Schema.TaggedErrorClass<T3ProjectFileLoadError>()( | ||
| "T3ProjectFileLoadError", | ||
| { | ||
| operation: Schema.Literals(["read", "decode"]), | ||
| workspaceRoot: Schema.String, | ||
| filePath: Schema.String, | ||
| cause: Schema.Defect(), | ||
| }, | ||
| ) { | ||
| override get message(): string { | ||
| return `Failed to ${this.operation} ${T3_PROJECT_FILE_NAME} at ${this.filePath}.`; | ||
| } | ||
| } | ||
|
|
||
| /** Service tag for t3.json project file loading. */ | ||
| export class T3ProjectFileLoader extends Context.Service< | ||
| T3ProjectFileLoader, | ||
| { | ||
| /** | ||
| * Load and decode `t3.json` at the workspace root. | ||
| * | ||
| * Never fails: missing, unreadable, or invalid files resolve to | ||
| * `Option.none` (invalid files are logged as warnings). | ||
| */ | ||
| readonly load: (workspaceRoot: string) => Effect.Effect<Option.Option<T3ProjectFile>>; | ||
| } | ||
| >()("t3/project/T3ProjectFileLoader") {} | ||
|
|
||
| const logT3ProjectFileLoadError = (error: T3ProjectFileLoadError) => | ||
| Effect.logWarning(error).pipe( | ||
| Effect.annotateLogs({ | ||
| operation: error.operation, | ||
| workspaceRoot: error.workspaceRoot, | ||
| filePath: error.filePath, | ||
| errorTag: error._tag, | ||
| }), | ||
| ); | ||
|
|
||
| export const make = Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
|
|
||
| const load: T3ProjectFileLoader["Service"]["load"] = Effect.fn("T3ProjectFileLoader.load")( | ||
| function* (workspaceRoot) { | ||
| const filePath = path.join(workspaceRoot, T3_PROJECT_FILE_NAME); | ||
| const raw = yield* fileSystem.readFileString(filePath).pipe( | ||
| Effect.map(Option.some), | ||
| Effect.catchTags({ | ||
| PlatformError: (error) => | ||
| error.reason._tag === "NotFound" | ||
| ? Effect.succeed(Option.none<string>()) | ||
| : logT3ProjectFileLoadError( | ||
| new T3ProjectFileLoadError({ | ||
| operation: "read", | ||
| workspaceRoot, | ||
| filePath, | ||
| cause: error, | ||
| }), | ||
| ).pipe(Effect.as(Option.none<string>())), | ||
| }), | ||
| ); | ||
| if (Option.isNone(raw)) { | ||
| return Option.none<T3ProjectFile>(); | ||
| } | ||
| return yield* decodeT3ProjectFileJson(raw.value).pipe( | ||
| Effect.map(Option.some), | ||
| Effect.catchTags({ | ||
| SchemaError: (error) => | ||
| logT3ProjectFileLoadError( | ||
| new T3ProjectFileLoadError({ | ||
| operation: "decode", | ||
| workspaceRoot, | ||
| filePath, | ||
| cause: error, | ||
| }), | ||
| ).pipe(Effect.as(Option.none<T3ProjectFile>())), | ||
| }), | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| return T3ProjectFileLoader.of({ load }); | ||
| }); | ||
|
|
||
| export const layer = Layer.effect(T3ProjectFileLoader, make); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.