#!/usr/bin/env node
/**
* Matrix Pattern CLI Entry Point
* Command-line interface for matrix pattern initialization and management
*/
import { parseArgs } from 'util';
import path from 'path';
import { fileURLToPath } from 'url';
import { server as matrixServer } from './index.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// CLI Configuration
const CLI_VERSION = '1.0.0';
const SUPPORTED_COMMANDS = ['init', 'start', 'help', 'version'];
// Command line argument parsing configuration
const parseOptions = {
allowPositionals: true,
options: {
defaults: {
type: 'boolean',
short: 'd',
default: false
},
horizontals: {
type: 'string',
short: 'h'
},
'no-mcp': {
type: 'boolean',
default: false
},
force: {
type: 'boolean',
short: 'f',
default: false
},
help: {
type: 'boolean',
default: false
},
version: {
type: 'boolean',
short: 'v',
default: false
}
}
};
// Help text
const HELP_TEXT = `
Matrix Pattern CLI v${CLI_VERSION}
USAGE:
matrix-pattern <command> [options]
COMMANDS:
init Initialize a new matrix pattern system
start Start the MCP server
help Show this help message
version Show version information
OPTIONS:
-d, --defaults Use default configuration for initialization
-h, --horizontals <pattern> Specify horizontal pattern configuration
--no-mcp Initialize without MCP server integration
-f, --force Force operation, overwrite existing files
--help Show help
-v, --version Show version
EXAMPLES:
matrix-pattern init # Interactive initialization
matrix-pattern init --defaults # Initialize with defaults
matrix-pattern init --horizontals "api,ui,db" # Custom horizontals
matrix-pattern start # Start MCP server
matrix-pattern help # Show help
For more information, visit: https://github.com/matrix-pattern/mcp-server
`;
/**
* Display help information
*/
function showHelp() {
console.log(HELP_TEXT);
}
/**
* Display version information
*/
function showVersion() {
console.log(`Matrix Pattern CLI v${CLI_VERSION}`);
console.log('Matrix Pattern System - Advanced pattern management and synchronization');
}
/**
* Display error message and help
*/
function showError(message) {
console.error(`Error: ${message}\n`);
showHelp();
process.exit(1);
}
/**
* Load and execute command
*/
async function loadCommand(commandName) {
try {
const commandModule = await import(`./commands/${commandName}.js`);
return commandModule.default;
} catch (error) {
if (error.code === 'ERR_MODULE_NOT_FOUND') {
showError(`Command '${commandName}' not found`);
} else {
console.error(`Failed to load command '${commandName}':`, error.message);
process.exit(1);
}
}
}
/**
* Main CLI entry point
*/
async function main() {
try {
// Parse command line arguments
const { values: flags, positionals: args } = parseArgs(parseOptions);
// Handle version flag
if (flags.version) {
showVersion();
return;
}
// Handle help flag or no command
if (flags.help || args.length === 0) {
showHelp();
return;
}
// Get command
const command = args[0].toLowerCase();
// Handle built-in commands
switch (command) {
case 'help':
showHelp();
return;
case 'version':
showVersion();
return;
case 'start':
// Start MCP server mode
console.log('Starting Matrix Pattern MCP Server...');
// The server will be started by importing index.js with CLI_MODE=false
process.env.CLI_MODE = 'false';
return;
case 'init':
// Load init command
const initCommand = await loadCommand('init');
await initCommand.execute({
args: args.slice(1),
flags,
cwd: process.cwd()
});
return;
default:
if (!SUPPORTED_COMMANDS.includes(command)) {
showError(`Unknown command: ${command}`);
}
}
// Load and execute dynamic command
if (SUPPORTED_COMMANDS.includes(command)) {
const commandModule = await loadCommand(command);
await commandModule.execute({
args: args.slice(1),
flags,
cwd: process.cwd()
});
}
} catch (error) {
if (error.code === 'ERR_PARSE_ARGS') {
showError(`Invalid arguments: ${error.message}`);
} else {
console.error('CLI Error:', error.message);
if (process.env.NODE_ENV === 'development') {
console.error(error.stack);
}
process.exit(1);
}
}
}
// Error handling
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error.message);
process.exit(1);
});
process.on('unhandledRejection', (reason) => {
console.error('Unhandled Rejection:', reason);
process.exit(1);
});
// Execute CLI if running as main module
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch((error) => {
console.error('Fatal Error:', error.message);
process.exit(1);
});
}
export { main as cli };