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
5 changes: 5 additions & 0 deletions .changeset/short-coins-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nicepkg/vsync": patch
---

fix cli not working
22 changes: 21 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,34 @@
* Single source of truth → Compile to multiple formats → Diff-based sync
*/

import { realpathSync } from "node:fs";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { runCLI } from "./cli-setup.js";

export async function main(): Promise<void> {
await runCLI();
}

export function isMainModule(
argvPath: string | undefined,
moduleUrl: string,
): boolean {
if (!argvPath) {
return false;
}

try {
const argvRealPath = realpathSync(resolve(argvPath));
const moduleRealPath = realpathSync(fileURLToPath(moduleUrl));
return argvRealPath === moduleRealPath;
} catch {
return false;
}
}

// Run CLI if this is the main module
if (import.meta.url === `file://${process.argv[1]}`) {
if (isMainModule(process.argv[1], import.meta.url)) {
main().catch((error) => {
console.error("Fatal error:", error);
process.exit(1);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/utils/config-initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export async function ensureLanguageConfig(): Promise<Language> {
message: "Choose your preferred language / 选择你的语言偏好:",
choices: [
{
name: `English (detected: ${systemLang === "en" ? "✓" : "×"})`,
name: `English`,
value: "en",
},
{
name: `中文 (detected: ${systemLang === "zh" ? "✓" : "×"})`,
name: `中文`,
value: "zh",
},
],
Expand Down
39 changes: 38 additions & 1 deletion cli/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { describe, it, expect } from "vitest";
import { main } from "@src/index.js";
import { mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, relative } from "node:path";
import { pathToFileURL } from "node:url";
import { isMainModule, main } from "@src/index.js";

describe("CLI Entry Point", () => {
it("should export main function", () => {
Expand All @@ -10,4 +14,37 @@ describe("CLI Entry Point", () => {
it("should be async function", () => {
expect(main.constructor.name).toBe("AsyncFunction");
});

it("detects main module with relative argv path", async () => {
const dir = await mkdtemp(join(tmpdir(), "vsync-index-"));
const filePath = join(dir, "index.js");
await writeFile(filePath, "console.log('test');");

try {
const metaUrl = pathToFileURL(filePath).href;
const argvPath = relative(process.cwd(), filePath);
expect(isMainModule(argvPath, metaUrl)).toBe(true);
} finally {
await rm(dir, { recursive: true, force: true });
}
});

it("detects main module when argv is a symlink", async () => {
const dir = await mkdtemp(join(tmpdir(), "vsync-index-"));
const filePath = join(dir, "index.js");
const linkPath = join(dir, "index-link.js");
await writeFile(filePath, "console.log('test');");
await symlink(filePath, linkPath);

try {
const metaUrl = pathToFileURL(filePath).href;
expect(isMainModule(linkPath, metaUrl)).toBe(true);
} finally {
await rm(dir, { recursive: true, force: true });
}
});

it("returns false when argv is missing", () => {
expect(isMainModule(undefined, "file:///tmp/index.js")).toBe(false);
});
});
Loading