Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/jsonOutputPrinter.ts
13383 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import * as fs from 'fs';
6
import * as path from 'path';
7
import { SimpleRPC } from '../src/extension/onboardDebug/node/copilotDebugWorker/rpc';
8
import { createServiceIdentifier } from '../src/util/common/services';
9
import { Output, STDOUT_FILENAME } from './simulation/shared/sharedTypes';
10
11
12
export const IJSONOutputPrinter = createServiceIdentifier<IJSONOutputPrinter>('IJSONOutputPrinter');
13
14
export interface IJSONOutputPrinter {
15
readonly _serviceBrand: undefined;
16
flush?(outputPath: string): Promise<void>;
17
print(obj: Output): void;
18
}
19
20
export class ConsoleJSONOutputPrinter implements IJSONOutputPrinter {
21
declare readonly _serviceBrand: undefined;
22
23
print(obj: Output): void {
24
console.log(JSON.stringify(obj));
25
}
26
}
27
28
export class CollectingJSONOutputPrinter implements IJSONOutputPrinter {
29
declare readonly _serviceBrand: undefined;
30
31
private readonly outputs: Output[] = [];
32
33
print(obj: Output): void {
34
this.outputs.push(obj);
35
}
36
37
async flush(outputPath: string): Promise<void> {
38
const filePath = path.join(outputPath, STDOUT_FILENAME);
39
await fs.promises.writeFile(filePath, JSON.stringify(this.outputs, null, '\t'));
40
}
41
}
42
43
export class NoopJSONOutputPrinter implements IJSONOutputPrinter {
44
declare readonly _serviceBrand: undefined;
45
46
print(obj: Output): void {
47
// noop
48
}
49
}
50
51
export class ProxiedSONOutputPrinter implements IJSONOutputPrinter {
52
declare readonly _serviceBrand: undefined;
53
54
public static registerTo(instance: IJSONOutputPrinter, rpc: SimpleRPC): IJSONOutputPrinter {
55
rpc.registerMethod('ProxiedJSONOutputPrinter.print', (obj: Output) => instance.print(obj));
56
rpc.registerMethod('ProxiedJSONOutputPrinter.flush', (outputPath: string) => instance.flush?.(outputPath));
57
return instance;
58
}
59
60
constructor(
61
private readonly rpc: SimpleRPC,
62
) { }
63
64
print(obj: Output): void {
65
this.rpc.callMethod('ProxiedJSONOutputPrinter.print', obj);
66
}
67
68
async flush(outputPath: string): Promise<void> {
69
await this.rpc.callMethod('ProxiedJSONOutputPrinter.flush', outputPath);
70
}
71
}
72
73