Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/base/fileUtils.ts
13389 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
7
/**
8
* @returns file contents (utf8) or undefined if the file does not exist
9
*
10
* @throws if reading the file fails for any reason other than the file not existing
11
*/
12
export async function readFileIfExists(filePath: string): Promise<string | undefined> {
13
try {
14
const fileContents = await fs.promises.readFile(filePath, 'utf8');
15
return fileContents;
16
} catch (err) {
17
if (err.code === 'ENOENT') {
18
return undefined;
19
}
20
throw err;
21
}
22
}
23
24