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
37 changes: 21 additions & 16 deletions cli/entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@ const require = createRequire(import.meta.url)

const globalPackageJson = require("../package.json")

const useGlobal = process.argv.includes("--use-global")
const args = process.argv.slice(2).filter((arg) => arg !== "--use-global")

let mainPath = join(packageRoot, "dist/main.js")

try {
const localRequire = createRequire(join(process.cwd(), "package.json"))
const localPackageJsonPath = localRequire.resolve(
"@tscircuit/cli/package.json",
)
const localPackageJson = localRequire(localPackageJsonPath)
const localPackageRoot = dirname(localPackageJsonPath)
const localMainPath = join(localPackageRoot, "dist/main.js")

if (localPackageRoot !== packageRoot && existsSync(localMainPath)) {
console.warn(
`Using local @tscircuit/cli v${localPackageJson.version} instead of global v${globalPackageJson.version}`,
if (!useGlobal) {
try {
const localRequire = createRequire(join(process.cwd(), "package.json"))
const localPackageJsonPath = localRequire.resolve(
"@tscircuit/cli/package.json",
)
mainPath = localMainPath
}
} catch {}
const localPackageJson = localRequire(localPackageJsonPath)
const localPackageRoot = dirname(localPackageJsonPath)
const localMainPath = join(localPackageRoot, "dist/main.js")

if (localPackageRoot !== packageRoot && existsSync(localMainPath)) {
console.warn(
`Using local @tscircuit/cli v${localPackageJson.version} instead of global v${globalPackageJson.version}`,
)
mainPath = localMainPath
}
} catch {}
}

const { status } = spawnSync(runner, [mainPath, ...process.argv.slice(2)], {
const { status } = spawnSync(runner, [mainPath, ...args], {
stdio: "inherit",
})

Expand Down
82 changes: 82 additions & 0 deletions tests/cli/entrypoint/use-global-flag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { expect, test } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { join, resolve } from "node:path"
import { spawnSync } from "node:child_process"
import { temporaryDirectory } from "tempy"

test("entrypoint uses local version by default when available", async () => {
const tmpDir = temporaryDirectory()
const localCliPath = join(tmpDir, "node_modules", "@tscircuit", "cli")

mkdirSync(localCliPath, { recursive: true })
mkdirSync(join(localCliPath, "dist"), { recursive: true })

writeFileSync(
join(tmpDir, "package.json"),
JSON.stringify({ name: "test-project", version: "1.0.0" }),
)

writeFileSync(
join(localCliPath, "package.json"),
JSON.stringify({ name: "@tscircuit/cli", version: "0.0.999-local" }),
)

writeFileSync(
join(localCliPath, "dist", "main.js"),
'console.log("LOCAL_CLI_EXECUTED")',
)

const entrypointPath = resolve(process.cwd(), "cli/entrypoint.js")

const result = spawnSync("bun", [entrypointPath, "--version"], {
cwd: tmpDir,
encoding: "utf-8",
})

const output = result.stdout + result.stderr

expect(output).toContain("Using local @tscircuit/cli v0.0.999-local")

rmSync(tmpDir, { recursive: true, force: true })
})

test("entrypoint skips local version when --use-global flag is passed", async () => {
const tmpDir = temporaryDirectory()
const localCliPath = join(tmpDir, "node_modules", "@tscircuit", "cli")

mkdirSync(localCliPath, { recursive: true })
mkdirSync(join(localCliPath, "dist"), { recursive: true })

writeFileSync(
join(tmpDir, "package.json"),
JSON.stringify({ name: "test-project", version: "1.0.0" }),
)

writeFileSync(
join(localCliPath, "package.json"),
JSON.stringify({ name: "@tscircuit/cli", version: "0.0.999-local" }),
)

writeFileSync(
join(localCliPath, "dist", "main.js"),
'console.log("LOCAL_CLI_EXECUTED")',
)

const entrypointPath = resolve(process.cwd(), "cli/entrypoint.js")

const result = spawnSync(
"bun",
[entrypointPath, "--use-global", "--version"],
{
cwd: tmpDir,
encoding: "utf-8",
},
)

const output = result.stdout + result.stderr

expect(output).not.toContain("Using local @tscircuit/cli")
expect(output).not.toContain("LOCAL_CLI_EXECUTED")

rmSync(tmpDir, { recursive: true, force: true })
})
Loading