Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-symlinked-cli-direct-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"leadtype": patch
---

Fix the CLI direct-run check so package-manager bin shims and symlinked workspace installs correctly run `leadtype generate`.
45 changes: 42 additions & 3 deletions packages/leadtype/src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { existsSync } from "node:fs";
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import {
mkdir,
mkdtemp,
readFile,
rm,
symlink,
writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { fileURLToPath, pathToFileURL } from "node:url";
import { glob as fg } from "tinyglobby";
import { afterEach, describe, expect, it } from "vitest";
import { runCli } from "./cli";
import { isDirectRun, runCli } from "./cli";

const tempDirs: string[] = [];
const repoRoot = path.resolve(
Expand Down Expand Up @@ -88,6 +95,38 @@ afterEach(async () => {
});

describe("leadtype CLI", () => {
it("treats symlinked package-manager bin paths as direct runs", async () => {
const fixtureDir = await createTempDir();
const realCliPath = path.join(fixtureDir, "packages", "leadtype", "dist");
const linkedPackagePath = path.join(
fixtureDir,
"node_modules",
"leadtype",
"dist"
);
const linkType = process.platform === "win32" ? "junction" : "dir";
const binPath = path.join(fixtureDir, "node_modules", ".bin", "leadtype");
const cliPath = path.join(realCliPath, "cli.js");

await mkdir(realCliPath, { recursive: true });
await mkdir(path.dirname(binPath), { recursive: true });
await writeFile(cliPath, "#!/usr/bin/env node\n");
await symlink(
path.join(fixtureDir, "packages", "leadtype"),
path.join(fixtureDir, "node_modules", "leadtype"),
linkType
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
await symlink(path.join(linkedPackagePath, "cli.js"), binPath);

expect(isDirectRun(binPath, pathToFileURL(cliPath).href)).toBe(true);
expect(
isDirectRun(
path.join(linkedPackagePath, "cli.js"),
pathToFileURL(cliPath).href
)
).toBe(true);
});

it("prints the command list", async () => {
const capture = createCapture();

Expand Down
21 changes: 17 additions & 4 deletions packages/leadtype/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { realpathSync } from "node:fs";
import { resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
import { getGenerateUsage, runGenerateCommand } from "./cli/generate";
import { logger, setLogStreams } from "./internal/logger";
import { getLintUsage, runLintCommand } from "./lint/cli";
Expand Down Expand Up @@ -61,9 +62,21 @@ export async function runCli(
return 2;
}

function isDirectRun(): boolean {
const entry = process.argv[1];
return entry ? import.meta.url === pathToFileURL(resolve(entry)).href : false;
function resolveRealPath(filePath: string): string {
try {
return realpathSync.native(resolve(filePath));
} catch {
return resolve(filePath);
}
}

export function isDirectRun(
entry = process.argv[1],
moduleUrl = import.meta.url
): boolean {
Comment on lines +73 to +76
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Add explicit parameter types to exported isDirectRun API.

Line 74 and Line 75 currently rely on inference; make the function contract explicit since this is an exported helper.

Proposed patch
 export function isDirectRun(
-  entry = process.argv[1],
-  moduleUrl = import.meta.url
+  entry: string | undefined = process.argv[1],
+  moduleUrl: string = import.meta.url
 ): boolean {

As per coding guidelines, **/*.{ts,tsx}: Use explicit types for function parameters and return values when they enhance clarity.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/leadtype/src/cli.ts` around lines 73 - 76, The exported helper
isDirectRun currently uses inferred types for its parameters and return type;
update its signature to declare explicit types (e.g., entry: string, moduleUrl:
string) and an explicit boolean return type while preserving the existing
default values and behavior so the function contract is clear for consumers and
matches exported API expectations.

return entry
? resolveRealPath(entry) === resolveRealPath(fileURLToPath(moduleUrl))
: false;
}

if (isDirectRun()) {
Expand Down
Loading