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 2 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
41 changes: 33 additions & 8 deletions readme.md
Expand Up @@ -41,16 +41,21 @@ 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 () => {
// Pipe the child process stdout to the current stdout
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Good spotting @ehmicky !

// Catching an error
try {
await execa('unknown', ['command']);
Expand All @@ -77,20 +82,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 +133,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