addons/shared/fs.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import * as fs from 'node:fs';
b69ab319
b69ab3110/**
b69ab3111 * Check if file path exists.
b69ab3112 * May still throw non-ENOENT fs access errors.
b69ab3113 * Note: this works on Node 10.x
b69ab3114 */
b69ab3115export function exists(file: string): Promise<boolean> {
b69ab3116 return fs.promises
b69ab3117 .stat(file)
b69ab3118 .then(() => true)
b69ab3119 .catch((error: NodeJS.ErrnoException) => {
b69ab3120 if (error.code === 'ENOENT') {
b69ab3121 return false;
b69ab3122 } else {
b69ab3123 throw error;
b69ab3124 }
b69ab3125 });
b69ab3126}