-
-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example documentation for the output handler
- Loading branch information
Showing
3 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
## Output Handler | ||
|
||
As `simple-git` receives data on either `stdout` or `stderr` streams from the `git` | ||
child processes it spawns, the data is buffered for parsing when the process has | ||
completed. | ||
|
||
Add an `outputHandler` to the instance to pipe these streams to another target, for | ||
example piping to the main process `stdout` / `stderr`: | ||
|
||
```typescript | ||
import { InitResult, SimpleGit, simpleGit } from "simple-git"; | ||
|
||
const git: SimpleGit = simpleGit() | ||
.outputHandler((_command, stdout, stderr) => { | ||
stdout.pipe(process.stdout); | ||
stderr.pipe(process.stderr); | ||
}); | ||
|
||
const init: InitResult = await git.init(); | ||
``` | ||
|
||
Note: there is a single `outputHandler` per `simple-git` instance, calling the method again | ||
will overwrite the existing `outputHandler`. | ||
|
||
Other uses for the `outputHandler` can include tracking the processes for metrics purposes, | ||
such as checking how many commands are currently being executed: | ||
|
||
```typescript | ||
let processes = new Set(); | ||
const currentlyRunning = () => processes.size; | ||
const git = context.git.outputHandler((_command, stdout, stderr) => { | ||
const start = new Date(); | ||
const onClose = () => processes.delete(start); | ||
|
||
stdout.on('close', onClose); | ||
stderr.on('close', onClose); | ||
|
||
processes.add(start); | ||
}); | ||
|
||
expect(currentlyRunning()).toBe(0); | ||
const queue = [git.init(), git.add('*.txt')]; | ||
|
||
await wait(0); | ||
expect(currentlyRunning()).toBe(2); | ||
|
||
await Promise.all(queue); | ||
expect(currentlyRunning()).toBe(0); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createTestContext, setUpInit, SimpleGitTestContext, wait } from '@simple-git/test-utils'; | ||
|
||
describe('outputHandler', function () { | ||
let context: SimpleGitTestContext; | ||
|
||
beforeEach(async () => (context = await createTestContext())); | ||
beforeEach(async () => { | ||
await setUpInit(context); | ||
await context.files('aaa.txt', 'bbb.txt', 'ccc.other'); | ||
}); | ||
|
||
it('using the outputHandler to count currently running processes', async () => { | ||
let processes = new Set(); | ||
const currentlyRunning = () => processes.size; | ||
const git = context.git.outputHandler((_x, stdout, stderr) => { | ||
const start = new Date(); | ||
const onClose = () => processes.delete(start); | ||
|
||
stdout.on('close', onClose); | ||
stderr.on('close', onClose); | ||
|
||
processes.add(start); | ||
}); | ||
|
||
expect(currentlyRunning()).toBe(0); | ||
const queue = [git.init(), git.add('*.txt')]; | ||
|
||
await wait(0); | ||
expect(currentlyRunning()).toBe(2); | ||
|
||
await Promise.all(queue); | ||
expect(currentlyRunning()).toBe(0); | ||
}); | ||
}); |