@node-kit/extra.fs
v3.2.0
Published
Some shared extra utilities for nodejs build-in fs modules
Downloads
20,264
Readme
@node-kit/extra.fs
Some shared extra utilities for nodejs build-in fs modules
Install
# use pnpm
$ pnpm install -D @node-kit/extra.fs
# use yarn
$ yarn add -D @node-kit/extra.fs
# use npm
$ npm install -D @node-kit/extra.fs
Usage
use import
import { cp } from '@node-kit/extra.fs'
cp()
use require
const { cp } = require('@node-kit/extra.fs')
cp()
API reference
1. cp & cpSync
copy files to new path
- Usage:
cp(path, target[, options])
&cpSync(path, target[, options])
- Parameters:
| Param | Description | Type | Optional value | Required | Default value |
| ------- | ------------ | ------------------------ | -------------- | -------- | ------------- |
| path | from path | PathLike \| PathLike[]
| - | true
| - |
| target | target path | PathLike
| - | true
| - |
| options | copy options | CpOptions
| - | false
| {}
|
- Types:
interface CpOptions extends RmDirOptions {
force?: boolean
silent?: boolean
}
declare function cp(
paths: PathLike | PathLike[],
target: PathLike,
options: CpOptions = { silent: true, force: true }
): Promise<void>
declare function cpSync(
paths: PathLike | PathLike[],
target: PathLike,
options: CpOptions = { silent: true, force: true }
): void
- Demos:
- copy ./a to ./b
import { cp, cpSync } from '@node-kit/extra.fs'
cp('./a', './b').then(() => {
console.log('copy done')
})
// or
cpSync('./a', './b')
- copy ./a and ./b to ./c
import { cp, cpSync } from '@node-kit/extra.fs'
cp(['./a', './b'], './c').then(() => {
console.log('copy done')
})
// or
cpSync(['./a', './b'], './c')
2. rm & rmSync
remove files
- Usage:
rm(path[, options])
&rmSync(path[, options])
- Parameters:
| Param | Description | Type | Optional value | Required | Default value |
| ------- | -------------- | ------------------------ | -------------- | -------- | ------------- |
| path | from path | PathLike \| PathLike[]
| - | true
| - |
| options | remove options | RmOptions
| - | false
| {}
|
- Types:
interface RmOptions extends RmDirOptions {
force?: boolean
silent?: boolean
}
declare function rm(
paths: PathLike | PathLike[],
options: RmOptions = { silent: true, force: true }
): Promise<void>
declare function rmSync(
paths: PathLike | PathLike[],
options: RmOptions = { silent: true, force: true }
): void
- Demos:
- remove ./a
import { rm, rmSync } from '@node-kit/extra.fs'
rm('./a', './b').then(() => {
console.log('remove done')
})
// or
rmSync('./a', './b')
- remove ./a and ./b
import { rm, rmSync } from '@node-kit/extra.fs'
rm(['./a', './b']).then(() => {
console.log('remove done')
})
// or
rmSync(['./a', './b'])
3. mv & mvSync
move files to new path
- Usage:
mv(path, target[, options])
&mvSync(path, target[, options])
- Parameters:
| Param | Description | Type | Optional value | Required | Default value |
| ------- | ------------ | ------------------------ | -------------- | -------- | ------------- |
| path | from path | PathLike \| PathLike[]
| - | true
| - |
| target | target path | PathLike
| - | true
| - |
| options | move options | MvOptions
| - | false
| {}
|
- Types:
interface MvOptions extends RmDirOptions {
force?: boolean
boolean?: boolean
}
declare function mv(
paths: PathLike | PathLike[],
target: PathLike,
options: MvOptions = { silent: true, force: true }
): Promise<void>
declare function mvSync(
paths: PathLike | PathLike[],
target: PathLike,
options: MvOptions = { silent: true, force: true }
): void
- Demos:
- move ./a to ./b
import { mv, mvSync } from '@node-kit/extra.fs'
mv('./a', './b').then(() => {
console.log('move done')
})
// or
mvSync('./a', './b')
- move ./a and ./b to ./c
import { mv, mvSync } from '@node-kit/extra.fs'
mv(['./a', './b'], './c').then(() => {
console.log('move done')
})
// or
mvSync(['./a', './b'], './c')
4. readJSON & readJSONSync
read json file
- Usage:
readJSON(path[, options])
&readJSONSync(path[, options])
- Parameters:
| Param | Description | Type | Optional value | Required | Default value |
| ------- | ----------------- | ---------------------------------------- | -------------- | -------- | ------------- |
| path | json path | PathLike
| - | true
| - |
| options | read file options | ReadJSONOptions \| ReadJSONSyncOptions
| - | false
| null
|
- Types:
type ReadJSONOptions = Parameters<typeof promises.readFile>[1]
type ReadJSONSyncOptions = Parameters<typeof promises.readFileSync>[1]
declare function readJSON(
...args: Parameters<typeof promises.readFile>
): Promise<Record<string, unknown> | null>
declare function readJSONSync(
...args: Parameters<typeof readFileSync>
): Record<string, unknown> | null
- Demos:
- read ./a.json
import { readJSON, readJSONSync } from '@node-kit/extra.fs'
readJSON('./a.json').then(data => {
console.log(data)
})
// or
const data = readJSONSync('./a.json')
5. writeJSON & writeJSONSync
write json to file
- Usage:
writeJSON(path, data[, options])
&writeJSONSync(path, data[, options])
- Parameters:
| Param | Description | Type | Optional value | Required | Default value |
| ------- | ------------------ | ------------------------------------ | -------------- | -------- | ------------- |
| path | json path | PathLike
| - | true
| - |
| data | json data | WriteJSONData \| WriteJSONSyncData
| - | true
| - |
| options | write file options | WriteFileOptions
| - | false
| null
|
- Types:
type WriteJSONData = Record<string, unknown> | Parameters<typeof promises.writeFile>[1]
type WriteJSONSyncData = Record<string, unknown> | Parameters<typeof promises.writeFileSync>[1]
declare function writeJSON(
file: Parameters<typeof promises.writeFile>[0],
data: Record<string, unknown> | Parameters<typeof promises.writeFile>[1],
options?: WriteFileOptions
): Promise<void>
declare function writeJSONSync(
file: PathOrFileDescriptor,
data: Record<string, unknown> | Parameters<typeof writeFileSync>[1],
options?: WriteFileOptions
): void
- Demos:
- write data to ./a.json
import { writeJSON, writeJSONSync } from '@node-kit/extra.fs'
writeJSON('./a.json', { name: 'saqqdy' }).then(data => {
console.log(data)
})
// or
const data = writeJSONSync('./a.json', { name: 'saqqdy' })
6. getRealPath & getRealPathSync
return the canonicalized absolute pathname
- Usage:
getRealPath(path)
&getRealPathSync(path)
- Parameters:
| Param | Description | Type | Optional value | Required | Default value |
| ----- | ----------- | ---------- | -------------- | -------- | ------------- |
| path | simple path | PathLike
| - | true
| - |
- Types:
declare function getRealPath(path: string): Promise<string>
declare function getRealPathSync(path: string): string
- Demos:
- simple use
import { getRealPath, getRealPathSync } from '@node-kit/extra.fs'
getRealPath('./a.json').then(data => {
console.log(data)
})
// or
const data = getRealPathSync('./a.json')
Issues & Support
Please open an issue here.