-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Always run the function in cli.js
- Loading branch information
Showing
2 changed files
with
73 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |