From 4493228b4f576de96b97e5496f325287bb46c7c1 Mon Sep 17 00:00:00 2001 From: jiasheng Date: Fri, 14 Aug 2026 09:31:41 +0800 Subject: [PATCH] feat(cli): implement CLI entry point and enhance error handling for missing options --- package.json | 10 +++++++--- src/cli.ts | 4 ++++ src/index.ts | 21 ++++++++++++--------- tests/cli.test.ts | 20 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 src/cli.ts diff --git a/package.json b/package.json index f32f3e9..b1821a4 100644 --- a/package.json +++ b/package.json @@ -6,15 +6,19 @@ "main": "./dist/index.js", "module": "./dist/index.js", "types": "./dist/index.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/zenstackhq/studio-mcp-remote" + }, "bin": { - "zenstack-mcp-remote": "./dist/index.js", - "studio-mcp-remote": "./dist/index.js" + "zenstack-mcp-remote": "./dist/cli.js", + "studio-mcp-remote": "./dist/cli.js" }, "files": [ "dist" ], "scripts": { - "build": "tsc && chmod +x dist/index.js", + "build": "tsc && chmod +x dist/cli.js dist/index.js", "test": "vitest run", "format": "prettier --write \"**/*.{ts,tsx,md}\"" }, diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..47944b7 --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,4 @@ +#!/usr/bin/env node +import { main } from './index.js'; + +main(); diff --git a/src/index.ts b/src/index.ts index 8191bf6..e338228 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env node +import fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import { runBridgeServer, type BridgeServerController } from './server.js'; @@ -36,7 +36,7 @@ export function parseArgs(argv: string[]) { return { remote, proxy, authorization }; } -async function main() { +export async function main() { const { remote, proxy, authorization } = parseArgs(process.argv.slice(2)); if (!remote || !proxy || !authorization) { @@ -79,12 +79,15 @@ async function main() { } const currentFile = fileURLToPath(import.meta.url); -if ( - process.argv[1] === currentFile || - process.argv[1]?.endsWith('/index.js') || - process.argv[1]?.endsWith('/index.ts') || - process.argv[1]?.endsWith('/cli.js') || - process.argv[1]?.endsWith('/cli.ts') -) { +function isDirectRun() { + if (!process.argv[1]) return false; + try { + return fs.realpathSync(process.argv[1]) === currentFile; + } catch { + return false; + } +} + +if (isDirectRun()) { main(); } diff --git a/tests/cli.test.ts b/tests/cli.test.ts index 01231e1..94a7076 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -75,3 +75,23 @@ describe('parseArgs', () => { expect(result.authorization).toBeUndefined(); }); }); + +describe('CLI execution', () => { + it('exits with status 1 and shows usage when required options are missing', () => { + const { execSync } = require('node:child_process'); + const path = require('node:path'); + const cliPath = path.resolve(__dirname, '../dist/cli.js'); + + let output = ''; + let status = 0; + try { + execSync(`node "${cliPath}"`, { encoding: 'utf-8', stdio: 'pipe' }); + } catch (err: any) { + status = err.status; + output = err.stderr.toString(); + } + + expect(status).toBe(1); + expect(output).toContain('Usage: zenstack-mcp-remote'); + }); +});