-
-
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.
Merge pull request #576 from steveukx/feat/progress
feat: Progress Handler
- Loading branch information
Showing
14 changed files
with
345 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
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,50 @@ | ||
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', 'push']; | ||
|
||
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 = /^([a-zA-Z ]+):\s*(\d+)% \((\d+)\/(\d+)\)/.exec(chunk.toString('utf8')); | ||
if (!message) { | ||
return; | ||
} | ||
|
||
progress({ | ||
method: context.method, | ||
stage: progressEventStage(message[1]), | ||
progress: asNumber(message[2]), | ||
processed: asNumber(message[3]), | ||
total: asNumber(message[4]), | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
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]; | ||
} | ||
|
||
function progressEventStage (input: string) { | ||
return String(input.toLowerCase().split(' ', 1)) || 'unknown'; | ||
} |
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
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 { 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 receivingUpdates = progressEventsAtStage(progress, 'receiving'); | ||
|
||
expect(receivingUpdates.length).toBeGreaterThan(0); | ||
|
||
receivingUpdates.reduce((previous, update) => { | ||
expect(update).toEqual({ | ||
method: 'clone', | ||
stage: 'receiving', | ||
progress: expect.any(Number), | ||
processed: expect.any(Number), | ||
total: expect.any(Number), | ||
}); | ||
|
||
expect(update.progress).toBeGreaterThanOrEqual(previous); | ||
return update.progress; | ||
}, 0); | ||
}); | ||
|
||
}); | ||
|
||
function progressEventsAtStage (mock: jest.Mock, stage: string) { | ||
return mock.mock.calls.filter(c => c[0].stage === stage).map(c => c[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
Oops, something went wrong.