sapling/addons/shared/fs.ts
Jun Wu d238184c3e codemod: use node: prefix for node builtin modules
Summary:
The `node:` prefix has a few benefits:
- Indicate the import is node builtin module explicitly. No need to look up in node_modules.
- Compatibile with Deno.

To make coding style consistant, migrate existing imports to use `node:` prefix style.
This is done by:

  sd "^(import.*')(_http_agent|_http_client|_http_common|_http_incoming|_http_outgoing|_http_server|_stream_duplex|_stream_passthrough|_stream_readable|_stream_transform|_stream_wrap|_stream_writable|_tls_common|_tls_wrap|assert|assert/strict|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|diagnostics_channel|dns|dns/promises|domain|events|fs|fs/promises|http|http2|https|inspector|module|net|os|path|path/posix|path/win32|perf_hooks|process|punycode|querystring|readline|repl|stream|stream/consumers|stream/promises|stream/web|string_decoder|sys|timers|timers/promises|tls|trace_events|tty|url|util|util/types|v8|vm|wasi|worker_threads|zlib)(.*)" '${1}node:$2$3' `rg -l import | grep -v node_modules`

Then manually fix import order issues via `yarn eslint --fix`.

The module list is from https://nodejs.org/api/module.html#modulebuiltinmodules

Reviewed By: evangrayk

Differential Revision: D56450070

fbshipit-source-id: 0a7defcb8c68932c80fbcd986f50faabdcf940af
2024-04-23 12:01:57 -07:00

27 lines
619 B
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as fs from 'node:fs';
/**
* Check if file path exists.
* May still throw non-ENOENT fs access errors.
* Note: this works on Node 10.x
*/
export function exists(file: string): Promise<boolean> {
return fs.promises
.stat(file)
.then(() => true)
.catch((error: NodeJS.ErrnoException) => {
if (error.code === 'ENOENT') {
return false;
} else {
throw error;
}
});
}