Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jooy2/fsman

๐Ÿ’พ A file system utility that can be used with Node.js fs module.
https://github.com/jooy2/fsman

directory esm file file-utility files filesystem fs javascript nodejs npm npm-package typescript utility

Last synced: about 1 month ago
JSON representation

๐Ÿ’พ A file system utility that can be used with Node.js fs module.

Awesome Lists containing this project

README

        

# ๐Ÿ’พ FsMan

A file system utility that can be used with **[Node.js](https://nodejs.org)** fs module.

> [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jooy2/fsman/blob/master/LICENSE) ![Programming Language Usage](https://img.shields.io/github/languages/top/jooy2/fsman) [![npm latest package](https://img.shields.io/npm/v/fsman/latest.svg)](https://www.npmjs.com/package/fsman) [![npm downloads](https://img.shields.io/npm/dm/fsman.svg)](https://www.npmjs.com/package/fsman) ![github repo size](https://img.shields.io/github/repo-size/jooy2/fsman) [![Followers](https://img.shields.io/github/followers/jooy2?style=social)](https://github.com/jooy2) ![Stars](https://img.shields.io/github/stars/jooy2/fsman?style=social) ![Commit Count](https://img.shields.io/github/commit-activity/y/jooy2/fsman) ![Line Count](https://img.shields.io/tokei/lines/github/jooy2/fsman)

## Installation

FsMan requires `Node.js 18.x` or higher, and the repository is serviced through **[NPM](https://npmjs.com)**.

After configuring the node environment, you can simply run the following command.

```shell
# via npm
$ npm install fsman

# via yarn
$ yarn add fsman

# via pnpm
$ pnpm install fsman
```

## How to use

### Using named import (Multiple utilities in a single require) - Recommend

```javascript
import { isHiddenFile, humanizeFileSize } from 'fsman';

async function main() {
console.log(await isHiddenFile('.hiddenFile')); // true
console.log(humanizeFileSize(1000000)); // '976.56 KB'
}
```

### Using whole class (multiple utilities simultaneously with one object)

```javascript
import fsman from 'fsman';

async function main() {
console.log(await fsman.isHiddenFile('.hiddenFile')); // true
console.log(fsman.humanizeFileSize(1000000)); // '976.56 KB'
}
```

## Methods

### `isHiddenFile (Promise)`

Checks whether a file or folder in the specified path is a hidden file. Determines system hidden files for Windows and the presence or absence of a `.`(dot) for Linux and macOS or other operating systems.

If Windows fails to get the file properties, it assumes the file is not hidden.

- `filePath `: File or directory path
- `isWindows `: Whether the target operating system to be checked is Windows

```javascript
console.log(await fsman.isHiddenFile('text.txt')); // false
console.log(await fsman.isHiddenFile('.hiddenFile')); // true
console.log(await fsman.isHiddenFile('.hiddenFile', true)); // false (Files with no hidden attribute applied in Windows)
```

### `isFileExists (Promise)`

Returns `true` if the file at the given path exists.

- `filePath `: File or directory path

```javascript
console.log(await fsman.isFileExists('text.txt')); // true
console.log(await fsman.isFileExists('not-exist.txt')); // false
```

### `humanizeFileSize ()`

Returns the given byte argument as a human-friendly string.

- `bytes `: Converts it to a human-friendly string via the bytes provided here.
- `decimals (Default: 2)`: Specifies the number of decimal places to represent.

```javascript
console.log(await fsman.humanizeFileSize(1000000)); // '976.56 KB'
console.log(await fsman.humanizeFileSize(100000000, 3)); // '95.367 MB'
```

### `toValidFilePath ()`

Remove invalid or unnecessary characters in the path.

- `filePath `: File or directory path
- `isWindows `: Whether the target operating system to be checked is Windows

```javascript
console.log(fsman.toValidFilePath('C:\\Windows\\System32\\'), true); // 'C:\Windows\System32'
console.log(fsman.toValidFilePath('home/user/.bashrc')); // '/home/user/.bashrc'
```

### `getFilePathLevel ()`

Determine how many steps the current path is. The root path (`/` or `C:\`) begins with step 1.

- `filePath `: File or directory path

```javascript
// Include 'C:\' root path
console.log(fsman.getFilePathLevel('C:\\Windows\\System32')); // 3
// Include '/' root path
console.log(fsman.getFilePathLevel('/home/user')); // 3
```

### `getParentFilePath ()`

Returns the parent path one level above the given path.

- `filePath `: File or directory path

```javascript
console.log(fsman.getParentFilePath('C:\\Windows\\System32', true)); // 'C:\Windows'
console.log(fsman.getParentFilePath('/home/user/text.txt')); // '/home/user'
```

### `toPosixFilePath ()`

Returns the given path as a path in POSIX format (usually used by Linux). For example, a Windows path will be converted to `/` instead of `\\`.

- `filePath `: File or directory path

```javascript
console.log(fsman.toPosixFilePath('C:\\Windows\\System32')); // 'C:/Windows/System32'
```

### `joinFilePath ()`

Combines paths for each operating system according to the given parameter values.

- `isWindows `: Whether the target operating system to be checked is Windows
- `paths `: A path value consisting of one or more strings. Omit the path separator and put it in the parameter.

```javascript
console.log(fsman.joinFilePath(true, 'C:\\', 'Windows', 'System32')); // 'C:\Windows\System32'
console.log(fsman.joinFilePath(false, 'home', '/user', '.bashrc')); // '/home/user/.bashrc'
```

### `isValidFileName ()`

Determines whether the passed path or filename is using a system-accepted string (Also check the valid file length). Returns false if the name is not available.

- `filePath `: File or directory path
- `unixType `: Passes true if the file type is unix type.

```javascript
console.log(fsman.isValidFileName('C:\\Windows\\System32*')); // false
console.log(fsman.isValidFileName('/home/user/.bashrc', true)); // true
```

### `createFolder ()`

Creates a directory with the specified path. Ignores the operation if the directory already exists.

- `filePath `: File or directory path
- `recursive `: Recursively creates all directories in the given path.

```javascript
fsman.createFolder('/home/user/a/b/c');
```

### `getFileName ()`

Returns the file name within the path.

- `filePath `: File or directory path
- `withExtension `: Returns the name with extension.

```javascript
console.log(fsman.getFileName('/home/user/test.txt')); // 'test'
console.log(fsman.getFileName('/home/user/test.txt', true)); // 'test.txt'
```

### `normalizeFile ()`

Changes a string (usually file names) according to the Unicode normalization form method used by each operating system.

- `filePath `: File or directory path
- `normalizationForm <'NFC'|'NFD'|'NFKC'|'NFKD'|undefined>`: Normalization method (If value is `undefined`, `NFC` is used.)

```javascript
console.log(fsman.normalizeFile('์•ˆ๋…•ํ•˜์„ธ์š”Hello.txt', 'NFC')); // '์•ˆ๋…•ํ•˜์„ธ์š”Hello.txt'
console.log(fsman.normalizeFile('์•ˆ๋…•ํ•˜์„ธ์š”Hello.txt', 'NFD')); // '์•ˆ๋…•ํ•˜์„ธ์š”Hello.txt'
```

### `getFileExtension ()`

Returns the file extension from the given file path. An empty string value is returned for files without extension.

- `filePath `: File or directory path
- `isWindows `: If the given file path is a Windows system path

```javascript
console.log(fsman.getFileExtension('/home/user/test.txt')); // 'txt'
console.log(fsman.getFileExtension('/home/user/test.txt.sample')); // 'sample'
```

### `getFileInfo ()`

Returns file or directory information as an easy-to-understand object.

- `filePath `: File or directory path

```javascript
console.log(fsman.getFileInfo('/home/user/test.txt'));
```

Examples of returned values:

```json5
{
success: true, // Whether the file stat import was successful
isDirectory: false,
ext: 'txt',
size: 33,
sizeHumanized: '33 Bytes',
name: 'test.txt',
dirname: 'user',
path: '/home/user/test.txt',
created: 1652581984, // Unix timestamp
modified: 1652581984 // Unix timestamp
}
```

### `headFile ()`

Returns the first line of the specified text file path. The `length` argument is the total number of lines to print. Default is `1`.

- `filePath `: File or directory path
- `length `: Number of lines of text to return

```javascript
console.log(fsman.headFile('/test/targets/hello.md')); // '# Hello, World!'
```

### `tailFile ()`

Returns the last line of the specified text file path. The `length` argument is the total number of lines to print. Default is `1`. The last line of newline characters is ignored.

- `filePath `: File or directory path
- `length `: Number of lines of text to return

```javascript
console.log(fsman.tailFile('/test/targets/hello.md')); // '--- Hello End ---'
```

### `touchFile ()`

Create a file of empty data. If the same file already exists, it is ignored.

- `filePath `: File or directory path

```javascript
fsman.touchFile('/home/user/test.txt');
```

### `touchFileWithDummy ()`

Creates a file with the specified size in bytes.

- `filePath `: File or directory path
- `size `: Size of the file to be created (Dummy data is filled as much as the given size)

```javascript
fsman.touchFileWithDummy('/home/user/test.txt', 100000);
```

### `deleteFile ()`

Delete files or directory in the specified path. If the file does not exist in the path, it is ignored.

This method also supports deleting directory paths. If files exist within the directory, they are included and removed.

- `filePath `: File or directory path

```javascript
fsman.deleteFile('/home/user/text.txt');
```

### `moveFile ()`

Moves a file in the specified file path to another path.

- `filePath `: File or directory path
- `targetFilePath `: Path of file to move

```javascript
fsman.moveFile('/home/user/text.txt', '/home/user/text2.txt');
```

### `deleteAllFileFromDirectory ()`

Deletes all files in the specified directory path. However, the directory is preserved.

- `directoryPath `: Directory path

```javascript
fsman.deleteAllFileFromDirectory('/home/user/Downloads');
```

### `hashFile (Promise)`

Returns the file in the specified path as a value hashed by a specific algorithm. The default algorithm is `md5`. This method uses a `Promise` to return a valid hash value.

- `filePath `: File or directory path
- `algorithm <'md5'|'sha1'|'sha256'|'sha512'>`: OpenSSL algorithm to be used for file hashing

```javascript
console.log(await fsman.hashFile('/home/user/text.txt', 'sha1')); // '38851813f75627d581c593f3ccfb7061dd013fbd'
```

## Contribute

You can report issues on [GitHub Issue Tracker](https://github.com/jooy2/fsman/issues). You can also request a pull to fix bugs and add frequently used features.

## License

Copyright ยฉ 2022-2024 [Jooy2](https://jooy2.com) <[[email protected]](mailto:[email protected])> Released under the MIT license.