Path: blob/main/extensions/copilot/src/extension/inlineEdits/test/node/fileLoading.ts
13405 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*--------------------------------------------------------------------------------------------*/45import { readFile } from 'fs/promises';6import { join } from '../../../../util/vs/base/common/path';78type FileRef = { filePath: string | { fullPath: string } } | {/** Relative */ fileName: string; fileContents: string };910export interface ILoadedFile {11fileContents: string;12fileName?: string; // relative13filePath?: string; // absolute14}1516/**17* This function allows [tools](https://github.com/microsoft/vscode-ts-file-path-support/tree/main) to inline/extract the file content.18*/19export async function loadFile(data: FileRef): Promise<ILoadedFile> {20let fileName: string | undefined = undefined;21let filePath: string | undefined = undefined;22let fileContents: string;2324if ('fileContents' in data) {25fileName = data.fileName;26fileContents = data.fileContents;27} else {28if (typeof data.filePath === 'string') {29filePath = data.filePath;30filePath = filePath;31} else {32filePath = data.filePath.fullPath;33}3435fileContents = await readFile(filePath, 'utf8');36}3738return { fileContents, fileName, filePath };39}4041export async function loadJSON<T>(data: FileRef): Promise<T> {42const { fileContents } = await loadFile(data);43return JSON.parse(fileContents);44}4546/** See https://github.com/microsoft/vscode-ts-file-path-support */47export type RelativeFilePath<T extends string> = string & { baseDir?: T }; export interface IInlineEditBaseFile {48fileContents: string;4950/** Relative or absolute */51virtualFileName: string;52}5354export function relativeFile(relativePath: RelativeFilePath<'$dir'>): { fullPath: string } {55const fullPath = join(__dirname, relativePath);56return { fullPath };57}585960