Skip to content

Commit c775838

Browse files
Copilotrzhao271
andauthored
Fix file read API to use modern Node.js fs promises and expand error handling
Agent-Logs-Url: https://github.com/microsoft/vscode-sublime-keybindings/sessions/96004e9f-5b64-488f-9019-381b82965b88 Co-authored-by: rzhao271 <7199958+rzhao271@users.noreply.github.com>
1 parent 9069958 commit c775838

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/extension.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ function validate(settingsFilePath: string): boolean {
9797
}
9898

9999
async function getSettings(sublimeSettingsPath: string): Promise<CategorizedSettings> {
100-
const settings: CategorizedSettings | undefined = await mapper.getMappedSettings(await readFileAsync(sublimeSettingsPath, 'utf-8'));
100+
let fileContents: string;
101+
try {
102+
fileContents = await readFileAsync(sublimeSettingsPath, 'utf-8');
103+
} catch (e: any) {
104+
vscode.window.showErrorMessage(vscode.l10n.t('Could not read Sublime settings file: {0}', e.message));
105+
throw e;
106+
}
107+
const settings: CategorizedSettings | undefined = await mapper.getMappedSettings(fileContents);
101108
settings.mappedSettings.sort((a, b) => {
102109
if (a.vscode.overwritesValue && b.vscode.overwritesValue) {
103110
return a.sublime.name.localeCompare(b.sublime.name);

src/fsWrapper.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,13 @@ import * as fs from 'fs';
22

33
export async function pathExists(stringPath: string): Promise<boolean> {
44
try {
5-
await fsAccess(stringPath, fs.constants.F_OK);
5+
await fs.promises.access(stringPath, fs.constants.F_OK);
66
return true;
77
} catch (e) {
88
return false;
99
}
1010
}
1111

12-
function fsAccess(stringPath: string, checks: number): Promise<any> {
13-
return promisifier(fs.access, stringPath, checks);
14-
}
15-
16-
// adapted from vs/base/common/async
17-
export function promisifier<T>(fn: Function, ...args: any[]): Promise<T> {
18-
return new Promise((c, e) => fn(...args, (err: any, result: any) => err ? e(err) : c(result)));
19-
}
20-
21-
export async function readFileAsync(filePath: string, encoding?: string): Promise<string> {
22-
return await promisifier<string>(fs.readFile, filePath, encoding);
12+
export async function readFileAsync(filePath: string, encoding: BufferEncoding = 'utf-8'): Promise<string> {
13+
return fs.promises.readFile(filePath, encoding);
2314
}

src/mapper.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@ export class Mapper {
3434
private async getSettings(): Promise<IMapperSettings> {
3535
if (!this.settings) {
3636
// make sure set node: false in /build/node_extension.webpack.config.json so that __dirname is correct
37-
const [mappingsFile, defaultsFile] = await Promise.all([readFileAsync(resolve(__dirname, '..', 'settings/mappings.json'), 'utf-8'),
38-
readFileAsync(resolve(__dirname, '..', 'settings/defaults.json'), 'utf-8')]);
37+
let mappingsFile: string;
38+
let defaultsFile: string;
39+
try {
40+
[mappingsFile, defaultsFile] = await Promise.all([
41+
readFileAsync(resolve(__dirname, '..', 'settings/mappings.json'), 'utf-8'),
42+
readFileAsync(resolve(__dirname, '..', 'settings/defaults.json'), 'utf-8'),
43+
]);
44+
} catch (e: any) {
45+
vscode.window.showErrorMessage(vscode.l10n.t('Could not read settings files: {0}', e.message));
46+
throw e;
47+
}
3948
this.settings = {
4049
mappings: jsoncParse(mappingsFile),
4150
defaults: (jsoncParse(defaultsFile) as [[string, any]]).map((setting) => new VscodeSetting(setting[0], setting[1])),

0 commit comments

Comments
 (0)