Architecture
Understanding how muxa works under the hood.
Overview
muxa is built as a wrapper around mprocs, providing:
- Simple CLI interface - Intuitive command-line syntax
- Workspace awareness - Native monorepo support
- Intelligent wrapping - Automatic shell handling
- Zero configuration - Works out of the box
Core Components
Command Parser
The parser handles multiple input formats:
// Basic mode
muxa 'cmd1' 'cmd2'
// Advanced mode with flags
muxa -c 'cmd1' name1 -c 'cmd2' name2
// Workspace scripts
muxa -s package script name
Key responsibilities:
- Detect input mode (basic vs advanced)
- Parse command flags and arguments
- Validate argument combinations
- Generate mprocs configuration
Workspace Resolver
Intelligently finds packages in monorepos:
// Resolution order
1. Exact package.json name match
2. Relative path match
3. Directory name (if unique)
Supports:
- npm/yarn/bun workspaces
- pnpm workspaces
- Nested workspace structures
- Scoped packages (@org/package)
Package Manager Detection
Automatic detection strategy:
1. Check lock files (bun.lockb, pnpm-lock.yaml, etc.)
2. Read packageManager field
3. Check workspace config files
4. Default to npm
Command Builder
Transforms user input into mprocs commands:
// User input
muxa -s backend dev
// Generated command
mprocs --names backend \
'sh -c "cd packages/backend && npm run dev"'
Technical Design
Why mprocs?
mprocs provides:
- Terminal multiplexing - Split terminal into panes
- Interactive support - Preserves stdin/stdout
- Clean output - No interleaved logs
- Process management - Graceful shutdown
Shell Wrapping Strategy
Commands are wrapped with sh -c
when they contain:
const needsShell = /[&|;><$`*?{}[\]()]/;
This ensures:
- Shell features work correctly
- Environment variables expand
- Glob patterns resolve
- Operators function properly
Process Execution Flow
- Parse - Analyze command-line arguments
- Resolve - Find workspace packages if needed
- Build - Generate mprocs command
- Execute - Spawn mprocs with built command
- Monitor - Pass through exit codes
Key Features Implementation
Workspace Commands
// -s flag implementation
1. Resolve package location
2. Build cd command
3. Append script execution
4. Wrap in shell
Process Naming
// Automatic naming
muxa -c 'npm run dev' -> [npm run dev]
// Custom naming
muxa -c 'npm run dev' frontend -> [frontend]
Exit Code Handling
// Pass through mprocs exit code
process.exit(mprocsProcess.exitCode);
Performance Considerations
Minimal Overhead
- Direct execution via mprocs
- No polling or watching
- Efficient process spawning
- Native terminal performance
Memory Usage
- Lightweight wrapper (~50KB)
- mprocs handles process management
- No persistent state
- Minimal dependencies
Error Handling
Validation Errors
- Mixed argument modes
- Missing required parameters
- Invalid package identifiers
- Ambiguous workspace names
Runtime Errors
- Command execution failures
- Missing scripts
- Package resolution failures
- mprocs spawn errors
Security Considerations
Command Injection
- Commands are passed directly to shell
- No additional escaping/sanitization
- Users responsible for command safety
- Same security model as direct shell usage
File System Access
- Only reads package.json files
- No file modifications
- Respects file permissions
- Works within project boundaries
Debugging
Debug Mode
Future versions may include:
muxa --debug -c 'npm run dev'
# Shows resolved packages, generated commands, etc.
Verbose Output
muxa --verbose workspaces
# Detailed workspace detection info
Comparison with Alternatives
vs Direct mprocs
muxa adds:
- Simpler syntax
- Workspace support
- Automatic detection
- No config files
vs concurrently
muxa provides:
- Better output formatting
- Interactive features
- Monorepo awareness
- Cleaner syntax
vs turbo/nx
muxa focuses on:
- Process running, not building
- Zero configuration
- Universal compatibility
- Simple interface
Future Architecture
Planned Improvements
- Plugin System - Extend functionality
- Config Files - Optional presets
- Better IPC - Inter-process communication
- Remote Execution - SSH support
Maintaining Simplicity
Core principles:
- Zero config by default
- Simple things stay simple
- Advanced features are optional
- Backward compatibility
Contributing
Key areas for contribution:
- Package manager detection
- Workspace resolution
- Platform compatibility
- Error messages
- Documentation
See the GitHub repository for contribution guidelines.