| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import * as fs from 'node:fs'; |
| 9 | |
| 10 | /** |
| 11 | * Check if file path exists. |
| 12 | * May still throw non-ENOENT fs access errors. |
| 13 | * Note: this works on Node 10.x |
| 14 | */ |
| 15 | export function exists(file: string): Promise<boolean> { |
| 16 | return fs.promises |
| 17 | .stat(file) |
| 18 | .then(() => true) |
| 19 | .catch((error: NodeJS.ErrnoException) => { |
| 20 | if (error.code === 'ENOENT') { |
| 21 | return false; |
| 22 | } else { |
| 23 | throw error; |
| 24 | } |
| 25 | }); |
| 26 | } |
| 27 | |