forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.ts
More file actions
54 lines (49 loc) · 2.52 KB
/
Copy pathproc.ts
File metadata and controls
54 lines (49 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { EventEmitter } from 'events';
import { inject, injectable } from 'inversify';
import 'rxjs/add/observable/of';
import { Observable } from 'rxjs/Observable';
import { ExecutionResult, IProcessService, ObservableExecutionResult, Output, SpawnOptions } from '../../client/common/process/types';
type ExecObservableCallback = (result: Observable<Output<string>> | Output<string>) => void;
type ExecCallback = (result: ExecutionResult<string>) => void;
export const IOriginalProcessService = Symbol('IProcessService');
@injectable()
export class MockProcessService extends EventEmitter implements IProcessService {
constructor( @inject(IOriginalProcessService) private procService: IProcessService) {
super();
}
public onExecObservable(handler: (file: string, args: string[], options: SpawnOptions, callback: ExecObservableCallback) => void) {
this.on('execObservable', handler);
}
public execObservable(file: string, args: string[], options: SpawnOptions = {}): ObservableExecutionResult<string> {
let value: Observable<Output<string>> | Output<string> | undefined;
let valueReturned = false;
this.emit('execObservable', file, args, options, (result: Observable<Output<string>> | Output<string>) => { value = result; valueReturned = true; });
if (valueReturned) {
const output = value as Output<string>;
if (['stderr', 'stdout'].some(source => source === output.source)) {
return {
// tslint:disable-next-line:no-any
proc: {} as any,
out: Observable.of(output)
};
} else {
return {
// tslint:disable-next-line:no-any
proc: {} as any,
out: value as Observable<Output<string>>
};
}
} else {
return this.procService.execObservable(file, args, options);
}
}
public onExec(handler: (file: string, args: string[], options: SpawnOptions, callback: ExecCallback) => void) {
this.on('exec', handler);
}
public async exec(file: string, args: string[], options: SpawnOptions = {}): Promise<ExecutionResult<string>> {
let value: ExecutionResult<string> | undefined;
let valueReturned = false;
this.emit('exec', file, args, options, (result: ExecutionResult<string>) => { value = result; valueReturned = true; });
return valueReturned ? value! : this.procService.exec(file, args, options);
}
}