@magic/fs

@magic/fs

nodejs fs promises and goodies.

install

be in a nodejs project

npm i --save-dev @magic/fs

import

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

usage

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

await fs.mkdirp('./path/to/dir')
rmrf

same as rm -rf on unix.

will not work outside process.cwd()

await fs.rmrf('./path/to/dir')
exists

same as fs.exists, but promisified.

getDirectories

get a list of directories in a directory, recursively.

import fs from '@magic/fs'const run = async () => {  // first level directories, no recursion  const directories = await fs.getDirectories(process.cwd(), false)  console.log(directories)  // recursive run  const deepDirectories = await fs.getDirectories(process.cwd())  console.log(deepDirectories)  // recursive run with specified depths  const deepDirectoriesDepth2 = await fs.getDirectories(process.cwd(), { maxDepth: 2, minDepth: 1 })  console.log(deepDirectoriesDepth2)  // maxDepth alias, both lines have the same result:  const files1 = await fs.getDirectories(process.cwd(), 3)  const files2 = await fs.getDirectories(process.cwd(), { maxDepth: 3 })}run()

getFiles

get a list of files in a directory, recursively.

import fs from '@magic/fs'const run = async () => {  // first level files only  const files = await fs.getFiles(process.cwd(), false)  console.log(files)  // recursive run  const deepFiles = await fs.getFiles(process.cwd())  console.log(deepFiles)  // recursive run with specified depth  const getFilesDepth2 = await fs.getFiles(process.cwd(), { maxDepth: 2, minDepth: 1 })  console.log(getFilesDepth2)  // maxDepth alias, both lines have the same result:  const files1 = await fs.getFiles(process.cwd(), 3)  const files2 = await fs.getFiles(process.cwd(), { maxDepth: 3 })}run()
getFileType

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

import fs from '@magic/fs'const fileType = fs.getFileType('html.html')console.log(fileType, fileType === 'html')const nonFileType = fs.getFileType()console.log(nonFileType, nonFileType === 'txt')

source

the source for this page is in the example directory and gets built and published to github using @magic/core