Skip to content

Commit

Permalink
chore: use try catch instead of existsSync
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jun 16, 2023
1 parent c01c880 commit d275a54
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
33 changes: 23 additions & 10 deletions packages/typescript-vue-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,37 @@ const init: ts.server.PluginModuleFactory = (modules) => {
size: stats.size,
};
}
} catch { }
}
catch {
return undefined;
}
}
},
readFile(uri, encoding) {
if (uri.startsWith('file://')) {
return fs.readFileSync(uriToFileName(uri), { encoding: encoding as 'utf-8' ?? 'utf-8' });
try {
return fs.readFileSync(uriToFileName(uri), { encoding: encoding as 'utf-8' ?? 'utf-8' });
}
catch {
return undefined;
}
}
},
readDirectory(uri) {
if (uri.startsWith('file://')) {
const dirName = uriToFileName(uri);
const files = fs.existsSync(dirName) ? fs.readdirSync(dirName, { withFileTypes: true }) : [];
return files.map<[string, FileType]>(file => {
return [file.name, file.isFile() ? 1 satisfies FileType.File
: file.isDirectory() ? 2 satisfies FileType.Directory
: file.isSymbolicLink() ? 64 satisfies FileType.SymbolicLink
: 0 satisfies FileType.Unknown];
});
try {
const dirName = uriToFileName(uri);
const files = fs.readdirSync(dirName, { withFileTypes: true });
return files.map<[string, FileType]>(file => {
return [file.name, file.isFile() ? 1 satisfies FileType.File
: file.isDirectory() ? 2 satisfies FileType.Directory
: file.isSymbolicLink() ? 64 satisfies FileType.SymbolicLink
: 0 satisfies FileType.Unknown];
});
}
catch {
return [];
}
}
return [];
},
Expand Down
33 changes: 23 additions & 10 deletions packages/vue-language-service/tests/utils/createTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,37 @@ function createTester(root: string) {
size: stats.size,
};
}
} catch { }
}
catch {
return undefined;
}
}
},
readFile(uri, encoding) {
if (uri.startsWith('file://')) {
return fs.readFileSync(uriToFileName(uri), { encoding: encoding as 'utf-8' ?? 'utf-8' });
try {
return fs.readFileSync(uriToFileName(uri), { encoding: encoding as 'utf-8' ?? 'utf-8' });
}
catch {
return undefined;
}
}
},
readDirectory(uri) {
if (uri.startsWith('file://')) {
const dirName = uriToFileName(uri);
const files = fs.existsSync(dirName) ? fs.readdirSync(dirName, { withFileTypes: true }) : [];
return files.map<[string, FileType]>(file => {
return [file.name, file.isFile() ? FileType.File
: file.isDirectory() ? FileType.Directory
: file.isSymbolicLink() ? FileType.SymbolicLink
: FileType.Unknown];
});
try {
const dirName = uriToFileName(uri);
const files = fs.readdirSync(dirName, { withFileTypes: true });
return files.map<[string, FileType]>(file => {
return [file.name, file.isFile() ? FileType.File
: file.isDirectory() ? FileType.Directory
: file.isSymbolicLink() ? FileType.SymbolicLink
: FileType.Unknown];
});
}
catch {
return [];
}
}
return [];
},
Expand Down
33 changes: 23 additions & 10 deletions packages/vue-tsc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,37 @@ export function createProgram(options: ts.CreateProgramOptions) {
size: stats.size,
};
}
} catch { }
}
catch {
return undefined;
}
}
},
readFile(uri, encoding) {
if (uri.startsWith('file://')) {
return fs.readFileSync(uriToFileName(uri), { encoding: encoding as 'utf-8' ?? 'utf-8' });
try {
return fs.readFileSync(uriToFileName(uri), { encoding: encoding as 'utf-8' ?? 'utf-8' });
}
catch {
return undefined;
}
}
},
readDirectory(uri) {
if (uri.startsWith('file://')) {
const dirName = uriToFileName(uri);
const files = fs.existsSync(dirName) ? fs.readdirSync(dirName, { withFileTypes: true }) : [];
return files.map<[string, FileType]>(file => {
return [file.name, file.isFile() ? 1 satisfies FileType.File
: file.isDirectory() ? 2 satisfies FileType.Directory
: file.isSymbolicLink() ? 64 satisfies FileType.SymbolicLink
: 0 satisfies FileType.Unknown];
});
try {
const dirName = uriToFileName(uri);
const files = fs.readdirSync(dirName, { withFileTypes: true });
return files.map<[string, FileType]>(file => {
return [file.name, file.isFile() ? 1 satisfies FileType.File
: file.isDirectory() ? 2 satisfies FileType.Directory
: file.isSymbolicLink() ? 64 satisfies FileType.SymbolicLink
: 0 satisfies FileType.Unknown];
});
}
catch {
return [];
}
}
return [];
},
Expand Down

0 comments on commit d275a54

Please sign in to comment.