-
-
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.
The `SimpleGitOptions` can now be used to supply a `progress` handler, called whenever progress data is received from one of the tasks. Setting the `progress` option will automatically add the `--progress` command to any `checkout` / `clone` or `pull` task when not already supplied in the `TaskOptions`. For any task that ran with `--progress` as a command (ie: even `git.raw` so long as the `--progress` command was present), monitors the `stdErr` for progress data.
- Loading branch information
Showing
11 changed files
with
283 additions
and
20 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
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,43 @@ | ||
import { SimpleGitOptions } from '../types'; | ||
import { asNumber, including } from '../utils'; | ||
|
||
import { SimpleGitPlugin } from './simple-git-plugin'; | ||
|
||
export function progressMonitorPlugin(progress: Exclude<SimpleGitOptions['progress'], void>) { | ||
const progressCommand = '--progress'; | ||
const progressMethods = ['checkout', 'clone', 'pull']; | ||
|
||
const onProgress: SimpleGitPlugin<'spawn.after'> = { | ||
type: 'spawn.after', | ||
action(_data, context) { | ||
if (!context.commands.includes(progressCommand)) { | ||
return; | ||
} | ||
|
||
context.spawned.stderr?.on('data', (chunk: Buffer) => { | ||
const message = /Receiving objects:\s*(\d+)% \((\d+)\/(\d+)\)/.exec(chunk.toString('utf8')); | ||
if (message) { | ||
progress({ | ||
method: context.method, | ||
progress: asNumber(message[1]), | ||
received: asNumber(message[2]), | ||
total: asNumber(message[3]), | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
const onArgs: SimpleGitPlugin<'spawn.args'> = { | ||
type: 'spawn.args', | ||
action(args, context) { | ||
if (!progressMethods.includes(context.method)) { | ||
return args; | ||
} | ||
|
||
return including(args, progressCommand); | ||
} | ||
} | ||
|
||
return [onArgs, onProgress]; | ||
} |
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
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
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,40 @@ | ||
import { createTestContext, newSimpleGit, SimpleGitTestContext } from '../__fixtures__'; | ||
import { SimpleGitOptions } from '../../src/lib/types'; | ||
|
||
describe('progress-monitor', () => { | ||
|
||
const upstream = 'https://github.com/steveukx/git-js.git'; | ||
|
||
let context: SimpleGitTestContext; | ||
|
||
beforeEach(async () => context = await createTestContext()); | ||
|
||
it('emits progress events', async () => { | ||
const progress = jest.fn(); | ||
const opt: Partial<SimpleGitOptions> = { | ||
baseDir: context.root, | ||
progress, | ||
}; | ||
|
||
await newSimpleGit(opt).clone(upstream); | ||
|
||
const count = progress.mock.calls.length; | ||
const last = progress.mock.calls[count - 1]; | ||
|
||
expect(count).toBeGreaterThan(0); | ||
expect(last[0]).toEqual({ | ||
method: 'clone', | ||
progress: 100, | ||
received: last[0].total, | ||
total: expect.any(Number), | ||
}); | ||
|
||
progress.mock.calls.reduce((previous, [{progress, method}]) => { | ||
expect(method).toBe('clone'); | ||
expect(progress).toBeGreaterThanOrEqual(previous); | ||
return progress; | ||
}, 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
Oops, something went wrong.