Mongez FS
Mongez FS is a sync
file system for Nodejs that aims to make working with file system easier and more efficient.
Installation
npm install @mongez/fs
Using Yarn
yarn add @mongez/fs
Using pnpm
pnpm add @mongez/fs
Usage
All methods are synced, so you can use them in your code without callbacks or promises.
import { fileExists } from '@mongez/fs';
Copy file or directory
Copy a file or directory to another location.
import { copyPath } from '@mongez/fs';
copyPath('path/to/file', 'path/to/destination');
Alternatively, you can use copyFile
and copyDirectory
to copy a file or directory respectively.
Check if file or directory exists
Check if a path exists
import { pathExists } from '@mongez/fs';
if (pathExists('path/to/file')) {
// do something
}
Check if a file exists
import { fileExists } from '@mongez/fs';
if (fileExists('path/to/file')) {
// do something
}
Check if a directory exists
import { directoryExists } from '@mongez/fs';
if (directoryExists('path/to/directory')) {
// do something
}
Check if is file
Check if a path is a file
import { isFile } from '@mongez/fs';
if (isFile('path/to/file')) {
// do something
}
Check if is directory
Check if a path is a directory
import { isDirectory } from '@mongez/fs';
if (isDirectory('path/to/directory')) {
// do something
}
isDir
is an alias toisDirectory
Get file contents
Get the content of a file
import { getFile } from '@mongez/fs';
const content = getFile('path/to/file');
Update or create file contents
Put content in a file
import { putFile } from '@mongez/fs';
putFile('path/to/file', 'content');
This function will update or create the file if it doesn't exist.
Append content to file
Append contents to the end of the file.
import { appendFile } from '@mongez/fs';
appendFile('path/to/file', 'content');
Prepend content to file
Prepend contents to the beginning of the file.
import { prependFile } from '@mongez/fs';
prependFile('path/to/file', 'content');
Get json file content
Get the content of a json file and parse it into an object
import { getJsonFile } from '@mongez/fs';
const content = getJsonFile('path/to/file.json'); // will return an object
This function will update or create the file if it doesn't exist.
Put json to file
Put content in a json file
import { putJsonFile } from '@mongez/fs';
putJsonFile('path/to/file.json', { key: 'value' });
Get file lines in array
Get the lines of a file line by line in an array
import { lines } from '@mongez/fs';
const lines: string[] = lines('path/to/file');
If the file doesn't exist, an empty array will be returned.
Remove file or directory
Remove a file or directory
import { removePath } from '@mongez/fs';
removePath('path/to/file');
To remove files you can use removeFile
or unlink
import { removeFile, unlink } from '@mongez/fs';
removeFile('path/to/file');
unlink('path/to/file');
To remove directories you can use removeDirectory
or rmdir
import { removeDirectory, rmdir } from '@mongez/fs';
removeDirectory('path/to/directory');
rmdir('path/to/directory');
Rename a file or directory
Rename a file or directory
import { renamePath } from '@mongez/fs';
renamePath('path/to/file', 'path/to/destination');
renameFile
renameDirectory
are aliases forrenamePath
Move a file or directory
Move a file or directory to another location
import { movePath } from '@mongez/fs';
movePath('path/to/file', 'path/to/destination');
moveFile
moveDirectory
are aliases formovePath
Create New Directory
Create a directory, if the directory exists nothing happens.
import { makeDirectory } from '@mongez/fs';
makeDirectory('path/to/directory');
mkdir
and createDirectory
are aliases for makeDirectory
If the directory already exists, it will throw an error.
You can also create a directory recursively
import { makeDirectory } from '@mongez/fs';
makeDirectory('path/to/directory', {
recursive: true,
});
By default
recursive
is set totrue
To set directory permissions, you can use mode
option
import { makeDirectory } from '@mongez/fs';
makeDirectory('path/to/directory', {
mode: 0o777, // this is default mode
});
Ensure Directory Exists
If you want to create the directory only if it doesn't exist, you can use ensureDirectory
import { ensureDirectory } from '@mongez/fs';
ensureDirectory('path/to/directory');
This will create the directory if it doesn't exist, otherwise it will do nothing.
Last Modified Time
Get the last modified date of a file or directory
import { lastModified } from '@mongez/fs';
const lastModified = lastModified('path/to/file'); // will return a Date object
To get last modified time for file, use fileLastModified
import { fileLastModified } from '@mongez/fs';
const lastModified = fileLastModified('path/to/file'); // will return a Date object
To get last modified time for directory, use directoryLastModified
import { directoryLastModified } from '@mongez/fs';
const lastModified = directoryLastModified('path/to/directory'); // will return a Date object
File And Directory Size
Get the size of a file or directory in bytes.
import { pathSize } from '@mongez/fs';
const size = pathSize('path/to/file');
To get file size, use fileSize
import { fileSize } from '@mongez/fs';
const size = fileSize('path/to/file');
To get directory size, use directorySize
import { directorySize } from '@mongez/fs';
const size = directorySize('path/to/directory');
Get file and directory size in human readable format
Get the size of a file or directory in human readable format.
import { humanSize } from '@mongez/fs';
const size = humanSize('path/to/file'); // 1.2 KB
Get path stats
Get the stats of a file or directory
import { stats } from '@mongez/fs';
const stats = stats('path/to/file');
The returned stats of the given file is cached, so the next time you call this function with the same path, the cached stats will be returned, if you want to disable the cache and get current stats pass second argument to false.
import { stats } from '@mongez/fs';
const stats = stats('path/to/file', false);
Get file extension
Get the extension of a file
import { extension } from '@mongez/fs';
const extension = extension('path/to/file.txt'); // txt
List files and directories in path
List files and directories in a path
import { list } from '@mongez/fs';
const files = list('path/to/directory');
It will return an array contains all files and directories in the given path.
List files in path
List files only in a path
import { listFiles } from '@mongez/fs';
const files = listFiles('path/to/directory');
It will return an array contains all files in the given path.
List directories in path
List directories only in a path
import { listDirectories } from '@mongez/fs';
const directories = listDirectories('path/to/directory');
It will return an array contains all directories in the given path.
Check if path is empty
Check if file is empty
import { isEmptyFile } from '@mongez/fs';
const isEmpty = isEmptyFile('path/to/file');
If the file doesn't exist, it will return true.
You can use isNotEmptyFile
to check if file is not empty
import { isNotEmptyFile } from '@mongez/fs';
const isNotEmpty = isNotEmptyFile('path/to/file');
Check if directory is empty
import { isEmptyDirectory } from '@mongez/fs';
const isEmpty = isEmptyDirectory('path/to/directory');
If the directory doesn't exist, it will return true.
You can use isNotEmptyDirectory
to check if directory is not empty
import { isNotEmptyDirectory } from '@mongez/fs';
const isNotEmpty = isNotEmptyDirectory('path/to/directory');
Conclusion
File system is a major part of any application, and it's important to have a good file system library, that's why I created this package, also having a readable and synchronized functions is a lot easier to work with, I hope you find it useful, if you have any suggestions or questions, please feel free to post a comment below.
Top comments (0)