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