Skip to content

Commit 684d897

Browse files
committed
Use fs/promises and fix eslint issues in headertreeprovider.ts
1 parent dc1e10b commit 684d897

File tree

1 file changed

+23
-56
lines changed

1 file changed

+23
-56
lines changed

vscode-wpilib/src/cppprovider/headertreeprovider.ts

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,31 @@ import * as vscode from 'vscode';
66
import { logger } from '../logger';
77
import { IEnabledBuildTypes } from './apiprovider';
88
import { IToolChain } from './jsonformats';
9+
import { readdir, stat } from 'fs/promises';
910

1011
//#region Utilities
1112

12-
function handleResult<T>(
13-
resolve: (result: T) => void,
14-
reject: (error: Error) => void,
15-
error: Error | null | undefined,
16-
result: T
17-
): void {
18-
if (error) {
19-
reject(massageError(error));
20-
} else {
21-
resolve(result);
22-
}
23-
}
24-
2513
function massageError(error: Error & { code?: string }): Error {
26-
if (error.code === 'ENOENT') {
27-
return vscode.FileSystemError.FileNotFound();
28-
}
29-
30-
if (error.code === 'EISDIR') {
31-
return vscode.FileSystemError.FileIsADirectory();
14+
switch (error.code) {
15+
case 'ENOENT':
16+
return vscode.FileSystemError.FileNotFound();
17+
case 'EISDIR':
18+
return vscode.FileSystemError.FileIsADirectory();
19+
case 'EEXIST':
20+
return vscode.FileSystemError.FileExists();
21+
case 'EPERM':
22+
case 'EACCESS':
23+
return vscode.FileSystemError.NoPermissions();
24+
default:
25+
return error;
3226
}
33-
34-
if (error.code === 'EEXIST') {
35-
return vscode.FileSystemError.FileExists();
36-
}
37-
38-
if (error.code === 'EPERM' || error.code === 'EACCESS') {
39-
return vscode.FileSystemError.NoPermissions();
40-
}
41-
42-
return error;
4327
}
4428

45-
function normalizeNFC(items: string): string;
46-
function normalizeNFC(items: string[]): string[];
47-
function normalizeNFC(items: string | string[]): string | string[] {
29+
function normalizeNFC(items: string[]): string[] {
4830
if (process.platform !== 'darwin') {
4931
return items;
5032
}
51-
52-
if (Array.isArray(items)) {
53-
return items.map((item) => item.normalize('NFC'));
54-
}
55-
56-
return items.normalize('NFC');
57-
}
58-
59-
function readdir(pth: string): Promise<string[]> {
60-
return new Promise<string[]>((resolve, reject) => {
61-
fs.readdir(pth, (error, children) =>
62-
handleResult(resolve, reject, error, normalizeNFC(children))
63-
);
64-
});
65-
}
66-
67-
function stat(pth: string): Promise<fs.Stats> {
68-
return new Promise<fs.Stats>((resolve, reject) => {
69-
fs.stat(pth, (error, st) => handleResult(resolve, reject, error, st));
70-
});
33+
return items.map((item) => item.normalize('NFC'));
7134
}
7235

7336
export class FileStat implements vscode.FileStat {
@@ -276,7 +239,11 @@ export class HeaderTreeProvider implements vscode.TreeDataProvider<Entry> {
276239
}
277240

278241
private async _stat(pth: string): Promise<vscode.FileStat> {
279-
return new FileStat(await stat(pth));
242+
try {
243+
return new FileStat(await stat(pth));
244+
} catch (e: any) {
245+
throw massageError(e);
246+
}
280247
}
281248

282249
private readDirectory(
@@ -288,9 +255,9 @@ export class HeaderTreeProvider implements vscode.TreeDataProvider<Entry> {
288255
private async _readDirectory(uri: vscode.Uri): Promise<[string, vscode.FileType][]> {
289256
let children: string[] = [];
290257
try {
291-
children = await readdir(uri.fsPath);
292-
} catch (err) {
293-
logger.log('Directory Warning', err);
258+
children = normalizeNFC(await readdir(uri.fsPath));
259+
} catch (err: any) {
260+
logger.log('Directory Warning', massageError(err));
294261
}
295262

296263
const result: [string, vscode.FileType][] = [];

0 commit comments

Comments
 (0)