Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/inlineEdits/test/node/fileLoading.ts
13405 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
6
import { readFile } from 'fs/promises';
7
import { join } from '../../../../util/vs/base/common/path';
8
9
type FileRef = { filePath: string | { fullPath: string } } | {/** Relative */ fileName: string; fileContents: string };
10
11
export interface ILoadedFile {
12
fileContents: string;
13
fileName?: string; // relative
14
filePath?: string; // absolute
15
}
16
17
/**
18
* This function allows [tools](https://github.com/microsoft/vscode-ts-file-path-support/tree/main) to inline/extract the file content.
19
*/
20
export async function loadFile(data: FileRef): Promise<ILoadedFile> {
21
let fileName: string | undefined = undefined;
22
let filePath: string | undefined = undefined;
23
let fileContents: string;
24
25
if ('fileContents' in data) {
26
fileName = data.fileName;
27
fileContents = data.fileContents;
28
} else {
29
if (typeof data.filePath === 'string') {
30
filePath = data.filePath;
31
filePath = filePath;
32
} else {
33
filePath = data.filePath.fullPath;
34
}
35
36
fileContents = await readFile(filePath, 'utf8');
37
}
38
39
return { fileContents, fileName, filePath };
40
}
41
42
export async function loadJSON<T>(data: FileRef): Promise<T> {
43
const { fileContents } = await loadFile(data);
44
return JSON.parse(fileContents);
45
}
46
47
/** See https://github.com/microsoft/vscode-ts-file-path-support */
48
export type RelativeFilePath<T extends string> = string & { baseDir?: T }; export interface IInlineEditBaseFile {
49
fileContents: string;
50
51
/** Relative or absolute */
52
virtualFileName: string;
53
}
54
55
export function relativeFile(relativePath: RelativeFilePath<'$dir'>): { fullPath: string } {
56
const fullPath = join(__dirname, relativePath);
57
return { fullPath };
58
}
59
60