LocalAdapter - Local Execution
LocalAdapter provides command execution in the local system through Node.js child_process API. This is the basic and most performant adapter.
Core Features
- ✅ Direct execution through spawn/spawnSync
- ✅ Bun runtime support
- ✅ Synchronous and asynchronous execution
- ✅ Stream output processing
- ✅ Full process control
- ✅ Minimal overhead
Usage
Basic Execution
import { $ } from '@xec-sh/core';
// LocalAdapter is used by default
await $`ls -la`;
// Explicit specification
const local = $.local();
await local`pwd`;
Configuration
const $ = new ExecutionEngine({
adapters: {
local: {
preferBun: true, // Prefer Bun runtime
uid: 1000, // Unix user ID
gid: 1000, // Unix group ID
killSignal: 'SIGTERM', // Signal for termination
defaultShell: '/bin/bash' // Default shell
}
}
});
Execution Modes
Shell Mode
// Automatic shell (true)
await $`echo $HOME && ls *.txt`;
// Specific shell
await $`echo $0`.shell('/bin/zsh');
// Without shell (false) - safer
await $`ls`.shell(false);
Synchronous Execution
import { ExecutionEngine } from '@xec-sh/core';
const $ = new ExecutionEngine();
const adapter = $.getAdapter('local');
// Synchronous execution (blocks event loop)
const result = adapter.executeSync({
command: 'ls',
args: ['-la'],
shell: false
});
console.log(result.stdout);
Process Management
Signals and Termination
// Graceful shutdown with timeout
const server = $`node server.js`;
setTimeout(() => {
server.kill('SIGTERM'); // Graceful termination
setTimeout(() => {
server.kill('SIGKILL'); // Force termination
}, 5000);
}, 30000);
await server;