From 52e766be372b2e39be810854774f288c82d61a49 Mon Sep 17 00:00:00 2001 From: Arnav K Date: Thu, 27 Nov 2025 11:43:47 +0530 Subject: [PATCH 1/2] feat(cli): add --use-global flag to skip local version check --- cli/entrypoint.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/cli/entrypoint.js b/cli/entrypoint.js index c4196092..65feab71 100644 --- a/cli/entrypoint.js +++ b/cli/entrypoint.js @@ -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", }) From 1819411988ee9cc93208ece8010134443c9231bc Mon Sep 17 00:00:00 2001 From: Arnav K Date: Thu, 27 Nov 2025 11:45:45 +0530 Subject: [PATCH 2/2] f --- tests/cli/entrypoint/use-global-flag.test.ts | 82 ++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/cli/entrypoint/use-global-flag.test.ts diff --git a/tests/cli/entrypoint/use-global-flag.test.ts b/tests/cli/entrypoint/use-global-flag.test.ts new file mode 100644 index 00000000..8c30170d --- /dev/null +++ b/tests/cli/entrypoint/use-global-flag.test.ts @@ -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 }) +})