Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the readme usage examples #385

Merged
merged 3 commits into from Nov 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 32 additions & 8 deletions readme.md
Expand Up @@ -41,16 +41,20 @@ const execa = require('execa');
})();
```

Additional examples:
### Pipe the child process stdout to the parent

```js
const execa = require('execa');

(async () => {
// Pipe the child process stdout to the current stdout
execa('echo', ['unicorns']).stdout.pipe(process.stdout);
execa('echo', ['unicorns']).stdout.pipe(process.stdout);
```

### Handling Errors

```js
const execa = require('execa');

(async () => {
// Catching an error
try {
await execa('unknown', ['command']);
Expand All @@ -77,20 +81,33 @@ const execa = require('execa');
*/
}

// Cancelling a spawned process
})();
```

### Cancelling a spawned process

```js
const execa = require('execa');

(async () => {
const subprocess = execa('node');

setTimeout(() => {
subprocess.cancel();
}, 1000);

try {
await subprocess;
} catch (error) {
console.log(subprocess.killed); // true
console.log(error.isCanceled); // true
}
})();
})()
```

### Catching an error with the sync method

// Catching an error with a sync method
```js
try {
execa.sync('unknown', ['command']);
} catch (error) {
Expand All @@ -115,16 +132,23 @@ try {
}
*/
}
```

### Kill a process

Using SIGTERM, and after 2 seconds, kill it with SIGKILL.

// Kill a process with SIGTERM, and after 2 seconds, kill it with SIGKILL
```js
const subprocess = execa('node');

setTimeout(() => {
subprocess.kill('SIGTERM', {
forceKillAfterTimeout: 2000
});
}, 1000);
```


## API

### execa(file, arguments, [options])
Expand Down