Skip to content

Commit

Permalink
use fs.promises isaacs#134
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Oct 5, 2024
1 parent ee57e13 commit fd7327d
Showing 1 changed file with 103 additions and 71 deletions.
174 changes: 103 additions & 71 deletions src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,99 @@
// promisify ourselves, because older nodes don't have fs.promises
// // promisify ourselves, because older nodes don't have fs.promises

// import fs, { Dirent } from 'fs'

// // sync ones just take the sync version from node
// export {
// chmodSync,
// mkdirSync,
// renameSync,
// rmdirSync,
// rmSync,
// statSync,
// lstatSync,
// unlinkSync,
// } from 'fs'

// import { readdirSync as rdSync } from 'fs'
// export const readdirSync = (path: fs.PathLike): Dirent[] =>
// rdSync(path, { withFileTypes: true })

// // unrolled for better inlining, this seems to get better performance
// // than something like:
// // const makeCb = (res, rej) => (er, ...d) => er ? rej(er) : res(...d)
// // which would be a bit cleaner.

// const chmod = (path: fs.PathLike, mode: fs.Mode): Promise<void> =>
// new Promise((res, rej) =>
// fs.chmod(path, mode, (er, ...d: any[]) => (er ? rej(er) : res(...d))),
// )

// const mkdir = (
// path: fs.PathLike,
// options?:
// | fs.Mode
// | (fs.MakeDirectoryOptions & { recursive?: boolean | null })
// | undefined
// | null,
// ): Promise<string | undefined> =>
// new Promise((res, rej) =>
// fs.mkdir(path, options, (er, made) => (er ? rej(er) : res(made))),
// )

// const readdir = (path: fs.PathLike): Promise<Dirent[]> =>
// new Promise<Dirent[]>((res, rej) =>
// fs.readdir(path, { withFileTypes: true }, (er, data) =>
// er ? rej(er) : res(data),
// ),
// )

// const rename = (oldPath: fs.PathLike, newPath: fs.PathLike): Promise<void> =>
// new Promise((res, rej) =>
// fs.rename(oldPath, newPath, (er, ...d: any[]) =>
// er ? rej(er) : res(...d),
// ),
// )

// const rm = (path: fs.PathLike, options: fs.RmOptions): Promise<void> =>
// new Promise((res, rej) =>
// fs.rm(path, options, (er, ...d: any[]) => (er ? rej(er) : res(...d))),
// )

// const rmdir = (path: fs.PathLike): Promise<void> =>
// new Promise((res, rej) =>
// fs.rmdir(path, (er, ...d: any[]) => (er ? rej(er) : res(...d))),
// )

// const stat = (path: fs.PathLike): Promise<fs.Stats> =>
// new Promise((res, rej) =>
// fs.stat(path, (er, data) => (er ? rej(er) : res(data))),
// )

// const lstat = (path: fs.PathLike): Promise<fs.Stats> =>
// new Promise((res, rej) =>
// fs.lstat(path, (er, data) => (er ? rej(er) : res(data))),
// )

// const unlink = (path: fs.PathLike): Promise<void> =>
// new Promise((res, rej) =>
// fs.unlink(path, (er, ...d: any[]) => (er ? rej(er) : res(...d))),
// )

// export const promises = {
// chmod,
// mkdir,
// readdir,
// rename,
// rm,
// rmdir,
// stat,
// lstat,
// unlink,
// }

import fs, { Dirent } from 'fs'
import fsPromises from 'fs/promises'

// sync ones just take the sync version from node
export {
chmodSync,
mkdirSync,
Expand All @@ -14,79 +105,20 @@ export {
unlinkSync,
} from 'fs'

import { readdirSync as rdSync } from 'fs'
export const readdirSync = (path: fs.PathLike): Dirent[] =>
rdSync(path, { withFileTypes: true })

// unrolled for better inlining, this seems to get better performance
// than something like:
// const makeCb = (res, rej) => (er, ...d) => er ? rej(er) : res(...d)
// which would be a bit cleaner.

const chmod = (path: fs.PathLike, mode: fs.Mode): Promise<void> =>
new Promise((res, rej) =>
fs.chmod(path, mode, (er, ...d: any[]) => (er ? rej(er) : res(...d))),
)

const mkdir = (
path: fs.PathLike,
options?:
| fs.Mode
| (fs.MakeDirectoryOptions & { recursive?: boolean | null })
| undefined
| null,
): Promise<string | undefined> =>
new Promise((res, rej) =>
fs.mkdir(path, options, (er, made) => (er ? rej(er) : res(made))),
)
fs.readdirSync(path, { withFileTypes: true })

const readdir = (path: fs.PathLike): Promise<Dirent[]> =>
new Promise<Dirent[]>((res, rej) =>
fs.readdir(path, { withFileTypes: true }, (er, data) =>
er ? rej(er) : res(data),
),
)

const rename = (oldPath: fs.PathLike, newPath: fs.PathLike): Promise<void> =>
new Promise((res, rej) =>
fs.rename(oldPath, newPath, (er, ...d: any[]) =>
er ? rej(er) : res(...d),
),
)

const rm = (path: fs.PathLike, options: fs.RmOptions): Promise<void> =>
new Promise((res, rej) =>
fs.rm(path, options, (er, ...d: any[]) => (er ? rej(er) : res(...d))),
)

const rmdir = (path: fs.PathLike): Promise<void> =>
new Promise((res, rej) =>
fs.rmdir(path, (er, ...d: any[]) => (er ? rej(er) : res(...d))),
)

const stat = (path: fs.PathLike): Promise<fs.Stats> =>
new Promise((res, rej) =>
fs.stat(path, (er, data) => (er ? rej(er) : res(data))),
)

const lstat = (path: fs.PathLike): Promise<fs.Stats> =>
new Promise((res, rej) =>
fs.lstat(path, (er, data) => (er ? rej(er) : res(data))),
)

const unlink = (path: fs.PathLike): Promise<void> =>
new Promise((res, rej) =>
fs.unlink(path, (er, ...d: any[]) => (er ? rej(er) : res(...d))),
)
fsPromises.readdir(path, { withFileTypes: true })

export const promises = {
chmod,
mkdir,
chmod: fsPromises.chmod,
mkdir: fsPromises.mkdir,
readdir,
rename,
rm,
rmdir,
stat,
lstat,
unlink,
rename: fsPromises.rename,
rm: fsPromises.rm,
rmdir: fsPromises.rmdir,
stat: fsPromises.stat,
lstat: fsPromises.lstat,
unlink: fsPromises.unlink,
}

0 comments on commit fd7327d

Please sign in to comment.