Skip to content

Commit

Permalink
feat: split out sync functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWitchBella committed Sep 2, 2021
1 parent b9e2745 commit 8ef89ab
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ import { readFile } from '@isbl/fs'
await readFile(...)
```

To use `*Sync` functions import them from `@isbl/fs/sync`

```ts
import fs from '@isbl/fs/sync'
fs.readFileSync(...)

// or

import { readFileSync } from '@isbl/fs/sync'
readFileSync(...)
```

## Available functions

See start of `index.js` file. It lists all the exports.
See start of `index.js` and `sync.js` files. They list all the exports.
27 changes: 0 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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 writeFileSync = wrapSync("writeFileSync")
export const mkdir = wrap("mkdir")
export const copyFile = wrap("copyFile")
export const rm = wrap("rm")
Expand All @@ -17,8 +14,6 @@ export const readdir = wrap("readdir")
export default {
readFile,
writeFile,
readFileSync,
writeFileSync,
mkdir,
copyFile,
rm,
Expand Down Expand Up @@ -49,25 +44,3 @@ function wrap(method) {
}
}
}

/** @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
}
}
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "@isbl/fs",
"version": "0.2.0",
"version": "0.3.0-beta.0",
"description": "Wrappers around node:fs with proper error stack support",
"repository": {
"type": "git",
"url": "https://github.com/CodeWitchBella/isbl-fs.git"
},
"exports": "./index.js",
"exports": {
".": "./index.js",
"./sync": "./sync.j"
},
"types": "./index.d.ts",
"author": "Isabella Skořepová <isabella@skorepova.info>",
"license": "MIT",
Expand Down
30 changes: 30 additions & 0 deletions sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as fsSync from "fs"

export const readFileSync = wrapSync("readFileSync")
export const writeFileSync = wrapSync("writeFileSync")
export default {
readFileSync,
writeFileSync,
}

/** @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
}
}
}

0 comments on commit 8ef89ab

Please sign in to comment.