-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb42ba5
commit a73caaa
Showing
6 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Autogenerated | ||
*.d.ts | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# @isbl/fs | ||
|
||
Wraps `node:fs/promises` and sync functions from `node:fs` to add proper async | ||
error stack. | ||
|
||
Install | ||
|
||
```sh | ||
yarn add @isbl/fs | ||
``` | ||
|
||
If you are using typescript then you should also install type definitions | ||
|
||
```sh | ||
yarn add -D @types/node | ||
``` | ||
|
||
## Usage | ||
|
||
Same as original wrapped functions, also includes typescript definitions. | ||
Note that this module is ESM-only so it can't be imported using `require`. | ||
|
||
```ts | ||
import fs from '@isbl/fs' | ||
await fs.readFile(...) | ||
|
||
// or | ||
|
||
import { readFile } from '@isbl/fs' | ||
await readFile(...) | ||
``` | ||
|
||
## Available functions | ||
|
||
See start of `index.mjs` file. It lists all the exports. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as fsPromises from "fs/promises" | ||
import * as fsSync from "fs" | ||
|
||
// Adding new function: | ||
// 1. add `export const ...` here | ||
// 2. add it to `export default { ... }` | ||
export const readFile = wrap("readFile") | ||
export const writeFile = wrap("writeFile") | ||
export const readFileSync = wrapSync("readFileSync") | ||
export const mkdir = wrap("mkdir") | ||
export const copyFile = wrap("copyFile") | ||
export const rm = wrap("rm") | ||
export default { readFile, writeFile, readFileSync, mkdir, copyFile, rm } | ||
|
||
/** @type {<M extends keyof typeof fsPromises>(method: M) => fsPromises[M]} */ | ||
function wrap(method) { | ||
if (!fsPromises[method]) return null | ||
Object.defineProperty(wrapper, "name", { writable: true }) | ||
wrapper.name = fsPromises[method].name | ||
Object.defineProperty(wrapper, "name", { writable: false }) | ||
return wrapper | ||
async function wrapper(...args) { | ||
try { | ||
return await fsPromises[method](...args) | ||
} catch (e) { | ||
const opt = { cause: e } | ||
const err = new Error( | ||
`Error in fsPromises.${method} ${args[0]}\n${e.message}`, | ||
opt, | ||
) | ||
Error.captureStackTrace?.(err, wrapper) | ||
err.code = e.code | ||
throw err | ||
} | ||
} | ||
} | ||
|
||
/** @type <M extends keyof typeof fsSync>(method: M) => fsSync[M] */ | ||
function wrapSync(method) { | ||
if (!fsSync[method]) return null | ||
Object.defineProperty(wrapper, "name", { writable: true }) | ||
wrapper.name = fsSync[method].name | ||
Object.defineProperty(wrapper, "name", { writable: false }) | ||
return wrapper | ||
function wrapper(...args) { | ||
try { | ||
return fsSync[method](...args) | ||
} catch (e) { | ||
const opt = { cause: e } | ||
const err = new Error( | ||
`Error in fs.${method} ${args[0]}\n${e.message}`, | ||
opt, | ||
) | ||
err.code = e.code | ||
throw err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2021", | ||
"module": "ES2020", | ||
"allowJs": true, | ||
"declaration": true, | ||
"emitDeclarationOnly": true, | ||
"strict": false, | ||
"skipLibCheck": false | ||
}, | ||
"include": ["index.js"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters