Skip to content

Repository files navigation

@magic/fs

exports all fs.promises + exists + mkdirp + rmrf + getFiles + getDirs functions.

NPM versionLinux Build StatusWindows Build StatusCoverage StatusGreenkeeper badgeKnown Vulnerabilities

html-docs

installation

be in a nodejs project

npm install @magic/fs

import

importfsfrom'@magic/fs'construn=async()=>{awaitfs.mkdirp('./test_235235/dir/to/create')console.log('dir created')awaitfs.rmrf('./test_235235')console.log('dir deleted, even though it had contents.')}run()

promises

promises from fs:

access
copyFile
open
opendir
rename
truncate
rmdir
mkdir
readdir
readlink
symlink
lstat
stat
link
unlink
chmod
lchmod
lchown
chown
utimes
realpath
mkdtemp
writeFile
appendFile
readFile
exists
readDir
readfile
rmDir

export overloads:

// rmdir, rmDir// readfile, readFile// readdir, readDir

Additional functions:

mkdirp

same as mkdir -p on unix

awaitfs.mkdirp('./path/to/dir')

rmrf

same as rm -rf on unix.

will not work outside process.cwd()

awaitfs.rmrf('./path/to/dir')

exists

same as fs.exists, but promisified and ready for esmodules.

getDirectories

get a list of directories in a directory, recursively.

importfsfrom'@magic/fs'construn=async()=>{// first level directoriesconstdirectories=awaitfs.getDirectories(process.cwd(),false)console.log(directories)// first level againconstdirs=awaitfs.getDirectories(process.cwd(),{depth: false})// recursive runconstdeepDirectories=awaitfs.getDirectories(process.cwd())console.log(deepDirectories)// recursive run with specified depthconstdeepDirectoriesDepth2=awaitfs.getDirectories(process.cwd(),{depth: 2})console.log(deepDirectoriesDepth2)// use /some/dir as root instead of process.cwd()constdeepDirWithRoot=awaitfs.getDirectories(process.cwd(),{depth: true,root: '/some/dir'})console.log(deepDirWithRoot)}run()

getFiles

get a list of files in a directory, recursively.

importfsfrom'@magic/fs'construn=async()=>{// first level directoriesconstfiles=awaitfs.getFiles(process.cwd())console.log(files)// recursive runconstdeepFiles=awaitfs.getFiles(process.cwd(),{depth: true})console.log(deepFiles)// recursive run with specified depth,constdeepFilesDepth2=awaitfs.getFiles(process.cwd(),{depth: 2})console.log(deepFilesDepth2)}run()

getFileType

get the file type of a file, based on extension, and defaulting to "txt"

importfsfrom'@magic/fs'constfileType=fs.getFileType('html.html')console.log(fileType,fileType==='html')constnonFileType=fs.getFileType()console.log(nonFileType,nonFileType==='txt')

changelog

0.0.1

first release

0.0.2

  • bump required node version
  • update dependencies

0.0.3

better error messages

0.0.4

rmrf returns true if directory does not exist.

0.0.5

use @magic/mime-types to export contentTypes

0.0.6

bump required node version to 14.2.0

0.0.7

rmrf: add dryRun option

0.0.8

update dependencies

0.0.9

  • remove unused imports from getDirectories
  • getDirectories and getFiles now accept a number as second argument.
// if a number is given instead of true/false, then this is the depth of recursion.
getFiles(directory, 2) // two levels down

0.0.10

  • bump required node version to 14.15.4
  • update dependencies
0.0.11

update dependencies (@magic/mime-types)

0.0.12

export all functions from native fs

0.0.13
  • getDirectories, getFiles: default root to process.cwd() if first argument is an array and root is not passed
  • update dependencies
0.0.14

update dependencies (@magic/mime-types)

0.0.15

update @magic/types to avoid circular dependency

0.0.16

add deprecation warning for calls to fs.getDirectories, fs.getFilePath and fs.getFiles that do not use an options object

0.0.17

update dependencies

0.0.18

update mime-types

0.0.19

update dependencies

0.0.20

update dependencies

0.0.21
  • add missing @magic/fs dependency
  • update dependencies
0.0.22

update dependencies

0.0.23

update dependencies

0.0.24

getFiles: allow files to be filtered by extension, using the .extension key of the second parameter

0.0.25

update dependencies

0.0.26

update dependencies

0.0.27

update dependencies

0.0.28

update dependencies

0.0.29
  • update dependencies
  • getFiles and getDirectories now accept maxDepth and minDepth config args
0.0.30
  • update dependencies
  • remove deprecated getFiles and getDirectories api
  • add minDepth option
0.0.31
  • add tests for [...threedot_dirs]
  • update dependencies
0.0.32 - broken import mapping
  • add jsdoc types
  • update dependencies
0.0.33
  • fix import mapping from .mjs to .js
0.0.34
  • update types
0.0.35
  • fs.getDirectories maxDepth will be at least 2 if noRoot is set.
  • update dependencies
0.0.36
  • getFiles: options.root can be undefined, leading to correct depth calculations
0.0.37
  • update dependencies
0.0.38
  • update types
  • update dependencies
0.0.39
  • update dependencies
0.0.40 - unreleased

...

About

node fs promises + additional functions

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages