diff --git a/.changeset/short-coins-push.md b/.changeset/short-coins-push.md new file mode 100644 index 0000000..c1b6e28 --- /dev/null +++ b/.changeset/short-coins-push.md @@ -0,0 +1,5 @@ +--- +"@nicepkg/vsync": patch +--- + +fix cli not working diff --git a/cli/src/index.ts b/cli/src/index.ts index 7bcfd50..699c8c5 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -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 { 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); diff --git a/cli/src/utils/config-initializer.ts b/cli/src/utils/config-initializer.ts index 43e593a..71030ca 100644 --- a/cli/src/utils/config-initializer.ts +++ b/cli/src/utils/config-initializer.ts @@ -47,11 +47,11 @@ export async function ensureLanguageConfig(): Promise { message: "Choose your preferred language / 选择你的语言偏好:", choices: [ { - name: `English (detected: ${systemLang === "en" ? "✓" : "×"})`, + name: `English`, value: "en", }, { - name: `中文 (detected: ${systemLang === "zh" ? "✓" : "×"})`, + name: `中文`, value: "zh", }, ], diff --git a/cli/test/index.test.ts b/cli/test/index.test.ts index b76e5b2..b0511fc 100644 --- a/cli/test/index.test.ts +++ b/cli/test/index.test.ts @@ -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", () => { @@ -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); + }); });