Commit 12b5003
Add filesystem utils (#148)
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";
```
Note that you will need to use a `moduleResolution` of `nodenext`
(and not `node`) in your TypeScript project in order to take advantage
of this.
[1]: nodejs/node#30944
[2]: https://www.npmjs.com/package/json5
---
Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com>1 parent d0db51f commit 12b5003
File tree
7 files changed
+1387
-62
lines changed- src
7 files changed
+1387
-62
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
| |||
65 | 70 | | |
66 | 71 | | |
67 | 72 | | |
| 73 | + | |
68 | 74 | | |
69 | 75 | | |
70 | 76 | | |
| |||
79 | 85 | | |
80 | 86 | | |
81 | 87 | | |
| 88 | + | |
82 | 89 | | |
83 | 90 | | |
84 | 91 | | |
| |||
0 commit comments