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
38 changes: 35 additions & 3 deletions packages/create-termui-app/src/commands/add.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it, beforeEach, afterEach, vi } from "vitest";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { mkdirSync, rmSync, existsSync, readFileSync } from "node:fs";
import { mkdirSync, rmSync, existsSync, readFileSync, writeFileSync } from "node:fs";

vi.mock("node:child_process", () => ({
execFileSync: vi.fn(),
Expand Down Expand Up @@ -133,8 +133,8 @@ describe("runAddCommand", () => {
'export const Badge = () => "badge";',
);
expect(execSpy).toHaveBeenCalledWith(
"bun",
["add", "@termuijs/core", "@termuijs/widgets"],
"npm",
["install", "@termuijs/core", "@termuijs/widgets"],
{
stdio: "inherit",
},
Expand All @@ -144,6 +144,38 @@ describe("runAddCommand", () => {
);
});

it("uses bun when bun.lock is present", async () => {
setupFetchMock();
writeFileSync(join(tempDir, "bun.lock"), "");
const execSpy = vi.spyOn(childProcess, "execFileSync");

await runAddCommand({ component: "Badge" });

expect(execSpy).toHaveBeenCalledWith(
"bun",
["add", "@termuijs/core", "@termuijs/widgets"],
{
stdio: "inherit",
},
);
});

it("uses pnpm when pnpm-lock.yaml is present", async () => {
setupFetchMock();
writeFileSync(join(tempDir, "pnpm-lock.yaml"), "");
const execSpy = vi.spyOn(childProcess, "execFileSync");

await runAddCommand({ component: "Badge" });

expect(execSpy).toHaveBeenCalledWith(
"pnpm",
["add", "@termuijs/core", "@termuijs/widgets"],
{
stdio: "inherit",
},
);
});

it("resolves component names case-insensitively", async () => {
setupFetchMock();
vi.spyOn(childProcess, "execFileSync");
Expand Down
17 changes: 16 additions & 1 deletion packages/create-termui-app/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ interface RegistrySchema {
components: RegistryComponent[];
}

type PackageManager = "npm" | "yarn" | "pnpm" | "bun";

export async function runAddCommand(options: AddCommandOptions): Promise<void> {
const componentName = options.component.trim();
if (!componentName) {
Expand Down Expand Up @@ -213,11 +215,24 @@ async function installPackages(entry: RegistryComponent): Promise<void> {
return;
}

execFileSync("bun", ["add", ...deps], {
const pm = detectPackageManager(process.cwd());
execFileSync(pm, installArgs(pm, deps), {
stdio: "inherit",
});
}

function detectPackageManager(cwd: string): PackageManager {
if (existsSync(join(cwd, "bun.lock"))) return "bun";
if (existsSync(join(cwd, "pnpm-lock.yaml"))) return "pnpm";
if (existsSync(join(cwd, "yarn.lock"))) return "yarn";
return "npm";
}

function installArgs(pm: PackageManager, deps: string[]): string[] {
if (pm === "npm") return ["install", ...deps];
return ["add", ...deps];
}

function pascalCase(value: string): string {
return value
.split(/[^a-zA-Z0-9]+/)
Expand Down
Loading