-
-
Notifications
You must be signed in to change notification settings - Fork 248
Docs: Add custom delimiter examples to lines.md #1229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import {$} from 'execa'; | ||
|
|
||
| // Example: Build script with error handling | ||
| // Demonstrates common patterns for build automation | ||
|
|
||
| async function build() { | ||
| console.log('🚀 Starting build process...\n'); | ||
|
|
||
| try { | ||
| // Clean previous build | ||
| console.log('🧹 Cleaning previous build...'); | ||
| await $`rm -rf dist`; | ||
|
|
||
| // Run linting | ||
| console.log('🔍 Running linter...'); | ||
| await $`eslint src/`; | ||
|
|
||
| // Run tests | ||
| console.log('🧪 Running tests...'); | ||
| await $`npm test`; | ||
|
|
||
| // Build the project | ||
| console.log('📦 Building project...'); | ||
| await $`npm run build`; | ||
|
|
||
| console.log('\n✅ Build completed successfully!'); | ||
| } catch (error) { | ||
| console.error('\n❌ Build failed:', error.shortMessage || error.message); | ||
| process.exit(1); | ||
|
Check failure on line 29 in examples/build-script.js
|
||
| } | ||
| } | ||
|
|
||
| build(); | ||
|
Check failure on line 33 in examples/build-script.js
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import {execa} from 'execa'; | ||
|
|
||
| // Example: Git helper functions | ||
| // Common git operations wrapped for programmatic usage | ||
|
|
||
| /** | ||
| * Get the current git branch | ||
| */ | ||
| export async function getCurrentBranch() { | ||
| const {stdout} = await execa`git branch --show-current`; | ||
| return stdout; | ||
| } | ||
|
|
||
| /** | ||
| * Check if working directory is clean | ||
| */ | ||
| export async function isWorkingDirectoryClean() { | ||
| try { | ||
| await execa`git diff --quiet`; | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the latest commit message | ||
| */ | ||
| export async function getLastCommitMessage() { | ||
| const {stdout} = await execa`git log -1 --pretty=%B`; | ||
| return stdout.trim(); | ||
| } | ||
|
|
||
| /** | ||
| * Get list of changed files | ||
| */ | ||
| export async function getChangedFiles() { | ||
| const {stdout} = await execa`git status --porcelain`; | ||
| if (!stdout) return []; | ||
|
Check failure on line 39 in examples/git-helpers.js
|
||
| return stdout.split('\n').map(line => line.slice(3).trim()).filter(Boolean); | ||
| } | ||
|
|
||
| // Demo: Run examples if executed directly | ||
| if (import.meta.url === `file://${process.argv[1]}`) { | ||
|
Check failure on line 44 in examples/git-helpers.js
|
||
| console.log('📁 Git Helpers Demo\n'); | ||
|
|
||
| const branch = await getCurrentBranch(); | ||
| console.log(`Current branch: ${branch}`); | ||
|
|
||
| const isClean = await isWorkingDirectoryClean(); | ||
| console.log(`Working directory clean: ${isClean ? '✅' : '⚠️ Has uncommitted changes'}`); | ||
|
|
||
| const lastCommit = await getLastCommitMessage(); | ||
| console.log(`Last commit: ${lastCommit.slice(0, 50)}${lastCommit.length > 50 ? '...' : ''}`); | ||
|
|
||
| const changedFiles = await getChangedFiles(); | ||
| console.log(`Changed files: ${changedFiles.length > 0 ? changedFiles.join(', ') : 'None'}`); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import {execa} from 'execa'; | ||
|
|
||
| // Example: Running multiple commands in parallel | ||
| // Demonstrates Promise.all() patterns with Execa | ||
|
|
||
| async function runParallel() { | ||
| console.log('⚡ Running parallel tasks...\n'); | ||
|
|
||
| // Example 1: Run independent commands in parallel | ||
| console.log('1️⃣ Running independent commands in parallel:'); | ||
| const startTime = Date.now(); | ||
|
|
||
| const [nodeVersion, npmVersion, gitVersion] = await Promise.all([ | ||
| execa`node --version`, | ||
| execa`npm --version`, | ||
| execa`git --version`, | ||
| ]); | ||
|
|
||
| console.log(` Node: ${nodeVersion.stdout}`); | ||
| console.log(` npm: v${npmVersion.stdout}`); | ||
| console.log(` Git: ${gitVersion.stdout}`); | ||
| console.log(` ⏱️ Time: ${Date.now() - startTime}ms\n`); | ||
|
|
||
| // Example 2: Parallel with error handling | ||
| console.log('2️⃣ Parallel with individual error handling:'); | ||
| const results = await Promise.allSettled([ | ||
| execa`node --version`, | ||
| execa`nonexistent-command`, | ||
| execa`git --version`, | ||
| ]); | ||
|
|
||
| results.forEach((result, index) => { | ||
|
Check failure on line 32 in examples/parallel-tasks.js
|
||
| if (result.status === 'fulfilled') { | ||
| console.log(` ✅ Task ${index + 1}: ${result.value.stdout.slice(0, 30)}`); | ||
| } else { | ||
| console.log(` ❌ Task ${index + 1}: Failed`); | ||
| } | ||
| }); | ||
|
|
||
| // Example 3: Parallel with timeout | ||
| console.log('\n3️⃣ Parallel with timeout:'); | ||
| try { | ||
| const {stdout} = await execa({timeout: 5000})`sleep 1 && echo "Completed"`; | ||
| console.log(` ✅ Result: ${stdout}`); | ||
| } catch (error) { | ||
| console.log(` ⏱️ Timed out or failed: ${error.shortMessage}`); | ||
| } | ||
| } | ||
|
|
||
| runParallel().catch(console.error); | ||
|
Check failure on line 50 in examples/parallel-tasks.js
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Execa Examples | ||
|
|
||
| This directory contains practical examples demonstrating common use cases of Execa. | ||
|
|
||
| ## Examples | ||
|
|
||
| - [`build-script.js`](build-script.js) - A build script with error handling and logging | ||
| - [`git-helpers.js`](git-helpers.js) - Git command wrappers for Node.js projects | ||
| - [`parallel-tasks.js`](parallel-tasks.js) - Running multiple commands in parallel | ||
| - [`stream-processing.js`](stream-processing.js) - Processing command output with transforms | ||
|
|
||
| ## Running Examples | ||
|
|
||
| All examples use ES modules. Run them with: | ||
|
|
||
| ```bash | ||
| node example-name.js | ||
| ``` | ||
|
|
||
| Make sure you have `execa` installed in your project first: | ||
|
|
||
| ```bash | ||
| npm install execa | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import {execa} from 'execa'; | ||
|
|
||
| // Example: Processing command output with transforms | ||
| // Shows how to filter and transform stdout/stderr streams | ||
|
|
||
| async function streamProcessing() { | ||
| console.log('🔄 Stream Processing Examples\n'); | ||
|
|
||
| // Example 1: Simple transform - prefix each line | ||
| console.log('1️⃣ Prefixing output lines:'); | ||
| const prefixTransform = function* (line) { | ||
|
Check failure on line 11 in examples/stream-processing.js
|
||
| yield `[${new Date().toISOString()}] ${line}`; | ||
| }; | ||
|
|
||
| await execa({stdout: prefixTransform})`echo -e "line1\nline2\nline3"`; | ||
|
|
||
| // Example 2: Filter lines containing specific text | ||
| console.log('\n2️⃣ Filtering npm audit output for high severity:'); | ||
| const filterHighSeverity = function* (line) { | ||
|
Check failure on line 19 in examples/stream-processing.js
|
||
| if (line.includes('high') || line.includes('critical')) { | ||
| yield line; | ||
| } | ||
| }; | ||
|
|
||
| try { | ||
| // Simulated: in real usage, this would be `npm audit` | ||
| await execa({stdout: filterHighSeverity})`echo -e "low: package1\nhigh: vulnerable-package\ninfo: package2"`; | ||
| } catch (error) { | ||
|
Check failure on line 28 in examples/stream-processing.js
|
||
| // npm audit exits with non-zero on vulnerabilities | ||
| console.log(' (Example output only)'); | ||
| } | ||
|
|
||
| // Example 3: Count lines transform | ||
| console.log('\n3️⃣ Line counter transform:'); | ||
| let lineCount = 0; | ||
| const countTransform = function* (line) { | ||
| lineCount++; | ||
| yield `${lineCount}: ${line}`; | ||
| }; | ||
|
|
||
| const {stdout} = await execa({stdout: countTransform, lines: true})`ls -la`; | ||
| console.log(` Total lines processed: ${lineCount}`); | ||
|
|
||
| // Example 4: Transform to uppercase | ||
| console.log('\n4️⃣ Uppercase transform:'); | ||
| const upperTransform = function* (line) { | ||
| yield line.toUpperCase(); | ||
| }; | ||
|
|
||
| await execa({stdout: upperTransform})`echo "hello world"`; | ||
| } | ||
|
|
||
| streamProcessing().catch(console.error); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those 3 examples are a little bigger in size and more specific than the other examples in our documentation, which are trying to explain the feature with as few lines as possible.
Would you please move them to the
examplesdirectory instead, then include some link to them fromlines.md? Thanks!