tsshell
v1.1.0
Published
TypeScript wrapper for executing shell commands with proper error handling
Downloads
14
Maintainers
Readme
TSShell
TypeScript wrapper for executing shell commands with proper error handling, inspired by Python's pyshell.
Installation
npm install tsshellQuick Start
import { shell, shellSync, ShellError } from 'tsshell';
async function example() {
try {
// Execute shell commands asynchronously
const output = await shell('ls -la');
console.log(output);
// Execute shell commands synchronously
const syncOutput = shellSync('pwd');
console.log(syncOutput);
// Use environment variables
const result = await shell('echo "Hello $NAME"', {
env: { NAME: 'World' }
});
// Git operations
await shell('git add . && git commit -m "update"');
} catch (error) {
if (error instanceof ShellError) {
console.log('Command failed:', error.returnCode);
console.log('Error:', error.stderr);
}
}
}Features
- 🚀 Promise-based: Async/await support
- ⚡ Sync support: Synchronous execution for simple scripts
- ⚡ Error propagation: Uses
set -efor proper error handling - 🔧 Environment variables: Easy env variable injection
- ⏰ Timeout support: Configurable command timeouts
- 📝 TypeScript: Full type safety and IntelliSense
- 🛠️ Rich errors: Detailed error information with stdout/stderr
API
shell(script, options?)
Executes a shell script asynchronously and returns the output.
Parameters:
script(string): Shell script to executeoptions(object, optional):env: Environment variables to mergecwd: Working directorytimeout: Command timeout in milliseconds
Returns: Promise<string> - Command output (stdout)
Throws: ShellError - When command fails
shellSync(script, options?)
Executes a shell script synchronously and returns the output.
Parameters:
script(string): Shell script to executeoptions(object, optional):env: Environment variables to mergecwd: Working directory- Note:
timeoutis not supported for sync execution
Returns: string - Command output (stdout)
Throws: ShellError - When command fails
ShellError
Error class with additional properties:
returnCode: Process exit codestdout: Standard outputstderr: Standard error
Documentation
- Usage Guide - Comprehensive examples and best practices
- Development Notes - Development workflow and testing
License
MIT
