Skip to content

Commit

Permalink
feat: add more apis
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Mar 5, 2025
1 parent 0282784 commit cf367ff
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 93 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"tsdown": "^0.6.0",
"tsx": "^4.19.3",
"typescript": "^5.7.3",
"unplugin-quansync": "^0.3.3",
"vitest": "^3.0.7"
},
"engines": {
Expand Down
58 changes: 0 additions & 58 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 122 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs, {
type symlink as _symlink,
type BigIntStats,
type BufferEncodingOption,
type Dirent,
Expand All @@ -9,18 +10,18 @@ import fs, {
type RmOptions,
type StatOptions,
type Stats,
type TimeLike,
type WriteFileOptions,
} from 'node:fs'
import { quansync } from 'quansync/macro'
import { quansync, type QuansyncFn } from 'quansync'
import type { Buffer } from 'node:buffer'
import type { QuansyncFn } from 'quansync'

/**
* @link https://nodejs.org/api/fs.html#fspromisesreadfilepath-options
*/
export const readFile = quansync({
sync: (path, options) => fs.readFileSync(path, options),
async: (path, options) => fs.promises.readFile(path, options),
sync: fs.readFileSync,
async: fs.promises.readFile as any,
}) as QuansyncFn<
Buffer,
[
Expand Down Expand Up @@ -52,20 +53,16 @@ export const writeFile: QuansyncFn<
options?: WriteFileOptions | undefined,
]
> = quansync({
sync: (
file: PathLike,
data: string | NodeJS.ArrayBufferView,
options?: WriteFileOptions,
) => fs.writeFileSync(file, data, options),
async: (file, data, options) => fs.promises.writeFile(file, data, options),
sync: fs.writeFileSync,
async: fs.promises.writeFile as any,
})

/**
* @link https://nodejs.org/api/fs.html#fspromisesunlinkpath
*/
export const unlink: QuansyncFn<void, [path: PathLike]> = quansync({
sync: (path: PathLike) => fs.unlinkSync(path),
async: (path) => fs.promises.unlink(path),
sync: fs.unlinkSync,
async: fs.promises.unlink,
})

/**
Expand All @@ -75,16 +72,16 @@ export const access: QuansyncFn<
void,
[path: PathLike, mode?: number | undefined]
> = quansync({
sync: (path: PathLike, mode?: number) => fs.accessSync(path, mode),
async: (path, mode) => fs.promises.access(path, mode),
sync: fs.accessSync,
async: fs.promises.access,
})

/**
* @link https://nodejs.org/api/fs.html#fspromisesstatpath-options
*/
export const stat = quansync({
sync: (path: PathLike, options) => fs.statSync(path, options),
async: (path, options) => fs.promises.stat(path, options),
sync: fs.statSync,
async: fs.promises.stat,
}) as QuansyncFn<
Stats,
[path: PathLike, opts?: StatOptions & { bigint?: false | undefined }]
Expand All @@ -96,8 +93,8 @@ export const stat = quansync({
QuansyncFn<Stats | BigIntStats, [path: PathLike, opts?: StatOptions]>

export const lstat = quansync({
sync: (path: PathLike, options) => fs.lstatSync(path, options),
async: (path, options) => fs.promises.lstat(path, options),
sync: fs.lstatSync,
async: fs.promises.lstat,
}) as typeof stat

/**
Expand All @@ -107,9 +104,8 @@ export const cp: QuansyncFn<
void,
[src: PathLike, dest: PathLike, mode?: number | undefined]
> = quansync({
sync: (src: PathLike, dest: PathLike, mode?: number) =>
fs.copyFileSync(src, dest, mode),
async: (src, dest, mode) => fs.promises.copyFile(src, dest, mode),
sync: fs.copyFileSync,
async: fs.promises.copyFile,
})

/**
Expand All @@ -119,16 +115,16 @@ export const rm: QuansyncFn<
void,
[path: PathLike, options?: RmOptions | undefined]
> = quansync({
sync: (path: PathLike, options?: RmOptions) => fs.rmSync(path, options),
async: (path, options) => fs.promises.rm(path, options),
sync: fs.rmSync,
async: fs.promises.rm,
})

/**
* @link https://nodejs.org/api/fs.html#fspromisesmkdirpath-options
*/
export const mkdir = quansync({
sync: (path: PathLike, options) => fs.mkdirSync(path, options),
async: (path, options) => fs.promises.mkdir(path, options),
sync: fs.mkdirSync,
async: fs.promises.mkdir,
}) as QuansyncFn<
string | undefined,
[path: PathLike, options: MakeDirectoryOptions & { recursive: true }]
Expand All @@ -149,17 +145,16 @@ export const mkdir = quansync({
*/
export const rename: QuansyncFn<void, [oldPath: PathLike, newPath: PathLike]> =
quansync({
sync: (oldPath: PathLike, newPath: PathLike) =>
fs.renameSync(oldPath, newPath),
async: (oldPath, newPath) => fs.promises.rename(oldPath, newPath),
sync: fs.renameSync,
async: fs.promises.rename,
})

/**
* @link https://nodejs.org/api/fs.html#fspromisesreaddirpath-options
*/
export const readdir = quansync({
sync: (path: PathLike, options) => fs.readdirSync(path, options),
async: (path, options) => fs.promises.readdir(path, options),
sync: fs.readdirSync,
async: fs.promises.readdir,
}) as QuansyncFn<
string[],
[
Expand Down Expand Up @@ -214,8 +209,24 @@ export const readdir = quansync({
* @link https://nodejs.org/api/fs.html#fspromisesrealpathpath-options
*/
export const realpath = quansync({
sync: (path: PathLike, options) => fs.realpathSync(path, options),
async: (path, options) => fs.promises.realpath(path, options),
sync: fs.realpathSync,
async: fs.promises.realpath,
}) as QuansyncFn<
string,
[path: PathLike, options?: ObjectEncodingOptions | BufferEncoding | null]
> &
QuansyncFn<Buffer, [path: PathLike, options: BufferEncodingOption]> &
QuansyncFn<
string | Buffer,
[path: PathLike, options?: ObjectEncodingOptions | BufferEncoding | null]
>

/**
* @link https://nodejs.org/api/fs.html#fspromisesreadlinkpath-options
*/
export const readlink = quansync({
sync: fs.readlinkSync,
async: fs.promises.readlink,
}) as QuansyncFn<
string,
[path: PathLike, options?: ObjectEncodingOptions | BufferEncoding | null]
Expand All @@ -225,3 +236,82 @@ export const realpath = quansync({
string | Buffer,
[path: PathLike, options?: ObjectEncodingOptions | BufferEncoding | null]
>

/**
* @link https://nodejs.org/api/fs.html#fspromisessymlinktarget-path-type
*/
export const symlink: QuansyncFn<
void,
[target: PathLike, path: PathLike, type?: _symlink.Type | null]
> = quansync({
sync: fs.symlinkSync,
async: fs.promises.symlink,
})

/**
* @link https://nodejs.org/api/fs.html#fspromiseschownpath-uid-gid
*/
export const chown: QuansyncFn<
void,
[path: PathLike, uid: number, gid: number]
> = quansync({
sync: fs.chownSync,
async: fs.promises.chown,
})

/**
* @link https://nodejs.org/api/fs.html#fspromiseslchownpath-uid-gid
*/
export const lchown: QuansyncFn<
void,
[path: PathLike, uid: number, gid: number]
> = quansync({
sync: fs.lchownSync,
async: fs.promises.lchown,
})

/**
* @link https://nodejs.org/api/fs.html#fspromiseschmodpath-mode
*/
export const chmod: QuansyncFn<void, [path: PathLike, mode: Mode]> = quansync({
sync: fs.chmodSync,
async: fs.promises.chmod,
})

/**
* @link https://nodejs.org/api/fs.html#fspromisesutimespath-atime-mtime
*/
export const utimes: QuansyncFn<
void,
[path: PathLike, atime: TimeLike, mtime: TimeLike]
> = quansync({
sync: fs.utimesSync,
async: fs.promises.utimes,
})

/**
* @link https://nodejs.org/api/fs.html#fspromiseslutimespath-atime-mtime
*/
export const lutimes: QuansyncFn<
void,
[path: PathLike, atime: TimeLike, mtime: TimeLike]
> = quansync({
sync: fs.lutimesSync,
async: fs.promises.lutimes,
})

/**
* @link https://nodejs.org/api/fs.html#fspromisesmkdtempprefix-options
*/
export const mkdtemp = quansync({
sync: fs.mkdtempSync,
async: fs.promises.mkdtemp,
}) as QuansyncFn<
string,
[prefix: string, options?: ObjectEncodingOptions | BufferEncoding | null]
> &
QuansyncFn<Buffer, [prefix: string, options: BufferEncodingOption]> &
QuansyncFn<
string | Buffer,
[prefix: string, options?: ObjectEncodingOptions | BufferEncoding | null]
>
2 changes: 0 additions & 2 deletions tsdown.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineConfig } from 'tsdown'
import Quansync from 'unplugin-quansync/rolldown'

export default defineConfig({
entry: ['./src/index.ts'],
Expand All @@ -8,5 +7,4 @@ export default defineConfig({
clean: true,
dts: { transformer: 'oxc' },
platform: 'node',
plugins: [Quansync()],
})

0 comments on commit cf367ff

Please sign in to comment.