Open History to see the last commit
1import * as Effect from "effect/Effect";
2
3import { HostProcessArguments } from "@t3tools/shared/hostProcess";
4
5import packageJson from "../../package.json" with { type: "json" };
6
7export type CliRunner = "npx" | "pnpm dlx" | "bunx";
8
9/**
10 * Detect package runners by their cache paths. Unrecognized paths, including
11 * global installs and repo checkouts, fall back to a plain `t3` command.
12 */
13export function detectCliRunner(entryPath: string): CliRunner | null {
14 const path = entryPath.replaceAll("\\", "/");
15 if (path.includes("/_npx/")) {
16 return "npx";
17 }
18 if (
19 path.includes("/pnpm/dlx/") ||
20 path.includes("/.pnpm/dlx/") ||
21 path.includes("/pnpm-cache/dlx/")
22 ) {
23 return "pnpm dlx";
24 }
25 if (path.includes("/.bun/install/cache/") || path.includes("/bunx-")) {
26 return "bunx";
27 }
28 return null;
29}
30
31/**
32 * The original package spec is resolved away before startup, so infer the
33 * nightly channel from the running version.
34 */
35export function suggestedPackageSpec(version: string): string {
36 return version.includes("-nightly.") ? "t3@nightly" : "t3";
37}
38
39/** Suggest a command using the current package runner and release channel. */
40export function formatCliCommand(input: {
41 readonly subcommand: string;
42 readonly entryPath: string;
43 readonly version: string;
44}): string {
45 const runner = detectCliRunner(input.entryPath);
46 if (runner === null) {
47 return `t3 ${input.subcommand}`;
48 }
49 return `${runner} ${suggestedPackageSpec(input.version)} ${input.subcommand}`;
50}
51
52/** `formatCliCommand` against this process's real entry path and version. */
53export const resolveCliCommand = (subcommand: string) =>
54 Effect.map(HostProcessArguments, (processArguments) =>
55 formatCliCommand({
56 subcommand,
57 entryPath: processArguments[1] ?? "",
58 version: packageJson.version,
59 }),
60 );