diff --git a/index.js b/index.js index 11433e4..552306d 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,15 @@ * A Model Context Protocol server that provides access to * Microsoft Outlook through the Microsoft Graph API. */ + +// Handle `--version` / `-v` before requiring anything else so we don't load +// config, auth modules, or emit MCP startup logging just to print a version. +if (process.argv.includes('--version') || process.argv.includes('-v')) { + const pkg = require('./package.json'); + process.stdout.write(`${pkg.name} v${pkg.version}\n`); + process.exit(0); +} + const { Server } = require('@modelcontextprotocol/sdk/server/index.js'); const { StdioServerTransport, diff --git a/test/cli-version.test.js b/test/cli-version.test.js new file mode 100644 index 0000000..3b20d6c --- /dev/null +++ b/test/cli-version.test.js @@ -0,0 +1,41 @@ +/** + * Tests for `--version` / `-v` CLI flag (GH #68). + * + * Spawns `node index.js` with the flag and asserts: + * - stdout matches `@littlebearapps/outlook-assistant v` + * - exit code is 0 + * - no MCP startup output is emitted (we exit before logging) + */ + +const path = require('path'); +const { spawnSync } = require('child_process'); + +const ENTRY = path.join(__dirname, '..', 'index.js'); +const pkg = require('../package.json'); + +function runCli(args) { + return spawnSync(process.execPath, [ENTRY, ...args], { + encoding: 'utf8', + timeout: 10_000, + }); +} + +describe('CLI --version flag', () => { + test('--version prints " v" and exits 0', () => { + const result = runCli(['--version']); + expect(result.status).toBe(0); + expect(result.stdout.trim()).toBe(`${pkg.name} v${pkg.version}`); + }); + + test('-v shorthand prints " v" and exits 0', () => { + const result = runCli(['-v']); + expect(result.status).toBe(0); + expect(result.stdout.trim()).toBe(`${pkg.name} v${pkg.version}`); + }); + + test('--version exits before MCP server startup logging', () => { + const result = runCli(['--version']); + expect(result.stderr).not.toMatch(/STARTING/); + expect(result.stderr).not.toMatch(/connected and listening/); + }); +});