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
48 changes: 48 additions & 0 deletions src/components/__tests__/add-command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { spawnSync } from "child_process";
import {
afterEach,
beforeEach,
describe,
expect,
it,
jest,
} from "@jest/globals";
import { createAddCommand } from "../commands/add";

jest.mock("child_process", () => ({
spawnSync: jest.fn(),
}));

describe("components add command", () => {
const mockedSpawnSync = spawnSync as unknown as jest.Mock;
let logSpy: jest.SpiedFunction<typeof console.log>;

beforeEach(() => {
mockedSpawnSync.mockReset();
mockedSpawnSync.mockReturnValue({ status: 0 });
logSpy = jest.spyOn(console, "log").mockImplementation(() => {});
});

afterEach(() => {
logSpy.mockRestore();
});

it("checks npx availability through the shell for Windows command resolution", async () => {
const command = createAddCommand();

await command.parseAsync(["bar-visualizer"], { from: "user" });

expect(mockedSpawnSync).toHaveBeenNthCalledWith(1, "npx", ["--version"], {
encoding: "utf-8",
shell: true,
});
expect(mockedSpawnSync).toHaveBeenNthCalledWith(
2,
"npx -y shadcn@latest add https://ui.elevenlabs.io/r/bar-visualizer.json",
{
stdio: "inherit",
shell: true,
}
);
});
});
4 changes: 2 additions & 2 deletions src/components/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function createAddCommand(): Command {
.argument('[name]', 'Name of the component to add (optional)')
.action(async (componentName?: string) => {
try {
// Check if npx is available
const npxCheck = spawnSync('npx', ['--version'], { encoding: 'utf-8' });
// Check if npx is available. shell:true lets Windows resolve npx.cmd/npx.ps1.
const npxCheck = spawnSync('npx', ['--version'], { encoding: 'utf-8', shell: true });
if (npxCheck.error) {
console.error('Error: npx is not available. Please install Node.js/npm.');
process.exit(1);
Expand Down
Loading