-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(connect): suggest a serve command that matches how you ran connect #4897
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
Merged
+160
−4
Merged
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,74 @@ | ||
| import { assert, it } from "@effect/vitest"; | ||
|
|
||
| import { detectCliRunner, formatCliCommand, suggestedPackageSpec } from "./invocation.ts"; | ||
|
|
||
| it("detects package runners from their cache entry paths", () => { | ||
| assert.equal(detectCliRunner("/home/theo/.npm/_npx/abc123/node_modules/t3/dist/bin.mjs"), "npx"); | ||
| assert.equal( | ||
| detectCliRunner( | ||
| "C:\\Users\\theo\\AppData\\Local\\npm-cache\\_npx\\abc\\node_modules\\t3\\dist\\bin.mjs", | ||
| ), | ||
| "npx", | ||
| ); | ||
| assert.equal( | ||
| detectCliRunner("/home/theo/.cache/pnpm/dlx/abc/node_modules/t3/dist/bin.mjs"), | ||
| "pnpm dlx", | ||
| ); | ||
| assert.equal( | ||
| detectCliRunner("/home/theo/.local/share/pnpm/.pnpm/dlx/abc/node_modules/t3/dist/bin.mjs"), | ||
| "pnpm dlx", | ||
| ); | ||
| assert.equal( | ||
| detectCliRunner( | ||
| "C:\\Users\\theo\\AppData\\Local\\pnpm-cache\\dlx\\abc\\node_modules\\t3\\dist\\bin.mjs", | ||
| ), | ||
| "pnpm dlx", | ||
| ); | ||
| assert.equal(detectCliRunner("/home/theo/.bun/install/cache/t3@0.0.31/dist/bin.mjs"), "bunx"); | ||
| assert.equal(detectCliRunner("/tmp/bunx-1000-t3@latest/node_modules/t3/dist/bin.mjs"), "bunx"); | ||
| assert.equal( | ||
| detectCliRunner( | ||
| "C:\\Users\\theo\\AppData\\Local\\Temp\\bunx-0-t3@latest\\node_modules\\t3\\dist\\bin.mjs", | ||
| ), | ||
| "bunx", | ||
| ); | ||
| }); | ||
|
|
||
| it("treats stable installs as direct invocations", () => { | ||
| assert.isNull(detectCliRunner("/usr/local/lib/node_modules/t3/dist/bin.mjs")); | ||
| assert.isNull(detectCliRunner("/home/theo/Code/work/t3code/apps/server/dist/bin.mjs")); | ||
| assert.isNull(detectCliRunner("/home/theo/.t3/runtime/0.0.31/node_modules/t3/dist/bin.mjs")); | ||
| assert.isNull(detectCliRunner("")); | ||
| }); | ||
|
|
||
| it("re-suggests the nightly channel only for nightly builds", () => { | ||
| assert.equal(suggestedPackageSpec("0.0.31-nightly.20260729"), "t3@nightly"); | ||
| assert.equal(suggestedPackageSpec("0.0.31"), "t3"); | ||
| }); | ||
|
|
||
| it("formats serve suggestions to match the launching command", () => { | ||
| assert.equal( | ||
| formatCliCommand({ | ||
| subcommand: "serve", | ||
| entryPath: "/home/theo/.npm/_npx/abc/node_modules/t3/dist/bin.mjs", | ||
| version: "0.0.31-nightly.20260729", | ||
| }), | ||
| "npx t3@nightly serve", | ||
| ); | ||
| assert.equal( | ||
| formatCliCommand({ | ||
| subcommand: "serve", | ||
| entryPath: "/tmp/bunx-1000-t3@latest/node_modules/t3/dist/bin.mjs", | ||
| version: "0.0.31", | ||
| }), | ||
| "bunx t3 serve", | ||
| ); | ||
| assert.equal( | ||
| formatCliCommand({ | ||
| subcommand: "serve", | ||
| entryPath: "/usr/local/lib/node_modules/t3/dist/bin.mjs", | ||
| version: "0.0.31-nightly.20260729", | ||
| }), | ||
| "t3 serve", | ||
| ); | ||
| }); |
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,75 @@ | ||
| import * as Effect from "effect/Effect"; | ||
|
|
||
| import { HostProcessArguments } from "@t3tools/shared/hostProcess"; | ||
|
|
||
| import packageJson from "../../package.json" with { type: "json" }; | ||
|
|
||
| export type CliRunner = "npx" | "pnpm dlx" | "bunx"; | ||
|
|
||
| /** | ||
| * How the CLI was launched, judged by where its entry script lives. Each | ||
| * package runner executes out of a distinctive cache/temp layout: | ||
| * | ||
| * npx ~/.npm/_npx/<hash>/node_modules/... | ||
| * pnpm dlx ~/.cache/pnpm/dlx/..., $PNPM_HOME/.pnpm/dlx/..., | ||
| * or %LOCALAPPDATA%/pnpm-cache/dlx/... on Windows | ||
| * bunx ~/.bun/install/cache/... or $TMPDIR/bunx-<uid>-<spec>/... | ||
| * | ||
| * Global installs and repo checkouts match none of these and return null. | ||
| * Detection is best-effort; callers must fail closed to a plain `t3` command. | ||
| */ | ||
| export function detectCliRunner(entryPath: string): CliRunner | null { | ||
| const path = entryPath.replaceAll("\\", "/"); | ||
| if (path.includes("/_npx/")) { | ||
| return "npx"; | ||
| } | ||
| if ( | ||
| path.includes("/pnpm/dlx/") || | ||
| path.includes("/.pnpm/dlx/") || | ||
| path.includes("/pnpm-cache/dlx/") | ||
| ) { | ||
| return "pnpm dlx"; | ||
| } | ||
| if (path.includes("/.bun/install/cache/") || path.includes("/bunx-")) { | ||
| return "bunx"; | ||
| } | ||
| return null; | ||
| } | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * The `t3` package spec to suggest. The literal spec the user typed (e.g. | ||
| * `t3@nightly`) is resolved away before our process starts, so re-derive it | ||
| * from the running version: nightly builds re-suggest the nightly channel, | ||
| * anything else suggests the bare package. | ||
| */ | ||
| export function suggestedPackageSpec(version: string): string { | ||
| return version.includes("-nightly.") ? "t3@nightly" : "t3"; | ||
| } | ||
|
|
||
| /** | ||
| * Render a `t3 <subcommand>` suggestion that matches how this process was | ||
| * launched, so copy/pasting it actually works: `npx t3 connect` suggests | ||
| * `npx t3 serve`, a global install suggests `t3 serve`, and a nightly build | ||
| * keeps the `@nightly` tag. | ||
| */ | ||
| export function formatCliCommand(input: { | ||
| readonly subcommand: string; | ||
| readonly entryPath: string; | ||
| readonly version: string; | ||
| }): string { | ||
| const runner = detectCliRunner(input.entryPath); | ||
| if (runner === null) { | ||
| return `t3 ${input.subcommand}`; | ||
| } | ||
| return `${runner} ${suggestedPackageSpec(input.version)} ${input.subcommand}`; | ||
| } | ||
|
|
||
| /** `formatCliCommand` against this process's real entry path and version. */ | ||
| export const resolveCliCommand = (subcommand: string) => | ||
| Effect.map(HostProcessArguments, (processArguments) => | ||
| formatCliCommand({ | ||
| subcommand, | ||
| entryPath: processArguments[1] ?? "", | ||
| version: packageJson.version, | ||
| }), | ||
| ); | ||
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.