@magic/fs
@magic/fs
nodejs fs promises and goodies.
install
be in a nodejs project
npm i --save-dev @magic/fsimport
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 recursionconst directories = await fs.getDirectories(process.cwd(), false)console.log(directories)// recursive runconst deepDirectories = await fs.getDirectories(process.cwd())console.log(deepDirectories)// recursive run with specified depthsconst 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 onlyconst files = await fs.getFiles(process.cwd(), false)console.log(files)// recursive runconst deepFiles = await fs.getFiles(process.cwd())console.log(deepFiles)// recursive run with specified depthconst 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