Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds several utility functions that are related to the filesystem in some way. - Most of the functions here are wrappers around basic filesystem operations with better error handling. Node's `fs.promises` module is somewhat difficult to work with because [the errors it produces lack proper stacktraces][1]. The functions that directly use `fs.promises` wrap caught errors in order to supply the missing stacktraces. - Two functions make it easier to read and write JSON files (with support for JSON-compatible interfaces, such as [JSON5][2]). - One function, `createSandbox`, is designed to wrap tests that need a temporary directory to work within, such as those for a command-line tool that makes changes to the filesystem. Here are places where we currently use these utilities (or something like them): - https://github.com/MetaMask/action-utils/blob/54ddd730746668cb4c1c88b4edfa720cbecf5e32/src/file-utils.ts - https://github.com/MetaMask/create-release-branch/blob/3556dee47163c921186051be7a1f3c98e2049db9/src/fs.ts - https://github.com/MetaMask/create-release-branch/blob/3556dee47163c921186051be7a1f3c98e2049db9/tests/helpers.ts One note about these utilities is that they require Node to use and will not work in the browser. Because we already create two kinds of bundles, one for CommonJS and another ESM, it would be difficult to create a second level of bundles, one for Node and another for the browser. Instead of requiring more complexity around the bundle configuration, this commit instead introduces another way to import the package. By default, you'll get all exports that are guaranteed to be cross-platform. That means that the file utilities won't show up: ``` typescript // ❌ import { readFile } from "@metamask/utils"; ``` If you want all of the cross-platform exports plus the Node-specific ones, you will have to import "@metamask/utils/node". For instance: ``` typescript // ✅ import { readFile } from "@metamask/utils/node"; ``` [1]: nodejs/node#30944 [2]: https://www.npmjs.com/package/json5
- Loading branch information