diff options
| author | Yoshiya Hinosawa <stibium121@gmail.com> | 2019-01-29 01:17:00 +0900 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-01-28 11:17:00 -0500 |
| commit | d2a336ee02649184c0e6c8e81bebc8e5fec1a4a0 (patch) | |
| tree | fd88eeee5f18c8f598053695c50528aa53ebc729 /format.ts | |
| parent | b79d2a9d41963b3c7cfa382d842a86c68b940666 (diff) | |
fix: skip formatting of deleted files (denoland/deno_std#161)
Original: https://github.com/denoland/deno_std/commit/c75884560117cbff8864ae48df9d2a280ba2cece
Diffstat (limited to 'format.ts')
| -rwxr-xr-x | format.ts | 28 |
1 files changed, 26 insertions, 2 deletions
@@ -38,6 +38,18 @@ async function getSourceFiles() { .split(/\r?\n/); } +async function readFileIfExists(filename: string): Promise<string | null> { + let data; + try { + data = await readFile(filename); + } catch (e) { + // The file is deleted. Returns null. + return null; + } + + return decoder.decode(data); +} + /** * Checks if the file has been formatted with prettier. */ @@ -45,7 +57,13 @@ async function checkFile( filename: string, parser: "typescript" | "markdown" ): Promise<boolean> { - const text = decoder.decode(await readFile(filename)); + const text = await readFileIfExists(filename); + + if (!text) { + // The file is deleted. Skip. + return; + } + const formatted = prettier.check(text, { parser, plugins: prettierPlugins @@ -66,7 +84,13 @@ async function formatFile( filename: string, parser: "typescript" | "markdown" ): Promise<void> { - const text = decoder.decode(await readFile(filename)); + const text = await readFileIfExists(filename); + + if (!text) { + // The file is deleted. Skip. + return; + } + const formatted = prettier.format(text, { parser, plugins: prettierPlugins |
