Skip to content

Commit

Permalink
fix: Always run the function in cli.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoseley committed Jan 7, 2025
1 parent 8a50f66 commit d38be04
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
13 changes: 4 additions & 9 deletions packages/create-gensx/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env node

import { fileURLToPath } from "url";

import { Command } from "commander";

import { createGensxProject, CreateOptions } from "./index.js";
Expand All @@ -27,10 +25,7 @@ export async function runCLI() {
await program.parseAsync();
}

// Only run CLI when this file is being executed directly
if (process.argv[1] === fileURLToPath(import.meta.url)) {
runCLI().catch((error) => {
console.error("Error:", error);
process.exit(1);
});
}
runCLI().catch((error) => {
console.error("Error:", error);
process.exit(1);
});
69 changes: 69 additions & 0 deletions packages/create-gensx/tests/package.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { exec as execCallback } from "child_process";
import path from "path";
import fs from "fs-extra";
import os from "os";
import { expect, it, beforeEach, afterEach } from "vitest";
import { promisify } from "util";

const exec = promisify(execCallback);

let tempDir: string;

beforeEach(async () => {
// Create a temporary directory for testing
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "gensx-pkg-test-"));
});

afterEach(async () => {
// Clean up
await fs.remove(tempDir);
});

it("package.json is correctly configured for npm create", async () => {
// Copy the built package to temp directory
const pkgDir = path.join(tempDir, "create-gensx");
await fs.copy(path.resolve(__dirname, "../dist"), path.join(pkgDir, "dist"));
await fs.copy(
path.resolve(__dirname, "../package.json"),
path.join(pkgDir, "package.json"),
);

// Install dependencies in the package directory
await exec("npm install", {
cwd: pkgDir,
});

// Create a test project directory
const testProjectDir = path.join(tempDir, "test-project");

try {
// Try to execute the package directly
await exec(
`node "${path.join(pkgDir, "dist/cli.js")}" "${testProjectDir}"`,
{
env: { ...process.env },
},
);

// Verify the project was created
const exists = await fs.pathExists(testProjectDir);
expect(exists).toBe(true);

// Verify package.json exists in created project
const projectPkgExists = await fs.pathExists(
path.join(testProjectDir, "package.json"),
);
expect(projectPkgExists).toBe(true);
} catch (error) {
// If execution fails, check the package.json configuration
const pkgJson = await fs.readJson(path.join(pkgDir, "package.json"));

// Verify essential fields
expect(pkgJson.bin).toBeDefined();
expect(pkgJson.files).toContain("dist");
expect(pkgJson.type).toBe("module");

// Re-throw the error if package.json looks correct
throw error;
}
}, 60000);

0 comments on commit d38be04

Please sign in to comment.