|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | import { exec } from "child_process"; |
4 | | -import { createReadStream, readFile, writeFile } from "fs"; |
| 4 | +import { createReadStream } from "fs"; |
| 5 | +import { readFile, writeFile } from "fs/promises"; |
5 | 6 | import { createInterface } from "readline"; |
6 | 7 |
|
7 | 8 | /** |
@@ -50,32 +51,19 @@ const run = (...args: string[]) => |
50 | 51 | export const isTruthy = <T>(value: T): value is NonNullable<T> => !!value; |
51 | 52 |
|
52 | 53 | /** Reads a file, transforms its contents, and writes the result if different */ |
53 | | -const transformFile = (path: string, transform: (before: string) => string) => |
54 | | - new Promise<void>((resolve, reject) => { |
55 | | - readFile(path, "utf8", (err, data) => { |
56 | | - // File unreadable |
57 | | - if (err) { |
58 | | - reject(err); |
59 | | - return; |
60 | | - } |
61 | | - |
62 | | - // File empty |
63 | | - if (data === "") { |
64 | | - resolve(); |
65 | | - return; |
66 | | - } |
67 | | - |
68 | | - // File unmodified |
69 | | - const after = transform(data); |
70 | | - if (data === after) { |
71 | | - resolve(); |
72 | | - return; |
73 | | - } |
74 | | - |
75 | | - // File modified |
76 | | - writeFile(path, after, "utf8", err => (err ? reject(err) : resolve())); |
77 | | - }); |
78 | | - }); |
| 54 | +const transformFile = async ( |
| 55 | + path: string, |
| 56 | + transform: (before: string) => string, |
| 57 | +) => { |
| 58 | + const data = await readFile(path, "utf8"); |
| 59 | + if (data === "") { |
| 60 | + return; |
| 61 | + } |
| 62 | + const after = transform(data); |
| 63 | + if (data !== after) { |
| 64 | + await writeFile(path, after, "utf8"); |
| 65 | + } |
| 66 | +}; |
79 | 67 |
|
80 | 68 | const enum HookName { |
81 | 69 | Autoflake = "autoflake", |
|
0 commit comments