Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@node-kit/extra.fs

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@node-kit/extra.fs

Some shared extra utilities for nodejs build-in fs modules

  • 3.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@node-kit/extra.fs

Some shared extra utilities for nodejs build-in fs modules

NPM version Codacy Badge Test coverage npm download License

Sonar

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:
ParamDescriptionTypeOptional valueRequiredDefault value
pathfrom pathPathLike | PathLike[]-true-
targettarget pathPathLike-true-
optionscopy optionsCpOptions-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:
  1. copy ./a to ./b
import { cp, cpSync } from '@node-kit/extra.fs'

cp('./a', './b').then(() => {
  console.log('copy done')
})
// or
cpSync('./a', './b')
  1. 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:
ParamDescriptionTypeOptional valueRequiredDefault value
pathfrom pathPathLike | PathLike[]-true-
optionsremove optionsRmOptions-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:
  1. remove ./a
import { rm, rmSync } from '@node-kit/extra.fs'

rm('./a', './b').then(() => {
  console.log('remove done')
})
// or
rmSync('./a', './b')
  1. 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:
ParamDescriptionTypeOptional valueRequiredDefault value
pathfrom pathPathLike | PathLike[]-true-
targettarget pathPathLike-true-
optionsmove optionsMvOptions-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:
  1. move ./a to ./b
import { mv, mvSync } from '@node-kit/extra.fs'

mv('./a', './b').then(() => {
  console.log('move done')
})
// or
mvSync('./a', './b')
  1. 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:
ParamDescriptionTypeOptional valueRequiredDefault value
pathjson pathPathLike-true-
optionsread file optionsReadJSONOptions | ReadJSONSyncOptions-falsenull
  • 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:
  1. 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:
ParamDescriptionTypeOptional valueRequiredDefault value
pathjson pathPathLike-true-
datajson dataWriteJSONData | WriteJSONSyncData-true-
optionswrite file optionsWriteFileOptions-falsenull
  • 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:
  1. 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:
ParamDescriptionTypeOptional valueRequiredDefault value
pathsimple pathPathLike-true-
  • Types:
declare function getRealPath(path: string): Promise<string>

declare function getRealPathSync(path: string): string
  • Demos:
  1. 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.

License

MIT

Keywords

FAQs

Package last updated on 26 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc