@glyphtek/scriptit
Version:
A cross-runtime CLI and library for running scripts with environment management, TUI, and support for lambda functions. Optimized for Bun with compatibility for Node.js and Deno.
522 lines (406 loc) ⢠11.7 kB
Markdown
ScriptIt provides flexible configuration options through configuration files, environment variables, and command-line flags. This allows you to customize behavior for different environments and use cases.
Configuration is applied in the following order (highest to lowest priority):
1. **Command-line flags** - `--config`, `--env`, etc.
2. **Environment variables** - `SCRIPTIT_RUNTIME`, etc.
3. **Configuration file** - `scriptit.config.js`
4. **Default values** - Built-in defaults
## Configuration File
### Basic Configuration
Create a `scriptit.config.js` file in your project root:
```javascript
// scriptit.config.js
export default {
// Runtime selection
runtime: 'auto', // 'auto', 'bun', 'deno', 'node'
// Directory configuration
scriptsDir: 'scripts', // Directory containing scripts
tmpDir: 'tmp', // Temporary files directory
// Console configuration
consoleColors: true, // Enable colored console output
// Execution configuration
timeout: 30000, // Script timeout in milliseconds
// Environment variables
env: {
NODE_ENV: 'development',
DEBUG: 'true'
}
}
```
```javascript
// scriptit.config.js
export default {
// Runtime configuration
runtime: 'auto',
runtimeOptions: {
bun: {
// Bun-specific options
experimental: true
},
deno: {
// Deno-specific options
permissions: ['--allow-all'],
unstable: true
},
node: {
// Node.js-specific options
useTsx: true,
nodeOptions: ['--max-old-space-size=4096']
}
},
// Directory configuration
scriptsDir: 'scripts',
tmpDir: 'tmp',
workingDirectory: process.cwd(),
// Console configuration
consoleColors: true,
logLevel: 'info', // 'debug', 'info', 'warn', 'error'
// Execution configuration
timeout: 30000,
maxConcurrentScripts: 5,
retryAttempts: 0,
// Environment configuration
env: {
NODE_ENV: 'development',
DEBUG: 'scriptit:*'
},
envFile: '.env', // Path to .env file
// TUI configuration
tui: {
enabled: true,
theme: 'default', // 'default', 'dark', 'light'
refreshInterval: 1000 // Milliseconds
},
// Script filtering
include: ['**/*.js', '**/*.ts'],
exclude: ['node_modules/**', '*.test.js'],
// Hooks
hooks: {
beforeScript: './hooks/before.js',
afterScript: './hooks/after.js',
onError: './hooks/error.js'
}
}
```
```javascript
// scriptit.config.js
const baseConfig = {
scriptsDir: 'scripts',
tmpDir: 'tmp',
consoleColors: true
}
const environments = {
development: {
...baseConfig,
runtime: 'bun',
logLevel: 'debug',
timeout: 60000,
env: {
NODE_ENV: 'development',
DEBUG: 'true'
}
},
staging: {
...baseConfig,
runtime: 'node',
logLevel: 'info',
timeout: 45000,
env: {
NODE_ENV: 'staging',
DEBUG: 'false'
}
},
production: {
...baseConfig,
runtime: 'node',
logLevel: 'warn',
timeout: 30000,
consoleColors: false,
env: {
NODE_ENV: 'production',
DEBUG: 'false'
}
}
}
const env = process.env.NODE_ENV || 'development'
export default environments[env]
```
```bash
scriptit exec script.js
NODE_ENV=staging scriptit exec script.js
NODE_ENV=production scriptit exec script.js
```
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `runtime` | `string` | `'auto'` | Runtime to use: `'auto'`, `'bun'`, `'deno'`, `'node'` |
| `scriptsDir` | `string` | `'scripts'` | Directory containing script files |
| `tmpDir` | `string` | `'tmp'` | Directory for temporary files |
| `workingDirectory` | `string` | `process.cwd()` | Working directory for script execution |
| `timeout` | `number` | `30000` | Script timeout in milliseconds |
| `consoleColors` | `boolean` | `true` | Enable colored console output |
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `runtimeOptions.bun` | `object` | `{}` | Bun-specific configuration |
| `runtimeOptions.deno` | `object` | `{}` | Deno-specific configuration |
| `runtimeOptions.node` | `object` | `{}` | Node.js-specific configuration |
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `env` | `object` | `{}` | Environment variables to set |
| `envFile` | `string` | `'.env'` | Path to .env file |
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `tui.enabled` | `boolean` | `true` | Enable Terminal UI |
| `tui.theme` | `string` | `'default'` | TUI theme |
| `tui.refreshInterval` | `number` | `1000` | Refresh interval in milliseconds |
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `include` | `string[]` | `['**/*.js', '**/*.ts']` | Glob patterns for included files |
| `exclude` | `string[]` | `['node_modules/**']` | Glob patterns for excluded files |
| Variable | Description | Example |
|----------|-------------|---------|
| `SCRIPTIT_RUNTIME` | Default runtime | `bun`, `deno`, `node` |
| `SCRIPTIT_DEBUG` | Enable debug mode | `true`, `false` |
| `SCRIPTIT_CONFIG` | Path to config file | `./custom.config.js` |
| `SCRIPTIT_SCRIPTS_DIR` | Scripts directory | `./src/scripts` |
| `SCRIPTIT_TMP_DIR` | Temporary directory | `./temp` |
```bash
SCRIPTIT_RUNTIME=bun scriptit exec script.js
SCRIPTIT_DEBUG=true scriptit run
SCRIPTIT_CONFIG=./prod.config.js scriptit exec deploy.js
SCRIPTIT_SCRIPTS_DIR=./src/scripts scriptit run
```
```bash
scriptit --pwd /path/to/project exec script.js
scriptit --debug exec script.js
scriptit --version
```
```bash
scriptit exec --config ./custom.config.js script.js
scriptit exec --env NODE_ENV=production --env DEBUG=false script.js
scriptit run --scripts-dir ./src/scripts
scriptit run --no-tui
```
```javascript
// dev.config.js
export default {
runtime: 'bun', // Fast development
scriptsDir: 'src/scripts',
tmpDir: 'tmp',
consoleColors: true,
logLevel: 'debug',
timeout: 60000, // Longer timeout for debugging
env: {
NODE_ENV: 'development',
DEBUG: 'scriptit:*',
API_URL: 'http://localhost:3000'
},
tui: {
enabled: true,
theme: 'dark',
refreshInterval: 500 // Faster refresh
}
}
```
```javascript
// prod.config.js
export default {
runtime: 'node', // Stable runtime
scriptsDir: 'dist/scripts',
tmpDir: '/tmp/scriptit',
consoleColors: false, // No colors in logs
logLevel: 'warn',
timeout: 30000,
env: {
NODE_ENV: 'production',
DEBUG: 'false'
},
tui: {
enabled: false // No TUI in production
},
// Production-specific options
maxConcurrentScripts: 10,
retryAttempts: 3
}
```
```javascript
// ci.config.js
export default {
runtime: 'node',
scriptsDir: 'scripts',
tmpDir: './tmp',
consoleColors: false, // No colors in CI logs
logLevel: 'info',
timeout: 120000, // Longer timeout for CI
env: {
NODE_ENV: 'test',
CI: 'true'
},
tui: {
enabled: false // Never use TUI in CI
},
// CI-specific filtering
include: ['scripts/**/*.js'],
exclude: ['scripts/**/*.test.js', 'scripts/dev/**']
}
```
ScriptIt validates configuration and provides helpful error messages:
```bash
ā ScriptIt Error: Invalid runtime 'invalid'
š” Valid options: auto, bun, deno, node
ā ScriptIt Error: Timeout must be a positive number
š” Current value: -1000
ā ScriptIt Error: Scripts directory not found: ./missing
š” Create the directory or update scriptsDir in config
```
ScriptIt automatically looks for configuration files in this order:
1. `scriptit.config.js`
2. `scriptit.config.mjs`
3. `scriptit.config.ts`
4. `.scriptitrc.js`
5. `.scriptitrc.json`
```bash
scriptit --config ./custom.config.js exec script.js
scriptit exec --config ./prod.config.js script.js
```
```javascript
// In your script
export async function execute(context) {
// Access configuration
console.log('Runtime:', context.config.runtime);
console.log('Scripts dir:', context.config.scriptsDir);
console.log('Environment:', context.config.env);
}
```
Use different configs for different environments:
```
configs/
āāā development.config.js
āāā staging.config.js
āāā production.config.js
āāā ci.config.js
```
Never commit sensitive data to config files:
```javascript
// ā
Good - use environment variables
export default {
env: {
API_KEY: process.env.API_KEY,
DATABASE_URL: process.env.DATABASE_URL
}
}
// ā Bad - hardcoded secrets
export default {
env: {
API_KEY: 'secret-key-123',
DATABASE_URL: 'postgresql://user:pass@host/db'
}
}
```
```javascript
// config.js
const requiredEnvVars = ['API_KEY', 'DATABASE_URL'];
const missingVars = requiredEnvVars.filter(name => !process.env[name]);
if (missingVars.length > 0) {
throw new Error(`Missing required environment variables: ${missingVars.join(', ')}`);
}
export default {
env: {
API_KEY: process.env.API_KEY,
DATABASE_URL: process.env.DATABASE_URL
}
}
```
```typescript
// scriptit.config.ts
import type { ScriptItConfig } from '@glyphtek/scriptit';
const config: ScriptItConfig = {
runtime: 'bun',
scriptsDir: 'scripts',
consoleColors: true,
env: {
NODE_ENV: 'development'
}
};
export default config;
```
**Config file not found:**
```bash
ā ScriptIt Error: Configuration file not found: ./missing.config.js
š” Check the file path or use --config flag
```
**Invalid configuration:**
```bash
ā ScriptIt Error: Invalid configuration
š” Check syntax and required properties
```
**Environment variable conflicts:**
```bash
ā ļø Warning: Environment variable NODE_ENV overridden by config
š” Command-line flags take precedence over config file
```
Use debug mode to see configuration loading:
```bash
scriptit --debug exec script.js
```
Output includes:
- Configuration file path
- Loaded configuration values
- Environment variable overrides
- Final merged configuration
- [CLI Commands](/cli/commands) - Available CLI commands
- [Environment Variables](/cli/environment) - Environment management
- [Runtime Selection](/cli/runtime) - Runtime-specific configuration
- [Examples](/examples/cli) - Configuration examples