Skip to content
Open
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
30 changes: 15 additions & 15 deletions cli/entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ import { createRequire } from "node:module"
import { dirname, join } from "node:path"
import { fileURLToPath } from "node:url"

function commandExists(cmd) {
try {
const res = spawnSync(cmd, ["--version"], { stdio: "ignore" })
return res.status === 0
} catch {
return false
}
}

const runner = commandExists("bun") ? "bun" : "tsx"

const __dirname = dirname(fileURLToPath(import.meta.url))
const packageRoot = join(__dirname, "..")
const require = createRequire(import.meta.url)
Expand All @@ -26,6 +15,7 @@ const useGlobal = process.argv.includes("--use-global")
const args = process.argv.slice(2).filter((arg) => arg !== "--use-global")

let mainPath = join(packageRoot, "dist/cli/main.js")
let tsxLoaderPath = join(packageRoot, "dist/cli/tsx-loader.js")

if (!useGlobal) {
try {
Expand All @@ -36,18 +26,28 @@ if (!useGlobal) {
const localPackageJson = localRequire(localPackageJsonPath)
const localPackageRoot = dirname(localPackageJsonPath)
const localMainPath = join(localPackageRoot, "dist/cli/main.js")
const localTsxLoaderPath = join(localPackageRoot, "dist/cli/tsx-loader.js")

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

const { status } = spawnSync(runner, [mainPath, ...args], {
stdio: "inherit",
})
const { status } = spawnSync(
process.execPath,
["--import", tsxLoaderPath, mainPath, ...args],
{
stdio: "inherit",
},
)

process.exit(status ?? 0)
1 change: 1 addition & 0 deletions cli/tsx-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "tsx/esm"
1 change: 1 addition & 0 deletions scripts/bun-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ALLOW_BUNDLING = ["@tscircuit/runframe"]
const result = await Bun.build({
entrypoints: [
"./cli/main.ts",
"./cli/tsx-loader.ts",
"./cli/build/build.worker.ts",
"./cli/snapshot/snapshot.worker.ts",
"./lib/index.ts",
Expand Down
21 changes: 18 additions & 3 deletions tests/cli/entrypoint/use-global-flag.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { expect, test } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { join, resolve } from "node:path"
import { spawnSync } from "node:child_process"
import { temporaryDirectory } from "tempy"

const nodeBin = process.env.npm_node_execpath ?? "node"

test("entrypoint uses local version by default when available", async () => {
const tmpDir = temporaryDirectory()
const localCliPath = join(tmpDir, "node_modules", "@tscircuit", "cli")
Expand All @@ -25,17 +27,19 @@ test("entrypoint uses local version by default when available", async () => {
join(localCliPath, "dist", "cli", "main.js"),
'console.log("LOCAL_CLI_EXECUTED")',
)
writeFileSync(join(localCliPath, "dist", "cli", "tsx-loader.js"), "")

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

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

const output = result.stdout + result.stderr

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

rmSync(tmpDir, { recursive: true, force: true })
})
Expand All @@ -61,11 +65,12 @@ test("entrypoint skips local version when --use-global flag is passed", async ()
join(localCliPath, "dist", "cli", "main.js"),
'console.log("LOCAL_CLI_EXECUTED")',
)
writeFileSync(join(localCliPath, "dist", "cli", "tsx-loader.js"), "")

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

const result = spawnSync(
"bun",
nodeBin,
[entrypointPath, "--use-global", "--version"],
{
cwd: tmpDir,
Expand All @@ -80,3 +85,13 @@ test("entrypoint skips local version when --use-global flag is passed", async ()

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

test("entrypoint does not require bun at runtime", () => {
const entrypointPath = resolve(process.cwd(), "cli/entrypoint.js")
const entrypointSource = readFileSync(entrypointPath, "utf-8")

expect(entrypointSource).toContain("#!/usr/bin/env node")
expect(entrypointSource).not.toContain('"bun"')
expect(entrypointSource).toContain('"--import"')
expect(entrypointSource).toContain("tsx-loader.js")
})
Loading