DEV Community

Hasan Zohdy
Hasan Zohdy

Posted on

Powerful File System manager for Nodejs

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
Enter fullscreen mode Exit fullscreen mode

Using Yarn

yarn add @mongez/fs
Enter fullscreen mode Exit fullscreen mode

Using pnpm

pnpm add @mongez/fs
Enter fullscreen mode Exit fullscreen mode

Usage

All methods are synced, so you can use them in your code without callbacks or promises.

import { fileExists } from '@mongez/fs';
Enter fullscreen mode Exit fullscreen mode

Copy file or directory

Copy a file or directory to another location.

import { copyPath } from '@mongez/fs';

copyPath('path/to/file', 'path/to/destination');
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Check if a file exists

import { fileExists } from '@mongez/fs';

if (fileExists('path/to/file')) {
    // do something
}
Enter fullscreen mode Exit fullscreen mode

Check if a directory exists

import { directoryExists } from '@mongez/fs';

if (directoryExists('path/to/directory')) {
    // do something
}
Enter fullscreen mode Exit fullscreen mode

Check if is file

Check if a path is a file

import { isFile } from '@mongez/fs';

if (isFile('path/to/file')) {
    // do something
}
Enter fullscreen mode Exit fullscreen mode

Check if is directory

Check if a path is a directory

import { isDirectory } from '@mongez/fs';

if (isDirectory('path/to/directory')) {
    // do something
}
Enter fullscreen mode Exit fullscreen mode

isDir is an alias to isDirectory

Get file contents

Get the content of a file

import { getFile } from '@mongez/fs';

const content = getFile('path/to/file');
Enter fullscreen mode Exit fullscreen mode

Update or create file contents

Put content in a file

import { putFile } from '@mongez/fs';

putFile('path/to/file', 'content');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

Prepend content to file

Prepend contents to the beginning of the file.

import { prependFile } from '@mongez/fs';

prependFile('path/to/file', 'content');
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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' });
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

To remove files you can use removeFile or unlink

import { removeFile, unlink } from '@mongez/fs';

removeFile('path/to/file');
unlink('path/to/file');
Enter fullscreen mode Exit fullscreen mode

To remove directories you can use removeDirectory or rmdir

import { removeDirectory, rmdir } from '@mongez/fs';

removeDirectory('path/to/directory');
rmdir('path/to/directory');
Enter fullscreen mode Exit fullscreen mode

Rename a file or directory

Rename a file or directory

import { renamePath } from '@mongez/fs';

renamePath('path/to/file', 'path/to/destination');
Enter fullscreen mode Exit fullscreen mode

renameFile renameDirectory are aliases for renamePath

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');
Enter fullscreen mode Exit fullscreen mode

moveFile moveDirectory are aliases for movePath

Create New Directory

Create a directory, if the directory exists nothing happens.

import { makeDirectory } from '@mongez/fs';

makeDirectory('path/to/directory');
Enter fullscreen mode Exit fullscreen mode

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,
});
Enter fullscreen mode Exit fullscreen mode

By default recursive is set to true

To set directory permissions, you can use mode option

import { makeDirectory } from '@mongez/fs';

makeDirectory('path/to/directory', {
    mode: 0o777, // this is default mode
});
Enter fullscreen mode Exit fullscreen 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');
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

To get file size, use fileSize

import { fileSize } from '@mongez/fs';

const size = fileSize('path/to/file');
Enter fullscreen mode Exit fullscreen mode

To get directory size, use directorySize

import { directorySize } from '@mongez/fs';

const size = directorySize('path/to/directory');
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Get path stats

Get the stats of a file or directory

import { stats } from '@mongez/fs';

const stats = stats('path/to/file');
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Get file extension

Get the extension of a file

import { extension } from '@mongez/fs';

const extension = extension('path/to/file.txt'); // txt
Enter fullscreen mode Exit fullscreen mode

List files and directories in path

List files and directories in a path

import { list } from '@mongez/fs';

const files = list('path/to/directory');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

Check if directory is empty

import { isEmptyDirectory } from '@mongez/fs';

const isEmpty = isEmptyDirectory('path/to/directory');
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

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)