-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bcbeaa
commit 0429299
Showing
9 changed files
with
275 additions
and
139 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,41 @@ | ||
import { EventEmitter } from 'vscode' | ||
import { CliEvent, CliResult } from '.' | ||
import { Process } from '../process/execution' | ||
|
||
export const notifyStarted = ( | ||
baseEvent: CliEvent, | ||
processStarted: EventEmitter<CliEvent> | ||
): void => processStarted.fire(baseEvent) | ||
|
||
export const notifyOutput = ( | ||
process: Process, | ||
processOutput: EventEmitter<string> | ||
): void => { | ||
process.all?.on('data', chunk => | ||
processOutput.fire( | ||
(chunk as Buffer) | ||
.toString() | ||
.split(/(\r?\n)/g) | ||
.join('\r') | ||
) | ||
) | ||
} | ||
|
||
export const notifyCompleted = ( | ||
{ command, pid, cwd, duration, exitCode, stderr }: CliResult, | ||
processCompleted: EventEmitter<CliResult> | ||
): void => | ||
processCompleted.fire({ | ||
command, | ||
cwd, | ||
duration, | ||
exitCode, | ||
pid, | ||
stderr: stderr?.replace(/\n+/g, '\n') | ||
}) | ||
|
||
export const captureStdErr = (process: Process): string => { | ||
let stderr = '' | ||
process.stderr?.on('data', chunk => (stderr += (chunk as Buffer).toString())) | ||
return stderr | ||
} |
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,92 @@ | ||
import { Event, EventEmitter } from 'vscode' | ||
import { CliEvent, CliResult, CliStarted } from '.' | ||
import { | ||
captureStdErr, | ||
notifyCompleted, | ||
notifyOutput, | ||
notifyStarted | ||
} from './util' | ||
import { getCommandString } from './command' | ||
import { ProcessOptions, createProcess } from '../process/execution' | ||
import { StopWatch } from '../util/time' | ||
import { PseudoTerminal } from '../vscode/pseudoTerminal' | ||
import { DeferredDisposable } from '../class/deferred' | ||
|
||
export class ViewableCliProcess extends DeferredDisposable { | ||
public readonly onDidDispose: Event<void> | ||
|
||
private readonly pseudoTerminal: PseudoTerminal | ||
|
||
constructor( | ||
termName: string, | ||
options: ProcessOptions, | ||
processStarted: EventEmitter<CliStarted>, | ||
processCompleted: EventEmitter<CliResult> | ||
) { | ||
super() | ||
const processOutput = this.dispose.track(new EventEmitter<string>()) | ||
const terminalClosed = this.dispose.track(new EventEmitter<void>()) | ||
const onDidCloseTerminal = terminalClosed.event | ||
|
||
this.pseudoTerminal = this.dispose.track( | ||
new PseudoTerminal(processOutput, terminalClosed, termName) | ||
) | ||
|
||
this.pseudoTerminal.setBlocked(true) | ||
void this.show().then(() => this.deferred.resolve()) | ||
|
||
this.createProcess(options, processStarted, processOutput, processCompleted) | ||
|
||
const disposed = this.dispose.track(new EventEmitter<void>()) | ||
this.onDidDispose = disposed.event | ||
|
||
this.dispose.track( | ||
onDidCloseTerminal(() => { | ||
disposed.fire() | ||
this.dispose() | ||
}) | ||
) | ||
} | ||
|
||
public show() { | ||
return this.pseudoTerminal.openCurrentInstance() | ||
} | ||
|
||
private createProcess( | ||
options: ProcessOptions, | ||
processStarted: EventEmitter<CliEvent>, | ||
processOutput: EventEmitter<string>, | ||
processCompleted: EventEmitter<CliResult> | ||
) { | ||
processOutput.fire( | ||
`Running: ${ | ||
options.executable === 'git' ? 'git' : 'dvc' | ||
} ${options.args.join(' ')}\r\n\n` | ||
) | ||
const stopWatch = new StopWatch() | ||
const process = this.dispose.track(createProcess(options)) | ||
|
||
const command = getCommandString(options) | ||
const baseEvent = { command, cwd: options.cwd, pid: process.pid } | ||
|
||
notifyStarted(baseEvent, processStarted) | ||
|
||
notifyOutput(process, processOutput) | ||
|
||
const stderr = captureStdErr(process) | ||
|
||
void process.on('close', exitCode => { | ||
void this.dispose.untrack(process) | ||
notifyCompleted( | ||
{ | ||
...baseEvent, | ||
duration: stopWatch.getElapsedTime(), | ||
exitCode, | ||
stderr | ||
}, | ||
processCompleted | ||
) | ||
processOutput.fire('\r\nPress any key to close\r\n\n') | ||
}) | ||
} | ||
} |
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.