Path: blob/main/extensions/copilot/test/jsonOutputPrinter.ts
13383 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import * as fs from 'fs';5import * as path from 'path';6import { SimpleRPC } from '../src/extension/onboardDebug/node/copilotDebugWorker/rpc';7import { createServiceIdentifier } from '../src/util/common/services';8import { Output, STDOUT_FILENAME } from './simulation/shared/sharedTypes';91011export const IJSONOutputPrinter = createServiceIdentifier<IJSONOutputPrinter>('IJSONOutputPrinter');1213export interface IJSONOutputPrinter {14readonly _serviceBrand: undefined;15flush?(outputPath: string): Promise<void>;16print(obj: Output): void;17}1819export class ConsoleJSONOutputPrinter implements IJSONOutputPrinter {20declare readonly _serviceBrand: undefined;2122print(obj: Output): void {23console.log(JSON.stringify(obj));24}25}2627export class CollectingJSONOutputPrinter implements IJSONOutputPrinter {28declare readonly _serviceBrand: undefined;2930private readonly outputs: Output[] = [];3132print(obj: Output): void {33this.outputs.push(obj);34}3536async flush(outputPath: string): Promise<void> {37const filePath = path.join(outputPath, STDOUT_FILENAME);38await fs.promises.writeFile(filePath, JSON.stringify(this.outputs, null, '\t'));39}40}4142export class NoopJSONOutputPrinter implements IJSONOutputPrinter {43declare readonly _serviceBrand: undefined;4445print(obj: Output): void {46// noop47}48}4950export class ProxiedSONOutputPrinter implements IJSONOutputPrinter {51declare readonly _serviceBrand: undefined;5253public static registerTo(instance: IJSONOutputPrinter, rpc: SimpleRPC): IJSONOutputPrinter {54rpc.registerMethod('ProxiedJSONOutputPrinter.print', (obj: Output) => instance.print(obj));55rpc.registerMethod('ProxiedJSONOutputPrinter.flush', (outputPath: string) => instance.flush?.(outputPath));56return instance;57}5859constructor(60private readonly rpc: SimpleRPC,61) { }6263print(obj: Output): void {64this.rpc.callMethod('ProxiedJSONOutputPrinter.print', obj);65}6667async flush(outputPath: string): Promise<void> {68await this.rpc.callMethod('ProxiedJSONOutputPrinter.flush', outputPath);69}70}717273